Skip to content

Commit 29ac079

Browse files
committed
Merge branch 'release_v4.7.2' into feature/isolate
2 parents 0ce3ad3 + d4c2761 commit 29ac079

11 files changed

Lines changed: 240 additions & 7 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class CommonParameter {
2828
public static CommonParameter PARAMETER = new CommonParameter();
2929
@Setter
3030
public static boolean ENERGY_LIMIT_HARD_FORK = false;
31+
@Getter
3132
@Parameter(names = {"-c", "--config"}, description = "Config file (default:config.conf)")
3233
public String shellConfFileName = "";
3334
@Getter
@@ -633,6 +634,14 @@ public class CommonParameter {
633634
@Setter
634635
public long dynamicEnergyMaxFactor = 0L;
635636

637+
@Getter
638+
@Setter
639+
public boolean dynamicConfigEnable;
640+
641+
@Getter
642+
@Setter
643+
public long dynamicConfigCheckInterval;
644+
636645
private static double calcMaxTimeRatio() {
637646
//return max(2.0, min(5.0, 5 * 4.0 / max(Runtime.getRuntime().availableProcessors(), 1)));
638647
return 5.0;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,7 @@ public class Constant {
366366
public static final String NODE_SHUTDOWN_BLOCK_COUNT = "node.shutdown.BlockCount";
367367

368368
public static final String BLOCK_CACHE_TIMEOUT = "node.blockCacheTimeout";
369+
370+
public static final String DYNAMIC_CONFIG_ENABLE = "node.dynamicConfig.enable";
371+
public static final String DYNAMIC_CONFIG_CHECK_INTERVAL = "node.dynamicConfig.checkInterval";
369372
}

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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ public static void clearParam() {
229229
PARAMETER.rateLimiterGlobalQps = 50000;
230230
PARAMETER.rateLimiterGlobalIpQps = 10000;
231231
PARAMETER.p2pDisable = false;
232+
PARAMETER.dynamicConfigEnable = false;
233+
PARAMETER.dynamicConfigCheckInterval = 600;
232234
}
233235

234236
/**
@@ -1152,6 +1154,18 @@ public static void setParam(final String[] args, final String confFileName) {
11521154
Math.max(PARAMETER.dynamicEnergyMaxFactor, 0);
11531155
}
11541156

1157+
PARAMETER.dynamicConfigEnable = config.hasPath(Constant.DYNAMIC_CONFIG_ENABLE)
1158+
&& config.getBoolean(Constant.DYNAMIC_CONFIG_ENABLE);
1159+
if (config.hasPath(Constant.DYNAMIC_CONFIG_CHECK_INTERVAL)) {
1160+
PARAMETER.dynamicConfigCheckInterval
1161+
= config.getLong(Constant.DYNAMIC_CONFIG_CHECK_INTERVAL);
1162+
if (PARAMETER.dynamicConfigCheckInterval <= 0) {
1163+
PARAMETER.dynamicConfigCheckInterval = 600;
1164+
}
1165+
} else {
1166+
PARAMETER.dynamicConfigCheckInterval = 600;
1167+
}
1168+
11551169
logConfig();
11561170
}
11571171

@@ -1204,7 +1218,7 @@ private static RateLimiterInitialization getRateLimiterFromConfig(
12041218
return initialization;
12051219
}
12061220

1207-
private static List<InetSocketAddress> getInetSocketAddress(
1221+
public static List<InetSocketAddress> getInetSocketAddress(
12081222
final com.typesafe.config.Config config, String path, boolean filter) {
12091223
List<InetSocketAddress> ret = new ArrayList<>();
12101224
if (!config.hasPath(path)) {
@@ -1229,7 +1243,7 @@ private static List<InetSocketAddress> getInetSocketAddress(
12291243
return ret;
12301244
}
12311245

1232-
private static List<InetAddress> getInetAddress(
1246+
public static List<InetAddress> getInetAddress(
12331247
final com.typesafe.config.Config config, String path) {
12341248
List<InetAddress> ret = new ArrayList<>();
12351249
if (!config.hasPath(path)) {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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.net.InetAddress;
8+
import java.net.InetSocketAddress;
9+
import java.util.List;
10+
import java.util.concurrent.Executors;
11+
import java.util.concurrent.ScheduledExecutorService;
12+
import java.util.concurrent.TimeUnit;
13+
14+
import lombok.extern.slf4j.Slf4j;
15+
import org.springframework.stereotype.Component;
16+
import org.tron.common.parameter.CommonParameter;
17+
import org.tron.core.Constant;
18+
import org.tron.core.config.Configuration;
19+
import org.tron.core.net.TronNetService;
20+
21+
22+
@Slf4j(topic = "app")
23+
@Component
24+
public class DynamicArgs {
25+
private final CommonParameter parameter = Args.getInstance();
26+
27+
private long lastModified = 0;
28+
29+
private ScheduledExecutorService reloadExecutor = Executors.newSingleThreadScheduledExecutor();
30+
31+
public void init() {
32+
if (parameter.isDynamicConfigEnable()) {
33+
logger.info("Start the dynamic loading configuration service");
34+
long checkInterval = parameter.getDynamicConfigCheckInterval();
35+
File config = getConfigFile();
36+
if (config == null) {
37+
return;
38+
}
39+
lastModified = config.lastModified();
40+
reloadExecutor.scheduleWithFixedDelay(() -> {
41+
try {
42+
run();
43+
} catch (Exception e) {
44+
logger.error("Exception caught when reloading configuration", e);
45+
}
46+
}, 10, checkInterval, TimeUnit.SECONDS);
47+
}
48+
}
49+
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;
57+
}
58+
}
59+
}
60+
61+
private File getConfigFile() {
62+
String confFilePath;
63+
if (isNoneBlank(parameter.getShellConfFileName())) {
64+
confFilePath = parameter.getShellConfFileName();
65+
} else {
66+
confFilePath = Constant.TESTNET_CONF;
67+
}
68+
69+
File confFile = new File(confFilePath);
70+
if (!confFile.exists()) {
71+
logger.warn("Configuration path is required! No such file {}", confFile);
72+
return null;
73+
}
74+
return confFile;
75+
}
76+
77+
public void reload() {
78+
logger.debug("Reloading ... ");
79+
Config config = Configuration.getByFileName(parameter.getShellConfFileName(),
80+
Constant.TESTNET_CONF);
81+
82+
updateActiveNodes(config);
83+
84+
updateTrustNodes(config);
85+
}
86+
87+
private void updateActiveNodes(Config config) {
88+
List<InetSocketAddress> newActiveNodes =
89+
Args.getInetSocketAddress(config, Constant.NODE_ACTIVE, true);
90+
parameter.setActiveNodes(newActiveNodes);
91+
List<InetSocketAddress> activeNodes = TronNetService.getP2pConfig().getActiveNodes();
92+
activeNodes.clear();
93+
activeNodes.addAll(newActiveNodes);
94+
logger.debug("p2p active nodes : {}",
95+
TronNetService.getP2pConfig().getActiveNodes().toString());
96+
}
97+
98+
private void updateTrustNodes(Config config) {
99+
List<InetAddress> newPassiveNodes = Args.getInetAddress(config, Constant.NODE_PASSIVE);
100+
parameter.setPassiveNodes(newPassiveNodes);
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());
108+
}
109+
110+
public void close() {
111+
logger.info("Closing the dynamic loading configuration service");
112+
reloadExecutor.shutdown();
113+
}
114+
}

framework/src/main/java/org/tron/core/net/peer/PeerConnection.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ public static boolean needToLog(Message msg) {
293293
return true;
294294
}
295295

296+
public synchronized boolean checkAndPutAdvInvRequest(Item key, Long value) {
297+
if (advInvRequest.containsKey(key)) {
298+
return false;
299+
}
300+
advInvRequest.put(key, value);
301+
return true;
302+
}
303+
296304
@Override
297305
public boolean equals(Object o) {
298306
if (!(o instanceof PeerConnection)) {

framework/src/main/java/org/tron/core/net/service/adv/AdvService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,9 @@ private void consumerInvToFetch() {
281281
&& invSender.getSize(peer) < MAX_TRX_FETCH_PER_PEER)
282282
.sorted(Comparator.comparingInt(peer -> invSender.getSize(peer)))
283283
.findFirst().ifPresent(peer -> {
284-
invSender.add(item, peer);
285-
peer.getAdvInvRequest().put(item, now);
284+
if (peer.checkAndPutAdvInvRequest(item, now)) {
285+
invSender.add(item, peer);
286+
}
286287
invToFetch.remove(item);
287288
});
288289
});

framework/src/main/java/org/tron/core/net/service/fetchblock/FetchBlockService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ private void fetchBlockProcess(FetchBlockInfo fetchBlock) {
117117

118118
if (optionalPeerConnection.isPresent()) {
119119
optionalPeerConnection.ifPresent(firstPeer -> {
120-
if (shouldFetchBlock(firstPeer, fetchBlock)) {
121-
firstPeer.getAdvInvRequest().put(item, System.currentTimeMillis());
120+
if (shouldFetchBlock(firstPeer, fetchBlock)
121+
&& firstPeer.checkAndPutAdvInvRequest(item, System.currentTimeMillis())) {
122122
firstPeer.sendMessage(new FetchInvDataMessage(Collections.singletonList(item.getHash()),
123123
item.getType()));
124124
this.fetchBlockInfo = null;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.tron.core.config.args;
2+
3+
import java.io.File;
4+
import org.junit.After;
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.tron.common.application.TronApplicationContext;
9+
import org.tron.common.parameter.CommonParameter;
10+
import org.tron.common.utils.FileUtil;
11+
import org.tron.common.utils.ReflectUtils;
12+
import org.tron.core.Constant;
13+
import org.tron.core.config.DefaultConfig;
14+
import org.tron.core.net.TronNetService;
15+
import org.tron.p2p.P2pConfig;
16+
17+
public class DynamicArgsTest {
18+
protected TronApplicationContext context;
19+
private DynamicArgs dynamicArgs;
20+
private String dbPath = "output-dynamic-config-test";
21+
22+
@Before
23+
public void init() {
24+
Args.setParam(new String[]{"--output-directory", dbPath},
25+
Constant.TEST_CONF);
26+
context = new TronApplicationContext(DefaultConfig.class);
27+
dynamicArgs = context.getBean(DynamicArgs.class);
28+
29+
}
30+
31+
@After
32+
public void destroy() {
33+
Args.clearParam();
34+
context.destroy();
35+
FileUtil.deleteDir(new File(dbPath));
36+
}
37+
38+
@Test
39+
public void start() {
40+
CommonParameter parameter = Args.getInstance();
41+
Assert.assertTrue(parameter.isDynamicConfigEnable());
42+
Assert.assertEquals(600, parameter.getDynamicConfigCheckInterval());
43+
44+
dynamicArgs.init();
45+
Assert.assertEquals(0, (long) ReflectUtils.getFieldObject(dynamicArgs, "lastModified"));
46+
47+
TronNetService tronNetService = context.getBean(TronNetService.class);
48+
ReflectUtils.setFieldValue(tronNetService, "p2pConfig", new P2pConfig());
49+
File config = new File(Constant.TESTNET_CONF);
50+
if (!config.exists()) {
51+
try {
52+
config.createNewFile();
53+
} catch (Exception e) {
54+
return;
55+
}
56+
dynamicArgs.run();
57+
try {
58+
config.delete();
59+
} catch (Exception e) {
60+
return;
61+
}
62+
}
63+
try {
64+
dynamicArgs.reload();
65+
} catch (Exception e) {
66+
// no need to deal with
67+
}
68+
69+
dynamicArgs.close();
70+
}
71+
}

framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ private void testBroadcast() {
8585

8686
try {
8787
peer = context.getBean(PeerConnection.class);
88+
Assert.assertFalse(peer.isDisconnect());
8889
p2pEventHandler = context.getBean(P2pEventHandlerImpl.class);
8990

9091
List<PeerConnection> peers = Lists.newArrayList();
@@ -96,6 +97,9 @@ private void testBroadcast() {
9697
service.broadcast(msg);
9798
Item item = new Item(blockCapsule.getBlockId(), InventoryType.BLOCK);
9899
Assert.assertNotNull(service.getMessage(item));
100+
peer.checkAndPutAdvInvRequest(item, System.currentTimeMillis());
101+
boolean res = peer.checkAndPutAdvInvRequest(item, System.currentTimeMillis());
102+
Assert.assertFalse(res);
99103
} catch (NullPointerException e) {
100104
System.out.println(e);
101105
}

0 commit comments

Comments
 (0)