blob: e3bb768a9c2eae384f6708b9c28577ed0ae2cc27 (
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.archive;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.CloudAccount;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.Zone;
import com.yahoo.lang.CachedSupplier;
import com.yahoo.vespa.curator.Lock;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.persistence.CuratorDb;
import java.time.Duration;
import java.util.Optional;
import java.util.function.Function;
/**
* Thread safe class to get and set archive URI for given account and tenants.
*
* @author freva
*/
public class ArchiveUriManager {
private static final Duration cacheTtl = Duration.ofMinutes(1);
private final CuratorDb db;
private final Zone zone;
private final CachedSupplier<ArchiveUris> archiveUris;
public ArchiveUriManager(CuratorDb db, Zone zone) {
this.db = db;
this.zone = zone;
this.archiveUris = new CachedSupplier<>(db::readArchiveUris, cacheTtl);
}
public ArchiveUris archiveUris() {
return archiveUris.get();
}
/** Returns the archive URI to use for given node */
public Optional<String> archiveUriFor(Node node) {
if (node.allocation().isEmpty()) return Optional.empty();
ApplicationId app = node.allocation().get().owner();
return Optional.ofNullable(node.cloudAccount().isExclave(zone) ?
archiveUris.get().accountArchiveUris().get(node.cloudAccount()) :
archiveUris.get().tenantArchiveUris().get(app.tenant()))
.map(uri -> {
StringBuilder sb = new StringBuilder(100).append(uri)
.append(app.tenant().value()).append('/')
.append(app.application().value()).append('/')
.append(app.instance().value()).append('/')
.append(node.allocation().get().membership().cluster().id().value()).append('/');
for (char c: node.hostname().toCharArray()) {
if (c == '.') break;
sb.append(c);
}
return sb.append('/').toString();
});
}
/** Set (or remove, if archiveURI is empty) archive URI to use for given tenant */
public void setArchiveUri(TenantName tenant, Optional<String> archiveUri) {
setArchiveUri(au -> au.with(tenant, archiveUri));
}
/** Set (or remove, if archiveURI is empty) archive URI to use for given account */
public void setArchiveUri(CloudAccount account, Optional<String> archiveUri) {
if (!account.isExclave(zone) || account.isUnspecified())
throw new IllegalArgumentException("Cannot set archive URI for non-enclave account: " + account);
setArchiveUri(au -> au.with(account, archiveUri));
}
private void setArchiveUri(Function<ArchiveUris, ArchiveUris> mapper) {
try (Lock lock = db.lockArchiveUris()) {
ArchiveUris archiveUris = db.readArchiveUris();
ArchiveUris updated = mapper.apply(archiveUris);
if (archiveUris.equals(updated)) return; // No change
db.writeArchiveUris(updated);
this.archiveUris.invalidate(); // Throw away current cache
}
}
}
|