aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/MeteringMonitorMaintainer.java
blob: 396ec1ec6f9b84b2c4f005619ff085493cd8e2f6 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.maintenance;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.jdisc.Metric;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.Instance;
import com.yahoo.vespa.hosted.controller.api.identifiers.DeploymentId;
import com.yahoo.vespa.hosted.controller.api.integration.resource.ResourceDatabaseClient;

import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Reports discrepancies between currently deployed applications and
 * recently stored metering data in ResourceDatabaseClient.
 *
 * @author olaa
 */
public class MeteringMonitorMaintainer extends ControllerMaintainer {

    private final ResourceDatabaseClient resourceDatabaseClient;
    private final Metric metric;

    protected static final String METERING_AGE_METRIC_NAME = "metering.age.seconds";
    private static final Logger logger = Logger.getLogger(MeteringMonitorMaintainer.class.getName());

    public MeteringMonitorMaintainer(Controller controller, Duration interval, ResourceDatabaseClient resourceDatabaseClient, Metric metric) {
        super(controller, interval, null, SystemName.allOf(SystemName::isPublic));
        this.resourceDatabaseClient = resourceDatabaseClient;
        this.metric = metric;
    }

    @Override
    protected double maintain() {
        var activeDeployments = activeDeployments();
        var lastSnapshotTime = resourceDatabaseClient.getOldestSnapshotTimestamp(activeDeployments);
        var age = controller().clock().instant().getEpochSecond() - lastSnapshotTime.getEpochSecond();
        metric.set(METERING_AGE_METRIC_NAME, age, metric.createContext(Collections.emptyMap()));
        return 1;
    }

    private Set<DeploymentId> activeDeployments() {
        return controller().applications().asList()
                .stream()
                .flatMap(app -> app.instances().values().stream())
                .flatMap(this::toProdDeployments)
                .collect(Collectors.toSet());
    }

    private Stream<DeploymentId> toProdDeployments(Instance instance) {
        return instance.deployments()
                .keySet()
                .stream()
                .filter(deployment -> deployment.environment().isProduction())
                .map(deployment -> new DeploymentId(instance.id(), deployment));
    }
}