Skip to content

Commit 5bbff44

Browse files
committed
feat(api): optimize api service startup
1 parent e7c8d40 commit 5bbff44

File tree

26 files changed

+289
-344
lines changed

26 files changed

+289
-344
lines changed

common/src/main/java/org/tron/common/parameter/CommonParameter.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,30 @@ public class CommonParameter {
453453
@Getter
454454
@Setter
455455
public String cryptoEngine = Constant.ECKey_ENGINE;
456+
457+
@Getter
458+
@Setter
459+
public boolean rpcEnable = true;
460+
461+
@Getter
462+
@Setter
463+
public boolean rpcSolidityEnable = true;
464+
465+
@Getter
466+
@Setter
467+
public boolean rpcPBFTEnable = true;
468+
456469
@Getter
457470
@Setter
458471
public boolean fullNodeHttpEnable = true;
459472
@Getter
460473
@Setter
461474
public boolean solidityNodeHttpEnable = true;
475+
476+
@Getter
477+
@Setter
478+
public boolean pBFTHttpEnable = true;
479+
462480
@Getter
463481
@Setter
464482
public boolean jsonRpcHttpFullNodeEnable = false;

common/src/main/java/org/tron/core/Constant.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,21 @@ public class Constant {
118118
public static final String NODE_DNS_AWS_REGION = "node.dns.awsRegion";
119119
public static final String NODE_DNS_AWS_HOST_ZONE_ID = "node.dns.awsHostZoneId";
120120

121+
// config for rpc
121122
public static final String NODE_RPC_PORT = "node.rpc.port";
122123
public static final String NODE_RPC_SOLIDITY_PORT = "node.rpc.solidityPort";
123124
public static final String NODE_RPC_PBFT_PORT = "node.rpc.PBFTPort";
125+
public static final String NODE_RPC_ENABLE = "node.rpc.enable";
126+
public static final String NODE_RPC_SOLIDITY_ENABLE = "node.rpc.solidityEnable";
127+
public static final String NODE_RPC_PBFT_ENABLE = "node.rpc.PBFTEnable";
128+
// config for http
124129
public static final String NODE_HTTP_FULLNODE_PORT = "node.http.fullNodePort";
125130
public static final String NODE_HTTP_SOLIDITY_PORT = "node.http.solidityPort";
126131
public static final String NODE_HTTP_FULLNODE_ENABLE = "node.http.fullNodeEnable";
127132
public static final String NODE_HTTP_SOLIDITY_ENABLE = "node.http.solidityEnable";
133+
public static final String NODE_HTTP_PBFT_ENABLE = "node.http.PBFTEnable";
128134
public static final String NODE_HTTP_PBFT_PORT = "node.http.PBFTPort";
129-
135+
// config for jsonrpc
130136
public static final String NODE_JSONRPC_HTTP_FULLNODE_ENABLE = "node.jsonrpc.httpFullNodeEnable";
131137
public static final String NODE_JSONRPC_HTTP_FULLNODE_PORT = "node.jsonrpc.httpFullNodePort";
132138
public static final String NODE_JSONRPC_HTTP_SOLIDITY_ENABLE = "node.jsonrpc.httpSolidityEnable";
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.tron.common.application;
2+
3+
import com.google.common.base.Objects;
4+
import lombok.Getter;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.tron.core.config.args.Args;
7+
8+
9+
@Slf4j(topic = "service")
10+
public abstract class AbstractService implements Service {
11+
12+
protected int port;
13+
@Getter
14+
protected boolean enable;
15+
protected final String name = this.getClass().getSimpleName();
16+
17+
18+
@Override
19+
public void start() {
20+
try {
21+
innerStart();
22+
logger.info("{} started, listening on {}", name, port);
23+
} catch (Exception e) {
24+
logger.error("{}", name, e);
25+
}
26+
}
27+
28+
@Override
29+
public void stop() {
30+
logger.info("{} shutdown...", name);
31+
try {
32+
innerStop();
33+
} catch (Exception e) {
34+
logger.warn("{}", name, e);
35+
}
36+
logger.info("{} shutdown complete", name);
37+
38+
39+
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
if (this == o) {
44+
return true;
45+
}
46+
if (o == null || getClass() != o.getClass()) {
47+
return false;
48+
}
49+
AbstractService that = (AbstractService) o;
50+
return port == that.port;
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return Objects.hashCode(name, port);
56+
}
57+
58+
public abstract void innerStart() throws Exception;
59+
60+
public abstract void innerStop() throws Exception;
61+
62+
protected boolean isFullNode() {
63+
return !Args.getInstance().isSolidityNode();
64+
}
65+
66+
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.tron.common.application;
22

3+
import java.util.concurrent.CountDownLatch;
34
import lombok.extern.slf4j.Slf4j;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.stereotype.Component;
@@ -15,6 +16,7 @@
1516
@Component
1617
public class ApplicationImpl implements Application {
1718

19+
@Autowired
1820
private ServiceContainer services;
1921

2022
@Autowired
@@ -29,6 +31,8 @@ public class ApplicationImpl implements Application {
2931
@Autowired
3032
private ConsensusService consensusService;
3133

34+
private final CountDownLatch shutdown = new CountDownLatch(1);
35+
3236
@Override
3337
public void setOptions(Args args) {
3438
// not used
@@ -37,24 +41,23 @@ public void setOptions(Args args) {
3741
@Override
3842
@Autowired
3943
public void init(CommonParameter parameter) {
40-
services = new ServiceContainer();
44+
// not used
4145
}
4246

4347
@Override
4448
public void addService(Service service) {
45-
services.add(service);
49+
// used by test
4650
}
4751

4852
@Override
4953
public void initServices(CommonParameter parameter) {
50-
services.init(parameter);
54+
// not used
5155
}
5256

5357
/**
5458
* start up the app.
5559
*/
5660
public void startup() {
57-
this.initServices(Args.getInstance());
5861
this.startServices();
5962
if ((!Args.getInstance().isSolidityNode()) && (!Args.getInstance().isP2pDisable())) {
6063
tronNetService.start();
@@ -71,6 +74,7 @@ public void shutdown() {
7174
tronNetService.close();
7275
}
7376
dbManager.close();
77+
shutdown.countDown();
7478
}
7579

7680
@Override
@@ -80,7 +84,12 @@ public void startServices() {
8084

8185
@Override
8286
public void blockUntilShutdown() {
83-
services.blockUntilShutdown();
87+
try {
88+
shutdown.await();
89+
} catch (final InterruptedException e) {
90+
logger.debug("Interrupted, exiting", e);
91+
Thread.currentThread().interrupt();
92+
}
8493
}
8594

8695
@Override

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

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,67 +15,25 @@
1515

1616
package org.tron.common.application;
1717

18-
import com.google.common.base.Objects;
1918
import lombok.extern.slf4j.Slf4j;
2019
import org.eclipse.jetty.server.Server;
2120

2221
@Slf4j(topic = "rpc")
23-
public abstract class HttpService implements Service {
22+
public abstract class HttpService extends AbstractService {
2423

2524
protected Server apiServer;
26-
protected int port;
2725

2826
@Override
29-
public void blockUntilShutdown() {
27+
public void innerStart() throws Exception {
3028
if (apiServer != null) {
31-
try {
32-
apiServer.join();
33-
} catch (InterruptedException e) {
34-
logger.warn("{}", e.getMessage());
35-
Thread.currentThread().interrupt();
36-
}
29+
apiServer.start();
3730
}
3831
}
3932

4033
@Override
41-
public void start() {
34+
public void innerStop() throws Exception {
4235
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-
}
36+
apiServer.stop();
4937
}
5038
}
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-
}
8139
}

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

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,71 +15,27 @@
1515

1616
package org.tron.common.application;
1717

18-
import com.google.common.base.Objects;
1918
import io.grpc.Server;
20-
import java.io.IOException;
2119
import java.util.concurrent.TimeUnit;
2220
import lombok.extern.slf4j.Slf4j;
2321

2422
@Slf4j(topic = "rpc")
25-
public abstract class RpcService implements Service {
23+
public abstract class RpcService extends AbstractService {
2624

2725
protected Server apiServer;
28-
protected int port;
2926

3027
@Override
31-
public void blockUntilShutdown() {
28+
public void innerStart() throws Exception {
3229
if (apiServer != null) {
33-
try {
34-
apiServer.awaitTermination();
35-
} catch (InterruptedException e) {
36-
logger.warn("{}", e.getMessage());
37-
Thread.currentThread().interrupt();
38-
}
30+
apiServer.start();
3931
}
4032
}
4133

4234
@Override
43-
public void start() {
35+
public void innerStop() throws Exception {
4436
if (apiServer != null) {
45-
try {
46-
apiServer.start();
47-
logger.info("{} started, listening on {}", this.getClass().getSimpleName(), port);
48-
} catch (IOException e) {
49-
logger.error("{}", this.getClass().getSimpleName(), e);
50-
}
37+
apiServer.shutdown().awaitTermination(5, TimeUnit.SECONDS);
5138
}
5239
}
5340

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-
8541
}

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,11 @@
1515

1616
package org.tron.common.application;
1717

18-
import org.tron.common.parameter.CommonParameter;
19-
2018
public interface Service {
2119

22-
void init();
23-
24-
void init(CommonParameter parameter);
25-
26-
/**
27-
* Start the service.
28-
* {@link Service#init(CommonParameter parameter) init(CommonParameter parameter)} must be called
29-
* before this method.
30-
*/
3120
void start();
3221

3322
void stop();
3423

35-
void blockUntilShutdown();
24+
boolean isEnable();
3625
}

0 commit comments

Comments
 (0)