Skip to content

feat(service): exit when services failed to start #6228

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

Merged
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
26 changes: 26 additions & 0 deletions common/src/main/java/org/tron/common/log/LogService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.tron.common.log;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import java.io.File;
import org.slf4j.LoggerFactory;
import org.tron.core.exception.TronError;

public class LogService {

public static void load(String path) {
try {
File file = new File(path);
if (!file.exists() || !file.isFile() || !file.canRead()) {
return;
}
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(lc);
lc.reset();
configurator.doConfigure(file);
} catch (Exception e) {
throw new TronError(e, TronError.ErrCode.LOG_LOAD);
}
}
}
7 changes: 3 additions & 4 deletions common/src/main/java/org/tron/common/prometheus/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import io.prometheus.client.Histogram;
import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.hotspot.DefaultExports;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.parameter.CommonParameter;
import org.tron.core.exception.TronError;

@Slf4j(topic = "metrics")
public class Metrics {
Expand All @@ -32,9 +32,8 @@ public static synchronized void init() {
new HTTPServer.Builder().withPort(port).build();
logger.info("prometheus exposed on port : {}", port);
initialized = true;
} catch (IOException e) {
CommonParameter.getInstance().setMetricsPrometheusEnable(false);
logger.error("{}", e.getMessage());
} catch (Exception e) {
throw new TronError(e, TronError.ErrCode.PROMETHEUS_INIT);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions common/src/main/java/org/tron/core/exception/TronError.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public enum ErrCode {
EVENT_SUBSCRIBE_ERROR(1),
AUTO_STOP_PARAMS(1),
API_SERVER_INIT(1),
EVENT_SUBSCRIBE_INIT(1),
PROMETHEUS_INIT(1),
TRON_NET_SERVICE_INIT(1),
ZCASH_INIT(1),
LOG_LOAD(1),
SOLID_NODE_INIT(0);

private final int code;
Expand Down
5 changes: 3 additions & 2 deletions framework/src/main/java/org/tron/core/db/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
import org.tron.core.metrics.MetricsUtil;
import org.tron.core.service.MortgageService;
import org.tron.core.service.RewardViCalService;
import org.tron.core.services.event.exception.EventException;
import org.tron.core.store.AccountAssetStore;
import org.tron.core.store.AccountIdIndexStore;
import org.tron.core.store.AccountIndexStore;
Expand Down Expand Up @@ -2098,7 +2099,7 @@ private void startEventSubscribing() {
.start(Args.getInstance().getEventPluginConfig());

if (!eventPluginLoaded) {
logger.error("Failed to load eventPlugin.");
throw new EventException("Failed to load eventPlugin.");
}

FilterQuery eventFilter = Args.getInstance().getEventFilter();
Expand All @@ -2107,7 +2108,7 @@ private void startEventSubscribing() {
}

} catch (Exception e) {
logger.error("{}", e);
throw new TronError(e, TronError.ErrCode.EVENT_SUBSCRIBE_INIT);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.tron.common.overlay.message.Message;
import org.tron.common.parameter.CommonParameter;
import org.tron.core.config.args.Args;
import org.tron.core.exception.TronError;
import org.tron.core.net.message.adv.TransactionMessage;
import org.tron.core.net.messagehandler.TransactionsMsgHandler;
import org.tron.core.net.peer.PeerConnection;
Expand Down Expand Up @@ -102,7 +103,7 @@ public void start() {
effectiveCheckService.init();
logger.info("Net service start successfully");
} catch (Exception e) {
logger.error("Net service start failed", e);
throw new TronError(e, TronError.ErrCode.TRON_NET_SERVICE_INIT);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Component;
import org.tron.common.zksnark.JLibrustzcash;
import org.tron.common.zksnark.LibrustzcashParam;
import org.tron.core.exception.TronError;
import org.tron.core.exception.ZksnarkException;

@Slf4j(topic = "API")
Expand Down Expand Up @@ -40,7 +41,7 @@ public static void librustzcashInitZksnarkParams() {
JLibrustzcash.librustzcashInitZksnarkParams(
new LibrustzcashParam.InitZksnarkParams(spendPath, spendHash, outputPath, outputHash));
} catch (ZksnarkException e) {
logger.error("librustzcashInitZksnarkParams fail!", e);
throw new TronError(e, TronError.ErrCode.ZCASH_INIT);
}
logger.info("init zk param done");
}
Expand Down
24 changes: 2 additions & 22 deletions framework/src/main/java/org/tron/program/FullNode.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.tron.program;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import com.beust.jcommander.JCommander;
import java.io.File;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.tron.common.application.Application;
import org.tron.common.application.ApplicationFactory;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.exit.ExitManager;
import org.tron.common.log.LogService;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.prometheus.Metrics;
import org.tron.core.Constant;
Expand All @@ -20,23 +17,6 @@
@Slf4j(topic = "app")
public class FullNode {


public static void load(String path) {
try {
File file = new File(path);
if (!file.exists() || !file.isFile() || !file.canRead()) {
return;
}
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(lc);
lc.reset();
configurator.doConfigure(file);
} catch (Exception e) {
logger.error(e.getMessage());
}
}

/**
* Start the FullNode.
*/
Expand All @@ -46,7 +26,7 @@ public static void main(String[] args) {
Args.setParam(args, Constant.TESTNET_CONF);
CommonParameter parameter = Args.getInstance();

load(parameter.getLogbackPath());
LogService.load(parameter.getLogbackPath());

if (parameter.isHelp()) {
JCommander jCommander = JCommander.newBuilder().addObject(Args.PARAMETER).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void init() throws IOException {
Args.setParam(new String[] {"--output-directory",
temporaryFolder.newFolder().toString()}, Constant.TEST_CONF);
CommonParameter.PARAMETER.setMinEffectiveConnection(0);
CommonParameter.getInstance().setP2pDisable(true);

context = new TronApplicationContext(DefaultConfig.class);
wallet = context.getBean(Wallet.class);
Expand Down
48 changes: 48 additions & 0 deletions framework/src/test/java/org/tron/core/exception/TronErrorTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
package org.tron.core.exception;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mockStatic;

import java.io.IOException;
import java.nio.file.Path;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.tron.common.log.LogService;
import org.tron.common.zksnark.JLibrustzcash;
import org.tron.core.zen.ZksnarkInitService;

@RunWith(MockitoJUnitRunner.class)
public class TronErrorTest {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@After
public void clearMocks() {
Mockito.clearAllCaches();
}

@Test
public void testTronError() {
TronError tronError = new TronError("message", TronError.ErrCode.WITNESS_KEYSTORE_LOAD);
Expand All @@ -16,4 +42,26 @@ public void testTronError() {
tronError = new TronError(new Throwable(), TronError.ErrCode.LEVELDB_INIT);
Assert.assertEquals(tronError.getErrCode(), TronError.ErrCode.LEVELDB_INIT);
}

@Test
public void ZksnarkInitTest() {
try (MockedStatic<JLibrustzcash> mock = mockStatic(JLibrustzcash.class)) {
mock.when(JLibrustzcash::isOpenZen).thenReturn(true);
mock.when(() -> JLibrustzcash.librustzcashInitZksnarkParams(any()))
.thenAnswer(invocation -> {
throw new ZksnarkException("Zksnark init failed");
});
TronError thrown = assertThrows(TronError.class,
ZksnarkInitService::librustzcashInitZksnarkParams);
assertEquals(TronError.ErrCode.ZCASH_INIT, thrown.getErrCode());
}
}

@Test
public void LogLoadTest() throws IOException {
LogService.load("non-existent.xml");
Path path = temporaryFolder.newFile("logback.xml").toPath();
TronError thrown = assertThrows(TronError.class, () -> LogService.load(path.toString()));
assertEquals(TronError.ErrCode.LOG_LOAD, thrown.getErrCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public static void init() throws IOException {
getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort());
getInstance().setMetricsPrometheusPort(PublicMethod.chooseRandomPort());
getInstance().setMetricsPrometheusEnable(true);
getInstance().setP2pDisable(true);
String fullNode = String.format("%s:%d", getInstance().getNodeLanIp(),
getInstance().getRpcPort());
String solidityNode = String.format("%s:%d", getInstance().getNodeLanIp(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class HttpApiAccessFilterTest extends BaseTest {
Args.getInstance().setPBFTHttpPort(PublicMethod.chooseRandomPort());
Args.getInstance().setSolidityNodeHttpEnable(true);
Args.getInstance().setSolidityHttpPort(PublicMethod.chooseRandomPort());
Args.getInstance().setP2pDisable(true);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static void init() throws IOException {
Args.getInstance().setRpcOnSolidityPort(PublicMethod.chooseRandomPort());
Args.getInstance().setRpcPBFTEnable(true);
Args.getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort());
Args.getInstance().setP2pDisable(true);
String fullnode = String.format("%s:%d", Args.getInstance().getNodeLanIp(),
Args.getInstance().getRpcPort());
String solidityNode = String.format("%s:%d", Args.getInstance().getNodeLanIp(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class LiteFnQueryHttpFilterTest extends BaseTest {
Args.getInstance().setJsonRpcHttpFullNodeEnable(false);
Args.getInstance().setJsonRpcHttpSolidityNodeEnable(false);
Args.getInstance().setJsonRpcHttpPBFTNodeEnable(false);
Args.getInstance().setP2pDisable(true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static void init() throws IOException {
Args.getInstance().setRpcOnSolidityPort(PublicMethod.chooseRandomPort());
Args.getInstance().setRpcPBFTEnable(true);
Args.getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort());
Args.getInstance().setP2pDisable(true);
String fullNode = String.format("%s:%d", Args.getInstance().getNodeLanIp(),
Args.getInstance().getRpcPort());
String solidityNode = String.format("%s:%d", Args.getInstance().getNodeLanIp(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static void init() throws Exception {
Args.getInstance().needSyncCheck = false;
Args.getInstance().setFullNodeHttpEnable(true);
Args.getInstance().setFullNodeHttpPort(PublicMethod.chooseRandomPort());
Args.getInstance().setP2pDisable(true);
httpNode = String.format("%s:%d", "127.0.0.1",
Args.getInstance().getFullNodeHttpPort());
}
Expand Down