Skip to content

Commit ec5ac70

Browse files
committed
feat(config):change the method of getting configuration file changes
1 parent ef6b534 commit ec5ac70

2 files changed

Lines changed: 63 additions & 117 deletions

File tree

framework/src/main/java/org/tron/core/config/args/DynamicArgs.java

Lines changed: 53 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44

55
import com.typesafe.config.Config;
66
import java.io.File;
7-
import java.io.IOException;
87
import java.net.InetAddress;
98
import java.net.InetSocketAddress;
10-
import java.nio.file.FileSystems;
11-
import java.nio.file.Path;
12-
import java.nio.file.StandardWatchEventKinds;
13-
import java.nio.file.WatchEvent;
14-
import java.nio.file.WatchKey;
15-
import java.nio.file.WatchService;
169
import java.util.List;
10+
import java.util.concurrent.Executors;
11+
import java.util.concurrent.ScheduledExecutorService;
12+
import java.util.concurrent.TimeUnit;
13+
1714
import lombok.extern.slf4j.Slf4j;
1815
import org.springframework.stereotype.Component;
1916
import org.tron.common.parameter.CommonParameter;
@@ -27,78 +24,54 @@
2724
public class DynamicArgs {
2825
private final CommonParameter parameter = Args.getInstance();
2926

30-
private volatile boolean shutdown = false;
27+
private long lastModified = 0;
28+
29+
private ScheduledExecutorService reloadExecutor = Executors.newSingleThreadScheduledExecutor();
3130

3231
public void init() {
3332
if (parameter.isDynamicConfigEnable()) {
34-
new Thread(this::start, "DynamicArgs").start();
33+
File config = getConfigFile();
34+
if (config != null) {
35+
lastModified = config.lastModified();
36+
} else {
37+
return;
38+
}
39+
logger.info("Start the dynamic loading configuration service");
40+
reloadExecutor.scheduleWithFixedDelay(() -> {
41+
try {
42+
run();
43+
} catch (Exception e) {
44+
logger.error("Exception caught when reloading configuration", e);
45+
}
46+
}, 100, 10, TimeUnit.SECONDS);
3547
}
3648
}
3749

38-
public void start() {
39-
WatchService watchService;
40-
Path path;
41-
String confFileName;
42-
try {
43-
logger.info("Start the dynamic loading configuration service");
44-
String confFile;
45-
if (isNoneBlank(parameter.getShellConfFileName())) {
46-
confFile = parameter.getShellConfFileName();
47-
} else {
48-
confFile = Constant.TESTNET_CONF;
49-
//logger.warn("Configuration path is required!");
50-
//return;
50+
public void run() {
51+
File config = getConfigFile();
52+
if (config != null) {
53+
long lastModifiedTime = config.lastModified();
54+
if (lastModifiedTime > lastModified) {
55+
reload();
56+
lastModified = lastModifiedTime;
5157
}
52-
53-
File confDir = new File(confFile);
54-
if (!confDir.exists()) {
55-
logger.warn("Configuration path is required! No such file {}", confFile);
56-
return;
57-
}
58-
confFileName = confDir.getName();
59-
if (confFile.contains(File.separator)) {
60-
path = FileSystems.getDefault().getPath(confDir.getPath()).getParent();
61-
} else {
62-
File directory = new File("");
63-
path = FileSystems.getDefault().getPath(directory.getAbsolutePath());
64-
}
65-
66-
logger.debug("confDirString = {}", confDir);
67-
watchService = FileSystems.getDefault().newWatchService();
68-
path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
69-
logger.debug("watch path : {}", path.toString());
70-
} catch (Exception e) {
71-
logger.error("Exception caught when register the watch key", e.getCause());
72-
return;
7358
}
59+
}
7460

75-
while (!shutdown) {
76-
try {
77-
WatchKey wk = watchService.take();
78-
long changeCount = 0;
79-
for (WatchEvent<?> event : wk.pollEvents()) {
80-
final Path changed = (Path)event.context();
81-
if (changed.endsWith(confFileName)) {
82-
reload();
83-
logger.info("The configuration was modified and we reloaded it");
84-
}
85-
changeCount++;
86-
}
87-
logger.debug("change count : {}", changeCount);
61+
private File getConfigFile() {
62+
String confFilePath;
63+
if (isNoneBlank(parameter.getShellConfFileName())) {
64+
confFilePath = parameter.getShellConfFileName();
65+
} else {
66+
confFilePath = Constant.TESTNET_CONF;
67+
}
8868

89-
boolean valid = wk.reset();
90-
if (!valid) {
91-
path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
92-
}
93-
} catch (InterruptedException e) {
94-
logger.warn("WatchService was interrupted");
95-
Thread.currentThread().interrupt();
96-
break;
97-
} catch (IOException e) {
98-
logger.error("Exception caught when register the watch key", e.getCause());
99-
break;
100-
}
69+
File confFile = new File(confFilePath);
70+
if (!confFile.exists()) {
71+
logger.warn("Configuration path is required! No such file {}", confFile);
72+
return null;
10173
}
74+
return confFile;
10275
}
10376

10477
public void reload() {
@@ -112,58 +85,30 @@ public void reload() {
11285
}
11386

11487
private void updateActiveNodes(Config config) {
115-
if (parameter.isWitness() || parameter.isFastForward()) {
116-
return;
117-
}
118-
119-
List<InetSocketAddress> lastActiveNodes = parameter.getActiveNodes();
12088
List<InetSocketAddress> newActiveNodes =
12189
Args.getInetSocketAddress(config, Constant.NODE_ACTIVE);
12290
parameter.setActiveNodes(newActiveNodes);
123-
parameter.getActiveNodes().forEach(n -> {
124-
if (!lastActiveNodes.contains(n)) {
125-
TronNetService.getP2pConfig().getActiveNodes().add(n);
126-
if (!TronNetService.getP2pConfig().getTrustNodes().contains(n.getAddress())) {
127-
TronNetService.getP2pConfig().getTrustNodes().add(n.getAddress());
128-
}
129-
}
130-
});
131-
132-
lastActiveNodes.forEach(ln -> {
133-
if (!parameter.getActiveNodes().contains(ln)) {
134-
TronNetService.getP2pConfig().getActiveNodes().remove(ln);
135-
TronNetService.getP2pConfig().getTrustNodes().remove(ln.getAddress());
136-
}
137-
});
91+
List<InetSocketAddress> activeNodes = TronNetService.getP2pConfig().getActiveNodes();
92+
activeNodes.clear();
93+
activeNodes.addAll(newActiveNodes);
13894
logger.debug("p2p active nodes : {}",
13995
TronNetService.getP2pConfig().getActiveNodes().toString());
14096
}
14197

14298
private void updateTrustNodes(Config config) {
143-
if (parameter.isWitness() || parameter.isFastForward()) {
144-
return;
145-
}
146-
147-
List<InetAddress> lastPassiveNodes = parameter.getPassiveNodes();
14899
List<InetAddress> newPassiveNodes = Args.getInetAddress(config, Constant.NODE_PASSIVE);
149100
parameter.setPassiveNodes(newPassiveNodes);
150-
parameter.getPassiveNodes().forEach(n -> {
151-
if (!lastPassiveNodes.contains(n)
152-
|| !TronNetService.getP2pConfig().getTrustNodes().contains(n)) {
153-
TronNetService.getP2pConfig().getTrustNodes().add(n);
154-
}
155-
});
156-
157-
lastPassiveNodes.forEach(ln -> {
158-
if (!parameter.getPassiveNodes().contains(ln)) {
159-
TronNetService.getP2pConfig().getTrustNodes().remove(ln);
160-
}
161-
});
162-
logger.debug("p2p trust nodes : {}", TronNetService.getP2pConfig().getTrustNodes().toString());
101+
List<InetAddress> trustNodes = TronNetService.getP2pConfig().getTrustNodes();
102+
trustNodes.clear();
103+
trustNodes.addAll(newPassiveNodes);
104+
parameter.getActiveNodes().forEach(n -> trustNodes.add(n.getAddress()));
105+
parameter.getFastForwardNodes().forEach(f -> trustNodes.add(f.getAddress()));
106+
logger.debug("p2p trust nodes : {}",
107+
TronNetService.getP2pConfig().getTrustNodes().toString());
163108
}
164109

165110
public void close() {
166-
logger.info("Closing watchService ...");
167-
shutdown = true;
111+
logger.info("Closing the dynamic loading configuration service ...");
112+
reloadExecutor.shutdown();
168113
}
169114
}

framework/src/test/java/org/tron/core/config/args/DynamicArgsTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.tron.core.config.args;
22

3+
import java.io.File;
34
import org.junit.After;
45
import org.junit.Assert;
56
import org.junit.Before;
@@ -10,10 +11,8 @@
1011
import org.tron.common.utils.ReflectUtils;
1112
import org.tron.core.Constant;
1213
import org.tron.core.config.DefaultConfig;
13-
import org.tron.core.net.TronNetService;
14-
import org.tron.p2p.P2pConfig;
15-
16-
import java.io.File;
14+
//import org.tron.core.net.TronNetService;
15+
//import org.tron.p2p.P2pConfig;
1716

1817
public class DynamicArgsTest {
1918
protected TronApplicationContext context;
@@ -45,11 +44,13 @@ public void get() {
4544

4645
@Test
4746
public void start() {
48-
dynamicArgs.start();
49-
TronNetService tronNetService = context.getBean(TronNetService.class);
50-
ReflectUtils.setFieldValue(tronNetService, "p2pConfig", new P2pConfig());
51-
dynamicArgs.reload();
47+
dynamicArgs.init();
48+
Assert.assertEquals(0, (long) ReflectUtils.getFieldObject(dynamicArgs, "lastModified"));
49+
50+
dynamicArgs.run();
51+
// TronNetService tronNetService = context.getBean(TronNetService.class);
52+
// ReflectUtils.setFieldValue(tronNetService, "p2pConfig", new P2pConfig());
53+
// dynamicArgs.reload();
5254
dynamicArgs.close();
53-
Assert.assertTrue((boolean)ReflectUtils.getFieldObject(dynamicArgs, "shutdown"));
5455
}
5556
}

0 commit comments

Comments
 (0)