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+ }
0 commit comments