blob: 60c702895f86865a60dd3cbb1a6ff55a7bbfc4e3 (
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 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.subscription.impl;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import com.yahoo.config.ConfigInstance;
import com.yahoo.config.ConfigurationRuntimeException;
import com.yahoo.config.subscription.CfgConfigPayloadBuilder;
import com.yahoo.config.subscription.ConfigInterruptedException;
import com.yahoo.config.subscription.ConfigSubscriber;
import com.yahoo.io.IOUtils;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.vespa.config.ConfigPayload;
import com.yahoo.log.LogLevel;
/**
* Subscription used when config id is file:...
* @author vegardh
* @since 5.1
*
*/
public class FileConfigSubscription<T extends ConfigInstance> extends ConfigSubscription<T> {
final File file;
long ts;
FileConfigSubscription(ConfigKey<T> key, ConfigSubscriber subscriber, File f) {
super(key, subscriber);
setGeneration(0L);
file=f;
if (!file.exists() && !file.isFile())
throw new IllegalArgumentException("Not a file: "+file);
}
@Override
public boolean nextConfig(long timeout) {
if (!file.exists() && !file.isFile()) throw new IllegalArgumentException("Not a file: "+file);
if (checkReloaded()) {
log.log(LogLevel.DEBUG, "User forced config reload at " + System.currentTimeMillis());
// User forced reload
updateConfig();
log.log(LogLevel.DEBUG, "Config updated at " + System.currentTimeMillis() + ", changed: " + isConfigChanged());
log.log(LogLevel.DEBUG, "Config: " + config.toString());
return true;
}
if (file.lastModified()!=ts) {
updateConfig();
generation++;
setGenerationChanged(true);
return true;
}
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
throw new ConfigInterruptedException(e);
}
// These shouldn't be checked anywhere since we return false now, but setting them still
setGenerationChanged(false);
setConfigChanged(false);
return false;
}
private void updateConfig() {
ts=file.lastModified();
ConfigInstance prev = config;
try {
ConfigPayload payload = new CfgConfigPayloadBuilder().deserialize(Arrays.asList(IOUtils.readFile(file).split("\n")));
config = payload.toInstance(configClass, key.getConfigId());
} catch (IOException e) {
throw new ConfigurationRuntimeException(e);
}
setConfigChanged(!config.equals(prev));
}
@Override
public boolean subscribe(long timeout) {
return true;
}
}
|