summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/ApplicationOwnershipConfirmer.java
blob: 1f20e48edf5b5f168fd9979e3c9c4173b7c5341c (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
// Copyright 2018 Yahoo Holdings. 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.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.Application;
import com.yahoo.vespa.hosted.controller.ApplicationController;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.Instance;
import com.yahoo.vespa.hosted.controller.api.integration.organization.ApplicationSummary;
import com.yahoo.vespa.hosted.controller.api.integration.organization.IssueId;
import com.yahoo.vespa.hosted.controller.api.integration.organization.OwnershipIssues;
import com.yahoo.vespa.hosted.controller.api.integration.organization.User;
import com.yahoo.vespa.hosted.controller.application.ApplicationList;
import com.yahoo.vespa.hosted.controller.application.TenantAndApplicationId;
import com.yahoo.vespa.hosted.controller.tenant.Tenant;
import com.yahoo.yolean.Exceptions;

import java.time.Duration;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;

/**
 * Periodically request application ownership confirmation through filing issues.
 *
 * When to file new issues, escalate inactive ones, etc., is handled by the enclosed OwnershipIssues.
 *
 * @author jonmv
 */
public class ApplicationOwnershipConfirmer extends ControllerMaintainer {

    private final OwnershipIssues ownershipIssues;
    private final ApplicationController applications;

    public ApplicationOwnershipConfirmer(Controller controller, Duration interval, OwnershipIssues ownershipIssues) {
        super(controller, interval);
        this.ownershipIssues = ownershipIssues;
        this.applications = controller.applications();
    }

    @Override
    protected boolean maintain() {
        return confirmApplicationOwnerships() &
               ensureConfirmationResponses() &
               updateConfirmedApplicationOwners();
    }

    /** File an ownership issue with the owners of all applications we know about. */
    private boolean confirmApplicationOwnerships() {
        AtomicBoolean success = new AtomicBoolean(true);
        applications()
                       .withProjectId()
                       .withProductionDeployment()
                       .asList()
                       .stream()
                       .filter(application -> application.createdAt().isBefore(controller().clock().instant().minus(Duration.ofDays(90))))
                       .forEach(application -> {
                           try {
                               // TODO jvenstad: Makes sense to require, and run this only in main?
                               tenantOf(application.id()).contact().flatMap(contact -> {
                                   return ownershipIssues.confirmOwnership(application.ownershipIssueId(),
                                                                           summaryOf(application.id()),
                                                                           determineAssignee(application),
                                                                           contact);
                               }).ifPresent(newIssueId -> store(newIssueId, application.id()));
                           }
                           catch (RuntimeException e) { // Catch errors due to wrong data in the controller, or issues client timeout.
                               success.set(false);
                               log.log(Level.INFO, "Exception caught when attempting to file an issue for '" + application.id() + "': " + Exceptions.toMessageString(e));
                           }
                       });
        return success.get();
    }

    private ApplicationSummary summaryOf(TenantAndApplicationId application) {
        var app = applications.requireApplication(application);
        var metrics = new HashMap<ZoneId, ApplicationSummary.Metric>();
        for (Instance instance : app.instances().values())
            for (var kv : instance.deployments().entrySet()) {
                var zone = kv.getKey();
                var deploymentMetrics = kv.getValue().metrics();
                metrics.put(zone, new ApplicationSummary.Metric(deploymentMetrics.documentCount(),
                                                                deploymentMetrics.queriesPerSecond(),
                                                                deploymentMetrics.writesPerSecond()));
            }
        return new ApplicationSummary(app.id().defaultInstance(), app.activity().lastQueried(), app.activity().lastWritten(),
                                      app.latestVersion().flatMap(version -> version.buildTime()), metrics);
    }

    /** Escalate ownership issues which have not been closed before a defined amount of time has passed. */
    private boolean ensureConfirmationResponses() {
        AtomicBoolean success = new AtomicBoolean(true);
        for (Application application : applications())
            application.ownershipIssueId().ifPresent(issueId -> {
                try {
                    Tenant tenant = tenantOf(application.id());
                    ownershipIssues.ensureResponse(issueId, tenant.contact());
                }
                catch (RuntimeException e) {
                    success.set(false);
                    log.log(Level.INFO, "Exception caught when attempting to escalate issue with id '" + issueId + "': " + Exceptions.toMessageString(e));
                }
            });
        return success.get();
    }

    private boolean updateConfirmedApplicationOwners() {
        applications()
                       .withProjectId()
                       .withProductionDeployment()
                       .asList()
                       .stream()
                       .filter(application -> application.ownershipIssueId().isPresent())
                       .forEach(application -> {
                           IssueId ownershipIssueId = application.ownershipIssueId().get();
                           ownershipIssues.getConfirmedOwner(ownershipIssueId).ifPresent(owner -> {
                               controller().applications().lockApplicationIfPresent(application.id(), lockedApplication ->
                                       controller().applications().store(lockedApplication.withOwner(owner)));
                           });
                       });
        return true;
    }

    private ApplicationList applications() {
        return ApplicationList.from(controller().applications().readable());
    }

    private User determineAssignee(Application application) {
        return application.owner().orElse(null);
    }

    private Tenant tenantOf(TenantAndApplicationId applicationId) {
        return controller().tenants().get(applicationId.tenant())
                .orElseThrow(() -> new IllegalStateException("No tenant found for application " + applicationId));
    }

    protected void store(IssueId issueId, TenantAndApplicationId applicationId) {
        controller().applications().lockApplicationIfPresent(applicationId, application ->
                controller().applications().store(application.withOwnershipIssueId(issueId)));
    }
}