summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/ApplicationList.java
blob: 8978af78574145a2143be45720f98260f4feaa27 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// 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.yahoo.collections.AbstractFilteringList;
import com.yahoo.component.Version;
import com.yahoo.config.application.api.DeploymentSpec;
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 com.yahoo.vespa.hosted.controller.Instance;

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

/**
 * A list of applications which can be filtered in various ways.
 *
 * @author jonmv
 */
public class ApplicationList extends AbstractFilteringList<Application, ApplicationList> {

    private ApplicationList(Collection<? extends Application> applications, boolean negate) {
        super(applications, negate, ApplicationList::new);
    }

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

    public static ApplicationList from(Collection<? extends Application> applications) {
        return new ApplicationList(applications, false);
    }

    public static ApplicationList from(Collection<ApplicationId> ids, ApplicationController applications) {
        return from(ids.stream()
                       .map(TenantAndApplicationId::from)
                       .distinct()
                       .map(applications::requireApplication)
                       .collect(Collectors.toUnmodifiableList()));
    }

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

    /** Returns the ids of the applications in this as an immutable list */
    public List<TenantAndApplicationId> idList() {
        return mapToList(Application::id);
    }

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

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

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

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

    /** Returns the subset of applications which are currently not upgrading to the given version */
    public ApplicationList notUpgradingTo(Version version) {
        return notUpgradingTo(Collections.singletonList(version));
    }

    public ApplicationList notFailingUpgrade() {
        return matching(application -> application.instances().values().stream()
                                                    .allMatch(instance -> JobList.from(instance)
                                                                                 .failing()
                                                                                 .not().failingApplicationChange()
                                                                                 .isEmpty()));
    }

    /** Returns the subset of applications which are currently not upgrading to any of the given versions */
    public ApplicationList notUpgradingTo(Collection<Version> versions) {
        return matching(application -> versions.stream().noneMatch(version -> 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.isEmpty()) return this;
        return notUpgradingTo(version.get());
    }

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

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

    /** Returns the subset of applications which currently does not have any failing jobs */
    public ApplicationList notFailing() {
        return matching(application -> application.instances().values().stream()
                                                    .noneMatch(instance -> instance.deploymentJobs().hasFailures()));
    }

    /** Returns the subset of applications which currently have failing jobs */
    public ApplicationList failing() {
        return matching(application -> application.instances().values().stream()
                                                    .anyMatch(instance -> instance.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 matching(application -> application.instances().values().stream()
                                                    .anyMatch(instance -> failingUpgradeToVersionSince(instance, version, threshold)));
    }

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

    /** Returns the subset of applications which currently does not have any failing jobs on the given version */
    public ApplicationList notFailingOn(Version version) {
        return matching(application -> application.instances().values().stream()
                          .noneMatch(instance -> failingOn(version, instance)));
    }

    /** Returns the subset of applications which have at least one production deployment */
    public ApplicationList withProductionDeployment() {
        return matching(application -> application.instances().values().stream()
                                                    .anyMatch(instance -> instance.productionDeployments().size() > 0));
    }

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

    /** Returns the subset of applications which has the given upgrade policy */
    // TODO jonmv: Make this instance based when instances are orchestrated, and deployments reported per instance.
    public ApplicationList with(UpgradePolicy policy) {
        return matching(application ->  application.deploymentSpec().instances().stream()
                                                     .anyMatch(instance -> instance.upgradePolicy() == policy));
    }

    /** Returns the subset of applications which does not have the given upgrade policy */
    // TODO jonmv: Make this instance based when instances are orchestrated, and deployments reported per instance.
    public ApplicationList without(UpgradePolicy policy) {
        return matching(application ->  application.deploymentSpec().instances().stream()
                                                     .allMatch(instance -> instance.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 matching(application -> application.instances().values().stream()
                                                    .flatMap(instance -> instance.productionDeployments().values().stream())
                                                    .anyMatch(deployment -> deployment.version().isBefore(version)));
    }

    /** Returns the subset of applications which have a project ID */
    public ApplicationList withProjectId() {
        return matching(application -> application.projectId().isPresent());
    }

    /** Returns the subset of applications that are allowed to upgrade at the given time */
    public ApplicationList canUpgradeAt(Instant instant) {
        return matching(application -> application.deploymentSpec().instances().stream()
                                                    .allMatch(instance -> instance.canUpgradeAt(instant)));
    }

    /** Returns the subset of applications that have at least one assigned rotation */
    public ApplicationList hasRotation() {
        return matching(application -> application.instances().values().stream()
                                                    .anyMatch(instance -> ! instance.rotations().isEmpty()));
    }

    /**
     * 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 matching(application -> targetMajorVersion <= application.deploymentSpec().majorVersion()
                                                                          .orElse(application.majorVersion()
                                                                                             .orElse(defaultMajorVersion)));
    }

    /** Returns the subset of application which have submitted a non-empty deployment spec. */
    public ApplicationList withDeploymentSpec() {
        return matching(application -> ! DeploymentSpec.empty.equals(application.deploymentSpec()));
    }

     // ----------------------------------- 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 sortedBy(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, Instance instance) {
        return ! JobList.from(instance)
                        .failing()
                        .lastCompleted().on(version)
                        .isEmpty();
    }

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

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

}