summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/ApplicationList.java
blob: a3fd0087662a62de076667edc79816e7d23b03a9 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.application;

import com.google.common.collect.ImmutableList;
import com.yahoo.component.Version;
import com.yahoo.config.application.api.DeploymentSpec.UpgradePolicy;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.vespa.hosted.controller.Application;
import com.yahoo.vespa.hosted.controller.ApplicationController;

import java.time.Instant;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * A list of applications which can be filtered in various ways.
 *
 * @author bratseth
 */
// TODO jvenstad: Make an AbstractFilteringList based on JobList and let this extend it for free not()s?
public class ApplicationList {

    private final ImmutableList<Application> list;

    private ApplicationList(Iterable<Application> applications) {
        this.list = ImmutableList.copyOf(applications);
    }

    // ----------------------------------- Factories

    public static ApplicationList from(Iterable<Application> applications) {
        return new ApplicationList(applications);
    }

    public static ApplicationList from(Collection<ApplicationId> ids, ApplicationController applications) {
        return listOf(ids.stream().map(applications::require));
    }

    // ----------------------------------- Accessors

    /** Returns the applications in this as an immutable list */
    public List<Application> asList() { return list; }

    /** Returns the ids of the applications in this as an immutable list */
    public List<ApplicationId> idList() { return ImmutableList.copyOf(list.stream().map(Application::id)::iterator); }

    public boolean isEmpty() { return list.isEmpty(); }

    public int size() { return list.size(); }

    // ----------------------------------- Filters

    /** Returns the subset of applications which are upgrading (to any version), not considering block windows. */
    public ApplicationList upgrading() {
        return listOf(list.stream().filter(application -> application.change().platform().isPresent()));
    }

    /** Returns the subset of applications which are currently upgrading to the given version */
    public ApplicationList upgradingTo(Version version) {
        return listOf(list.stream().filter(application -> isUpgradingTo(version, application)));
    }

    /** Returns the subset of applications which are not pinned to a certain Vespa version. */
    public ApplicationList notPinned() {
        return listOf(list.stream().filter(application -> ! application.change().isPinned()));
    }

    /** Returns the subset of applications which are currently not upgrading to the given version */
    public ApplicationList notUpgradingTo(Version version) {
        return listOf(list.stream().filter(application -> ! isUpgradingTo(version, application)));
    }

    /**
     * Returns the subset of applications which are currently not upgrading to the given version,
     * or returns all if no version is specified
     */
    public ApplicationList notUpgradingTo(Optional<Version> version) {
        if ( ! version.isPresent()) return this;
        return notUpgradingTo(version.get());
    }

    /** Returns the subset of applications which have changes left to deploy; blocked, or deploying */
    public ApplicationList withChanges() {
        return listOf(list.stream().filter(application -> application.change().isPresent() || application.outstandingChange().isPresent()));
    }

    /** Returns the subset of applications which are currently not deploying a change */
    public ApplicationList notDeploying() {
        return listOf(list.stream().filter(application -> ! application.change().isPresent()));
    }

    /** Returns the subset of applications which currently does not have any failing jobs */
    public ApplicationList notFailing() {
        return listOf(list.stream().filter(application -> ! application.deploymentJobs().hasFailures()));
    }

    /** Returns the subset of applications which currently have failing jobs */
    public ApplicationList failing() {
        return listOf(list.stream().filter(application -> application.deploymentJobs().hasFailures()));
    }

    /** Returns the subset of applications which have been failing an upgrade to the given version since the given instant */
    public ApplicationList failingUpgradeToVersionSince(Version version, Instant threshold) {
        return listOf(list.stream().filter(application -> failingUpgradeToVersionSince(application, version, threshold)));
    }

    /** Returns the subset of applications which have been failing an application change since the given instant */
    public ApplicationList failingApplicationChangeSince(Instant threshold) {
        return listOf(list.stream().filter(application -> failingApplicationChangeSince(application, threshold)));
    }

    /** Returns the subset of applications which currently does not have any failing jobs on the given version */
    public ApplicationList notFailingOn(Version version) {
        return listOf(list.stream().filter(application -> ! failingOn(version, application)));
    }

    /** Returns the subset of applications which have at least one production deployment */
    public ApplicationList hasDeployment() {
        return listOf(list.stream().filter(a -> !a.productionDeployments().isEmpty()));
    }

    /** Returns the subset of applications which started failing on the given version */
    public ApplicationList startedFailingOn(Version version) {
        return listOf(list.stream().filter(application -> ! JobList.from(application).firstFailing().on(version).isEmpty()));
    }

    /** Returns the subset of applications which has the given upgrade policy */
    public ApplicationList with(UpgradePolicy policy) {
        return listOf(list.stream().filter(a ->  a.deploymentSpec().upgradePolicy() == policy));
    }

    /** Returns the subset of applications which does not have the given upgrade policy */
    public ApplicationList without(UpgradePolicy policy) {
        return listOf(list.stream().filter(a ->  a.deploymentSpec().upgradePolicy() != policy));
    }

    /** Returns the subset of applications which have at least one deployment on a lower version than the given one */
    public ApplicationList onLowerVersionThan(Version version) {
        return listOf(list.stream()
                          .filter(a -> a.productionDeployments().values().stream()
                                                                         .anyMatch(d -> d.version().isBefore(version))));
    }

    /** Returns the subset of applications which have a project ID */
    public ApplicationList withProjectId() {
        return listOf(list.stream().filter(a -> a.deploymentJobs().projectId().isPresent()));
    }

    /** Returns the subset of applications which have at least one production deployment */
    public ApplicationList hasProductionDeployment() {
        return listOf(list.stream().filter(a -> ! a.productionDeployments().isEmpty()));
    }

    /** Returns the subset of applications that are allowed to upgrade at the given time */
    public ApplicationList canUpgradeAt(Instant instant) {
        return listOf(list.stream().filter(a -> a.deploymentSpec().canUpgradeAt(instant)));
    }

    /**
     * Returns the subset of applications that hasn't pinned to an an earlier major version than the given one.
     *
     * @param targetMajorVersion the target major version which applications returned allows upgrading to
     * @param defaultMajorVersion the default major version to assume for applications not specifying one
     */
    public ApplicationList allowMajorVersion(int targetMajorVersion, int defaultMajorVersion) {
        return listOf(list.stream().filter(a -> a.deploymentSpec().majorVersion().orElse(a.majorVersion().orElse(defaultMajorVersion))
                                           >= targetMajorVersion));
    }

    /** Returns the first n application in this (or all, if there are less than n). */
    public ApplicationList first(int n) {
        if (list.size() < n) return this;
        return new ApplicationList(list.subList(0, n));
    }

     // ----------------------------------- Sorting

    /**
     * Returns this list sorted by increasing deployed version.
     * If multiple versions are deployed the oldest is used.
     * Applications without any deployments are ordered first.
     */
    public ApplicationList byIncreasingDeployedVersion() {
        return listOf(list.stream().sorted(Comparator.comparing(application -> application.oldestDeployedPlatform().orElse(Version.emptyVersion))));
    }

    // ----------------------------------- Internal helpers

    private static boolean isUpgradingTo(Version version, Application application) {
        return application.change().platform().equals(Optional.of(version));
    }

    private static boolean failingOn(Version version, Application application) {
        return ! JobList.from(application)
                .failing()
                .lastCompleted().on(version)
                .isEmpty();
    }

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

    private static boolean failingApplicationChangeSince(Application application, Instant threshold) {
        return ! JobList.from(application)
                .failingApplicationChange()
                .firstFailing().before(threshold)
                .isEmpty();
    }

    /** Convenience converter from a stream to an ApplicationList */
    private static ApplicationList listOf(Stream<Application> applications) {
        return from(applications::iterator);
    }

}