aboutsummaryrefslogtreecommitdiffstats
path: root/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/application/InfraApplication.java
blob: 1c4f6ea3d2a328ad827603cbda5d24c3bbbf1408 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.service.monitor.application;

import com.yahoo.config.model.api.ApplicationInfo;
import com.yahoo.config.model.api.HostInfo;
import com.yahoo.config.model.api.PortInfo;
import com.yahoo.config.model.api.ServiceInfo;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Capacity;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.NodeType;
import com.yahoo.config.provision.TenantName;
import com.yahoo.component.Version;
import com.yahoo.vespa.applicationmodel.ConfigId;
import com.yahoo.vespa.applicationmodel.ServiceType;
import com.yahoo.vespa.service.monitor.internal.ModelGenerator;
import com.yahoo.vespa.service.monitor.internal.health.ApplicationHealthMonitor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author freva
 */
public abstract class InfraApplication implements InfraApplicationApi {
    static final int HEALTH_PORT = 8080;

    private static final TenantName TENANT_NAME = TenantName.from("hosted-vespa");
    private static final String CONFIG_ID_PREFIX = "configid.";

    private final ApplicationId applicationId;
    private final Capacity capacity;
    private final ClusterSpec.Type clusterType;
    private final ClusterSpec.Id clusterId;
    private final ServiceType serviceType;

    public static ApplicationId createHostedVespaApplicationId(String applicationName) {
        return new ApplicationId.Builder()
                .tenant(TENANT_NAME)
                .applicationName(applicationName)
                .build();
    }

    protected InfraApplication(String applicationName,
                               NodeType nodeType,
                               ClusterSpec.Type clusterType,
                               ClusterSpec.Id clusterId,
                               ServiceType serviceType) {
        this.applicationId = createHostedVespaApplicationId(applicationName);
        this.capacity = Capacity.fromRequiredNodeType(nodeType);
        this.clusterType = clusterType;
        this.clusterId = clusterId;
        this.serviceType = serviceType;
    }

    @Override
    public ApplicationId getApplicationId() {
        return applicationId;
    }

    @Override
    public Capacity getCapacity() {
        return capacity;
    }

    @Override
    public ClusterSpec getClusterSpecWithVersion(Version version) {
        return ClusterSpec.request(clusterType, clusterId, version, true);
    }

    public ClusterSpec.Type getClusterType() {
        return clusterType;
    }

    public ClusterSpec.Id getClusterId() {
        return clusterId;
    }

    public ApplicationInfo makeApplicationInfo(List<HostName> hostnames) {
        List<HostInfo> hostInfos = new ArrayList<>();
        for (int index = 0; index < hostnames.size(); ++index) {
            hostInfos.add(makeHostInfo(hostnames.get(index), HEALTH_PORT, index, serviceType, clusterId));
        }

        return new ApplicationInfo(applicationId, 0, new HostsModel(hostInfos));
    }

    private static HostInfo makeHostInfo(HostName hostname, int port, int configIndex, ServiceType serviceType, ClusterSpec.Id clusterId) {
        PortInfo portInfo = new PortInfo(port, ApplicationHealthMonitor.PORT_TAGS_HEALTH);

        Map<String, String> properties = new HashMap<>();
        properties.put(ModelGenerator.CLUSTER_ID_PROPERTY_NAME, clusterId.value());

        ServiceInfo serviceInfo = new ServiceInfo(
                // service name == service type for the first service of each type on each host
                serviceType.s(),
                serviceType.s(),
                Collections.singletonList(portInfo),
                properties,
                configIdFrom(configIndex).s(),
                hostname.value());

        return new HostInfo(hostname.value(), Collections.singletonList(serviceInfo));
    }

    private static ConfigId configIdFrom(int index) {
        return new ConfigId(CONFIG_ID_PREFIX + index);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        InfraApplication that = (InfraApplication) o;
        return Objects.equals(applicationId, that.applicationId) &&
                Objects.equals(capacity, that.capacity) &&
                clusterType == that.clusterType &&
                Objects.equals(clusterId, that.clusterId) &&
                Objects.equals(serviceType, that.serviceType);
    }

    @Override
    public int hashCode() {
        return Objects.hash(applicationId, capacity, clusterType, clusterId, serviceType);
    }

    @Override
    public String toString() {
        return "InfraApplication{" +
                "applicationId=" + applicationId +
                ", capacity=" + capacity +
                ", clusterType=" + clusterType +
                ", clusterId=" + clusterId +
                ", serviceType=" + serviceType +
                '}';
    }
}