-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(all):make @PreDestroy work #5421
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the shutDownSign be assigned once the validate executor be closed? Is this reasonable to be here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can find something in here, #4020. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the channelSolidity have not been shut down after 5 seconds? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Waits for the channel to become terminated, giving up if the timeout is reached. |
||
} | ||
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); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't all the places where the destroy method is called be replaced, including all test code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
destroy ->close -> doClose
![image](https://private-user-images.githubusercontent.com/82020050/262219255-3c238e67-cc6b-4337-a20f-8c138cf37037.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjEyMDIxNjUsIm5iZiI6MTcyMTIwMTg2NSwicGF0aCI6Ii84MjAyMDA1MC8yNjIyMTkyNTUtM2MyMzhlNjctY2M2Yi00MzM3LWEyMGYtOGMxMzhjZjM3MDM3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzE3VDA3Mzc0NVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWE0ZTA5OWFhZDY3OWU5ZWQwODcxMGM3NDA1NGE4MzQzMzhjZTk3Y2UwZjljZGFjMDQyMjczMDcwMDk2OGZmYjcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.M4popcInySvIoW2id5KGErNff8hGD8RomZx4ixCh9mQ)