Skip to content

feat(service): exit when params init failed #6233

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
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
2 changes: 2 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 @@ -45,6 +45,8 @@ public enum ErrCode {
TRON_NET_SERVICE_INIT(1),
ZCASH_INIT(1),
LOG_LOAD(1),
WITNESS_INIT(1),
RATE_LIMITER_INIT(1),
SOLID_NODE_INIT(0);

private final int code;
Expand Down
5 changes: 3 additions & 2 deletions framework/src/main/java/org/tron/core/config/args/Args.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ public static void setParam(final Config config) {

if (PARAMETER.isWitness()
&& CollectionUtils.isEmpty(localWitnesses.getPrivateKeys())) {
logger.warn("This is a witness node, but localWitnesses is null");
throw new TronError("This is a witness node, but localWitnesses is null",
TronError.ErrCode.WITNESS_INIT);
}

if (config.hasPath(Constant.VM_SUPPORT_CONSTANT)) {
Expand Down Expand Up @@ -1141,7 +1142,7 @@ public static void setParam(final Config config) {
PARAMETER.shutdownBlockTime = new CronExpression(config.getString(
Constant.NODE_SHUTDOWN_BLOCK_TIME));
} catch (ParseException e) {
logger.error(e.getMessage(), e);
throw new TronError(e, TronError.ErrCode.AUTO_STOP_PARAMS);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.tron.common.prometheus.MetricLabels;
import org.tron.common.prometheus.Metrics;
import org.tron.core.config.args.Args;
import org.tron.core.exception.TronError;
import org.tron.core.services.ratelimiter.GlobalRateLimiter;
import org.tron.core.services.ratelimiter.RateLimiterContainer;
import org.tron.core.services.ratelimiter.RuntimeData;
Expand All @@ -40,6 +41,7 @@ private void addRateContainer() {
RateLimiterInitialization.HttpRateLimiterItem item = Args.getInstance()
.getRateLimiterInitialization().getHttpMap().get(getClass().getSimpleName());
boolean success = false;
final String name = getClass().getSimpleName();
if (item != null) {
String cName = "";
String params = "";
Expand All @@ -54,32 +56,35 @@ private void addRateContainer() {
|| c == IPQPSRateLimiterAdapter.class) {
constructor = c.getConstructor(String.class);
obj = constructor.newInstance(params);
container.add(KEY_PREFIX_HTTP, getClass().getSimpleName(), (IRateLimiter) obj);
container.add(KEY_PREFIX_HTTP, name, (IRateLimiter) obj);
} else {
constructor = c.getConstructor();
obj = constructor.newInstance(QpsStrategy.DEFAULT_QPS_PARAM);
container.add(KEY_PREFIX_HTTP, getClass().getSimpleName(), (IRateLimiter) obj);
container.add(KEY_PREFIX_HTTP, name, (IRateLimiter) obj);
}
success = true;
} catch (Exception e) {
logger.warn("failure to add the rate limiter strategy. servlet = {}, "
+ "strategy name = {}, params = \"{}\".",
getClass().getSimpleName(), cName, params);
this.throwTronError(cName, params, name, e);
}
}
if (!success) {
// if the specific rate limiter strategy of servlet is not defined or fail to add,
// then add a default Strategy.
try {
IRateLimiter rateLimiter = new DefaultBaseQqsAdapter(QpsStrategy.DEFAULT_QPS_PARAM);
container.add(KEY_PREFIX_HTTP, getClass().getSimpleName(), rateLimiter);
container.add(KEY_PREFIX_HTTP, name, rateLimiter);
} catch (Exception e) {
logger.warn("failure to add the default rate limiter strategy. servlet = {}.",
getClass().getSimpleName());
this.throwTronError("DefaultBaseQqsAdapter", QpsStrategy.DEFAULT_QPS_PARAM, name, e);
}
}
}

private void throwTronError(String strategy, String params, String servlet, Exception e) {
throw new TronError("failure to add the rate limiter strategy. servlet = " + servlet
+ ", strategy name = " + strategy + ", params = \"" + params + "\".",
e, TronError.ErrCode.RATE_LIMITER_INIT);
}

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public class BandWidthRuntimeOutOfTimeTest extends BaseTest {
"--output-directory", dbPath(),
"--storage-db-directory", dbDirectory,
"--storage-index-directory", indexDirectory,
"-w",
"--debug"
},
"config-test-mainnet.conf"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public class BandWidthRuntimeOutOfTimeWithCheckTest extends BaseTest {
"--output-directory", dbPath(),
"--storage-db-directory", dbDirectory,
"--storage-index-directory", indexDirectory,
"-w",
"--debug"
},
"config-test-mainnet.conf"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public static void init() {
"--output-directory", dbPath(),
"--storage-db-directory", dbDirectory,
"--storage-index-directory", indexDirectory,
"-w"
},
"config-test-mainnet.conf"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public class BandWidthRuntimeWithCheckTest extends BaseTest {
"--output-directory", dbPath(),
"--storage-db-directory", dbDirectory,
"--storage-index-directory", indexDirectory,
"-w"
},
"config-test-mainnet.conf"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
Expand All @@ -41,9 +42,7 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.FileUtil;
Expand Down Expand Up @@ -74,9 +73,6 @@ public class LevelDbDataSourceImplTest {
private byte[] key5 = "00000005aa".getBytes();
private byte[] key6 = "00000006aa".getBytes();

@Rule
public final ExpectedException exception = ExpectedException.none();

/**
* Release resources.
*/
Expand Down Expand Up @@ -351,12 +347,11 @@ public void prefixQueryTest() {

@Test
public void initDbTest() {
exception.expect(TronError.class);
makeExceptionDb("test_initDb");
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
Args.getInstance().getOutputDirectory(), "test_initDb");
dataSource.initDB();
dataSource.closeDB();
TronError thrown = assertThrows(TronError.class, dataSource::initDB);
assertEquals(TronError.ErrCode.LEVELDB_INIT, thrown.getErrCode());
}

private void makeExceptionDb(String dbName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
Expand All @@ -23,9 +24,7 @@
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.tron.common.storage.rocksdb.RocksDbDataSourceImpl;
import org.tron.common.utils.ByteArray;
Expand Down Expand Up @@ -56,9 +55,6 @@ public class RocksDbDataSourceImplTest {
private byte[] key5 = "00000005aa".getBytes();
private byte[] key6 = "00000006aa".getBytes();

@Rule
public final ExpectedException exception = ExpectedException.none();

/**
* Release resources.
*/
Expand Down Expand Up @@ -393,12 +389,11 @@ public void prefixQueryTest() {

@Test
public void initDbTest() {
exception.expect(TronError.class);
makeExceptionDb("test_initDb");
RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl(
Args.getInstance().getOutputDirectory(), "test_initDb");
dataSource.initDB();
dataSource.closeDB();
TronError thrown = assertThrows(TronError.class, dataSource::initDB);
assertEquals(TronError.ErrCode.ROCKSDB_INIT, thrown.getErrCode());
}

private void makeExceptionDb(String dbName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ForkControllerTest {
@Before
public void init() throws IOException {
Args.setParam(new String[]{"-d",
temporaryFolder.newFolder().toString(), "-w"}, Constant.TEST_CONF);
temporaryFolder.newFolder().toString()}, Constant.TEST_CONF);
context = new TronApplicationContext(DefaultConfig.class);
dynamicPropertiesStore = context.getBean(DynamicPropertiesStore.class);
chainBaseManager = context.getBean(ChainBaseManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AccountCapsuleTest extends BaseTest {
static AccountCapsule accountCapsule;

static {
Args.setParam(new String[]{"-d", dbPath(), "-w"}, Constant.TEST_CONF);
Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF);
OWNER_ADDRESS = Wallet.getAddressPreFixString() + "a06a17a49648a8ad32055c06f60fa14ae46df91234";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AssetUtilTest extends BaseTest {


static {
Args.setParam(new String[] {"-d", dbPath(), "-w"}, Constant.TEST_CONF);
Args.setParam(new String[] {"-d", dbPath()}, Constant.TEST_CONF);
}

public static byte[] randomBytes(int length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void destroy() {

@Test
public void get() {
Args.setParam(new String[] {"-w", "-c", Constant.TEST_CONF}, Constant.TESTNET_CONF);
Args.setParam(new String[] {"-c", Constant.TEST_CONF}, Constant.TESTNET_CONF);

CommonParameter parameter = Args.getInstance();

Expand Down Expand Up @@ -132,7 +132,7 @@ public void get() {
@Test
public void testIpFromLibP2p()
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Args.setParam(new String[] {"-w"}, Constant.TEST_CONF);
Args.setParam(new String[] {}, Constant.TEST_CONF);
CommonParameter parameter = Args.getInstance();

String configuredExternalIp = parameter.getNodeExternalIp();
Expand All @@ -153,7 +153,7 @@ public void testIpFromLibP2p()
@Test
public void testOldRewardOpt() {
thrown.expect(IllegalArgumentException.class);
Args.setParam(new String[] {"-w", "-c", "args-test.conf"}, Constant.TESTNET_CONF);
Args.setParam(new String[] {"-c", "args-test.conf"}, Constant.TESTNET_CONF);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.google.protobuf.ByteString;
import java.lang.reflect.Field;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
Expand All @@ -21,6 +22,12 @@
public class DposServiceTest {
DposService service = new DposService();


@AfterClass
public static void destroy() {
Args.clearParam();
}

@Test
public void testValidBlockTime() throws Exception {
long headTime = 1724036757000L;
Expand Down Expand Up @@ -53,8 +60,7 @@ public void testValidBlockTime() throws Exception {

@Test
public void testValidSlot() throws Exception {
Args.setParam(new String[] {"-w"}, Constant.TEST_CONF);
CommonParameter parameter = Args.getInstance();
Args.setParam(new String[] {}, Constant.TEST_CONF);
long headTime = 1724036757000L;
ByteString witness = ByteString.copyFrom(NetUtil.getNodeId());
ByteString witness2 = ByteString.copyFrom(NetUtil.getNodeId());
Expand Down
16 changes: 7 additions & 9 deletions framework/src/test/java/org/tron/core/db/ManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.crypto.ECKey;
Expand Down Expand Up @@ -115,8 +114,6 @@ public class ManagerTest extends BlockGenerate {
private static BlockCapsule blockCapsule2;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public final ExpectedException exception = ExpectedException.none();
private static AtomicInteger port = new AtomicInteger(0);
private static String accountAddress =
Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc";
Expand All @@ -126,7 +123,7 @@ public class ManagerTest extends BlockGenerate {
@Before
public void init() throws IOException {
Args.setParam(new String[]{"-d",
temporaryFolder.newFolder().toString(), "-w"}, Constant.TEST_CONF);
temporaryFolder.newFolder().toString()}, Constant.TEST_CONF);
Args.getInstance().setNodeListenPort(10000 + port.incrementAndGet());
context = new TronApplicationContext(DefaultConfig.class);

Expand Down Expand Up @@ -726,7 +723,7 @@ public void fork()
BadBlockException, TaposException, BadNumberBlockException, NonCommonBlockException,
ReceiptCheckErrException, VMIllegalException, TooBigTransactionResultException,
ZksnarkException, EventBloomException {
Args.setParam(new String[]{"--witness"}, Constant.TEST_CONF);
Args.setParam(new String[]{}, Constant.TEST_CONF);
long size = chainManager.getBlockStore().size();
// System.out.print("block store size:" + size + "\n");
String key = PublicMethod.getRandomPrivateKey();
Expand Down Expand Up @@ -873,7 +870,7 @@ public void doNotSwitch()
TaposException, BadNumberBlockException, NonCommonBlockException,
ReceiptCheckErrException, VMIllegalException, TooBigTransactionResultException,
ZksnarkException, EventBloomException {
Args.setParam(new String[]{"--witness"}, Constant.TEST_CONF);
Args.setParam(new String[]{}, Constant.TEST_CONF);
long size = chainManager.getBlockStore().size();
System.out.print("block store size:" + size + "\n");
String key = PublicMethod.getRandomPrivateKey();
Expand Down Expand Up @@ -985,7 +982,7 @@ public void switchBack()
BadBlockException, TaposException, BadNumberBlockException, NonCommonBlockException,
ReceiptCheckErrException, VMIllegalException, TooBigTransactionResultException,
ZksnarkException, EventBloomException {
Args.setParam(new String[]{"--witness"}, Constant.TEST_CONF);
Args.setParam(new String[]{}, Constant.TEST_CONF);
long size = chainManager.getBlockStore().size();
System.out.print("block store size:" + size + "\n");
String key = PublicMethod.getRandomPrivateKey();;
Expand Down Expand Up @@ -1238,10 +1235,11 @@ public void testExpiration() {

@Test
public void blockTrigger() {
exception.expect(TronError.class);
Manager manager = spy(new Manager());
doThrow(new RuntimeException("postBlockTrigger mock")).when(manager).postBlockTrigger(any());
manager.blockTrigger(new BlockCapsule(Block.newBuilder().build()), 1, 1);
TronError thrown = Assert.assertThrows(TronError.class, () ->
manager.blockTrigger(new BlockCapsule(Block.newBuilder().build()), 1, 1));
Assert.assertEquals(TronError.ErrCode.EVENT_SUBSCRIBE_ERROR, thrown.getErrCode());
}

public void adjustBalance(AccountStore accountStore, byte[] accountAddress, long amount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class TransactionStoreTest extends BaseTest {
*/
@BeforeClass
public static void init() {
Args.setParam(new String[]{"--output-directory", dbPath(), "-w"}, Constant.TEST_CONF);
Args.setParam(new String[]{"--output-directory", dbPath()}, Constant.TEST_CONF);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public class TransactionTraceTest extends BaseTest {
"--output-directory", dbPath(),
"--storage-db-directory", dbDirectory,
"--storage-index-directory", indexDirectory,
"-w",
"--debug"
},
"config-test-mainnet.conf"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void init() {
String dbDirectory = "db_TransactionCache_test";
String indexDirectory = "index_TransactionCache_test";
Args.setParam(new String[]{"--output-directory", dbPath(), "--storage-db-directory",
dbDirectory, "--storage-index-directory", indexDirectory, "-w"}, Constant.TEST_CONF);
dbDirectory, "--storage-index-directory", indexDirectory}, Constant.TEST_CONF);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class AssetUpdateHelperTest extends BaseTest {
private static boolean init;

static {
Args.setParam(new String[]{"-d", dbPath(), "-w"}, "config-test-index.conf");
Args.setParam(new String[]{"-d", dbPath()}, "config-test-index.conf");
Args.getInstance().setSolidityNode(true);
}

Expand Down
Loading