Skip to content

Commit 5959a1b

Browse files
committed
feat(config):add support for dynamically loading configuration
1 parent da2673c commit 5959a1b

6 files changed

Lines changed: 184 additions & 4 deletions

File tree

common/src/main/java/org/tron/common/parameter/CommonParameter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class CommonParameter {
3030
public static CommonParameter PARAMETER = new CommonParameter();
3131
@Setter
3232
public static boolean ENERGY_LIMIT_HARD_FORK = false;
33+
@Getter
3334
@Parameter(names = {"-c", "--config"}, description = "Config file (default:config.conf)")
3435
public String shellConfFileName = "";
3536
@Getter
@@ -608,6 +609,10 @@ public class CommonParameter {
608609
@Setter
609610
public long dynamicEnergyMaxFactor = 0L;
610611

612+
@Getter
613+
@Setter
614+
public boolean dynamicConfigEnable;
615+
611616
private static double calcMaxTimeRatio() {
612617
//return max(2.0, min(5.0, 5 * 4.0 / max(Runtime.getRuntime().availableProcessors(), 1)));
613618
return 5.0;

common/src/main/java/org/tron/core/Constant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,6 @@ public class Constant {
343343
public static final String NODE_SHUTDOWN_BLOCK_COUNT = "node.shutdown.BlockCount";
344344

345345
public static final String BLOCK_CACHE_TIMEOUT = "node.blockCacheTimeout";
346+
347+
public static final String DYNAMIC_CONFIG_ENABLE = "dynamicConfigEnable";
346348
}

framework/src/main/java/org/tron/common/application/ApplicationImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.tron.common.parameter.CommonParameter;
88
import org.tron.core.ChainBaseManager;
99
import org.tron.core.config.args.Args;
10+
import org.tron.core.config.args.DynamicArgs;
1011
import org.tron.core.consensus.ConsensusService;
1112
import org.tron.core.db.Manager;
1213
import org.tron.core.metrics.MetricsUtil;
@@ -32,6 +33,9 @@ public class ApplicationImpl implements Application {
3233
@Autowired
3334
private ConsensusService consensusService;
3435

36+
@Autowired
37+
private DynamicArgs dynamicArgs;
38+
3539
@Override
3640
public void setOptions(Args args) {
3741
// not used
@@ -62,6 +66,7 @@ public void startup() {
6266
}
6367
consensusService.start();
6468
MetricsUtil.init();
69+
dynamicArgs.init();
6570
}
6671

6772
@Override
@@ -80,6 +85,7 @@ public void shutdown() {
8085
dbManager.stopRePushTriggerThread();
8186
EventPluginLoader.getInstance().stopPlugin();
8287
dbManager.stopFilterProcessThread();
88+
dynamicArgs.close();
8389
logger.info("******** end to shutdown ********");
8490
FullNode.shutDownSign = true;
8591
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ public static void clearParam() {
221221
PARAMETER.allowNewRewardAlgorithm = 0;
222222
PARAMETER.allowNewReward = 0;
223223
PARAMETER.memoFee = 0;
224+
PARAMETER.dynamicConfigEnable = false;
224225
}
225226

226227
/**
@@ -1122,6 +1123,9 @@ public static void setParam(final String[] args, final String confFileName) {
11221123
Math.max(PARAMETER.dynamicEnergyMaxFactor, 0);
11231124
}
11241125

1126+
PARAMETER.dynamicConfigEnable = config.hasPath(Constant.DYNAMIC_CONFIG_ENABLE)
1127+
&& config.getBoolean(Constant.DYNAMIC_CONFIG_ENABLE);
1128+
11251129
logConfig();
11261130
}
11271131

@@ -1174,7 +1178,7 @@ private static RateLimiterInitialization getRateLimiterFromConfig(
11741178
return initialization;
11751179
}
11761180

1177-
private static List<InetSocketAddress> getInetSocketAddress(
1181+
public static List<InetSocketAddress> getInetSocketAddress(
11781182
final com.typesafe.config.Config config, String path) {
11791183
List<InetSocketAddress> ret = new ArrayList<>();
11801184
if (!config.hasPath(path)) {
@@ -1195,7 +1199,7 @@ private static List<InetSocketAddress> getInetSocketAddress(
11951199
return ret;
11961200
}
11971201

1198-
private static List<InetAddress> getInetAddress(
1202+
public static List<InetAddress> getInetAddress(
11991203
final com.typesafe.config.Config config, String path) {
12001204
List<InetAddress> ret = new ArrayList<>();
12011205
if (!config.hasPath(path)) {
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package org.tron.core.config.args;
2+
3+
import static org.apache.commons.lang3.StringUtils.isNoneBlank;
4+
5+
import com.typesafe.config.Config;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.net.InetAddress;
9+
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;
16+
import java.util.List;
17+
import lombok.extern.slf4j.Slf4j;
18+
import org.springframework.stereotype.Component;
19+
import org.tron.common.parameter.CommonParameter;
20+
import org.tron.core.Constant;
21+
import org.tron.core.config.Configuration;
22+
import org.tron.core.net.TronNetService;
23+
24+
25+
@Slf4j(topic = "app")
26+
@Component
27+
public class DynamicArgs {
28+
private final CommonParameter parameter = Args.getInstance();
29+
30+
private volatile boolean shutdown = false;
31+
32+
public void init() {
33+
if (parameter.isDynamicConfigEnable()) {
34+
new Thread(this::start, "DynamicArgs").start();
35+
}
36+
}
37+
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+
//
50+
//logger.warn("Configuration path is required!");
51+
//return;
52+
}
53+
54+
File confDir = new File(confFile);
55+
confFileName = confDir.getName();
56+
if (confFile.contains(File.separator)) {
57+
path = FileSystems.getDefault().getPath(confDir.getPath()).getParent();
58+
} else {
59+
File directory = new File("");
60+
path = FileSystems.getDefault().getPath(directory.getAbsolutePath());
61+
}
62+
63+
logger.info("confDirString = {}", confDir);
64+
watchService = FileSystems.getDefault().newWatchService();
65+
path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
66+
logger.info("watch path : {}", path.toString());
67+
} catch (Exception e) {
68+
logger.error("Exception caught when register the watch key", e.getCause());
69+
return;
70+
}
71+
72+
while (!shutdown) {
73+
try {
74+
WatchKey wk = watchService.take();
75+
long changeCount = 0;
76+
for (WatchEvent<?> event : wk.pollEvents()) {
77+
final Path changed = (Path)event.context();
78+
if (changed.endsWith(confFileName)) {
79+
reload();
80+
logger.info("Config was modify and we reload it");
81+
}
82+
changeCount++;
83+
}
84+
logger.info("change count : {}", changeCount);
85+
86+
boolean valid = wk.reset();
87+
if (!valid) {
88+
path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
89+
}
90+
} catch (InterruptedException e) {
91+
logger.warn("");
92+
break;
93+
} catch (IOException e) {
94+
break;
95+
}
96+
}
97+
}
98+
99+
public void reload() {
100+
logger.info("reloading ... ");
101+
Config config = Configuration.getByFileName(parameter.getShellConfFileName(),
102+
Constant.TESTNET_CONF);
103+
104+
updateActiveNodes(config);
105+
106+
updateTrustNodes(config);
107+
}
108+
109+
private void updateActiveNodes(Config config) {
110+
if (parameter.isWitness() || parameter.isFastForward()) {
111+
return;
112+
}
113+
114+
List<InetSocketAddress> lastActiveNodes = parameter.getActiveNodes();
115+
List<InetSocketAddress> newActiveNodes =
116+
Args.getInetSocketAddress(config, Constant.NODE_ACTIVE);
117+
parameter.setActiveNodes(newActiveNodes);
118+
parameter.getActiveNodes().forEach(n -> {
119+
logger.info("active node : {}", n.toString());
120+
if (!lastActiveNodes.contains(n)) {
121+
TronNetService.getP2pConfig().getActiveNodes().add(n);
122+
if (!TronNetService.getP2pConfig().getTrustNodes().contains(n.getAddress())) {
123+
TronNetService.getP2pConfig().getTrustNodes().add(n.getAddress());
124+
}
125+
}
126+
});
127+
128+
lastActiveNodes.forEach(ln -> {
129+
if (!parameter.getActiveNodes().contains(ln)) {
130+
TronNetService.getP2pConfig().getActiveNodes().remove(ln);
131+
TronNetService.getP2pConfig().getTrustNodes().remove(ln.getAddress());
132+
}
133+
});
134+
}
135+
136+
private void updateTrustNodes(Config config) {
137+
if (parameter.isWitness() || parameter.isFastForward()) {
138+
return;
139+
}
140+
141+
List<InetAddress> lastPassiveNodes = parameter.getPassiveNodes();
142+
List<InetAddress> newPassiveNodes = Args.getInetAddress(config, Constant.NODE_PASSIVE);
143+
parameter.setPassiveNodes(newPassiveNodes);
144+
parameter.getPassiveNodes().forEach(n -> {
145+
logger.info("passive node : {}", n.toString());
146+
if (!lastPassiveNodes.contains(n)
147+
|| !TronNetService.getP2pConfig().getTrustNodes().contains(n)) {
148+
TronNetService.getP2pConfig().getTrustNodes().add(n);
149+
}
150+
});
151+
152+
lastPassiveNodes.forEach(ln -> {
153+
if (!parameter.getPassiveNodes().contains(ln)) {
154+
TronNetService.getP2pConfig().getTrustNodes().remove(ln);
155+
}
156+
});
157+
}
158+
159+
public void close() {
160+
logger.info("Closing watchService ...");
161+
shutdown = true;
162+
}
163+
}

framework/src/main/java/org/tron/core/net/TronNetService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ private P2pConfig getConfig() {
136136

137137
P2pConfig config = new P2pConfig();
138138
config.setSeedNodes(seeds);
139-
config.setActiveNodes(parameter.getActiveNodes());
140-
config.setTrustNodes(parameter.getPassiveNodes());
139+
config.getActiveNodes().addAll(parameter.getActiveNodes());
140+
config.getTrustNodes().addAll(parameter.getPassiveNodes());
141141
config.getActiveNodes().forEach(n -> config.getTrustNodes().add(n.getAddress()));
142142
parameter.getFastForwardNodes().forEach(f -> config.getTrustNodes().add(f.getAddress()));
143143
int maxConnections = parameter.getMaxConnections();

0 commit comments

Comments
 (0)