Skip to content

Commit 8b62bc1

Browse files
committed
refactor(db): optimize logs for db module
1. update log levels 2. optimize log contents 3. add some necessary logs
1 parent 626e31e commit 8b62bc1

38 files changed

Lines changed: 426 additions & 449 deletions

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,24 @@ public LevelDbDataSourceImpl(String parentPath, String dataBaseName) {
102102
public void initDB() {
103103
resetDbLock.writeLock().lock();
104104
try {
105-
logger.debug("~> LevelDbDataSourceImpl.initDB(): " + dataBaseName);
105+
logger.debug("Init DB: {}.", dataBaseName);
106106

107107
if (isAlive()) {
108108
return;
109109
}
110110

111111
if (dataBaseName == null) {
112-
throw new NullPointerException("no name set to the dbStore");
112+
throw new IllegalArgumentException("No name set to the dbStore");
113113
}
114114

115115
try {
116116
openDatabase(options);
117117
alive = true;
118118
} catch (IOException ioe) {
119-
throw new RuntimeException("Can't initialize database", ioe);
119+
throw new RuntimeException(String.format("Can't initialize database, %s", dataBaseName),
120+
ioe);
120121
}
122+
logger.debug("Init DB {} done.", dataBaseName);
121123
} finally {
122124
resetDbLock.writeLock().unlock();
123125
}
@@ -126,19 +128,21 @@ public void initDB() {
126128
private void openDatabase(Options dbOptions) throws IOException {
127129
final Path dbPath = getDbPath();
128130
if (dbPath == null || dbPath.getParent() == null) {
129-
return;
131+
throw new IOException(String.format("db path is illegal, %s/%s", parentPath, dataBaseName));
130132
}
131133
if (!Files.isSymbolicLink(dbPath.getParent())) {
132134
Files.createDirectories(dbPath.getParent());
133135
}
134136
try {
135137
database = factory.open(dbPath.toFile(), dbOptions);
136-
logger.info("DB {} open success with : writeBufferSize {}M,cacheSize {}M,maxOpenFiles {}.",
138+
logger.info("DB {} open success with writeBufferSize {} M, cacheSize {} M, maxOpenFiles {}.",
137139
this.getDBName(), dbOptions.writeBufferSize() / 1024 / 1024,
138140
dbOptions.cacheSize() / 1024 / 1024, dbOptions.maxOpenFiles());
139141
} catch (IOException e) {
140142
if (e.getMessage().contains("Corruption:")) {
143+
logger.warn("DB {} corruption detected, try to repair it.", this.getDBName());
141144
factory.repair(dbPath.toFile(), dbOptions);
145+
logger.warn("DB {} corruption detected, repair done.", this.getDBName());
142146
database = factory.open(dbPath.toFile(), dbOptions);
143147
} else {
144148
throw e;
@@ -460,7 +464,7 @@ public void closeDB() {
460464
database.close();
461465
alive = false;
462466
} catch (IOException e) {
463-
logger.error("Failed to find the dbStore file on the closeDB: {} ", dataBaseName);
467+
logger.error("Failed to find the dbStore file on the closeDB: {}.", dataBaseName, e);
464468
} finally {
465469
resetDbLock.writeLock().unlock();
466470
}

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@
4343
import org.tron.core.db2.common.WrappedByteArray;
4444

4545

46-
@Slf4j
46+
@Slf4j(topic = "DB")
4747
@NoArgsConstructor
4848
public class RocksDbDataSourceImpl extends DbStat implements DbSourceInter<byte[]>,
4949
Iterable<Map.Entry<byte[], byte[]>>, Instance<RocksDbDataSourceImpl> {
5050

5151
ReadOptions readOpts;
52-
private static final String FAIL_TO_INIT_DATABASE = "Failed to initialize database";
5352
private String dataBaseName;
5453
private RocksDB database;
5554
private boolean alive;
@@ -102,6 +101,7 @@ public void closeDB() {
102101
database.close();
103102
alive = false;
104103
} catch (Exception e) {
104+
logger.error("Failed to find the dbStore file on the closeDB: {}.", dataBaseName, e);
105105
} finally {
106106
resetDbLock.writeLock().unlock();
107107
}
@@ -116,7 +116,7 @@ public void resetDb() {
116116

117117
private boolean quitIfNotAlive() {
118118
if (!isAlive()) {
119-
logger.warn("db is not alive");
119+
logger.warn("DB {} is not alive.", dataBaseName);
120120
}
121121
return !isAlive();
122122
}
@@ -181,8 +181,8 @@ public boolean checkOrInitEngine() {
181181

182182
public void initDB() {
183183
if (!checkOrInitEngine()) {
184-
logger.error("database engine do not match");
185-
throw new RuntimeException(FAIL_TO_INIT_DATABASE);
184+
throw new RuntimeException(
185+
String.format("failed to check database: %s, engine do not match", dataBaseName));
186186
}
187187
initDB(RocksDbSettings.getSettings());
188188
}
@@ -194,7 +194,7 @@ public void initDB(RocksDbSettings settings) {
194194
return;
195195
}
196196
if (dataBaseName == null) {
197-
throw new NullPointerException("no name set to the dbStore");
197+
throw new IllegalArgumentException("No name set to the dbStore");
198198
}
199199

200200
try (Options options = new Options()) {
@@ -238,7 +238,7 @@ public void initDB(RocksDbSettings settings) {
238238
.setVerifyChecksums(false);
239239

240240
try {
241-
logger.debug("Opening database");
241+
logger.debug("Opening database {}.", dataBaseName);
242242
final Path dbPath = getDbPath();
243243

244244
if (!Files.isSymbolicLink(dbPath.getParent())) {
@@ -248,17 +248,17 @@ public void initDB(RocksDbSettings settings) {
248248
try {
249249
database = RocksDB.open(options, dbPath.toString());
250250
} catch (RocksDBException e) {
251-
logger.error(e.getMessage(), e);
252-
throw new RuntimeException(FAIL_TO_INIT_DATABASE, e);
251+
throw new RuntimeException(
252+
String.format("failed to open database: %s", dataBaseName), e);
253253
}
254254

255255
alive = true;
256256
} catch (IOException ioe) {
257-
logger.error(ioe.getMessage(), ioe);
258-
throw new RuntimeException(FAIL_TO_INIT_DATABASE, ioe);
257+
throw new RuntimeException(
258+
String.format("failed to init database: %s", dataBaseName), ioe);
259259
}
260260

261-
logger.debug("<~ RocksDbDataSource.initDB(): " + dataBaseName);
261+
logger.debug("Init DB {} done.", dataBaseName);
262262
}
263263
} finally {
264264
resetDbLock.writeLock().unlock();
@@ -274,7 +274,7 @@ public void putData(byte[] key, byte[] value) {
274274
try {
275275
database.put(key, value);
276276
} catch (RocksDBException e) {
277-
throw new RuntimeException(e);
277+
throw new RuntimeException(dataBaseName, e);
278278
} finally {
279279
resetDbLock.readLock().unlock();
280280
}
@@ -289,7 +289,7 @@ public byte[] getData(byte[] key) {
289289
try {
290290
return database.get(key);
291291
} catch (RocksDBException e) {
292-
throw new RuntimeException(e);
292+
throw new RuntimeException(dataBaseName, e);
293293
} finally {
294294
resetDbLock.readLock().unlock();
295295
}
@@ -304,7 +304,7 @@ public void deleteData(byte[] key) {
304304
try {
305305
database.delete(key);
306306
} catch (RocksDBException e) {
307-
throw new RuntimeException(e);
307+
throw new RuntimeException(dataBaseName, e);
308308
} finally {
309309
resetDbLock.readLock().unlock();
310310
}
@@ -365,7 +365,7 @@ public void updateByBatch(Map<byte[], byte[]> rows, WriteOptionsWrapper optionsW
365365
try {
366366
updateByBatchInner(rows);
367367
} catch (Exception e1) {
368-
throw new RuntimeException(e);
368+
throw new RuntimeException(dataBaseName, e1);
369369
}
370370
} finally {
371371
resetDbLock.readLock().unlock();
@@ -384,7 +384,7 @@ public void updateByBatch(Map<byte[], byte[]> rows) {
384384
try {
385385
updateByBatchInner(rows);
386386
} catch (Exception e1) {
387-
throw new RuntimeException(e);
387+
throw new RuntimeException(dataBaseName, e1);
388388
}
389389
} finally {
390390
resetDbLock.readLock().unlock();

chainbase/src/main/java/org/tron/common/utils/Commons.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public static void adjustBalance(AccountStore accountStore, AccountCapsule accou
7474

7575
if (amount < 0 && balance < -amount) {
7676
throw new BalanceInsufficientException(
77-
StringUtil.createReadableString(account.createDbKey()) + " insufficient balance");
77+
String.format("%s insufficient balance, balance: %d, amount: %d",
78+
StringUtil.createReadableString(account.createDbKey()), balance, -amount));
7879
}
7980
account.setBalance(Math.addExact(balance, amount));
8081
accountStore.put(account.getAddress().toByteArray(), account);
@@ -120,12 +121,16 @@ public static void adjustAssetBalanceV2(AccountCapsule account, String AssetID,
120121
if (amount < 0) {
121122
if (!account.reduceAssetAmountV2(AssetID.getBytes(), -amount, dynamicPropertiesStore,
122123
assetIssueStore)) {
123-
throw new BalanceInsufficientException("reduceAssetAmount failed !");
124+
throw new BalanceInsufficientException(
125+
String.format("reduceAssetAmount failed! account: %s",
126+
StringUtil.encode58Check(account.createDbKey())));
124127
}
125128
} else if (amount > 0 &&
126129
!account.addAssetAmountV2(AssetID.getBytes(), amount, dynamicPropertiesStore,
127130
assetIssueStore)) {
128-
throw new BalanceInsufficientException("addAssetAmount failed !");
131+
throw new BalanceInsufficientException(
132+
String.format("addAssetAmount failed! account: %s",
133+
StringUtil.encode58Check(account.createDbKey())));
129134
}
130135
accountStore.put(account.getAddress().toByteArray(), account);
131136
}
@@ -135,7 +140,8 @@ public static void adjustTotalShieldedPoolValue(long valueBalance,
135140
long totalShieldedPoolValue = Math
136141
.subtractExact(dynamicPropertiesStore.getTotalShieldedPoolValue(), valueBalance);
137142
if (totalShieldedPoolValue < 0) {
138-
throw new BalanceInsufficientException("Total shielded pool value can not below 0");
143+
throw new BalanceInsufficientException(String.format(
144+
"total shielded pool value can not below 0, actual: %d", totalShieldedPoolValue));
139145
}
140146
dynamicPropertiesStore.saveTotalShieldedPoolValue(totalShieldedPoolValue);
141147
}

chainbase/src/main/java/org/tron/common/utils/ForkController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private boolean passOld(int version) {
6363
private boolean passNew(int version) {
6464
ForkBlockVersionEnum versionEnum = ForkBlockVersionEnum.getForkBlockVersionEnum(version);
6565
if (versionEnum == null) {
66-
logger.error("not exist block version: {}", version);
66+
logger.error("Not exist block version: {}.", version);
6767
return false;
6868
}
6969
long latestBlockTime = manager.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp();
@@ -167,7 +167,7 @@ public synchronized void update(BlockCapsule blockCapsule) {
167167
stats[slot] = VERSION_UPGRADE;
168168
manager.getDynamicPropertiesStore().statsByVersion(version, stats);
169169
logger.info(
170-
"*******update hard fork:{}, witness size:{}, solt:{}, witness:{}, version:{}",
170+
"******* Update hard fork: {}, witness size: {}, solt: {}, witness: {}, version: {}.",
171171
Streams.zip(witnesses.stream(), Stream.of(ArrayUtils.toObject(stats)), Maps::immutableEntry)
172172
.map(e -> Maps
173173
.immutableEntry(encode58Check(e.getKey().toByteArray()), e.getValue()))

chainbase/src/main/java/org/tron/common/utils/LocalWitnesses.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ private void validate(String privateKey) {
8888
if (StringUtils.isNotBlank(privateKey)
8989
&& privateKey.length() != ChainConstant.PRIVATE_KEY_LENGTH) {
9090
throw new IllegalArgumentException(
91-
"Private key(" + privateKey + ") must be " + ChainConstant.PRIVATE_KEY_LENGTH
92-
+ "-bits hex string.");
91+
String.format("private key must be %d-bits hex string, actual: %d",
92+
ChainConstant.PRIVATE_KEY_LENGTH, privateKey.length()));
9393
}
9494
}
9595

@@ -101,15 +101,15 @@ public void addPrivateKeys(String privateKey) {
101101
//get the first one recently
102102
public String getPrivateKey() {
103103
if (CollectionUtils.isEmpty(privateKeys)) {
104-
logger.warn("privateKey is null");
104+
logger.warn("PrivateKey is null.");
105105
return null;
106106
}
107107
return privateKeys.get(0);
108108
}
109109

110110
public byte[] getPublicKey() {
111111
if (CollectionUtils.isEmpty(privateKeys)) {
112-
logger.warn("privateKey is null");
112+
logger.warn("PrivateKey is null.");
113113
return null;
114114
}
115115
byte[] privateKey = ByteArray.fromHexString(getPrivateKey());

chainbase/src/main/java/org/tron/common/utils/WalletUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static boolean checkPermissionOperations(Permission permission, Contract
2828
throws PermissionException {
2929
ByteString operations = permission.getOperations();
3030
if (operations.size() != 32) {
31-
throw new PermissionException("operations size must 32");
31+
throw new PermissionException(String.format("operations size must 32, actual: %d",
32+
operations.size()));
3233
}
3334
int contractType = contract.getTypeValue();
3435
boolean b = (operations.byteAt(contractType / 8) & (1 << (contractType % 8))) != 0;

chainbase/src/main/java/org/tron/core/ChainBaseManager.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,13 @@ public class ChainBaseManager {
233233
private DbStatService dbStatService;
234234

235235
public void closeOneStore(ITronChainBase database) {
236-
logger.info("******** begin to close " + database.getName() + " ********");
236+
logger.info("******** Begin to close {}. ********", database.getName());
237237
try {
238238
database.close();
239239
} catch (Exception e) {
240-
logger.info("failed to close " + database.getName() + ". " + e);
240+
logger.info("Failed to close {}.", database.getName(), e);
241241
} finally {
242-
logger.info("******** end to close " + database.getName() + " ********");
242+
logger.info("******** End to close {}. ********", database.getName());
243243
}
244244
}
245245

@@ -297,8 +297,7 @@ public BlockCapsule getHead() throws HeaderNotFound {
297297
if (CollectionUtils.isNotEmpty(blocks)) {
298298
return blocks.get(0);
299299
} else {
300-
logger.info("Header block Not Found");
301-
throw new HeaderNotFound("Header block Not Found");
300+
throw new HeaderNotFound("header block not found");
302301
}
303302
}
304303

chainbase/src/main/java/org/tron/core/db/BlockIndexStore.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public BlockId get(Long num)
2929
throws ItemNotFoundException {
3030
BytesCapsule value = getUnchecked(ByteArray.fromLong(num));
3131
if (value == null || value.getData() == null) {
32-
throw new ItemNotFoundException("number: " + num + " is not found!");
32+
throw new ItemNotFoundException(String.format("number: %d is not found!", num));
3333
}
3434
return new BlockId(Sha256Hash.wrap(value.getData()), num);
3535
}
@@ -39,7 +39,8 @@ public BytesCapsule get(byte[] key)
3939
throws ItemNotFoundException {
4040
byte[] value = revokingDB.getUnchecked(key);
4141
if (ArrayUtils.isEmpty(value)) {
42-
throw new ItemNotFoundException("number: " + Arrays.toString(key) + " is not found!");
42+
throw new ItemNotFoundException(String.format("number: %d is not found!",
43+
ByteArray.toLong(key)));
4344
}
4445
return new BytesCapsule(value);
4546
}

chainbase/src/main/java/org/tron/core/db/BlockStore.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515

1616
package org.tron.core.db;
1717

18+
import java.util.ArrayList;
1819
import java.util.Comparator;
1920
import java.util.List;
20-
import java.util.Objects;
21-
import java.util.stream.Collectors;
21+
import java.util.Set;
2222
import lombok.extern.slf4j.Slf4j;
2323
import org.springframework.beans.factory.annotation.Autowired;
2424
import org.springframework.beans.factory.annotation.Value;
2525
import org.springframework.stereotype.Component;
26+
import org.tron.common.error.TronDBException;
2627
import org.tron.common.utils.Sha256Hash;
2728
import org.tron.core.capsule.BlockCapsule;
2829
import org.tron.core.capsule.BlockCapsule.BlockId;
@@ -39,31 +40,23 @@ private BlockStore(@Value("block") String dbName) {
3940

4041
public List<BlockCapsule> getLimitNumber(long startNumber, long limit) {
4142
BlockId startBlockId = new BlockId(Sha256Hash.ZERO_HASH, startNumber);
42-
return revokingDB.getValuesNext(startBlockId.getBytes(), limit).stream()
43-
.map(bytes -> {
44-
try {
45-
return new BlockCapsule(bytes);
46-
} catch (BadItemException ignored) {
47-
}
48-
return null;
49-
})
50-
.filter(Objects::nonNull)
51-
.sorted(Comparator.comparing(BlockCapsule::getNum))
52-
.collect(Collectors.toList());
43+
return pack(revokingDB.getValuesNext(startBlockId.getBytes(), limit));
5344
}
5445

5546
public List<BlockCapsule> getBlockByLatestNum(long getNum) {
47+
return pack(revokingDB.getlatestValues(getNum));
48+
}
5649

57-
return revokingDB.getlatestValues(getNum).stream()
58-
.map(bytes -> {
59-
try {
60-
return new BlockCapsule(bytes);
61-
} catch (BadItemException ignored) {
62-
}
63-
return null;
64-
})
65-
.filter(Objects::nonNull)
66-
.sorted(Comparator.comparing(BlockCapsule::getNum))
67-
.collect(Collectors.toList());
50+
private List<BlockCapsule> pack(Set<byte[]> values) {
51+
List<BlockCapsule> blocks = new ArrayList<>();
52+
for (byte[] bytes : values) {
53+
try {
54+
blocks.add(new BlockCapsule(bytes));
55+
} catch (BadItemException e) {
56+
throw new TronDBException(e);
57+
}
58+
}
59+
blocks.sort(Comparator.comparing(BlockCapsule::getNum));
60+
return blocks;
6861
}
6962
}

0 commit comments

Comments
 (0)