aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/application/ApplicationVersions.java
blob: 458815c500bd6454d78ae006158a034863213b6c (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.application;

import com.yahoo.component.Version;
import com.yahoo.config.model.api.HostInfo;
import com.yahoo.config.provision.ApplicationId;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * Immutable set of {@link Application}s with the same {@link ApplicationId}, applications have difference vespa versions.
 *
 * @author vegard
 */
public final class ApplicationVersions {

    private final Version latestVersion;
    private final ApplicationId applicationId;
    private final long generation;
    private final HashMap<Version, Application> applications = new HashMap<>();

    private ApplicationVersions(List<Application> applications) {
        if (applications.isEmpty()) throw new IllegalArgumentException("application list cannot be empty");

        Application firstApp = applications.get(0);
        applicationId = firstApp.getId();
        generation = firstApp.getApplicationGeneration();
        for (Application application : applications) {
            this.applications.put(application.getVespaVersion(), application);
            ApplicationId applicationId = application.getId();
            if ( ! applicationId.equals(this.applicationId)) {
                throw new IllegalArgumentException("Trying to create set with different application ids (" +
                                                   application + " and " + this.applicationId + ")");
            }
            if ( ! application.getApplicationGeneration().equals(generation)) {
                throw new IllegalArgumentException("Trying to create set with different generations ("  +
                                                   generation + " and " + this.generation + ")");
            }
        }
        latestVersion = this.applications.keySet().stream().max(Version::compareTo).get();
    }

    public static ApplicationVersions fromList(List<Application> applications) {
        return new ApplicationVersions(applications);
    }

    // For testing
    public static ApplicationVersions from(Application application) {
        return fromList(List.of(application));
    }

    /**
     * Returns an application for the specified version, or the latest if not specified, or if the given version is not
     * available and the latest is a permissible substitution.
     *
     * @throws VersionDoesNotExistException if the specified version is not available and the latest version is not
     *                                      a permissible substitution
     */
    public Application getForVersionOrLatest(Optional<Version> optionalVersion, Instant now) {
        Version version = optionalVersion.orElse(latestVersion);
        return resolveForVersion(version, now)
                 .orElseThrow(() -> new VersionDoesNotExistException(applicationId + " has no model for Vespa version " + version));
    }

    private Optional<Application> resolveForVersion(Version vespaVersion, Instant now) {
        Application application = applications.get(vespaVersion);
        if (application != null)
            return Optional.of(application);

        // Does the latest version specify we can use it regardless?
        Application latest = applications.get(latestVersion);
        if (latest.getModel().allowModelVersionMismatch(now))
            return Optional.of(latest);

        return Optional.empty();
    }

    /** Returns the application for the given version, or empty if not found */
    public Optional<Application> get(Version version) {
        return Optional.ofNullable(applications.get(version));
    }

    public ApplicationId getId() { return applicationId; }

    public Collection<String> allHosts() {
        return applications.values().stream()
                .flatMap(app -> app.getModel().getHosts().stream()
                        .map(HostInfo::getHostname))
                .toList();
    }

    public void updateHostMetrics() {
        applications.values().forEach(app -> app.updateHostMetrics(app.getModel().getHosts().size()));
    }

    public long applicationGeneration() {
        return generation;
    }

    List<Application> applications() {
        return new ArrayList<>(applications.values());
    }

    public List<Version> versions(ApplicationId applicationId) {
        return applications.values().stream()
                .filter(application -> application.getId().equals(applicationId))
                .map(Application::getVespaVersion)
                .sorted()
                .toList();
    }

}