aboutsummaryrefslogtreecommitdiffstats
path: root/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/ModelGenerator.java
blob: d9f019ade6357de1a6fa7f964ccc1cb826bdf4da (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.service.monitor;

import com.yahoo.config.model.api.ApplicationInfo;
import com.yahoo.config.model.api.HostInfo;
import com.yahoo.config.model.api.ServiceInfo;
import com.yahoo.config.model.api.SuperModel;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Zone;
import com.yahoo.vespa.applicationmodel.ApplicationInstance;
import com.yahoo.vespa.applicationmodel.ApplicationInstanceId;
import com.yahoo.vespa.applicationmodel.ApplicationInstanceReference;
import com.yahoo.vespa.applicationmodel.ClusterId;
import com.yahoo.vespa.applicationmodel.ConfigId;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.applicationmodel.ServiceCluster;
import com.yahoo.vespa.applicationmodel.ServiceClusterKey;
import com.yahoo.vespa.applicationmodel.ServiceInstance;
import com.yahoo.vespa.applicationmodel.ServiceStatus;
import com.yahoo.vespa.applicationmodel.ServiceType;
import com.yahoo.vespa.applicationmodel.TenantId;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Util to convert SuperModel to ServiceModel and application model classes
 */
public class ModelGenerator {
    public static final String CLUSTER_ID_PROPERTY_NAME = "clustername";

    /**
     * Create service model based primarily on super model.
     *
     * If the configServerhosts is non-empty, a config server application is added.
     */
    ServiceModel toServiceModel(
            SuperModel superModel,
            Zone zone,
            List<String> configServerHosts,
            SlobrokMonitorManager slobrokMonitorManager) {
        Map<ApplicationInstanceReference, ApplicationInstance> applicationInstances = new HashMap<>();

        for (ApplicationInfo applicationInfo : superModel.getAllApplicationInfos()) {

            ApplicationInstance applicationInstance = toApplicationInstance(
                    applicationInfo,
                    zone,
                    slobrokMonitorManager);
            applicationInstances.put(applicationInstance.reference(), applicationInstance);
        }

        // The config server is part of the service model (but not super model)
        if (!configServerHosts.isEmpty()) {
            ConfigServerApplication configServerApplication = new ConfigServerApplication();
            ApplicationInstance configServerApplicationInstance =
                    configServerApplication.toApplicationInstance(configServerHosts);
            applicationInstances.put(configServerApplicationInstance.reference(), configServerApplicationInstance);
        }

        return new ServiceModel(applicationInstances);
    }

    ApplicationInstance toApplicationInstance(
            ApplicationInfo applicationInfo,
            Zone zone,
            SlobrokMonitorManager slobrokMonitorManager) {
        Map<ServiceClusterKey, Set<ServiceInstance>> groupedServiceInstances = new HashMap<>();

        for (HostInfo host : applicationInfo.getModel().getHosts()) {
            HostName hostName = new HostName(host.getHostname());
            for (ServiceInfo serviceInfo : host.getServices()) {
                ServiceClusterKey serviceClusterKey = toServiceClusterKey(serviceInfo);
                ServiceInstance serviceInstance =
                        toServiceInstance(
                                applicationInfo.getApplicationId(),
                                serviceInfo,
                                hostName,
                                slobrokMonitorManager);

                if (!groupedServiceInstances.containsKey(serviceClusterKey)) {
                    groupedServiceInstances.put(serviceClusterKey, new HashSet<>());
                }
                groupedServiceInstances.get(serviceClusterKey).add(serviceInstance);
            }
        }

        Set<ServiceCluster> serviceClusters = groupedServiceInstances.entrySet().stream()
                .map(entry -> new ServiceCluster(
                        entry.getKey().clusterId(),
                        entry.getKey().serviceType(),
                        entry.getValue()))
                .collect(Collectors.toSet());

        ApplicationInstance applicationInstance = new ApplicationInstance(
                new TenantId(applicationInfo.getApplicationId().tenant().toString()),
                toApplicationInstanceId(applicationInfo, zone),
                serviceClusters);

        return applicationInstance;
    }

    ServiceClusterKey toServiceClusterKey(ServiceInfo serviceInfo) {
        ClusterId clusterId = new ClusterId(serviceInfo.getProperty(CLUSTER_ID_PROPERTY_NAME).orElse(""));
        ServiceType serviceType = toServiceType(serviceInfo);
        return new ServiceClusterKey(clusterId, serviceType);
    }

    ServiceInstance toServiceInstance(
            ApplicationId applicationId,
            ServiceInfo serviceInfo,
            HostName hostName,
            SlobrokMonitorManager slobrokMonitorManager) {
        ConfigId configId = new ConfigId(serviceInfo.getConfigId());

        ServiceStatus status = slobrokMonitorManager.getStatus(
                applicationId,
                toServiceType(serviceInfo),
                configId);

        return new ServiceInstance(configId, hostName, status);
    }

    ApplicationInstanceId toApplicationInstanceId(ApplicationInfo applicationInfo, Zone zone) {
        return new ApplicationInstanceId(String.format("%s:%s:%s:%s",
                applicationInfo.getApplicationId().application().value(),
                zone.environment().value(),
                zone.region().value(),
                applicationInfo.getApplicationId().instance().value()));
    }

    ServiceType toServiceType(ServiceInfo serviceInfo) {
        return new ServiceType(serviceInfo.getServiceType());
    }
}