Skip to content

Commit 8ca7085

Browse files
Merge pull request #6233 from halibobo1205/feat/services_init_opt
feat(service): exit when params init failed
2 parents da05a19 + c9b7fbc commit 8ca7085

31 files changed

+112
-75
lines changed

common/src/main/java/org/tron/core/exception/TronError.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public enum ErrCode {
4545
TRON_NET_SERVICE_INIT(1),
4646
ZCASH_INIT(1),
4747
LOG_LOAD(1),
48+
WITNESS_INIT(1),
49+
RATE_LIMITER_INIT(1),
4850
SOLID_NODE_INIT(0);
4951

5052
private final int code;

framework/src/main/java/org/tron/core/config/args/Args.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ public static void setParam(final Config config) {
451451

452452
if (PARAMETER.isWitness()
453453
&& CollectionUtils.isEmpty(localWitnesses.getPrivateKeys())) {
454-
logger.warn("This is a witness node, but localWitnesses is null");
454+
throw new TronError("This is a witness node, but localWitnesses is null",
455+
TronError.ErrCode.WITNESS_INIT);
455456
}
456457

457458
if (config.hasPath(Constant.VM_SUPPORT_CONSTANT)) {
@@ -1142,7 +1143,7 @@ public static void setParam(final Config config) {
11421143
PARAMETER.shutdownBlockTime = new CronExpression(config.getString(
11431144
Constant.NODE_SHUTDOWN_BLOCK_TIME));
11441145
} catch (ParseException e) {
1145-
logger.error(e.getMessage(), e);
1146+
throw new TronError(e, TronError.ErrCode.AUTO_STOP_PARAMS);
11461147
}
11471148
}
11481149

framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.tron.common.prometheus.MetricLabels;
1717
import org.tron.common.prometheus.Metrics;
1818
import org.tron.core.config.args.Args;
19+
import org.tron.core.exception.TronError;
1920
import org.tron.core.services.ratelimiter.GlobalRateLimiter;
2021
import org.tron.core.services.ratelimiter.RateLimiterContainer;
2122
import org.tron.core.services.ratelimiter.RuntimeData;
@@ -40,6 +41,7 @@ private void addRateContainer() {
4041
RateLimiterInitialization.HttpRateLimiterItem item = Args.getInstance()
4142
.getRateLimiterInitialization().getHttpMap().get(getClass().getSimpleName());
4243
boolean success = false;
44+
final String name = getClass().getSimpleName();
4345
if (item != null) {
4446
String cName = "";
4547
String params = "";
@@ -54,32 +56,35 @@ private void addRateContainer() {
5456
|| c == IPQPSRateLimiterAdapter.class) {
5557
constructor = c.getConstructor(String.class);
5658
obj = constructor.newInstance(params);
57-
container.add(KEY_PREFIX_HTTP, getClass().getSimpleName(), (IRateLimiter) obj);
59+
container.add(KEY_PREFIX_HTTP, name, (IRateLimiter) obj);
5860
} else {
5961
constructor = c.getConstructor();
6062
obj = constructor.newInstance(QpsStrategy.DEFAULT_QPS_PARAM);
61-
container.add(KEY_PREFIX_HTTP, getClass().getSimpleName(), (IRateLimiter) obj);
63+
container.add(KEY_PREFIX_HTTP, name, (IRateLimiter) obj);
6264
}
6365
success = true;
6466
} catch (Exception e) {
65-
logger.warn("failure to add the rate limiter strategy. servlet = {}, "
66-
+ "strategy name = {}, params = \"{}\".",
67-
getClass().getSimpleName(), cName, params);
67+
this.throwTronError(cName, params, name, e);
6868
}
6969
}
7070
if (!success) {
7171
// if the specific rate limiter strategy of servlet is not defined or fail to add,
7272
// then add a default Strategy.
7373
try {
7474
IRateLimiter rateLimiter = new DefaultBaseQqsAdapter(QpsStrategy.DEFAULT_QPS_PARAM);
75-
container.add(KEY_PREFIX_HTTP, getClass().getSimpleName(), rateLimiter);
75+
container.add(KEY_PREFIX_HTTP, name, rateLimiter);
7676
} catch (Exception e) {
77-
logger.warn("failure to add the default rate limiter strategy. servlet = {}.",
78-
getClass().getSimpleName());
77+
this.throwTronError("DefaultBaseQqsAdapter", QpsStrategy.DEFAULT_QPS_PARAM, name, e);
7978
}
8079
}
8180
}
8281

82+
private void throwTronError(String strategy, String params, String servlet, Exception e) {
83+
throw new TronError("failure to add the rate limiter strategy. servlet = " + servlet
84+
+ ", strategy name = " + strategy + ", params = \"" + params + "\".",
85+
e, TronError.ErrCode.RATE_LIMITER_INIT);
86+
}
87+
8388
@Override
8489
protected void service(HttpServletRequest req, HttpServletResponse resp)
8590
throws ServletException, IOException {

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public class BandWidthRuntimeOutOfTimeTest extends BaseTest {
7373
"--output-directory", dbPath(),
7474
"--storage-db-directory", dbDirectory,
7575
"--storage-index-directory", indexDirectory,
76-
"-w",
7776
"--debug"
7877
},
7978
"config-test-mainnet.conf"

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public class BandWidthRuntimeOutOfTimeWithCheckTest extends BaseTest {
7474
"--output-directory", dbPath(),
7575
"--storage-db-directory", dbDirectory,
7676
"--storage-index-directory", indexDirectory,
77-
"-w",
7877
"--debug"
7978
},
8079
"config-test-mainnet.conf"

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public static void init() {
6969
"--output-directory", dbPath(),
7070
"--storage-db-directory", dbDirectory,
7171
"--storage-index-directory", indexDirectory,
72-
"-w"
7372
},
7473
"config-test-mainnet.conf"
7574
);

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeWithCheckTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public class BandWidthRuntimeWithCheckTest extends BaseTest {
7676
"--output-directory", dbPath(),
7777
"--storage-db-directory", dbDirectory,
7878
"--storage-index-directory", indexDirectory,
79-
"-w"
8079
},
8180
"config-test-mainnet.conf"
8281
);

framework/src/test/java/org/tron/common/storage/leveldb/LevelDbDataSourceImplTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertFalse;
2323
import static org.junit.Assert.assertNotNull;
2424
import static org.junit.Assert.assertNull;
25+
import static org.junit.Assert.assertThrows;
2526

2627
import com.google.common.collect.Maps;
2728
import com.google.common.collect.Sets;
@@ -41,9 +42,7 @@
4142
import org.junit.Assert;
4243
import org.junit.Before;
4344
import org.junit.ClassRule;
44-
import org.junit.Rule;
4545
import org.junit.Test;
46-
import org.junit.rules.ExpectedException;
4746
import org.junit.rules.TemporaryFolder;
4847
import org.tron.common.utils.ByteArray;
4948
import org.tron.common.utils.FileUtil;
@@ -74,9 +73,6 @@ public class LevelDbDataSourceImplTest {
7473
private byte[] key5 = "00000005aa".getBytes();
7574
private byte[] key6 = "00000006aa".getBytes();
7675

77-
@Rule
78-
public final ExpectedException exception = ExpectedException.none();
79-
8076
/**
8177
* Release resources.
8278
*/
@@ -351,12 +347,11 @@ public void prefixQueryTest() {
351347

352348
@Test
353349
public void initDbTest() {
354-
exception.expect(TronError.class);
355350
makeExceptionDb("test_initDb");
356351
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
357352
Args.getInstance().getOutputDirectory(), "test_initDb");
358-
dataSource.initDB();
359-
dataSource.closeDB();
353+
TronError thrown = assertThrows(TronError.class, dataSource::initDB);
354+
assertEquals(TronError.ErrCode.LEVELDB_INIT, thrown.getErrCode());
360355
}
361356

362357
private void makeExceptionDb(String dbName) {

framework/src/test/java/org/tron/common/storage/leveldb/RocksDbDataSourceImplTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.Assert.assertFalse;
55
import static org.junit.Assert.assertNotNull;
66
import static org.junit.Assert.assertNull;
7+
import static org.junit.Assert.assertThrows;
78

89
import com.google.common.collect.Maps;
910
import com.google.common.collect.Sets;
@@ -23,9 +24,7 @@
2324
import org.junit.Assert;
2425
import org.junit.BeforeClass;
2526
import org.junit.ClassRule;
26-
import org.junit.Rule;
2727
import org.junit.Test;
28-
import org.junit.rules.ExpectedException;
2928
import org.junit.rules.TemporaryFolder;
3029
import org.tron.common.storage.rocksdb.RocksDbDataSourceImpl;
3130
import org.tron.common.utils.ByteArray;
@@ -56,9 +55,6 @@ public class RocksDbDataSourceImplTest {
5655
private byte[] key5 = "00000005aa".getBytes();
5756
private byte[] key6 = "00000006aa".getBytes();
5857

59-
@Rule
60-
public final ExpectedException exception = ExpectedException.none();
61-
6258
/**
6359
* Release resources.
6460
*/
@@ -393,12 +389,11 @@ public void prefixQueryTest() {
393389

394390
@Test
395391
public void initDbTest() {
396-
exception.expect(TronError.class);
397392
makeExceptionDb("test_initDb");
398393
RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl(
399394
Args.getInstance().getOutputDirectory(), "test_initDb");
400-
dataSource.initDB();
401-
dataSource.closeDB();
395+
TronError thrown = assertThrows(TronError.class, dataSource::initDB);
396+
assertEquals(TronError.ErrCode.ROCKSDB_INIT, thrown.getErrCode());
402397
}
403398

404399
private void makeExceptionDb(String dbName) {

framework/src/test/java/org/tron/core/ForkControllerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class ForkControllerTest {
3131
@Before
3232
public void init() throws IOException {
3333
Args.setParam(new String[]{"-d",
34-
temporaryFolder.newFolder().toString(), "-w"}, Constant.TEST_CONF);
34+
temporaryFolder.newFolder().toString()}, Constant.TEST_CONF);
3535
context = new TronApplicationContext(DefaultConfig.class);
3636
dynamicPropertiesStore = context.getBean(DynamicPropertiesStore.class);
3737
chainBaseManager = context.getBean(ChainBaseManager.class);

0 commit comments

Comments
 (0)