Skip to content

Commit d67eef9

Browse files
fix(LiteTool): fix data inconsistency of snapshot generation in the crash scenario
1 parent cbf2417 commit d67eef9

File tree

1 file changed

+27
-41
lines changed

1 file changed

+27
-41
lines changed

plugins/src/main/java/org/tron/plugins/DbLite.java

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.nio.file.Path;
1414
import java.nio.file.Paths;
1515
import java.util.Arrays;
16+
import java.util.Collections;
1617
import java.util.List;
1718
import java.util.NoSuchElementException;
1819
import java.util.Objects;
@@ -353,28 +354,12 @@ private void generateInfoProperties(String propertyfile, long num)
353354
}
354355

355356
private long getLatestBlockHeaderNum(String databaseDir) throws IOException, RocksDBException {
356-
// query latest_block_header_number from checkpoint first
357-
final String latestBlockHeaderNumber = "latest_block_header_number";
358-
DBInterface checkpointDb = getCheckpointDb(databaseDir);
359-
Long blockNumber = getLatestBlockHeaderNumFromCP(checkpointDb,
360-
latestBlockHeaderNumber.getBytes());
361-
if (blockNumber != null) {
362-
return blockNumber;
363-
}
364-
// query from propertiesDb if checkpoint not contains latest_block_header_number
365-
DBInterface propertiesDb = DbTool.getDB(databaseDir, PROPERTIES_DB_NAME);
366-
return Optional.ofNullable(propertiesDb.get(ByteArray.fromString(latestBlockHeaderNumber)))
367-
.map(ByteArray::toLong)
368-
.orElseThrow(
369-
() -> new IllegalArgumentException("not found latest block header number"));
370-
}
371-
372-
private Long getLatestBlockHeaderNumFromCP(DBInterface db, byte[] key) {
373-
byte[] value = db.get(Bytes.concat(simpleEncode(PROPERTIES_DB_NAME), key));
374-
if (value != null && value.length > 1) {
375-
return ByteArray.toLong(Arrays.copyOfRange(value, 1, value.length));
376-
}
377-
return null;
357+
final byte[] latestBlockHeaderNumber = "latest_block_header_number".getBytes();
358+
byte[] value = getDataFromSourceDB(databaseDir, PROPERTIES_DB_NAME, latestBlockHeaderNumber);
359+
return Optional.ofNullable(value)
360+
.map(ByteArray::toLong)
361+
.orElseThrow(
362+
() -> new IllegalArgumentException("not found latest block header number"));
378363
}
379364

380365
/**
@@ -585,16 +570,30 @@ private void mergeBak2Database(String liteDir, BlockNumInfo blockNumInfo) throws
585570

586571
private byte[] getDataFromSourceDB(String sourceDir, String dbName, byte[] key)
587572
throws IOException, RocksDBException {
573+
byte[] keyInCp = Bytes.concat(simpleEncode(dbName), key);
574+
byte[] valueInCp = null;
588575
DBInterface sourceDb = DbTool.getDB(sourceDir, dbName);
589-
DBInterface checkpointDb = getCheckpointDb(sourceDir);
590-
// get data from tmp first.
591-
byte[] valueFromTmp = checkpointDb.get(Bytes.concat(simpleEncode(dbName), key));
576+
// get data from checkpoint first.
577+
List<String> cpList = getCheckpointV2List(sourceDir);
578+
if (cpList.size() > 0) {
579+
// reverse iteration
580+
Collections.reverse(cpList);
581+
for (String cp: cpList) {
582+
valueInCp = DbTool.getDB(
583+
sourceDir + "/" + DBUtils.CHECKPOINT_DB_V2, cp).get(keyInCp);
584+
if (!isEmptyBytes(valueInCp)) {
585+
break;
586+
}
587+
}
588+
} else {
589+
valueInCp = DbTool.getDB(sourceDir, CHECKPOINT_DB).get(keyInCp);
590+
}
592591
byte[] value;
593-
if (isEmptyBytes(valueFromTmp)) {
592+
if (isEmptyBytes(valueInCp)) {
594593
value = sourceDb.get(key);
595594
} else {
596-
value = valueFromTmp.length == 1
597-
? null : Arrays.copyOfRange(valueFromTmp, 1, valueFromTmp.length);
595+
value = valueInCp.length == 1
596+
? null : Arrays.copyOfRange(valueInCp, 1, valueInCp.length);
598597
}
599598
if (isEmptyBytes(value)) {
600599
throw new RuntimeException(String.format("data not found in store, dbName: %s, key: %s",
@@ -664,19 +663,6 @@ private long getSecondBlock(String databaseDir) throws RocksDBException, IOExcep
664663
return num;
665664
}
666665

667-
private DBInterface getCheckpointDb(String sourceDir) throws IOException, RocksDBException {
668-
List<String> cpList = getCheckpointV2List(sourceDir);
669-
DBInterface checkpointDb;
670-
if (cpList.size() > 0) {
671-
String latestCp = cpList.get(cpList.size() - 1);
672-
checkpointDb = DbTool.getDB(
673-
sourceDir + "/" + DBUtils.CHECKPOINT_DB_V2, latestCp);
674-
} else {
675-
checkpointDb = DbTool.getDB(sourceDir, CHECKPOINT_DB);
676-
}
677-
return checkpointDb;
678-
}
679-
680666
@VisibleForTesting
681667
public static void setRecentBlks(long recentBlks) {
682668
RECENT_BLKS = recentBlks;

0 commit comments

Comments
 (0)