-
Notifications
You must be signed in to change notification settings - Fork 1.6k
refactor(config): extract ConfigKey and remove testnet config #6565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9bb2026
218d076
e6b4bff
8d83788
db0f59b
10750ee
5990de2
f24db7b
a9f0ed3
51594c7
a2e6af1
5a2d8bf
808fa99
037be81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| package org.tron.core.config.args; | ||
|
|
||
| import static org.apache.commons.lang3.StringUtils.isNoneBlank; | ||
|
|
||
| import com.typesafe.config.Config; | ||
| import java.io.File; | ||
| import java.net.InetAddress; | ||
|
|
@@ -15,7 +13,6 @@ | |
| import org.springframework.stereotype.Component; | ||
| import org.tron.common.es.ExecutorServiceManager; | ||
| import org.tron.common.parameter.CommonParameter; | ||
| import org.tron.core.Constant; | ||
| import org.tron.core.config.Configuration; | ||
| import org.tron.core.net.TronNetService; | ||
|
|
||
|
|
@@ -25,6 +22,7 @@ | |
| public class DynamicArgs { | ||
| private final CommonParameter parameter = Args.getInstance(); | ||
|
|
||
| private File configFile; | ||
| private long lastModified = 0; | ||
|
|
||
| private ScheduledExecutorService reloadExecutor; | ||
|
|
@@ -36,11 +34,12 @@ public void init() { | |
| reloadExecutor = ExecutorServiceManager.newSingleThreadScheduledExecutor(esName); | ||
| logger.info("Start the dynamic loading configuration service"); | ||
| long checkInterval = parameter.getDynamicConfigCheckInterval(); | ||
| File config = getConfigFile(); | ||
| if (config == null) { | ||
| configFile = new File(parameter.getConfigFilePath()); | ||
| if (!configFile.exists()) { | ||
| logger.warn("Configuration path is required! No such file {}", configFile); | ||
| return; | ||
| } | ||
| lastModified = config.lastModified(); | ||
| lastModified = configFile.lastModified(); | ||
| reloadExecutor.scheduleWithFixedDelay(() -> { | ||
| try { | ||
| run(); | ||
|
|
@@ -52,36 +51,16 @@ public void init() { | |
| } | ||
|
|
||
| public void run() { | ||
| File config = getConfigFile(); | ||
| if (config != null) { | ||
| long lastModifiedTime = config.lastModified(); | ||
| if (lastModifiedTime > lastModified) { | ||
| reload(); | ||
| lastModified = lastModifiedTime; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private File getConfigFile() { | ||
| String confFilePath; | ||
| if (isNoneBlank(parameter.getShellConfFileName())) { | ||
| confFilePath = parameter.getShellConfFileName(); | ||
| } else { | ||
| confFilePath = Constant.NET_CONF; | ||
| } | ||
|
|
||
| File confFile = new File(confFilePath); | ||
| if (!confFile.exists()) { | ||
| logger.warn("Configuration path is required! No such file {}", confFile); | ||
| return null; | ||
| long lastModifiedTime = configFile.lastModified(); | ||
| if (lastModifiedTime > lastModified) { | ||
| reload(); | ||
| lastModified = lastModifiedTime; | ||
| } | ||
| return confFile; | ||
| } | ||
|
|
||
| public void reload() { | ||
| logger.debug("Reloading ... "); | ||
| Config config = Configuration.getByFileName(parameter.getShellConfFileName(), | ||
| Constant.NET_CONF); | ||
| Config config = Configuration.getByFileName(parameter.getConfigFilePath(), null); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please confirm is it really safe to remove the fallback
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the current code logic, this is fully guaranteed. The readability issue will be addressed in the parameter initialization refactoring. |
||
|
|
||
| updateActiveNodes(config); | ||
|
|
||
|
|
@@ -90,7 +69,7 @@ public void reload() { | |
|
|
||
| private void updateActiveNodes(Config config) { | ||
| List<InetSocketAddress> newActiveNodes = | ||
| Args.getInetSocketAddress(config, Constant.NODE_ACTIVE, true); | ||
| Args.getInetSocketAddress(config, ConfigKey.NODE_ACTIVE, true); | ||
| parameter.setActiveNodes(newActiveNodes); | ||
| List<InetSocketAddress> activeNodes = TronNetService.getP2pConfig().getActiveNodes(); | ||
| activeNodes.clear(); | ||
|
|
@@ -100,7 +79,7 @@ private void updateActiveNodes(Config config) { | |
| } | ||
|
|
||
| private void updateTrustNodes(Config config) { | ||
| List<InetAddress> newPassiveNodes = Args.getInetAddress(config, Constant.NODE_PASSIVE); | ||
| List<InetAddress> newPassiveNodes = Args.getInetAddress(config, ConfigKey.NODE_PASSIVE); | ||
| parameter.setPassiveNodes(newPassiveNodes); | ||
| List<InetAddress> trustNodes = TronNetService.getP2pConfig().getTrustNodes(); | ||
| trustNodes.clear(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,6 @@ | |
| import org.tron.common.log.LogService; | ||
| import org.tron.common.parameter.CommonParameter; | ||
| import org.tron.common.prometheus.Metrics; | ||
| import org.tron.core.Constant; | ||
| import org.tron.core.config.DefaultConfig; | ||
| import org.tron.core.config.args.Args; | ||
|
|
||
|
|
@@ -21,7 +20,7 @@ public class FullNode { | |
| */ | ||
| public static void main(String[] args) { | ||
| ExitManager.initExceptionHandler(); | ||
| Args.setParam(args, Constant.NET_CONF); | ||
| Args.setParam(args, "config.conf"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, it should be a constant.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer keeping it as a literal here. As the entry point of the application, explicitly specifying the config filename in |
||
| CommonParameter parameter = Args.getInstance(); | ||
|
|
||
| LogService.load(parameter.getLogbackPath()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| net { | ||
| type = mainnet | ||
| # type = testnet | ||
| # type is deprecated and has no effect. | ||
| # type = mainnet | ||
| } | ||
|
|
||
| storage { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| net { | ||
| # type is deprecated and has no effect. | ||
| # type = mainnet | ||
| type = testnet | ||
| } | ||
|
|
||
| storage { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| net { | ||
| type = mainnet | ||
| # type = testnet | ||
| # type is deprecated and has no effect. | ||
| # type = mainnet | ||
| } | ||
|
|
||
| storage { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| net { | ||
| type = mainnet | ||
| # type = testnet | ||
| # type is deprecated and has no effect. | ||
| # type = mainnet | ||
| } | ||
|
|
||
| storage { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here configFile need a Null protection, since here
run()is a public method, what ifparameter.isDynamicConfigEnable()return false and hererun()is called, it will return NullPointerException.Another risk would be: while running the configuration file is accidentally deleted, what will happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DynamicArgs is a Spring @component — no external code calls run() directly. This is an internal class, not a public API.
Config file deleted at runtime — File.lastModified() returns 0L when the file doesn't exist and won't throw an exception. Since 0 > lastModified is false, reload() won't be triggered. No issue here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but the test code is actually call this
run()There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minimizing over-defensive checks is a good practice. The original null checks could actually hide bugs. Unnecessary null checks mislead readers into thinking null is a possible state. As for the test case, it just needs to be properly initialized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DynamicArgs is an internal @component where run() is only invoked after init() guarantees configFile is initialized.