aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/DeploymentJobs.java
blob: dc1dcb2d5ed6fa21b895c4bc2672155d0caec60c (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// 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.ImmutableMap;
import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.Zone;
import com.yahoo.vespa.hosted.controller.Controller;

import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * Information about which deployment jobs an application should run and their current status.
 * This is immutable.
 * 
 * @author bratseth
 */
public class DeploymentJobs {

    private final Optional<Long> projectId;
    private final ImmutableMap<JobType, JobStatus> status;
    private final Optional<String> jiraIssueId;
    private final boolean selfTriggering; // TODO: Remove this when no projects are self-triggering.

    /** Creates an empty set of deployment jobs */
    public DeploymentJobs(long projectId) {
        this(Optional.of(projectId), ImmutableMap.of(), Optional.empty(),true);
    }
    
    public DeploymentJobs(Optional<Long> projectId, Collection<JobStatus> jobStatusEntries, Optional<String> jiraIssueId, boolean selfTriggering) {
        this(projectId, asMap(jobStatusEntries), jiraIssueId, selfTriggering);
    }
    
    private DeploymentJobs(Optional<Long> projectId, Map<JobType, JobStatus> status, Optional<String> jiraIssueId, boolean selfTriggering) {
        Objects.requireNonNull(projectId, "projectId cannot be null");
        Objects.requireNonNull(status, "status cannot be null");
        Objects.requireNonNull(jiraIssueId, "jiraIssueId cannot be null");
        this.projectId = projectId;
        this.status = ImmutableMap.copyOf(status);
        this.jiraIssueId = jiraIssueId;
        this.selfTriggering = selfTriggering;
    }
    
    private static Map<JobType, JobStatus> asMap(Collection<JobStatus> jobStatusEntries) {
        ImmutableMap.Builder<JobType, JobStatus> b = new ImmutableMap.Builder<>();
        for (JobStatus jobStatusEntry : jobStatusEntries)
            b.put(jobStatusEntry.type(), jobStatusEntry);
        return b.build();
    }

    /** Return a new instance with the given completion */
    public DeploymentJobs withCompletion(JobReport report, Instant notificationTime, Controller controller) {
        Map<JobType, JobStatus> status = new LinkedHashMap<>(this.status);
        status.compute(report.jobType(), (type, job) -> {
            if (job == null) job = JobStatus.initial(report.jobType());
            return job.withCompletion(report.jobError(), notificationTime, controller);
        });
        return new DeploymentJobs(Optional.of(report.projectId()), status, jiraIssueId, report.selfTriggering());
    }

    public DeploymentJobs withTriggering(DeploymentJobs.JobType jobType, 
                                         Version version, 
                                         Optional<ApplicationRevision> revision, 
                                         Instant triggerTime) {
        Map<JobType, JobStatus> status = new LinkedHashMap<>(this.status);
        status.compute(jobType, (type, job) -> {
            if (job == null) job = JobStatus.initial(jobType);
            return job.withTriggering(version, revision, triggerTime);
        });
        return new DeploymentJobs(projectId, status, jiraIssueId, selfTriggering);
    }

    public DeploymentJobs withProjectId(long projectId) {
        return new DeploymentJobs(Optional.of(projectId), status, jiraIssueId, selfTriggering);
    }

    public DeploymentJobs withJiraIssueId(Optional<String> jiraIssueId) {
        return new DeploymentJobs(projectId, status, jiraIssueId, selfTriggering);
    }

    public DeploymentJobs without(JobType job) {
        Map<JobType, JobStatus> status = new HashMap<>(this.status);
        status.remove(job);
        return new DeploymentJobs(projectId, status, jiraIssueId, selfTriggering);
    }
    
    public DeploymentJobs asSelfTriggering(boolean selfTriggering) {
        return new DeploymentJobs(projectId, status, jiraIssueId, selfTriggering);
    }

    /** Returns an immutable map of the status entries in this */
    public Map<JobType, JobStatus> jobStatus() { return status; }

    /** Returns whether this application's deployment jobs trigger each other, and should be left alone, or not. */
    public boolean isSelfTriggering() { return selfTriggering; }

    /** Returns whether this has some job status which is not a success */
    public boolean hasFailures() {
        return status.values().stream().anyMatch(jobStatus -> ! jobStatus.isSuccess());
    }

    /** Returns whether any job is currently in progress */
    public boolean inProgress() {
        return status.values().stream().anyMatch(JobStatus::inProgress);
    }

    /** Returns whether change can be deployed to the given environment */
    public boolean isDeployableTo(Environment environment, Optional<Change> change) {
        if (environment == null || !change.isPresent()) {
            return true;
        }
        if (environment == Environment.staging) {
            return isSuccessful(change.get(), JobType.systemTest);
        } else if (environment == Environment.prod) {
            return isSuccessful(change.get(), JobType.stagingTest);
        }
        return true; // other environments do not have any preconditions
    }

    /** Returns whether change has been deployed completely */
    public boolean isDeployed(Change change) {
        return status.values().stream()
                .filter(status -> status.type().isProduction())
                .allMatch(status -> isSuccessful(change, status.type()));
    }

    /** Returns whether job has completed successfully */
    public boolean isSuccessful(Change change, JobType jobType) {
        return Optional.ofNullable(jobStatus().get(jobType))
                .filter(JobStatus::isSuccess)
                .filter(status -> status.lastCompletedFor(change))
                .isPresent();
    }
    
    /** Returns the oldest failingSince time of the jobs of this, or null if none are failing */
    public Instant failingSince() {
        Instant failingSince = null;
        for (JobStatus jobStatus : jobStatus().values()) {
            if (jobStatus.isSuccess()) continue;
            if (failingSince == null || failingSince.isAfter(jobStatus.firstFailing().get().at()))
                failingSince = jobStatus.firstFailing().get().at();
        }
        return failingSince;
    }

    /**
     * Returns the id of the Screwdriver project running these deployment jobs 
     * - or empty when this is not known or does not exist.
     * It is not known until the jobs have run once and reported back to the controller.
     */
    public Optional<Long> projectId() { return projectId; }

    public Optional<String> jiraIssueId() { return jiraIssueId; }

    /** Job types that exist in the build system */
    public enum JobType {

        component("component"),
        systemTest("system-test", zone(SystemName.cd, "test", "cd-us-central-1"), zone("test", "us-east-1")),
        stagingTest("staging-test", zone(SystemName.cd, "staging", "cd-us-central-1"), zone("staging", "us-east-3")),
        productionCorpUsEast1("production-corp-us-east-1", zone("prod", "corp-us-east-1")),
        productionUsEast3("production-us-east-3", zone("prod", "us-east-3")),
        productionUsWest1("production-us-west-1", zone("prod", "us-west-1")),
        productionUsCentral1("production-us-central-1", zone("prod", "us-central-1")),
        productionApNortheast1("production-ap-northeast-1", zone("prod", "ap-northeast-1")),
        productionApNortheast2("production-ap-northeast-2", zone("prod", "ap-northeast-2")),
        productionApSoutheast1("production-ap-southeast-1", zone("prod", "ap-southeast-1")),
        productionEuWest1("production-eu-west-1", zone("prod", "eu-west-1")),
        productionCdUsCentral1("production-cd-us-central-1", zone(SystemName.cd, "prod", "cd-us-central-1")),
        productionCdUsCentral2("production-cd-us-central-2", zone(SystemName.cd, "prod", "cd-us-central-2"));

        private final String id;
        private final Map<SystemName, Zone> zones;

        JobType(String id, Zone... zone) {
            this.id = id;
            Map<SystemName, Zone> zones = new HashMap<>();
            for (Zone z : zone) {
                if (zones.containsKey(z.system())) {
                    throw new IllegalArgumentException("A job can only map to a single zone per system");
                }
                zones.put(z.system(), z);
            }
            this.zones = Collections.unmodifiableMap(zones);
        }

        public String id() { return id; }

        /** Returns the zone for this job in the given system, or empty if this job does not have a zone */
        public Optional<Zone> zone(SystemName system) {
            return Optional.ofNullable(zones.get(system));
        }

        /** Returns whether this is a production job */
        public boolean isProduction() { return environment() == Environment.prod; }
        
        /** Returns the environment of this job type, or null if it does not have an environment */
        public Environment environment() {
            switch (this) {
                case component: return null;
                case systemTest: return Environment.test;
                case stagingTest: return Environment.staging;
                default: return Environment.prod;
            }
        }

        /** Returns the region of this job type, or null if it does not have a region */
        public Optional<RegionName> region(SystemName system) {
            return zone(system).map(Zone::region);
        }

        public static JobType fromId(String id) {
            switch (id) {
                case "component" : return component;
                case "system-test" : return systemTest;
                case "staging-test" : return stagingTest;
                case "production-corp-us-east-1" : return productionCorpUsEast1;
                case "production-us-east-3" : return productionUsEast3;
                case "production-us-west-1" : return productionUsWest1;
                case "production-us-central-1" : return productionUsCentral1;
                case "production-ap-northeast-1" : return productionApNortheast1;
                case "production-ap-northeast-2" : return productionApNortheast2;
                case "production-ap-southeast-1" : return productionApSoutheast1;
                case "production-eu-west-1" : return productionEuWest1;
                case "production-cd-us-central-1" : return productionCdUsCentral1;
                case "production-cd-us-central-2" : return productionCdUsCentral2;
                default : throw new IllegalArgumentException("Unknown job id '" + id + "'");
            }
        }
        
        /** Returns the job type for the given zone, or null if none */
        public static JobType from(SystemName system, com.yahoo.config.provision.Zone zone) {
           for (JobType job : values()) {
               Optional<com.yahoo.config.provision.Zone> jobZone = job.zone(system);
               if (jobZone.isPresent() && jobZone.get().equals(zone))
                   return job;
           }
           return null;
        }

        /** Returns the job job type for the given environment and region or null if none */
        public static JobType from(SystemName system, Environment environment, RegionName region) {
            switch (environment) {
                case test: return systemTest;
                case staging: return stagingTest;
            }
            return from(system, new com.yahoo.config.provision.Zone(environment, region));
        }

        private static Zone zone(SystemName system, String environment, String region) {
            return new Zone(system, Environment.from(environment), RegionName.from(region));
        }

        private static Zone zone(String environment, String region) {
            return new Zone(Environment.from(environment), RegionName.from(region));
        }
    }

    /** A job report. This class is immutable. */
    public static class JobReport {

        private final ApplicationId applicationId;
        private final JobType jobType;
        private final long projectId;
        private final long buildNumber;
        private final Optional<JobError> jobError;
        private final boolean selfTriggering;

        public JobReport(ApplicationId applicationId, JobType jobType, long projectId, long buildNumber,
                         Optional<JobError> jobError, boolean selfTriggering) {
            Objects.requireNonNull(applicationId, "applicationId cannot be null");
            Objects.requireNonNull(jobType, "jobType cannot be null");
            Objects.requireNonNull(jobError, "jobError cannot be null");
            this.applicationId = applicationId;
            this.projectId = projectId;
            this.buildNumber = buildNumber;
            this.jobType = jobType;
            this.jobError = jobError;
            this.selfTriggering = selfTriggering;
        }

        public ApplicationId applicationId() { return applicationId; }
        public JobType jobType() { return jobType; }
        public long projectId() { return projectId; }
        public long buildNumber() { return buildNumber; }
        public boolean success() { return !jobError.isPresent(); }
        public Optional<JobError> jobError() { return jobError; }
        public boolean selfTriggering() { return selfTriggering; }

    }

    public enum JobError {
        unknown,
        outOfCapacity;

        public static Optional<JobError> from(boolean success) {
            return Optional.of(success)
                    .filter(b -> !b)
                    .map(ignored -> unknown);
        }
    }

}