Skip to content

Commit 597917b

Browse files
committed
feat(api): optimize api service startup
1 parent 773b374 commit 597917b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1148
-1232
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
@@ -124,15 +124,21 @@ public class Constant {
124124
public static final String NODE_DNS_AWS_REGION = "node.dns.awsRegion";
125125
public static final String NODE_DNS_AWS_HOST_ZONE_ID = "node.dns.awsHostZoneId";
126126

127+
// config for rpc
127128
public static final String NODE_RPC_PORT = "node.rpc.port";
128129
public static final String NODE_RPC_SOLIDITY_PORT = "node.rpc.solidityPort";
129130
public static final String NODE_RPC_PBFT_PORT = "node.rpc.PBFTPort";
131+
public static final String NODE_RPC_ENABLE = "node.rpc.enable";
132+
public static final String NODE_RPC_SOLIDITY_ENABLE = "node.rpc.solidityEnable";
133+
public static final String NODE_RPC_PBFT_ENABLE = "node.rpc.PBFTEnable";
134+
// config for http
130135
public static final String NODE_HTTP_FULLNODE_PORT = "node.http.fullNodePort";
131136
public static final String NODE_HTTP_SOLIDITY_PORT = "node.http.solidityPort";
132137
public static final String NODE_HTTP_FULLNODE_ENABLE = "node.http.fullNodeEnable";
133138
public static final String NODE_HTTP_SOLIDITY_ENABLE = "node.http.solidityEnable";
139+
public static final String NODE_HTTP_PBFT_ENABLE = "node.http.PBFTEnable";
134140
public static final String NODE_HTTP_PBFT_PORT = "node.http.PBFTPort";
135-
141+
// config for jsonrpc
136142
public static final String NODE_JSONRPC_HTTP_FULLNODE_ENABLE = "node.jsonrpc.httpFullNodeEnable";
137143
public static final String NODE_JSONRPC_HTTP_FULLNODE_PORT = "node.jsonrpc.httpFullNodePort";
138144
public static final String NODE_JSONRPC_HTTP_SOLIDITY_ENABLE = "node.jsonrpc.httpSolidityEnable";
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.tron.common.application;
2+
3+
import com.google.common.base.Objects;
4+
import java.util.concurrent.CompletableFuture;
5+
import lombok.Getter;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.tron.core.config.args.Args;
8+
9+
10+
@Slf4j(topic = "service")
11+
public abstract class AbstractService implements Service {
12+
13+
protected int port;
14+
@Getter
15+
protected boolean enable;
16+
@Getter
17+
protected final String name = this.getClass().getSimpleName();
18+
19+
20+
@Override
21+
public CompletableFuture<Boolean> start() {
22+
logger.info("{} starting on {}", name, port);
23+
final CompletableFuture<Boolean> resultFuture = new CompletableFuture<>();
24+
try {
25+
innerStart();
26+
resultFuture.complete(true);
27+
logger.info("{} started, listening on {}", name, port);
28+
} catch (Exception e) {
29+
resultFuture.completeExceptionally(e);
30+
}
31+
return resultFuture;
32+
}
33+
34+
@Override
35+
public CompletableFuture<Boolean> stop() {
36+
logger.info("{} shutdown...", name);
37+
final CompletableFuture<Boolean> resultFuture = new CompletableFuture<>();
38+
try {
39+
innerStop();
40+
resultFuture.complete(true);
41+
logger.info("{} shutdown complete", name);
42+
} catch (Exception e) {
43+
resultFuture.completeExceptionally(e);
44+
}
45+
return resultFuture;
46+
}
47+
48+
@Override
49+
public boolean equals(Object o) {
50+
if (this == o) {
51+
return true;
52+
}
53+
if (o == null || getClass() != o.getClass()) {
54+
return false;
55+
}
56+
AbstractService that = (AbstractService) o;
57+
return port == that.port;
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return Objects.hashCode(name, port);
63+
}
64+
65+
public abstract void innerStart() throws Exception;
66+
67+
public abstract void innerStop() throws Exception;
68+
69+
protected boolean isFullNode() {
70+
return !Args.getInstance().isSolidityNode();
71+
}
72+
73+
}

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

Lines changed: 20 additions & 6 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,16 +74,27 @@ public void shutdown() {
7174
tronNetService.close();
7275
}
7376
dbManager.close();
77+
shutdown.countDown();
7478
}
7579

7680
@Override
7781
public void startServices() {
78-
services.start();
82+
try {
83+
services.start();
84+
} catch (Exception e) {
85+
logger.error("Failed to start services", e);
86+
System.exit(1);
87+
}
7988
}
8089

8190
@Override
8291
public void blockUntilShutdown() {
83-
services.blockUntilShutdown();
92+
try {
93+
shutdown.await();
94+
} catch (final InterruptedException e) {
95+
logger.debug("Interrupted, exiting", e);
96+
Thread.currentThread().interrupt();
97+
}
8498
}
8599

86100
@Override

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

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,67 +15,61 @@
1515

1616
package org.tron.common.application;
1717

18-
import com.google.common.base.Objects;
18+
import java.util.concurrent.CompletableFuture;
1919
import lombok.extern.slf4j.Slf4j;
20+
import org.eclipse.jetty.server.ConnectionLimit;
2021
import org.eclipse.jetty.server.Server;
22+
import org.eclipse.jetty.servlet.ServletContextHandler;
23+
import org.tron.core.config.args.Args;
2124

2225
@Slf4j(topic = "rpc")
23-
public abstract class HttpService implements Service {
26+
public abstract class HttpService extends AbstractService {
2427

2528
protected Server apiServer;
26-
protected int port;
29+
30+
protected String contextPath;
2731

2832
@Override
29-
public void blockUntilShutdown() {
30-
if (apiServer != null) {
31-
try {
32-
apiServer.join();
33-
} catch (InterruptedException e) {
34-
logger.warn("{}", e.getMessage());
35-
Thread.currentThread().interrupt();
36-
}
33+
public void innerStart() throws Exception {
34+
if (this.apiServer != null) {
35+
this.apiServer.start();
3736
}
3837
}
3938

4039
@Override
41-
public void start() {
42-
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-
}
40+
public void innerStop() throws Exception {
41+
if (this.apiServer != null) {
42+
this.apiServer.stop();
4943
}
5044
}
5145

5246
@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-
}
47+
public CompletableFuture<Boolean> start() {
48+
initServer();
49+
ServletContextHandler context = initContextHandler();
50+
addServlet(context);
51+
addFilter(context);
52+
return super.start();
6353
}
6454

65-
@Override
66-
public boolean equals(Object o) {
67-
if (this == o) {
68-
return true;
55+
protected void initServer() {
56+
this.apiServer = new Server(this.port);
57+
int maxHttpConnectNumber = Args.getInstance().getMaxHttpConnectNumber();
58+
if (maxHttpConnectNumber > 0) {
59+
this.apiServer.addBean(new ConnectionLimit(maxHttpConnectNumber, this.apiServer));
6960
}
70-
if (o == null || getClass() != o.getClass()) {
71-
return false;
72-
}
73-
HttpService that = (HttpService) o;
74-
return port == that.port;
7561
}
7662

77-
@Override
78-
public int hashCode() {
79-
return Objects.hashCode(getClass().getSimpleName(), port);
63+
protected ServletContextHandler initContextHandler() {
64+
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
65+
context.setContextPath(this.contextPath);
66+
this.apiServer.setHandler(context);
67+
return context;
68+
}
69+
70+
protected abstract void addServlet(ServletContextHandler context);
71+
72+
protected void addFilter(ServletContextHandler context) {
73+
8074
}
8175
}

0 commit comments

Comments
 (0)