summaryrefslogtreecommitdiffstats
path: root/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/slobrok/SlobrokMonitorManagerImpl.java
blob: 68958c94dfda51e32c7c547eee152cb89f78e84a (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
143
144
145
146
147
148
149
150
151
152
153
154
// 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.internal.slobrok;

import com.google.inject.Inject;
import com.yahoo.config.model.api.ApplicationInfo;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.jrt.slobrok.api.Mirror;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.applicationmodel.ClusterId;
import com.yahoo.vespa.applicationmodel.ConfigId;
import com.yahoo.vespa.applicationmodel.ServiceStatus;
import com.yahoo.vespa.applicationmodel.ServiceType;
import com.yahoo.vespa.service.monitor.SlobrokApi;
import com.yahoo.vespa.service.monitor.application.ConfigServerApplication;
import com.yahoo.vespa.service.monitor.internal.MonitorManager;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.logging.Logger;

public class SlobrokMonitorManagerImpl implements SlobrokApi, MonitorManager {
    private static final Logger logger =
            Logger.getLogger(SlobrokMonitorManagerImpl.class.getName());

    private final Supplier<SlobrokMonitor> slobrokMonitorFactory;

    private final Object monitor = new Object();
    private final HashMap<ApplicationId, SlobrokMonitor> slobrokMonitors = new HashMap<>();

    @Inject
    public SlobrokMonitorManagerImpl() {
        this(SlobrokMonitor::new);
    }

    SlobrokMonitorManagerImpl(Supplier<SlobrokMonitor> slobrokMonitorFactory) {
        this.slobrokMonitorFactory = slobrokMonitorFactory;
    }

    @Override
    public void applicationActivated(ApplicationInfo application) {
        if (!applicationMonitoredWithSlobrok(application.getApplicationId())) {
            return;
        }

        synchronized (monitor) {
            SlobrokMonitor slobrokMonitor = slobrokMonitors.computeIfAbsent(
                    application.getApplicationId(),
                    id -> slobrokMonitorFactory.get());
            slobrokMonitor.updateSlobrokList(application);
        }
    }

    @Override
    public void applicationRemoved(ApplicationId id) {
        if (!applicationMonitoredWithSlobrok(id)) {
            return;
        }

        synchronized (monitor) {
            SlobrokMonitor slobrokMonitor = slobrokMonitors.remove(id);
            if (slobrokMonitor == null) {
                logger.log(LogLevel.WARNING, "Removed application " + id +
                        ", but it was never registered");
            } else {
                slobrokMonitor.close();
            }
        }
    }

    @Override
    public List<Mirror.Entry> lookup(ApplicationId id, String pattern) {
        synchronized (monitor) {
            SlobrokMonitor slobrokMonitor = slobrokMonitors.get(id);
            if (slobrokMonitor == null) {
                throw new IllegalArgumentException("Slobrok manager has no knowledge of application " + id);
            } else {
                return slobrokMonitor.lookup(pattern);
            }
        }
    }

    @Override
    public ServiceStatus getStatus(ApplicationId applicationId,
                                   ClusterId clusterId,
                                   ServiceType serviceType,
                                   ConfigId configId) {
        if (!applicationMonitoredWithSlobrok(applicationId)) {
            return ServiceStatus.NOT_CHECKED;
        }

        Optional<String> slobrokServiceName = findSlobrokServiceName(serviceType, configId);
        if (slobrokServiceName.isPresent()) {
            synchronized (monitor) {
                SlobrokMonitor slobrokMonitor = slobrokMonitors.get(applicationId);
                if (slobrokMonitor != null &&
                        slobrokMonitor.registeredInSlobrok(slobrokServiceName.get())) {
                    return ServiceStatus.UP;
                } else {
                    return ServiceStatus.DOWN;
                }
            }
        } else {
            return ServiceStatus.NOT_CHECKED;
        }
    }

    private boolean applicationMonitoredWithSlobrok(ApplicationId applicationId) {
        if (applicationId.equals(ConfigServerApplication.CONFIG_SERVER_APPLICATION.getApplicationId())) {
            return false;
        }

        return true;
    }

    /**
     * Get the Slobrok service name of the service, or empty if the service
     * is not registered with Slobrok.
     */
    Optional<String> findSlobrokServiceName(ServiceType serviceType, ConfigId configId) {
        switch (serviceType.s()) {
            case "adminserver":
            case "config-sentinel":
            case "configproxy":
            case "configserver":
            case "logd":
            case "logserver":
            case "metricsproxy":
            case "slobrok":
            case "transactionlogserver":
                return Optional.empty();

            case "topleveldispatch":
                return Optional.of(configId.s());

            case "qrserver":
            case "container":
            case "docprocservice":
            case "container-clustercontroller":
                return Optional.of("vespa/service/" + configId.s());

            case "searchnode": //TODO: handle only as storagenode instead of both as searchnode/storagenode
                return Optional.of(configId.s() + "/realtimecontroller");
            case "distributor":
            case "storagenode":
                return Optional.of("storage/cluster." + configId.s());
            default:
                logger.log(LogLevel.DEBUG, "Unknown service type " + serviceType.s() +
                        " with config id " + configId.s());
                return Optional.empty();
        }
    }
}