aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/deployment/DeploymentStatusList.java
blob: 4a00a272c751a39f1644c2fea32eabbc402e815d (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.deployment;

import com.yahoo.collections.AbstractFilteringList;
import com.yahoo.component.Version;
import com.yahoo.vespa.hosted.controller.application.Change;

import java.time.Instant;
import java.util.Collection;

/**
 * List for filtering deployment status of applications, for inspection and decision making.
 *
 * @author jonmv
 */
public class DeploymentStatusList extends AbstractFilteringList<DeploymentStatus, DeploymentStatusList> {

    private DeploymentStatusList(Collection<? extends DeploymentStatus> items, boolean negate) {
        super(items, negate, DeploymentStatusList::new);
    }

    public static DeploymentStatusList from(Collection<? extends DeploymentStatus> status) {
        return new DeploymentStatusList(status, false);
    }

    /** Returns the subset of applications which have changes left to deploy; blocked, or deploying */
    public DeploymentStatusList withChanges() {
        return matching(status -> status.application().productionInstances().values().stream()
                                        .anyMatch(instance -> instance.change().hasTargets() || status.outstandingChange(instance.name()).hasTargets()));
    }

    /** Returns the subset of applications which have been failing an upgrade to the given version since the given instant */
    public DeploymentStatusList failingUpgradeToVersionSince(Version version, Instant threshold) {
        return matching(status -> status.instanceJobs().values().stream()
                                        .anyMatch(jobs -> failingUpgradeToVersionSince(jobs, version, threshold)));
    }

    /** Returns the subset of applications which have been failing an application change since the given instant */
    public DeploymentStatusList failingApplicationChangeSince(Instant threshold) {
        return matching(status -> status.instanceJobs().entrySet().stream()
                                        .anyMatch(jobs -> failingApplicationChangeSince(jobs.getValue(),
                                                                                        status.application().require(jobs.getKey().instance()).change(),
                                                                                        threshold)));
    }

    private static boolean failingUpgradeToVersionSince(JobList jobs, Version version, Instant threshold) {
        return ! jobs.not().failingApplicationChange()
                     .firstFailing().endedNoLaterThan(threshold)
                     .lastCompleted().on(version)
                     .isEmpty();
    }

    private static boolean failingApplicationChangeSince(JobList jobs, Change change, Instant threshold) {
        return change.revision().map(revision -> ! jobs.failingWithBrokenRevisionSince(revision, threshold).isEmpty()).orElse(false);
    }

}