Skip to content

Commit

Permalink
feat(api): optimize api service startup
Browse files Browse the repository at this point in the history
  • Loading branch information
halibobo1205 committed Dec 12, 2024
1 parent f16c3f8 commit 99b9767
Show file tree
Hide file tree
Showing 44 changed files with 1,169 additions and 1,333 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,30 @@ public class CommonParameter {
@Getter
@Setter
public String cryptoEngine = Constant.ECKey_ENGINE;

@Getter
@Setter
public boolean rpcEnable = true;

@Getter
@Setter
public boolean rpcSolidityEnable = true;

@Getter
@Setter
public boolean rpcPBFTEnable = true;

@Getter
@Setter
public boolean fullNodeHttpEnable = true;
@Getter
@Setter
public boolean solidityNodeHttpEnable = true;

@Getter
@Setter
public boolean pBFTHttpEnable = true;

@Getter
@Setter
public boolean jsonRpcHttpFullNodeEnable = false;
Expand Down
8 changes: 7 additions & 1 deletion common/src/main/java/org/tron/core/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,21 @@ public class Constant {
public static final String NODE_DNS_AWS_REGION = "node.dns.awsRegion";
public static final String NODE_DNS_AWS_HOST_ZONE_ID = "node.dns.awsHostZoneId";

// config for rpc
public static final String NODE_RPC_PORT = "node.rpc.port";
public static final String NODE_RPC_SOLIDITY_PORT = "node.rpc.solidityPort";
public static final String NODE_RPC_PBFT_PORT = "node.rpc.PBFTPort";
public static final String NODE_RPC_ENABLE = "node.rpc.enable";
public static final String NODE_RPC_SOLIDITY_ENABLE = "node.rpc.solidityEnable";
public static final String NODE_RPC_PBFT_ENABLE = "node.rpc.PBFTEnable";
// config for http
public static final String NODE_HTTP_FULLNODE_PORT = "node.http.fullNodePort";
public static final String NODE_HTTP_SOLIDITY_PORT = "node.http.solidityPort";
public static final String NODE_HTTP_FULLNODE_ENABLE = "node.http.fullNodeEnable";
public static final String NODE_HTTP_SOLIDITY_ENABLE = "node.http.solidityEnable";
public static final String NODE_HTTP_PBFT_ENABLE = "node.http.PBFTEnable";
public static final String NODE_HTTP_PBFT_PORT = "node.http.PBFTPort";

// config for jsonrpc
public static final String NODE_JSONRPC_HTTP_FULLNODE_ENABLE = "node.jsonrpc.httpFullNodeEnable";
public static final String NODE_JSONRPC_HTTP_FULLNODE_PORT = "node.jsonrpc.httpFullNodePort";
public static final String NODE_JSONRPC_HTTP_SOLIDITY_ENABLE = "node.jsonrpc.httpSolidityEnable";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.tron.core.exception;

public class ServiceStartException extends TronRuntimeException {

public ServiceStartException(String message) {
super(message);
}

public ServiceStartException(String message, Throwable cause) {
super(message, cause);
}

public ServiceStartException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.tron.common.application;

import com.google.common.base.Objects;
import java.util.concurrent.CompletableFuture;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.tron.core.config.args.Args;


@Slf4j(topic = "service")
public abstract class AbstractService implements Service {

protected int port;
@Getter
protected boolean enable;
@Getter
protected final String name = this.getClass().getSimpleName();


@Override
public CompletableFuture<Boolean> start() {
logger.info("{} starting on {}", name, port);
final CompletableFuture<Boolean> resultFuture = new CompletableFuture<>();
try {
innerStart();
resultFuture.complete(true);
logger.info("{} started, listening on {}", name, port);
} catch (Exception e) {
resultFuture.completeExceptionally(e);
}
return resultFuture;
}

@Override
public CompletableFuture<Boolean> stop() {
logger.info("{} shutdown...", name);
final CompletableFuture<Boolean> resultFuture = new CompletableFuture<>();
try {
innerStop();
resultFuture.complete(true);
logger.info("{} shutdown complete", name);
} catch (Exception e) {
resultFuture.completeExceptionally(e);
}
return resultFuture;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AbstractService that = (AbstractService) o;
return port == that.port;
}

@Override
public int hashCode() {
return Objects.hashCode(name, port);
}

public abstract void innerStart() throws Exception;

public abstract void innerStop() throws Exception;

protected boolean isFullNode() {
return !Args.getInstance().isSolidityNode();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,11 @@

package org.tron.common.application;

import org.tron.common.parameter.CommonParameter;
import org.tron.core.ChainBaseManager;
import org.tron.core.config.args.Args;
import org.tron.core.db.Manager;

public interface Application {

void setOptions(Args args);

void init(CommonParameter parameter);

void initServices(CommonParameter parameter);

void startup();

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

void shutdownServices();

void addService(Service service);

Manager getDbManager();

ChainBaseManager getChainBaseManager();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tron.common.application;

import java.util.concurrent.CountDownLatch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -8,13 +9,15 @@
import org.tron.core.config.args.Args;
import org.tron.core.consensus.ConsensusService;
import org.tron.core.db.Manager;
import org.tron.core.exception.ServiceStartException;
import org.tron.core.metrics.MetricsUtil;
import org.tron.core.net.TronNetService;

@Slf4j(topic = "app")
@Component
public class ApplicationImpl implements Application {

@Autowired
private ServiceContainer services;

@Autowired
Expand All @@ -29,32 +32,12 @@ public class ApplicationImpl implements Application {
@Autowired
private ConsensusService consensusService;

@Override
public void setOptions(Args args) {
// not used
}

@Override
@Autowired
public void init(CommonParameter parameter) {
services = new ServiceContainer();
}

@Override
public void addService(Service service) {
services.add(service);
}

@Override
public void initServices(CommonParameter parameter) {
services.init(parameter);
}
private final CountDownLatch shutdown = new CountDownLatch(1);

/**
* start up the app.
*/
public void startup() {
this.initServices(Args.getInstance());
this.startServices();
if ((!Args.getInstance().isSolidityNode()) && (!Args.getInstance().isP2pDisable())) {
tronNetService.start();
Expand All @@ -71,16 +54,26 @@ public void shutdown() {
tronNetService.close();
}
dbManager.close();
shutdown.countDown();
}

@Override
public void startServices() {
services.start();
try {
services.start();
} catch (Exception e) {
throw new ServiceStartException("Failed to start services", e);
}
}

@Override
public void blockUntilShutdown() {
services.blockUntilShutdown();
try {
shutdown.await();
} catch (final InterruptedException e) {
logger.debug("Interrupted, exiting", e);
Thread.currentThread().interrupt();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,11 @@

package org.tron.common.application;

import org.tron.common.parameter.CommonParameter;
import org.tron.core.ChainBaseManager;
import org.tron.core.config.args.Args;
import org.tron.core.db.Manager;

public class CliApplication implements Application {

@Override
public void setOptions(Args args) {

}

@Override
public void init(CommonParameter parameter) {

}

@Override
public void initServices(CommonParameter parameter) {

}

@Override
public void startup() {

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

}

@Override
public void addService(Service service) {

}

@Override
public Manager getDbManager() {
return null;
Expand Down
Loading

0 comments on commit 99b9767

Please sign in to comment.