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 May 14, 2024
1 parent e7c8d40 commit bc899f9
Show file tree
Hide file tree
Showing 25 changed files with 184 additions and 333 deletions.
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,71 @@
package org.tron.common.application;

import com.google.common.base.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;


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

protected int port;
@Getter
protected boolean enable;
protected final String name = this.getClass().getSimpleName();
protected final AtomicBoolean started = new AtomicBoolean(false);
protected final AtomicBoolean stopped = new AtomicBoolean(false);


@Override
public void start() {
if (started.compareAndSet(false, true)) {
try {
innerStart();
logger.info("{} started, listening on {}", name, port);
} catch (Exception e) {
logger.error("{}", name, e);
}
} else {
logger.error("Attempted to start already running {}.", name);
}
}

@Override
public void stop() {
if (started.get() && stopped.compareAndSet(false, true)) {
logger.info("{} shutdown...", name);
try {
innerStop();
} catch (Exception e) {
logger.warn("{}", name, e);
}
logger.info("{} shutdown complete", name);
} else {
logger.error("Attempted to stop already stopped {}.", name);
}

}

@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;

}
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,6 +74,7 @@ public void shutdown() {
tronNetService.close();
}
dbManager.close();
shutdown.countDown();
}

@Override
Expand All @@ -80,7 +84,12 @@ public void startServices() {

@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,25 @@

package org.tron.common.application;

import com.google.common.base.Objects;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.server.Server;

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

protected Server apiServer;
protected int port;

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

@Override
public void start() {
public void innerStop() throws Exception {
if (apiServer != null) {
try {
apiServer.start();
logger.info("{} started, listening on {}", this.getClass().getSimpleName(), port);
} catch (Exception e) {
logger.error("{}", this.getClass().getSimpleName(), e);
}
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());
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,71 +15,27 @@

package org.tron.common.application;

import com.google.common.base.Objects;
import io.grpc.Server;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;

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

protected Server apiServer;
protected int port;

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

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

@Override
public void stop() {
if (apiServer != null) {
logger.info("{} shutdown...", this.getClass().getSimpleName());
try {
apiServer.shutdown().awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.warn("{}", this.getClass().getSimpleName(), e);
}
logger.info("{} shutdown complete", this.getClass().getSimpleName());
}
}

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

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

}
13 changes: 1 addition & 12 deletions framework/src/main/java/org/tron/common/application/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,11 @@

package org.tron.common.application;

import org.tron.common.parameter.CommonParameter;

public interface Service {

void init();

void init(CommonParameter parameter);

/**
* Start the service.
* {@link Service#init(CommonParameter parameter) init(CommonParameter parameter)} must be called
* before this method.
*/
void start();

void stop();

void blockUntilShutdown();
boolean isEnable();
}
Loading

0 comments on commit bc899f9

Please sign in to comment.