Skip to content

Commit c0d240c

Browse files
committed
feat(api): optimize api service startup
1 parent f16c3f8 commit c0d240c

Some content is hidden

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

44 files changed

+1168
-1331
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
@@ -456,12 +456,30 @@ public class CommonParameter {
456456
@Getter
457457
@Setter
458458
public String cryptoEngine = Constant.ECKey_ENGINE;
459+
460+
@Getter
461+
@Setter
462+
public boolean rpcEnable = true;
463+
464+
@Getter
465+
@Setter
466+
public boolean rpcSolidityEnable = true;
467+
468+
@Getter
469+
@Setter
470+
public boolean rpcPBFTEnable = true;
471+
459472
@Getter
460473
@Setter
461474
public boolean fullNodeHttpEnable = true;
462475
@Getter
463476
@Setter
464477
public boolean solidityNodeHttpEnable = true;
478+
479+
@Getter
480+
@Setter
481+
public boolean pBFTHttpEnable = true;
482+
465483
@Getter
466484
@Setter
467485
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.tron.core.exception;
2+
3+
public class ServiceStartException extends TronRuntimeException {
4+
5+
public ServiceStartException(String message) {
6+
super(message);
7+
}
8+
9+
public ServiceStartException(String message, Throwable cause) {
10+
super(message, cause);
11+
}
12+
13+
public ServiceStartException(Throwable cause) {
14+
super(cause);
15+
}
16+
17+
}
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/Application.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,11 @@
1515

1616
package org.tron.common.application;
1717

18-
import org.tron.common.parameter.CommonParameter;
1918
import org.tron.core.ChainBaseManager;
20-
import org.tron.core.config.args.Args;
2119
import org.tron.core.db.Manager;
2220

2321
public interface Application {
2422

25-
void setOptions(Args args);
26-
27-
void init(CommonParameter parameter);
28-
29-
void initServices(CommonParameter parameter);
30-
3123
void startup();
3224

3325
void shutdown();
@@ -40,8 +32,6 @@ default void blockUntilShutdown() {
4032

4133
void shutdownServices();
4234

43-
void addService(Service service);
44-
4535
Manager getDbManager();
4636

4737
ChainBaseManager getChainBaseManager();

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

Lines changed: 16 additions & 23 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;
@@ -8,13 +9,15 @@
89
import org.tron.core.config.args.Args;
910
import org.tron.core.consensus.ConsensusService;
1011
import org.tron.core.db.Manager;
12+
import org.tron.core.exception.ServiceStartException;
1113
import org.tron.core.metrics.MetricsUtil;
1214
import org.tron.core.net.TronNetService;
1315

1416
@Slf4j(topic = "app")
1517
@Component
1618
public class ApplicationImpl implements Application {
1719

20+
@Autowired
1821
private ServiceContainer services;
1922

2023
@Autowired
@@ -29,32 +32,12 @@ public class ApplicationImpl implements Application {
2932
@Autowired
3033
private ConsensusService consensusService;
3134

32-
@Override
33-
public void setOptions(Args args) {
34-
// not used
35-
}
36-
37-
@Override
38-
@Autowired
39-
public void init(CommonParameter parameter) {
40-
services = new ServiceContainer();
41-
}
42-
43-
@Override
44-
public void addService(Service service) {
45-
services.add(service);
46-
}
47-
48-
@Override
49-
public void initServices(CommonParameter parameter) {
50-
services.init(parameter);
51-
}
35+
private final CountDownLatch shutdown = new CountDownLatch(1);
5236

5337
/**
5438
* start up the app.
5539
*/
5640
public void startup() {
57-
this.initServices(Args.getInstance());
5841
this.startServices();
5942
if ((!Args.getInstance().isSolidityNode()) && (!Args.getInstance().isP2pDisable())) {
6043
tronNetService.start();
@@ -71,16 +54,26 @@ public void shutdown() {
7154
tronNetService.close();
7255
}
7356
dbManager.close();
57+
shutdown.countDown();
7458
}
7559

7660
@Override
7761
public void startServices() {
78-
services.start();
62+
try {
63+
services.start();
64+
} catch (Exception e) {
65+
throw new ServiceStartException("Failed to start services", e);
66+
}
7967
}
8068

8169
@Override
8270
public void blockUntilShutdown() {
83-
services.blockUntilShutdown();
71+
try {
72+
shutdown.await();
73+
} catch (final InterruptedException e) {
74+
logger.debug("Interrupted, exiting", e);
75+
Thread.currentThread().interrupt();
76+
}
8477
}
8578

8679
@Override

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,11 @@
1515

1616
package org.tron.common.application;
1717

18-
import org.tron.common.parameter.CommonParameter;
1918
import org.tron.core.ChainBaseManager;
20-
import org.tron.core.config.args.Args;
2119
import org.tron.core.db.Manager;
2220

2321
public class CliApplication implements Application {
2422

25-
@Override
26-
public void setOptions(Args args) {
27-
28-
}
29-
30-
@Override
31-
public void init(CommonParameter parameter) {
32-
33-
}
34-
35-
@Override
36-
public void initServices(CommonParameter parameter) {
37-
38-
}
39-
4023
@Override
4124
public void startup() {
4225

@@ -57,11 +40,6 @@ public void shutdownServices() {
5740

5841
}
5942

60-
@Override
61-
public void addService(Service service) {
62-
63-
}
64-
6543
@Override
6644
public Manager getDbManager() {
6745
return null;

0 commit comments

Comments
 (0)