Skip to content

Commit dc8e0bc

Browse files
committed
feat(all):tune the sequence of resources closure
1 parent a4100b6 commit dc8e0bc

37 files changed

+962
-1099
lines changed

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

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
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;
1110
import org.tron.core.consensus.ConsensusService;
1211
import org.tron.core.db.Manager;
1312
import org.tron.core.metrics.MetricsUtil;
@@ -31,9 +30,6 @@ public class ApplicationImpl implements Application {
3130
@Autowired
3231
private ConsensusService consensusService;
3332

34-
@Autowired
35-
private DynamicArgs dynamicArgs;
36-
3733
@Override
3834
public void setOptions(Args args) {
3935
// not used
@@ -64,33 +60,24 @@ public void startup() {
6460
}
6561
consensusService.start();
6662
MetricsUtil.init();
67-
dynamicArgs.init();
63+
this.initServices(Args.getInstance());
64+
this.startServices();
6865
}
6966

7067
@Override
7168
public void shutdown() {
72-
logger.info("******** start to shutdown ********");
69+
this.shutdownServices();
70+
consensusService.stop();
7371
if (!Args.getInstance().isSolidityNode() && (!Args.getInstance().p2pDisable)) {
7472
tronNetService.close();
7573
}
76-
consensusService.stop();
77-
synchronized (dbManager.getRevokingStore()) {
78-
dbManager.getSession().reset();
79-
closeRevokingStore();
80-
}
81-
dbManager.stopRePushThread();
82-
dbManager.stopRePushTriggerThread();
83-
EventPluginLoader.getInstance().stopPlugin();
84-
dbManager.stopFilterProcessThread();
85-
dbManager.stopValidateSignThread();
86-
getChainBaseManager().shutdown();
87-
dynamicArgs.close();
88-
logger.info("******** end to shutdown ********");
74+
dbManager.close();
8975
}
9076

9177
@Override
9278
public void startServices() {
9379
services.start();
80+
services.blockUntilShutdown();
9481
}
9582

9683
@Override
@@ -108,9 +95,4 @@ public ChainBaseManager getChainBaseManager() {
10895
return chainBaseManager;
10996
}
11097

111-
private void closeRevokingStore() {
112-
logger.info("******** start to closeRevokingStore ********");
113-
dbManager.getRevokingStore().shutdown();
114-
}
115-
11698
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* java-tron is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* java-tron is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
package org.tron.common.application;
17+
18+
import com.google.common.base.Objects;
19+
import lombok.extern.slf4j.Slf4j;
20+
import org.eclipse.jetty.server.Server;
21+
22+
@Slf4j(topic = "rpc")
23+
public abstract class HttpService implements Service {
24+
25+
protected Server apiServer;
26+
protected int port;
27+
28+
@Override
29+
public void blockUntilShutdown() {
30+
if (apiServer != null) {
31+
try {
32+
apiServer.join();
33+
} catch (InterruptedException e) {
34+
logger.warn("{}", e.getMessage());
35+
Thread.currentThread().interrupt();
36+
}
37+
}
38+
}
39+
40+
@Override
41+
public void start() {
42+
if (apiServer != null) {
43+
try {
44+
apiServer.start();
45+
logger.info("{} started, listening on {}", this.getClass().getSimpleName(), port);
46+
} catch (Exception e) {
47+
logger.error("{}", this.getClass().getSimpleName(), e);
48+
}
49+
}
50+
}
51+
52+
@Override
53+
public void stop() {
54+
if (apiServer != null) {
55+
logger.info("{} shutdown...", this.getClass().getSimpleName());
56+
try {
57+
apiServer.stop();
58+
} catch (Exception e) {
59+
logger.warn("{}", this.getClass().getSimpleName(), e);
60+
}
61+
logger.info("{} shutdown complete", this.getClass().getSimpleName());
62+
}
63+
}
64+
65+
@Override
66+
public boolean equals(Object o) {
67+
if (this == o) {
68+
return true;
69+
}
70+
if (o == null || getClass() != o.getClass()) {
71+
return false;
72+
}
73+
HttpService that = (HttpService) o;
74+
return port == that.port;
75+
}
76+
77+
@Override
78+
public int hashCode() {
79+
return Objects.hashCode(getClass().getSimpleName(), port);
80+
}
81+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* java-tron is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* java-tron is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
package org.tron.common.application;
17+
18+
import com.google.common.base.Objects;
19+
import io.grpc.Server;
20+
import java.io.IOException;
21+
import java.util.concurrent.TimeUnit;
22+
import lombok.extern.slf4j.Slf4j;
23+
24+
@Slf4j(topic = "rpc")
25+
public abstract class RpcService implements Service {
26+
27+
protected Server apiServer;
28+
protected int port;
29+
30+
@Override
31+
public void blockUntilShutdown() {
32+
if (apiServer != null) {
33+
try {
34+
apiServer.awaitTermination();
35+
} catch (InterruptedException e) {
36+
logger.warn("{}", e.getMessage());
37+
Thread.currentThread().interrupt();
38+
}
39+
}
40+
}
41+
42+
@Override
43+
public void start() {
44+
if (apiServer != null) {
45+
try {
46+
apiServer.start();
47+
} catch (IOException e) {
48+
logger.error("{}", this.getClass().getSimpleName(), e);
49+
}
50+
logger.info("{} started, listening on {}", this.getClass().getSimpleName(), port);
51+
}
52+
}
53+
54+
@Override
55+
public void stop() {
56+
if (apiServer != null) {
57+
logger.info("{} shutdown...", this.getClass().getSimpleName());
58+
try {
59+
apiServer.shutdown().awaitTermination(5, TimeUnit.SECONDS);
60+
} catch (InterruptedException e) {
61+
Thread.currentThread().interrupt();
62+
logger.warn("{}", this.getClass().getSimpleName(), e);
63+
}
64+
logger.info("{} shutdown complete", this.getClass().getSimpleName());
65+
}
66+
}
67+
68+
@Override
69+
public boolean equals(Object o) {
70+
if (this == o) {
71+
return true;
72+
}
73+
if (o == null || getClass() != o.getClass()) {
74+
return false;
75+
}
76+
RpcService that = (RpcService) o;
77+
return port == that.port;
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
return Objects.hashCode(getClass().getSimpleName(), port);
83+
}
84+
85+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ public interface Service {
2626
void start();
2727

2828
void stop();
29+
30+
void blockUntilShutdown();
2931
}

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@
1515

1616
package org.tron.common.application;
1717

18-
import java.util.ArrayList;
18+
import java.util.Collections;
19+
import java.util.LinkedHashSet;
20+
import java.util.Set;
1921
import lombok.extern.slf4j.Slf4j;
2022
import org.tron.common.parameter.CommonParameter;
2123

2224
@Slf4j(topic = "app")
2325
public class ServiceContainer {
2426

25-
private ArrayList<Service> services;
27+
private final Set<Service> services;
2628

2729
public ServiceContainer() {
28-
this.services = new ArrayList<>();
30+
this.services = Collections.synchronizedSet(new LinkedHashSet<>());
2931
}
3032

3133
public void add(Service service) {
@@ -34,31 +36,38 @@ public void add(Service service) {
3436

3537

3638
public void init() {
37-
for (Service service : this.services) {
39+
this.services.forEach(service -> {
3840
logger.debug("Initing {}.", service.getClass().getSimpleName());
3941
service.init();
40-
}
42+
});
4143
}
4244

4345
public void init(CommonParameter parameter) {
44-
for (Service service : this.services) {
46+
this.services.forEach(service -> {
4547
logger.debug("Initing {}.", service.getClass().getSimpleName());
4648
service.init(parameter);
47-
}
49+
});
4850
}
4951

5052
public void start() {
51-
logger.debug("Starting services.");
52-
for (Service service : this.services) {
53+
logger.info("Starting api services.");
54+
this.services.forEach(service -> {
5355
logger.debug("Starting {}.", service.getClass().getSimpleName());
5456
service.start();
55-
}
57+
});
58+
logger.info("All api services started.");
5659
}
5760

5861
public void stop() {
59-
for (Service service : this.services) {
62+
logger.info("Stopping api services.");
63+
this.services.forEach(service -> {
6064
logger.debug("Stopping {}.", service.getClass().getSimpleName());
6165
service.stop();
62-
}
66+
});
67+
logger.info("All api services stopped.");
68+
}
69+
70+
public void blockUntilShutdown() {
71+
this.services.stream().findFirst().ifPresent(Service::blockUntilShutdown);
6372
}
6473
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public TronApplicationContext(String... basePackages) {
2525
public void doClose() {
2626
logger.info("******** start to close ********");
2727
Application appT = ApplicationFactory.create(this);
28-
appT.shutdownServices();
2928
appT.shutdown();
3029
super.doClose();
3130
logger.info("******** close end ********");

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import java.util.concurrent.Executors;
1111
import java.util.concurrent.ScheduledExecutorService;
1212
import java.util.concurrent.TimeUnit;
13-
13+
import javax.annotation.PostConstruct;
14+
import javax.annotation.PreDestroy;
1415
import lombok.extern.slf4j.Slf4j;
1516
import org.springframework.stereotype.Component;
1617
import org.tron.common.parameter.CommonParameter;
@@ -28,6 +29,7 @@ public class DynamicArgs {
2829

2930
private ScheduledExecutorService reloadExecutor = Executors.newSingleThreadScheduledExecutor();
3031

32+
@PostConstruct
3133
public void init() {
3234
if (parameter.isDynamicConfigEnable()) {
3335
logger.info("Start the dynamic loading configuration service");
@@ -107,6 +109,7 @@ private void updateTrustNodes(Config config) {
107109
TronNetService.getP2pConfig().getTrustNodes().toString());
108110
}
109111

112+
@PreDestroy
110113
public void close() {
111114
logger.info("Closing the dynamic loading configuration service");
112115
reloadExecutor.shutdown();

framework/src/main/java/org/tron/core/db/Manager.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,6 +2416,17 @@ private boolean isBlockWaitingLock() {
24162416
return blockWaitLock.get() > NO_BLOCK_WAITING_LOCK;
24172417
}
24182418

2419+
public void close() {
2420+
stopRePushThread();
2421+
stopRePushTriggerThread();
2422+
EventPluginLoader.getInstance().stopPlugin();
2423+
stopFilterProcessThread();
2424+
stopValidateSignThread();
2425+
chainBaseManager.shutdown();
2426+
revokingStore.shutdown();
2427+
session.reset();
2428+
}
2429+
24192430
private static class ValidateSignTask implements Callable<Boolean> {
24202431

24212432
private TransactionCapsule trx;

0 commit comments

Comments
 (0)