Skip to content

Commit e1d096b

Browse files
authored
feat(all):make @PreDestroy work (#5421)
1 parent ac90c45 commit e1d096b

7 files changed

Lines changed: 69 additions & 84 deletions

File tree

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import org.tron.core.db.Manager;
1313
import org.tron.core.metrics.MetricsUtil;
1414
import org.tron.core.net.TronNetService;
15-
import org.tron.program.FullNode;
16-
import org.tron.program.SolidityNode;
1715

1816
@Slf4j(topic = "app")
1917
@Component
@@ -87,7 +85,6 @@ public void shutdown() {
8785
dbManager.stopFilterProcessThread();
8886
dynamicArgs.close();
8987
logger.info("******** end to shutdown ********");
90-
FullNode.shutDownSign = true;
9188
}
9289

9390
@Override

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
44
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5-
import org.tron.core.db.Manager;
6-
import org.tron.core.net.TronNetService;
5+
import org.tron.program.FullNode;
76

87
public class TronApplicationContext extends AnnotationConfigApplicationContext {
98

@@ -23,19 +22,13 @@ public TronApplicationContext(String... basePackages) {
2322
}
2423

2524
@Override
26-
public void destroy() {
27-
25+
public void doClose() {
26+
logger.info("******** start to close ********");
2827
Application appT = ApplicationFactory.create(this);
2928
appT.shutdownServices();
3029
appT.shutdown();
31-
32-
TronNetService tronNetService = getBean(TronNetService.class);
33-
tronNetService.close();
34-
35-
Manager dbManager = getBean(Manager.class);
36-
dbManager.stopRePushThread();
37-
dbManager.stopRePushTriggerThread();
38-
dbManager.stopFilterProcessThread();
39-
super.destroy();
30+
super.doClose();
31+
logger.info("******** close end ********");
32+
FullNode.shutDownSign = true;
4033
}
4134
}

framework/src/main/java/org/tron/program/FullNode.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static void main(String[] args) {
8181
context.register(DefaultConfig.class);
8282
context.refresh();
8383
Application appT = ApplicationFactory.create(context);
84-
shutdown(appT);
84+
context.registerShutdownHook();
8585

8686
// grpc api server
8787
RpcApiService rpcApiService = context.getBean(RpcApiService.class);
@@ -138,9 +138,4 @@ public static void main(String[] args) {
138138

139139
rpcApiService.blockUntilShutdown();
140140
}
141-
142-
public static void shutdown(final Application app) {
143-
logger.info("********register application shutdown hook********");
144-
Runtime.getRuntime().addShutdownHook(new Thread(app::shutdown));
145-
}
146141
}

framework/src/main/java/org/tron/program/SolidityNode.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public static void main(String[] args) {
7171
}
7272
parameter.setSolidityNode(true);
7373

74-
ApplicationContext context = new TronApplicationContext(DefaultConfig.class);
74+
TronApplicationContext context = new TronApplicationContext(DefaultConfig.class);
75+
context.registerShutdownHook();
7576

7677
if (parameter.isHelp()) {
7778
logger.info("Here is the help message.");
@@ -81,8 +82,6 @@ public static void main(String[] args) {
8182
Metrics.init();
8283

8384
Application appT = ApplicationFactory.create(context);
84-
FullNode.shutdown(appT);
85-
8685
RpcApiService rpcApiService = context.getBean(RpcApiService.class);
8786
appT.addService(rpcApiService);
8887
//http

framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
import io.grpc.ManagedChannel;
44
import io.grpc.ManagedChannelBuilder;
55
import io.grpc.StatusRuntimeException;
6-
import java.io.File;
6+
import java.io.IOException;
77
import java.util.concurrent.TimeUnit;
88
import lombok.extern.slf4j.Slf4j;
9-
10-
import org.junit.After;
9+
import org.junit.AfterClass;
1110
import org.junit.Assert;
12-
import org.junit.Before;
11+
import org.junit.BeforeClass;
12+
import org.junit.ClassRule;
1313
import org.junit.Rule;
1414
import org.junit.Test;
1515
import org.junit.rules.ExpectedException;
16+
import org.junit.rules.TemporaryFolder;
1617
import org.tron.api.GrpcAPI;
1718
import org.tron.api.WalletGrpc;
1819
import org.tron.api.WalletSolidityGrpc;
1920
import org.tron.common.application.Application;
2021
import org.tron.common.application.ApplicationFactory;
2122
import org.tron.common.application.TronApplicationContext;
22-
import org.tron.common.utils.FileUtil;
2323
import org.tron.common.utils.PublicMethod;
2424
import org.tron.core.ChainBaseManager;
2525
import org.tron.core.Constant;
@@ -32,54 +32,58 @@
3232
@Slf4j
3333
public class LiteFnQueryGrpcInterceptorTest {
3434

35-
private TronApplicationContext context;
36-
private ManagedChannel channelFull = null;
37-
private ManagedChannel channelpBFT = null;
38-
private WalletGrpc.WalletBlockingStub blockingStubFull = null;
39-
private WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubSolidity = null;
40-
private WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubpBFT = null;
41-
private RpcApiService rpcApiService;
42-
private RpcApiServiceOnSolidity rpcApiServiceOnSolidity;
43-
private RpcApiServiceOnPBFT rpcApiServiceOnPBFT;
44-
private Application appTest;
45-
private ChainBaseManager chainBaseManager;
46-
47-
private String dbPath = "output_grpc_interceptor_test";
35+
private static TronApplicationContext context;
36+
private static ManagedChannel channelFull = null;
37+
private static ManagedChannel channelSolidity = null;
38+
private static ManagedChannel channelpBFT = null;
39+
private static WalletGrpc.WalletBlockingStub blockingStubFull = null;
40+
private static WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubSolidity = null;
41+
private static WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubpBFT = null;
42+
private static ChainBaseManager chainBaseManager;
43+
private static final String ERROR_MSG =
44+
"UNAVAILABLE: this API is closed because this node is a lite fullnode";
4845

4946
@Rule
5047
public ExpectedException thrown = ExpectedException.none();
5148

49+
@ClassRule
50+
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
51+
5252
/**
5353
* init logic.
5454
*/
55-
@Before
56-
public void init() {
57-
Args.setParam(new String[]{"-d", dbPath}, Constant.TEST_CONF);
55+
@BeforeClass
56+
public static void init() throws IOException {
57+
Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF);
5858
Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort());
5959
Args.getInstance().setRpcOnSolidityPort(PublicMethod.chooseRandomPort());
6060
Args.getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort());
6161
String fullnode = String.format("%s:%d", Args.getInstance().getNodeDiscoveryBindIp(),
6262
Args.getInstance().getRpcPort());
63+
String solidityNode = String.format("%s:%d", Args.getInstance().getNodeDiscoveryBindIp(),
64+
Args.getInstance().getRpcOnSolidityPort());
6365
String pBFTNode = String.format("%s:%d", Args.getInstance().getNodeDiscoveryBindIp(),
64-
Args.getInstance().getRpcOnPBFTPort());
66+
Args.getInstance().getRpcOnPBFTPort());
6567
channelFull = ManagedChannelBuilder.forTarget(fullnode)
6668
.usePlaintext()
6769
.build();
70+
channelSolidity = ManagedChannelBuilder.forTarget(solidityNode)
71+
.usePlaintext()
72+
.build();
6873
channelpBFT = ManagedChannelBuilder.forTarget(pBFTNode)
6974
.usePlaintext()
7075
.build();
7176
context = new TronApplicationContext(DefaultConfig.class);
7277
blockingStubFull = WalletGrpc.newBlockingStub(channelFull);
73-
blockingStubSolidity = WalletSolidityGrpc.newBlockingStub(channelFull);
78+
blockingStubSolidity = WalletSolidityGrpc.newBlockingStub(channelSolidity);
7479
blockingStubpBFT = WalletSolidityGrpc.newBlockingStub(channelpBFT);
75-
blockingStubSolidity = WalletSolidityGrpc.newBlockingStub(channelFull);
76-
rpcApiService = context.getBean(RpcApiService.class);
77-
rpcApiServiceOnSolidity = context.getBean(RpcApiServiceOnSolidity.class);
78-
rpcApiServiceOnPBFT = context.getBean(RpcApiServiceOnPBFT.class);
80+
RpcApiService rpcApiService = context.getBean(RpcApiService.class);
81+
RpcApiServiceOnSolidity rpcOnSolidity = context.getBean(RpcApiServiceOnSolidity.class);
82+
RpcApiServiceOnPBFT rpcApiServiceOnPBFT = context.getBean(RpcApiServiceOnPBFT.class);
7983
chainBaseManager = context.getBean(ChainBaseManager.class);
80-
appTest = ApplicationFactory.create(context);
84+
Application appTest = ApplicationFactory.create(context);
8185
appTest.addService(rpcApiService);
82-
appTest.addService(rpcApiServiceOnSolidity);
86+
appTest.addService(rpcOnSolidity);
8387
appTest.addService(rpcApiServiceOnPBFT);
8488
appTest.initServices(Args.getInstance());
8589
appTest.startServices();
@@ -89,40 +93,45 @@ public void init() {
8993
/**
9094
* destroy the context.
9195
*/
92-
@After
93-
public void destroy() throws InterruptedException {
96+
@AfterClass
97+
public static void destroy() throws InterruptedException {
9498
if (channelFull != null) {
9599
channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS);
96100
}
101+
if (channelSolidity != null) {
102+
channelSolidity.shutdown().awaitTermination(5, TimeUnit.SECONDS);
103+
}
97104
if (channelpBFT != null) {
98105
channelpBFT.shutdown().awaitTermination(5, TimeUnit.SECONDS);
99106
}
107+
context.close();
100108
Args.clearParam();
101-
appTest.shutdownServices();
102-
appTest.shutdown();
103-
context.destroy();
104-
if (FileUtil.deleteDir(new File(dbPath))) {
105-
logger.info("Release resources successful.");
106-
} else {
107-
logger.info("Release resources failure.");
108-
}
109109
}
110110

111111
@Test
112112
public void testGrpcApiThrowStatusRuntimeException() {
113113
final GrpcAPI.NumberMessage message = GrpcAPI.NumberMessage.newBuilder().setNum(0).build();
114114
chainBaseManager.setNodeType(ChainBaseManager.NodeType.LITE);
115115
thrown.expect(StatusRuntimeException.class);
116-
thrown.expectMessage("UNAVAILABLE: this API is closed because this node is a lite fullnode");
116+
thrown.expectMessage(ERROR_MSG);
117117
blockingStubFull.getBlockByNum(message);
118118
}
119119

120+
@Test
121+
public void testGrpcSolidityThrowStatusRuntimeException() {
122+
final GrpcAPI.NumberMessage message = GrpcAPI.NumberMessage.newBuilder().setNum(0).build();
123+
chainBaseManager.setNodeType(ChainBaseManager.NodeType.LITE);
124+
thrown.expect(StatusRuntimeException.class);
125+
thrown.expectMessage(ERROR_MSG);
126+
blockingStubSolidity.getBlockByNum(message);
127+
}
128+
120129
@Test
121130
public void testpBFTGrpcApiThrowStatusRuntimeException() {
122131
final GrpcAPI.NumberMessage message = GrpcAPI.NumberMessage.newBuilder().setNum(0).build();
123132
chainBaseManager.setNodeType(ChainBaseManager.NodeType.LITE);
124133
thrown.expect(StatusRuntimeException.class);
125-
thrown.expectMessage("UNAVAILABLE: this API is closed because this node is a lite fullnode");
134+
thrown.expectMessage(ERROR_MSG);
126135
blockingStubpBFT.getBlockByNum(message);
127136
}
128137

framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import io.grpc.ManagedChannelBuilder;
55
import io.grpc.StatusRuntimeException;
66
import io.grpc.stub.ServerCallStreamObserver;
7-
import java.io.File;
7+
import java.io.IOException;
88
import java.util.ArrayList;
99
import java.util.Collections;
1010
import java.util.List;
@@ -13,16 +13,17 @@
1313
import org.junit.AfterClass;
1414
import org.junit.Assert;
1515
import org.junit.BeforeClass;
16+
import org.junit.ClassRule;
1617
import org.junit.Rule;
1718
import org.junit.Test;
1819
import org.junit.rules.ExpectedException;
20+
import org.junit.rules.TemporaryFolder;
1921
import org.tron.api.GrpcAPI;
2022
import org.tron.api.WalletGrpc;
2123
import org.tron.api.WalletSolidityGrpc;
2224
import org.tron.common.application.Application;
2325
import org.tron.common.application.ApplicationFactory;
2426
import org.tron.common.application.TronApplicationContext;
25-
import org.tron.common.utils.FileUtil;
2627
import org.tron.common.utils.PublicMethod;
2728
import org.tron.core.Constant;
2829
import org.tron.core.config.DefaultConfig;
@@ -41,17 +42,18 @@ public class RpcApiAccessInterceptorTest {
4142
private static WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubPBFT = null;
4243
private static Application appTest;
4344

44-
private static String dbPath = "output_rpc_api_access_interceptor_test";
45-
4645
@Rule
4746
public ExpectedException thrown = ExpectedException.none();
4847

48+
@ClassRule
49+
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
50+
4951
/**
5052
* init logic.
5153
*/
5254
@BeforeClass
53-
public static void init() {
54-
Args.setParam(new String[] {"-d", dbPath}, Constant.TEST_CONF);
55+
public static void init() throws IOException {
56+
Args.setParam(new String[] {"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF);
5557
Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort());
5658
Args.getInstance().setRpcOnSolidityPort(PublicMethod.chooseRandomPort());
5759
Args.getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort());
@@ -97,15 +99,8 @@ public static void init() {
9799
*/
98100
@AfterClass
99101
public static void destroy() {
102+
context.close();
100103
Args.clearParam();
101-
appTest.shutdownServices();
102-
appTest.shutdown();
103-
context.destroy();
104-
if (FileUtil.deleteDir(new File(dbPath))) {
105-
logger.info("Release resources successful.");
106-
} else {
107-
logger.info("Release resources failure.");
108-
}
109104
}
110105

111106
@Test

framework/src/test/java/org/tron/program/LiteFullNodeToolTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.io.File;
66
import java.nio.file.Paths;
77
import java.util.concurrent.TimeUnit;
8-
98
import lombok.extern.slf4j.Slf4j;
109
import org.junit.After;
1110
import org.junit.Test;
@@ -76,9 +75,7 @@ public void shutdown() throws InterruptedException {
7675
if (channelFull != null) {
7776
channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS);
7877
}
79-
appTest.shutdownServices();
80-
appTest.shutdown();
81-
context.destroy();
78+
context.close();
8279
}
8380

8481
public void init() {

0 commit comments

Comments
 (0)