blob: 76284d2bd948b91659056ba31b118084be5eaa8a (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.subscription.impl;
import com.yahoo.config.ConfigInstance;
import com.yahoo.config.subscription.ConfigSet;
import com.yahoo.vespa.config.ConfigKey;
import java.lang.reflect.Constructor;
/**
* Subscription on a programmatically built set of configs
*
* @author Vegard Havdal
*/
public class ConfigSetSubscription<T extends ConfigInstance> extends ConfigSubscription<T> {
private final ConfigSet set;
private final ConfigKey<T> subKey;
ConfigSetSubscription(ConfigKey<T> key, ConfigSet cset) {
super(key);
this.set = cset;
this.subKey = new ConfigKey<>(configClass, key.getConfigId());
if ( ! set.contains(subKey)) {
throw new IllegalArgumentException("The given ConfigSet " + set + " does not contain a config for " + subKey);
}
setGeneration(0L);
}
private boolean hasConfigChanged() {
T myInstance = getNewInstance();
ConfigState<T> configState = getConfigState();
// User forced reload
if (checkReloaded()) {
setConfigIfChanged(myInstance);
return true;
}
if (!myInstance.equals(configState.getConfig())) {
setConfigIncGen(myInstance);
return true;
}
return false;
}
@Override
public boolean nextConfig(long timeout) {
if (hasConfigChanged()) return true;
if (timeout <= 0) return false;
long startNanos = System.nanoTime();
do {
sleep();
if (hasConfigChanged()) return true;
} while (System.nanoTime() - startNanos < timeout * 1_000_000);
return false;
}
private void sleep() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException("nextConfig aborted", e);
}
}
@Override
public boolean subscribe(long timeout) {
return true;
}
@SuppressWarnings("unchecked")
private T getNewInstance() {
try {
ConfigInstance.Builder builder = set.get(subKey);
Constructor<?> constructor = builder.getClass().getDeclaringClass().getConstructor(builder.getClass());
return (T) constructor.newInstance(builder);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
|