Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): optimize api service startup #26

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,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 @@ -118,15 +118,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
@@ -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,6 +31,8 @@ public class ApplicationImpl implements Application {
@Autowired
private ConsensusService consensusService;

private final CountDownLatch shutdown = new CountDownLatch(1);

@Override
public void setOptions(Args args) {
// not used
Expand All @@ -37,24 +41,23 @@ public void setOptions(Args args) {
@Override
@Autowired
public void init(CommonParameter parameter) {
services = new ServiceContainer();
// not used
}

@Override
public void addService(Service service) {
services.add(service);
// used by test
}

@Override
public void initServices(CommonParameter parameter) {
services.init(parameter);
// not used
}

/**
* 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 +74,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,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
Loading