From e1d096be16bd8dcda1673cbfc9cab7f8490573e9 Mon Sep 17 00:00:00 2001 From: halibobo1205 <82020050+halibobo1205@users.noreply.github.com> Date: Wed, 23 Aug 2023 10:57:05 +0800 Subject: [PATCH] feat(all):make @PreDestroy work (#5421) --- .../common/application/ApplicationImpl.java | 3 - .../application/TronApplicationContext.java | 19 ++-- .../main/java/org/tron/program/FullNode.java | 7 +- .../java/org/tron/program/SolidityNode.java | 5 +- .../LiteFnQueryGrpcInterceptorTest.java | 91 ++++++++++--------- .../filter/RpcApiAccessInterceptorTest.java | 23 ++--- .../tron/program/LiteFullNodeToolTest.java | 5 +- 7 files changed, 69 insertions(+), 84 deletions(-) diff --git a/framework/src/main/java/org/tron/common/application/ApplicationImpl.java b/framework/src/main/java/org/tron/common/application/ApplicationImpl.java index 0e38e97baaf..26200abec2d 100644 --- a/framework/src/main/java/org/tron/common/application/ApplicationImpl.java +++ b/framework/src/main/java/org/tron/common/application/ApplicationImpl.java @@ -12,8 +12,6 @@ import org.tron.core.db.Manager; import org.tron.core.metrics.MetricsUtil; import org.tron.core.net.TronNetService; -import org.tron.program.FullNode; -import org.tron.program.SolidityNode; @Slf4j(topic = "app") @Component @@ -87,7 +85,6 @@ public void shutdown() { dbManager.stopFilterProcessThread(); dynamicArgs.close(); logger.info("******** end to shutdown ********"); - FullNode.shutDownSign = true; } @Override diff --git a/framework/src/main/java/org/tron/common/application/TronApplicationContext.java b/framework/src/main/java/org/tron/common/application/TronApplicationContext.java index 7f0aea813a3..482e9e6219d 100644 --- a/framework/src/main/java/org/tron/common/application/TronApplicationContext.java +++ b/framework/src/main/java/org/tron/common/application/TronApplicationContext.java @@ -2,8 +2,7 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.tron.core.db.Manager; -import org.tron.core.net.TronNetService; +import org.tron.program.FullNode; public class TronApplicationContext extends AnnotationConfigApplicationContext { @@ -23,19 +22,13 @@ public TronApplicationContext(String... basePackages) { } @Override - public void destroy() { - + public void doClose() { + logger.info("******** start to close ********"); Application appT = ApplicationFactory.create(this); appT.shutdownServices(); appT.shutdown(); - - TronNetService tronNetService = getBean(TronNetService.class); - tronNetService.close(); - - Manager dbManager = getBean(Manager.class); - dbManager.stopRePushThread(); - dbManager.stopRePushTriggerThread(); - dbManager.stopFilterProcessThread(); - super.destroy(); + super.doClose(); + logger.info("******** close end ********"); + FullNode.shutDownSign = true; } } diff --git a/framework/src/main/java/org/tron/program/FullNode.java b/framework/src/main/java/org/tron/program/FullNode.java index 62732dd5e60..5ebf70e0d7a 100644 --- a/framework/src/main/java/org/tron/program/FullNode.java +++ b/framework/src/main/java/org/tron/program/FullNode.java @@ -81,7 +81,7 @@ public static void main(String[] args) { context.register(DefaultConfig.class); context.refresh(); Application appT = ApplicationFactory.create(context); - shutdown(appT); + context.registerShutdownHook(); // grpc api server RpcApiService rpcApiService = context.getBean(RpcApiService.class); @@ -138,9 +138,4 @@ public static void main(String[] args) { rpcApiService.blockUntilShutdown(); } - - public static void shutdown(final Application app) { - logger.info("********register application shutdown hook********"); - Runtime.getRuntime().addShutdownHook(new Thread(app::shutdown)); - } } diff --git a/framework/src/main/java/org/tron/program/SolidityNode.java b/framework/src/main/java/org/tron/program/SolidityNode.java index 0ca001da7bb..6101db8068c 100644 --- a/framework/src/main/java/org/tron/program/SolidityNode.java +++ b/framework/src/main/java/org/tron/program/SolidityNode.java @@ -71,7 +71,8 @@ public static void main(String[] args) { } parameter.setSolidityNode(true); - ApplicationContext context = new TronApplicationContext(DefaultConfig.class); + TronApplicationContext context = new TronApplicationContext(DefaultConfig.class); + context.registerShutdownHook(); if (parameter.isHelp()) { logger.info("Here is the help message."); @@ -81,8 +82,6 @@ public static void main(String[] args) { Metrics.init(); Application appT = ApplicationFactory.create(context); - FullNode.shutdown(appT); - RpcApiService rpcApiService = context.getBean(RpcApiService.class); appT.addService(rpcApiService); //http diff --git a/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java b/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java index 4ee8fd051d0..439bf1f718f 100644 --- a/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java +++ b/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java @@ -3,23 +3,23 @@ import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; import io.grpc.StatusRuntimeException; -import java.io.File; +import java.io.IOException; import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; - -import org.junit.After; +import org.junit.AfterClass; import org.junit.Assert; -import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; import org.tron.api.GrpcAPI; import org.tron.api.WalletGrpc; import org.tron.api.WalletSolidityGrpc; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; -import org.tron.common.utils.FileUtil; import org.tron.common.utils.PublicMethod; import org.tron.core.ChainBaseManager; import org.tron.core.Constant; @@ -32,54 +32,58 @@ @Slf4j public class LiteFnQueryGrpcInterceptorTest { - private TronApplicationContext context; - private ManagedChannel channelFull = null; - private ManagedChannel channelpBFT = null; - private WalletGrpc.WalletBlockingStub blockingStubFull = null; - private WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubSolidity = null; - private WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubpBFT = null; - private RpcApiService rpcApiService; - private RpcApiServiceOnSolidity rpcApiServiceOnSolidity; - private RpcApiServiceOnPBFT rpcApiServiceOnPBFT; - private Application appTest; - private ChainBaseManager chainBaseManager; - - private String dbPath = "output_grpc_interceptor_test"; + private static TronApplicationContext context; + private static ManagedChannel channelFull = null; + private static ManagedChannel channelSolidity = null; + private static ManagedChannel channelpBFT = null; + private static WalletGrpc.WalletBlockingStub blockingStubFull = null; + private static WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubSolidity = null; + private static WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubpBFT = null; + private static ChainBaseManager chainBaseManager; + private static final String ERROR_MSG = + "UNAVAILABLE: this API is closed because this node is a lite fullnode"; @Rule public ExpectedException thrown = ExpectedException.none(); + @ClassRule + public static TemporaryFolder temporaryFolder = new TemporaryFolder(); + /** * init logic. */ - @Before - public void init() { - Args.setParam(new String[]{"-d", dbPath}, Constant.TEST_CONF); + @BeforeClass + public static void init() throws IOException { + Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort()); Args.getInstance().setRpcOnSolidityPort(PublicMethod.chooseRandomPort()); Args.getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort()); String fullnode = String.format("%s:%d", Args.getInstance().getNodeDiscoveryBindIp(), Args.getInstance().getRpcPort()); + String solidityNode = String.format("%s:%d", Args.getInstance().getNodeDiscoveryBindIp(), + Args.getInstance().getRpcOnSolidityPort()); String pBFTNode = String.format("%s:%d", Args.getInstance().getNodeDiscoveryBindIp(), - Args.getInstance().getRpcOnPBFTPort()); + Args.getInstance().getRpcOnPBFTPort()); channelFull = ManagedChannelBuilder.forTarget(fullnode) .usePlaintext() .build(); + channelSolidity = ManagedChannelBuilder.forTarget(solidityNode) + .usePlaintext() + .build(); channelpBFT = ManagedChannelBuilder.forTarget(pBFTNode) .usePlaintext() .build(); context = new TronApplicationContext(DefaultConfig.class); blockingStubFull = WalletGrpc.newBlockingStub(channelFull); - blockingStubSolidity = WalletSolidityGrpc.newBlockingStub(channelFull); + blockingStubSolidity = WalletSolidityGrpc.newBlockingStub(channelSolidity); blockingStubpBFT = WalletSolidityGrpc.newBlockingStub(channelpBFT); - blockingStubSolidity = WalletSolidityGrpc.newBlockingStub(channelFull); - rpcApiService = context.getBean(RpcApiService.class); - rpcApiServiceOnSolidity = context.getBean(RpcApiServiceOnSolidity.class); - rpcApiServiceOnPBFT = context.getBean(RpcApiServiceOnPBFT.class); + RpcApiService rpcApiService = context.getBean(RpcApiService.class); + RpcApiServiceOnSolidity rpcOnSolidity = context.getBean(RpcApiServiceOnSolidity.class); + RpcApiServiceOnPBFT rpcApiServiceOnPBFT = context.getBean(RpcApiServiceOnPBFT.class); chainBaseManager = context.getBean(ChainBaseManager.class); - appTest = ApplicationFactory.create(context); + Application appTest = ApplicationFactory.create(context); appTest.addService(rpcApiService); - appTest.addService(rpcApiServiceOnSolidity); + appTest.addService(rpcOnSolidity); appTest.addService(rpcApiServiceOnPBFT); appTest.initServices(Args.getInstance()); appTest.startServices(); @@ -89,23 +93,19 @@ public void init() { /** * destroy the context. */ - @After - public void destroy() throws InterruptedException { + @AfterClass + public static void destroy() throws InterruptedException { if (channelFull != null) { channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS); } + if (channelSolidity != null) { + channelSolidity.shutdown().awaitTermination(5, TimeUnit.SECONDS); + } if (channelpBFT != null) { channelpBFT.shutdown().awaitTermination(5, TimeUnit.SECONDS); } + context.close(); Args.clearParam(); - appTest.shutdownServices(); - appTest.shutdown(); - context.destroy(); - if (FileUtil.deleteDir(new File(dbPath))) { - logger.info("Release resources successful."); - } else { - logger.info("Release resources failure."); - } } @Test @@ -113,16 +113,25 @@ public void testGrpcApiThrowStatusRuntimeException() { final GrpcAPI.NumberMessage message = GrpcAPI.NumberMessage.newBuilder().setNum(0).build(); chainBaseManager.setNodeType(ChainBaseManager.NodeType.LITE); thrown.expect(StatusRuntimeException.class); - thrown.expectMessage("UNAVAILABLE: this API is closed because this node is a lite fullnode"); + thrown.expectMessage(ERROR_MSG); blockingStubFull.getBlockByNum(message); } + @Test + public void testGrpcSolidityThrowStatusRuntimeException() { + final GrpcAPI.NumberMessage message = GrpcAPI.NumberMessage.newBuilder().setNum(0).build(); + chainBaseManager.setNodeType(ChainBaseManager.NodeType.LITE); + thrown.expect(StatusRuntimeException.class); + thrown.expectMessage(ERROR_MSG); + blockingStubSolidity.getBlockByNum(message); + } + @Test public void testpBFTGrpcApiThrowStatusRuntimeException() { final GrpcAPI.NumberMessage message = GrpcAPI.NumberMessage.newBuilder().setNum(0).build(); chainBaseManager.setNodeType(ChainBaseManager.NodeType.LITE); thrown.expect(StatusRuntimeException.class); - thrown.expectMessage("UNAVAILABLE: this API is closed because this node is a lite fullnode"); + thrown.expectMessage(ERROR_MSG); blockingStubpBFT.getBlockByNum(message); } diff --git a/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java b/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java index edd15fc19de..ac1f21f6160 100644 --- a/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java +++ b/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java @@ -4,7 +4,7 @@ import io.grpc.ManagedChannelBuilder; import io.grpc.StatusRuntimeException; import io.grpc.stub.ServerCallStreamObserver; -import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -13,16 +13,17 @@ import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; import org.tron.api.GrpcAPI; import org.tron.api.WalletGrpc; import org.tron.api.WalletSolidityGrpc; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; -import org.tron.common.utils.FileUtil; import org.tron.common.utils.PublicMethod; import org.tron.core.Constant; import org.tron.core.config.DefaultConfig; @@ -41,17 +42,18 @@ public class RpcApiAccessInterceptorTest { private static WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubPBFT = null; private static Application appTest; - private static String dbPath = "output_rpc_api_access_interceptor_test"; - @Rule public ExpectedException thrown = ExpectedException.none(); + @ClassRule + public static TemporaryFolder temporaryFolder = new TemporaryFolder(); + /** * init logic. */ @BeforeClass - public static void init() { - Args.setParam(new String[] {"-d", dbPath}, Constant.TEST_CONF); + public static void init() throws IOException { + Args.setParam(new String[] {"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort()); Args.getInstance().setRpcOnSolidityPort(PublicMethod.chooseRandomPort()); Args.getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort()); @@ -97,15 +99,8 @@ public static void init() { */ @AfterClass public static void destroy() { + context.close(); Args.clearParam(); - appTest.shutdownServices(); - appTest.shutdown(); - context.destroy(); - if (FileUtil.deleteDir(new File(dbPath))) { - logger.info("Release resources successful."); - } else { - logger.info("Release resources failure."); - } } @Test diff --git a/framework/src/test/java/org/tron/program/LiteFullNodeToolTest.java b/framework/src/test/java/org/tron/program/LiteFullNodeToolTest.java index e7e164e8e3b..205b2fbd30d 100644 --- a/framework/src/test/java/org/tron/program/LiteFullNodeToolTest.java +++ b/framework/src/test/java/org/tron/program/LiteFullNodeToolTest.java @@ -5,7 +5,6 @@ import java.io.File; import java.nio.file.Paths; import java.util.concurrent.TimeUnit; - import lombok.extern.slf4j.Slf4j; import org.junit.After; import org.junit.Test; @@ -76,9 +75,7 @@ public void shutdown() throws InterruptedException { if (channelFull != null) { channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS); } - appTest.shutdownServices(); - appTest.shutdown(); - context.destroy(); + context.close(); } public void init() {