summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/deployment/Versions.java
blob: bef61dda8756ce53aa8bb8b4d6b447dfaad93678 (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
// 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.deployment;

import com.yahoo.component.Version;
import com.yahoo.vespa.hosted.controller.Application;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.JobType;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.ApplicationVersion;
import com.yahoo.vespa.hosted.controller.application.Change;
import com.yahoo.vespa.hosted.controller.application.Deployment;
import com.yahoo.vespa.hosted.controller.application.JobStatus;

import java.util.Objects;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
 * Source and target versions for an application.
 *
 * @author jvenstad
 * @author mpolden
 */
public class Versions {

    private final Version targetPlatform;
    private final ApplicationVersion targetApplication;
    private final Optional<Version> sourcePlatform;
    private final Optional<ApplicationVersion> sourceApplication;

    public Versions(Version targetPlatform, ApplicationVersion targetApplication, Optional<Version> sourcePlatform,
                    Optional<ApplicationVersion> sourceApplication) {
        if (sourcePlatform.isPresent() ^ sourceApplication.isPresent())
            throw new IllegalArgumentException("Sources must both be present or absent.");

        this.targetPlatform = requireNonNull(targetPlatform);
        this.targetApplication = requireNonNull(targetApplication);
        this.sourcePlatform = requireNonNull(sourcePlatform);
        this.sourceApplication = requireNonNull(sourceApplication);
    }

    /** Target platform version for this */
    public Version targetPlatform() {
        return targetPlatform;
    }

    /** Target application version for this */
    public ApplicationVersion targetApplication() {
        return targetApplication;
    }

    /** Source platform version for this */
    public Optional<Version> sourcePlatform() {
        return sourcePlatform;
    }

    /** Source application version for this */
    public Optional<ApplicationVersion> sourceApplication() {
        return sourceApplication;
    }

    /** Returns whether source versions are present and match those of the given job run */
    public boolean sourcesMatchIfPresent(JobStatus.JobRun jobRun) {
        return (!sourcePlatform.filter(version -> !version.equals(targetPlatform)).isPresent() ||
                sourcePlatform.equals(jobRun.sourcePlatform())) &&
               (!sourceApplication.filter(version -> !version.equals(targetApplication)).isPresent() ||
                sourceApplication.equals(jobRun.sourceApplication()));
    }

    /** Returns whether source versions are present and match those of the given job other versions. */
    public boolean sourcesMatchIfPresent(Versions versions) {
        return ( ! sourcePlatform.filter(version -> ! version.equals(targetPlatform)).isPresent() ||
                sourcePlatform.equals(versions.sourcePlatform())) &&
               ( ! sourceApplication.filter(version -> ! version.equals(targetApplication)).isPresent() ||
                sourceApplication.equals(versions.sourceApplication()));
    }

    public boolean targetsMatch(Versions versions) {
        return targetPlatform.equals(versions.targetPlatform()) &&
               targetApplication.equals(versions.targetApplication());
    }

    public boolean targetsMatch(JobStatus.JobRun jobRun) {
        return targetPlatform.equals(jobRun.platform()) &&
               targetApplication.equals(jobRun.application());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if ( ! (o instanceof Versions)) return false;
        Versions versions = (Versions) o;
        return Objects.equals(targetPlatform, versions.targetPlatform) &&
               Objects.equals(targetApplication, versions.targetApplication) &&
               Objects.equals(sourcePlatform, versions.sourcePlatform) &&
               Objects.equals(sourceApplication, versions.sourceApplication);
    }

    @Override
    public int hashCode() {
        return Objects.hash(targetPlatform, targetApplication, sourcePlatform, sourceApplication);
    }

    @Override
    public String toString() {
        return String.format("platform %s%s, application %s%s",
                             sourcePlatform.filter(source -> !source.equals(targetPlatform))
                                           .map(source -> source + " -> ").orElse(""),
                             targetPlatform,
                             sourceApplication.filter(source -> !source.equals(targetApplication))
                                              .map(source -> source.id() + " -> ").orElse(""),
                             targetApplication.id());
    }

    /** Create versions using change contained in application */
    public static Versions from(Application application, Version defaultPlatformVersion) {
        return from(application.change(), application, Optional.empty(), defaultPlatformVersion);
    }

    /** Create versions using given change and application */
    public static Versions from(Change change, Application application, Optional<Deployment> deployment,
                                Version defaultPlatformVersion) {
        return new Versions(targetPlatform(application, change, deployment, defaultPlatformVersion),
                            targetApplication(application, change, deployment),
                            deployment.map(Deployment::version),
                            deployment.map(Deployment::applicationVersion));
    }

    private static Version targetPlatform(Application application, Change change, Optional<Deployment> deployment,
                                          Version defaultVersion) {
        if (change.isPinned() && change.platform().isPresent())
            return change.platform().get();

        return max(change.platform(), deployment.map(Deployment::version))
                .orElse(application.oldestDeployedPlatform()
                                   .orElse(defaultVersion));
    }

    private static ApplicationVersion targetApplication(Application application, Change change,
                                                        Optional<Deployment> deployment) {
        return max(change.application(), deployment.map(Deployment::applicationVersion))
                .orElse(application.oldestDeployedApplication()
                                   .orElse(application.deploymentJobs().jobStatus().get(JobType.component)
                                                      .lastSuccess()
                                                      .get()
                                                      .application()));
    }

    private static <T extends Comparable<T>> Optional<T> max(Optional<T> o1, Optional<T> o2) {
        return ! o1.isPresent() ? o2 : ! o2.isPresent() ? o1 : o1.get().compareTo(o2.get()) >= 0 ? o1 : o2;
    }

}