blob: 681f7776581ba7c56fabbd3d3a9383260aaf83ec (
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
|
// 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;
import com.yahoo.config.model.api.ConfigDefinitionRepo;
import com.yahoo.vespa.config.ConfigCacheKey;
import com.yahoo.vespa.config.ConfigDefinitionKey;
import com.yahoo.vespa.config.PayloadChecksum;
import com.yahoo.vespa.config.buildergen.ConfigDefinition;
import com.yahoo.vespa.config.protocol.ConfigResponse;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import static com.yahoo.vespa.config.PayloadChecksum.Type.XXHASH64;
/**
* Cache that holds configs and config definitions (builtin and user config definitions).
*
* @author vegardh
*/
public class ServerCache {
private final ConfigDefinitionRepo builtinConfigDefinitions;
private final ConfigDefinitionRepo userConfigDefinitions;
// NOTE: The reason we do a double mapping here is to de-dupe configs that have the same checksum.
private final Map<ConfigCacheKey, PayloadChecksum> checksums = new ConcurrentHashMap<>();
private final Map<PayloadChecksum, ConfigResponse> checksumToConfig = new ConcurrentHashMap<>();
private final Object [] stripedLocks = new Object[113];
public ServerCache(ConfigDefinitionRepo builtinConfigDefinitions, ConfigDefinitionRepo userConfigDefinitions) {
this.builtinConfigDefinitions = builtinConfigDefinitions;
this.userConfigDefinitions = userConfigDefinitions;
for (int i = 0; i < stripedLocks.length; i++) {
stripedLocks[i] = new Object();
}
}
// For testing only
public ServerCache() {
this(new StaticConfigDefinitionRepo(), new UserConfigDefinitionRepo());
}
private void put(ConfigCacheKey key, ConfigResponse config) {
PayloadChecksum xxhash64 = config.getPayloadChecksums().getForType(XXHASH64);
checksums.put(key, xxhash64);
checksumToConfig.put(xxhash64, config);
}
ConfigResponse get(ConfigCacheKey key) {
PayloadChecksum xxhash64 = checksums.get(key);
if (xxhash64 == null) return null;
return checksumToConfig.get(xxhash64);
}
public ConfigResponse computeIfAbsent(ConfigCacheKey key, Function<ConfigCacheKey, ConfigResponse> mappingFunction) {
ConfigResponse config = get(key);
if (config != null) {
return config;
}
synchronized (stripedLocks[Math.abs(key.hashCode()%stripedLocks.length)]) {
PayloadChecksum xxhash64 = checksums.get(key);
if (xxhash64 == null) {
config = mappingFunction.apply(key);
put(key, config);
return config;
}
return checksumToConfig.get(xxhash64);
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Cache\n");
sb.append("builtin defs: ").append(builtinConfigDefinitions.getConfigDefinitions().size()).append("\n");
sb.append("user defs: ").append(userConfigDefinitions.getConfigDefinitions().size()).append("\n");
sb.append("md5sums: ").append(checksums.size()).append("\n");
sb.append("md5ToConfig: ").append(checksumToConfig.size()).append("\n");
return sb.toString();
}
public ConfigDefinition getDef(ConfigDefinitionKey defKey) {
ConfigDefinition def = userConfigDefinitions.get(defKey);
return (def != null) ? def : builtinConfigDefinitions.getConfigDefinitions().get(defKey);
}
/**
* The number of different {@link ConfigResponse} elements
* @return elems
*/
public int configElems() {
return checksumToConfig.size();
}
/**
* The number of different key→checksum mappings
* @return elems
*/
public int checkSumElems() {
return checksums.size();
}
}
|