Skip to content

Commit 61c6177

Browse files
authored
feat(log): optimize error tips for db open(#5385)
1 parent 1a33965 commit 61c6177

5 files changed

Lines changed: 74 additions & 7 deletions

File tree

chainbase/src/main/java/org/tron/common/storage/leveldb/LevelDbDataSourceImpl.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,22 @@ private void openDatabase(Options dbOptions) throws IOException {
142142
if (!Files.isSymbolicLink(dbPath.getParent())) {
143143
Files.createDirectories(dbPath.getParent());
144144
}
145-
database = factory.open(dbPath.toFile(), dbOptions);
146-
if (!this.getDBName().startsWith("checkpoint")) {
147-
logger.info("DB {} open success with writeBufferSize {} M, cacheSize {} M, maxOpenFiles {}.",
148-
this.getDBName(), dbOptions.writeBufferSize() / 1024 / 1024,
149-
dbOptions.cacheSize() / 1024 / 1024, dbOptions.maxOpenFiles());
145+
try {
146+
database = factory.open(dbPath.toFile(), dbOptions);
147+
if (!this.getDBName().startsWith("checkpoint")) {
148+
logger
149+
.info("DB {} open success with writeBufferSize {} M, cacheSize {} M, maxOpenFiles {}.",
150+
this.getDBName(), dbOptions.writeBufferSize() / 1024 / 1024,
151+
dbOptions.cacheSize() / 1024 / 1024, dbOptions.maxOpenFiles());
152+
}
153+
} catch (IOException e) {
154+
if (e.getMessage().contains("Corruption:")) {
155+
logger.error("Database {} corrupted, please delete database directory({}) and restart.",
156+
dataBaseName, parentPath, e);
157+
} else {
158+
logger.error("Open Database {} failed", dataBaseName, e);
159+
}
160+
System.exit(1);
150161
}
151162
}
152163

chainbase/src/main/java/org/tron/common/storage/rocksdb/RocksDbDataSourceImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.rocksdb.RocksDBException;
3333
import org.rocksdb.RocksIterator;
3434
import org.rocksdb.Statistics;
35+
import org.rocksdb.Status;
3536
import org.rocksdb.WriteBatch;
3637
import org.rocksdb.WriteOptions;
3738
import org.slf4j.LoggerFactory;
@@ -265,8 +266,13 @@ protected void log(InfoLogLevel infoLogLevel, String logMsg) {
265266
try {
266267
database = RocksDB.open(options, dbPath.toString());
267268
} catch (RocksDBException e) {
268-
throw new RuntimeException(
269-
String.format("failed to open database: %s", dataBaseName), e);
269+
if (Objects.equals(e.getStatus().getCode(), Status.Code.Corruption)) {
270+
logger.error("Database {} corrupted, please delete database directory({}) " +
271+
"and restart.", dataBaseName, parentPath, e);
272+
} else {
273+
logger.error("Open Database {} failed", dataBaseName, e);
274+
}
275+
System.exit(1);
270276
}
271277

272278
alive = true;

framework/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dependencies {
4343
testCompile group: 'junit', name: 'junit', version: '4.13.2'
4444
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.13.0'
4545
testCompile group: 'org.hamcrest', name: 'hamcrest-junit', version: '1.0.0.1'
46+
testCompile group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.16.0'
4647

4748
compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.69'
4849

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
import org.junit.AfterClass;
3838
import org.junit.Assert;
3939
import org.junit.Before;
40+
import org.junit.Rule;
4041
import org.junit.Test;
42+
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
4143
import org.tron.common.utils.ByteArray;
4244
import org.tron.common.utils.FileUtil;
4345
import org.tron.common.utils.PublicMethod;
@@ -65,6 +67,9 @@ public class LevelDbDataSourceImplTest {
6567
private byte[] key5 = "00000005aa".getBytes();
6668
private byte[] key6 = "00000006aa".getBytes();
6769

70+
@Rule
71+
public final ExpectedSystemExit exit = ExpectedSystemExit.none();
72+
6873
/**
6974
* Release resources.
7075
*/
@@ -332,4 +337,24 @@ public void prefixQueryTest() {
332337
dataSource.resetDb();
333338
dataSource.closeDB();
334339
}
340+
341+
@Test
342+
public void initDbTest() {
343+
exit.expectSystemExitWithStatus(1);
344+
makeExceptionDb("test_initDb");
345+
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
346+
Args.getInstance().getOutputDirectory(), "test_initDb");
347+
dataSource.initDB();
348+
dataSource.closeDB();
349+
}
350+
351+
private void makeExceptionDb(String dbName) {
352+
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
353+
Args.getInstance().getOutputDirectory(), "test_initDb");
354+
dataSource.initDB();
355+
dataSource.closeDB();
356+
FileUtil.saveData(dataSource.getDbPath().toString() + "/CURRENT",
357+
"...", Boolean.FALSE);
358+
}
359+
335360
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import org.junit.Assert;
2121
import org.junit.Before;
2222
import org.junit.BeforeClass;
23+
import org.junit.Rule;
2324
import org.junit.Test;
25+
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
2426
import org.tron.common.storage.rocksdb.RocksDbDataSourceImpl;
2527
import org.tron.common.utils.ByteArray;
2628
import org.tron.common.utils.FileUtil;
@@ -48,6 +50,9 @@ public class RocksDbDataSourceImplTest {
4850
private byte[] key5 = "00000005aa".getBytes();
4951
private byte[] key6 = "00000006aa".getBytes();
5052

53+
@Rule
54+
public final ExpectedSystemExit exit = ExpectedSystemExit.none();
55+
5156
/**
5257
* Release resources.
5358
*/
@@ -382,4 +387,23 @@ public void prefixQueryTest() {
382387
dataSource.resetDb();
383388
dataSource.closeDB();
384389
}
390+
391+
@Test
392+
public void initDbTest() {
393+
exit.expectSystemExitWithStatus(1);
394+
makeExceptionDb("test_initDb");
395+
RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl(
396+
Args.getInstance().getOutputDirectory(), "test_initDb");
397+
dataSource.initDB();
398+
dataSource.closeDB();
399+
}
400+
401+
private void makeExceptionDb(String dbName) {
402+
RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl(
403+
Args.getInstance().getOutputDirectory(), "test_initDb");
404+
dataSource.initDB();
405+
dataSource.closeDB();
406+
FileUtil.saveData(dataSource.getDbPath().toString() + "/CURRENT",
407+
"...", Boolean.FALSE);
408+
}
385409
}

0 commit comments

Comments
 (0)