aboutsummaryrefslogtreecommitdiffstats
path: root/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/SuperModelListenerImpl.java
blob: f509809c33d3e1f7f6902ee45b15a941b6cd0251 (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
// 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.internal;

import com.yahoo.config.model.api.ApplicationInfo;
import com.yahoo.config.model.api.SuperModel;
import com.yahoo.config.model.api.SuperModelListener;
import com.yahoo.config.model.api.SuperModelProvider;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Zone;
import com.yahoo.vespa.service.monitor.ServiceModel;
import com.yahoo.vespa.service.monitor.ServiceStatusProvider;

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

public class SuperModelListenerImpl implements SuperModelListener, Supplier<ServiceModel> {
    private static final Logger logger = Logger.getLogger(SuperModelListenerImpl.class.getName());

    private final ServiceMonitorMetrics metrics;
    private final DuperModel duperModel;
    private final ModelGenerator modelGenerator;
    private final Zone zone;

    // superModel and monitorManager are always updated together
    // and atomically using this monitor.
    private final Object monitor = new Object();
    private final MonitorManager monitorManager;
    private SuperModel superModel;

    SuperModelListenerImpl(MonitorManager monitorManager,
                           ServiceMonitorMetrics metrics,
                           DuperModel duperModel,
                           ModelGenerator modelGenerator,
                           Zone zone) {
        this.monitorManager = monitorManager;
        this.metrics = metrics;
        this.duperModel = duperModel;
        this.modelGenerator = modelGenerator;
        this.zone = zone;
    }

    void start(SuperModelProvider superModelProvider) {
        synchronized (monitor) {
            // This snapshot() call needs to be within the synchronized block,
            // since applicationActivated()/applicationRemoved() may be called
            // asynchronously even before snapshot() returns.
            this.superModel = superModelProvider.snapshot(this);
            duperModel.getApplicationInfos(superModel).forEach(monitorManager::applicationActivated);
        }
    }

    @Override
    public void applicationActivated(SuperModel superModel, ApplicationInfo application) {
        synchronized (monitor) {
            this.superModel = superModel;
            monitorManager.applicationActivated(application);
        }
    }

    @Override
    public void applicationRemoved(SuperModel superModel, ApplicationId id) {
        synchronized (monitor) {
            this.superModel = superModel;
            monitorManager.applicationRemoved(id);
        }
    }

    @Override
    public ServiceModel get() {
        try (LatencyMeasurement measurement = metrics.startServiceModelSnapshotLatencyMeasurement()) {
            // Reference 'measurement' in a dummy statement, otherwise the compiler
            // complains about "auto-closeable resource is never referenced in body of
            // corresponding try statement". Why hasn't javac fixed this!?
            dummy(measurement);

            // WARNING: The slobrok monitor manager may be out-of-sync with super model (no locking)
            List<ApplicationInfo> applicationInfos = duperModel.getApplicationInfos(superModel);

            return modelGenerator.toServiceModel(applicationInfos, zone, (ServiceStatusProvider) monitorManager);
        }
    }

    private void dummy(LatencyMeasurement measurement) {}
}