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 2, 2024
1 parent f16c3f8 commit 2baa0b8
Show file tree
Hide file tree
Showing 43 changed files with 1,153 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,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 @@ -15,6 +16,7 @@
@Component
public class ApplicationImpl implements Application {

@Autowired
private ServiceContainer services;

@Autowired
Expand All @@ -29,32 +31,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 +53,27 @@ public void shutdown() {
tronNetService.close();
}
dbManager.close();
shutdown.countDown();
}

@Override
public void startServices() {
services.start();
try {
services.start();
} catch (Exception e) {
logger.error("Failed to start services", e);
System.exit(1);
}
}

@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
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,61 @@

package org.tron.common.application;

import com.google.common.base.Objects;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.server.ConnectionLimit;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.tron.core.config.args.Args;

@Slf4j(topic = "rpc")
public abstract class HttpService implements Service {
public abstract class HttpService extends AbstractService {

protected Server apiServer;
protected int port;

protected String contextPath;

@Override
public void blockUntilShutdown() {
if (apiServer != null) {
try {
apiServer.join();
} catch (InterruptedException e) {
logger.warn("{}", e.getMessage());
Thread.currentThread().interrupt();
}
public void innerStart() throws Exception {
if (this.apiServer != null) {
this.apiServer.start();
}
}

@Override
public void start() {
if (apiServer != null) {
try {
apiServer.start();
logger.info("{} started, listening on {}", this.getClass().getSimpleName(), port);
} catch (Exception e) {
logger.error("{}", this.getClass().getSimpleName(), e);
}
public void innerStop() throws Exception {
if (this.apiServer != null) {
this.apiServer.stop();
}
}

@Override
public void stop() {
if (apiServer != null) {
logger.info("{} shutdown...", this.getClass().getSimpleName());
try {
apiServer.stop();
} catch (Exception e) {
logger.warn("{}", this.getClass().getSimpleName(), e);
}
logger.info("{} shutdown complete", this.getClass().getSimpleName());
}
public CompletableFuture<Boolean> start() {
initServer();
ServletContextHandler context = initContextHandler();
addServlet(context);
addFilter(context);
return super.start();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
protected void initServer() {
this.apiServer = new Server(this.port);
int maxHttpConnectNumber = Args.getInstance().getMaxHttpConnectNumber();
if (maxHttpConnectNumber > 0) {
this.apiServer.addBean(new ConnectionLimit(maxHttpConnectNumber, this.apiServer));
}
if (o == null || getClass() != o.getClass()) {
return false;
}
HttpService that = (HttpService) o;
return port == that.port;
}

@Override
public int hashCode() {
return Objects.hashCode(getClass().getSimpleName(), port);
protected ServletContextHandler initContextHandler() {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath(this.contextPath);
this.apiServer.setHandler(context);
return context;
}

protected abstract void addServlet(ServletContextHandler context);

protected void addFilter(ServletContextHandler context) {

}
}
Loading

0 comments on commit 2baa0b8

Please sign in to comment.