blob: 7c429ed9dcdfbfec79fd88c22330ea255d8e600a (
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
|
// Copyright 2017 Yahoo Holdings. 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.config.model.api.HostInfo;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.component.Version;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
/**
* Immutable set of {@link Application}s with the same {@link ApplicationId}. With methods for getting defaults.
*
* @author vegard
*/
public final class ApplicationSet {
private final Version latestVersion;
// TODO: Should not need these as part of application?
private final ApplicationId applicationId;
private final long generation;
private final HashMap<Version, Application> applications = new HashMap<>();
private ApplicationSet(List<Application> applicationList) {
applicationId = applicationList.get(0).getId();
generation = applicationList.get(0).getApplicationGeneration();
for (Application application : applicationList) {
applications.put(application.getVespaVersion(), application);
if (!application.getId().equals(applicationId)) {
throw new IllegalArgumentException("Trying to create set with different application ids");
}
}
latestVersion = applications.keySet().stream().max((a, b) -> a.compareTo(b)).get();
}
/**
* Returns 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, if available */
public Optional<Application> get(Version version) {
return Optional.ofNullable(applications.get(version));
}
public ApplicationId getId() { return applicationId; }
public static ApplicationSet fromList(List<Application> applications) {
return new ApplicationSet(applications);
}
public static ApplicationSet fromSingle(Application application) {
return fromList(Arrays.asList(application));
}
public Collection<String> getAllHosts() {
return applications.values().stream()
.flatMap(app -> app.getModel().getHosts().stream()
.map(HostInfo::getHostname))
.collect(Collectors.toList());
}
public void updateHostMetrics() {
for (Application application : applications.values()) {
application.updateHostMetrics(application.getModel().getHosts().size());
}
}
public long getApplicationGeneration() {
return generation;
}
List<Application> getAllApplications() {
return new ArrayList<>(applications.values());
}
public List<Version> getAllApplicationVersions() {
return new ArrayList<>(applications.keySet());
}
}
|