Skip to content

Commit

Permalink
feat(all):make @PreDestroy work (tronprotocol#5421)
Browse files Browse the repository at this point in the history
  • Loading branch information
halibobo1205 authored Aug 23, 2023
1 parent ac90c45 commit e1d096b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -87,7 +85,6 @@ public void shutdown() {
dbManager.stopFilterProcessThread();
dynamicArgs.close();
logger.info("******** end to shutdown ********");
FullNode.shutDownSign = true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
}
}
7 changes: 1 addition & 6 deletions framework/src/main/java/org/tron/program/FullNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
}
5 changes: 2 additions & 3 deletions framework/src/main/java/org/tron/program/SolidityNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -89,40 +93,45 @@ 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
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit e1d096b

Please sign in to comment.