Skip to content

Commit 83f0aba

Browse files
committed
feat(rocksdb): release option when db is created
1 parent 6e8742a commit 83f0aba

6 files changed

Lines changed: 27 additions & 89 deletions

File tree

chainbase/src/main/java/org/tron/common/storage/OptionsPicker.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import lombok.extern.slf4j.Slf4j;
4242
import org.iq80.leveldb.DB;
4343
import org.iq80.leveldb.DBIterator;
44-
import org.iq80.leveldb.Logger;
4544
import org.iq80.leveldb.Options;
4645
import org.iq80.leveldb.ReadOptions;
4746
import org.iq80.leveldb.WriteBatch;
@@ -71,27 +70,10 @@ public class LevelDbDataSourceImpl extends DbStat implements DbSourceInter<byte[
7170
private WriteOptions writeOptions;
7271
private ReadWriteLock resetDbLock = new ReentrantReadWriteLock();
7372
private static final org.slf4j.Logger innerLogger = LoggerFactory.getLogger(LEVELDB);
74-
private Logger leveldbLogger = new Logger() {
75-
@Override
76-
public void log(String message) {
77-
innerLogger.info("{} {}", dataBaseName, message);
78-
}
79-
};
8073

8174
/**
8275
* constructor.
8376
*/
84-
public LevelDbDataSourceImpl(String parentPath, String dataBaseName, Options options,
85-
WriteOptions writeOptions) {
86-
this.parentPath = Paths.get(
87-
parentPath,
88-
CommonParameter.getInstance().getStorage().getDbDirectory()
89-
).toString();
90-
this.dataBaseName = dataBaseName;
91-
this.options = options.logger(leveldbLogger);
92-
this.writeOptions = writeOptions;
93-
initDB();
94-
}
9577

9678
public LevelDbDataSourceImpl(String parentPath, String dataBaseName) {
9779
this.parentPath = Paths.get(
@@ -100,8 +82,11 @@ public LevelDbDataSourceImpl(String parentPath, String dataBaseName) {
10082
).toString();
10183

10284
this.dataBaseName = dataBaseName;
103-
options = new Options().logger(leveldbLogger);
104-
writeOptions = new WriteOptions();
85+
this.options = StorageUtils.getOptionsByDbName(dataBaseName).logger(
86+
message -> innerLogger.info("{} {}", dataBaseName, message));
87+
this.writeOptions = new WriteOptions().sync(CommonParameter.getInstance()
88+
.getStorage().isDbSync());
89+
initDB();
10590
}
10691

10792
@Override
@@ -455,7 +440,7 @@ public Stream<Entry<byte[], byte[]>> stream() {
455440
@Override
456441
public LevelDbDataSourceImpl newInstance() {
457442
return new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(dataBaseName),
458-
dataBaseName, options, writeOptions);
443+
dataBaseName);
459444
}
460445

461446
private DBIterator getDBIterator() {

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

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,17 @@ public class RocksDbDataSourceImpl extends DbStat implements DbSourceInter<byte[
5252

5353
private String dataBaseName;
5454
private RocksDB database;
55-
private Options options;
5655
private volatile boolean alive;
5756
private String parentPath;
5857
private ReadWriteLock resetDbLock = new ReentrantReadWriteLock();
5958
private static final org.slf4j.Logger rocksDbLogger = LoggerFactory.getLogger(ROCKSDB);
6059

61-
public RocksDbDataSourceImpl(String parentPath, String name, Options options) {
60+
public RocksDbDataSourceImpl(String parentPath, String name) {
6261
this.dataBaseName = name;
6362
this.parentPath = parentPath;
64-
this.options = options;
6563
initDB();
6664
}
6765

68-
@VisibleForTesting
69-
public RocksDbDataSourceImpl(String parentPath, String name) {
70-
this.parentPath = parentPath;
71-
this.dataBaseName = name;
72-
this.options = RocksDbSettings.getOptionsByDbName(name);
73-
}
74-
7566
public Path getDbPath() {
7667
return Paths.get(parentPath, dataBaseName);
7768
}
@@ -192,14 +183,14 @@ public void initDB() {
192183
if (dataBaseName == null) {
193184
throw new IllegalArgumentException("No name set to the dbStore");
194185
}
195-
options.setLogger(new Logger(options) {
196-
@Override
197-
protected void log(InfoLogLevel infoLogLevel, String logMsg) {
198-
rocksDbLogger.info("{} {}", dataBaseName, logMsg);
199-
}
200-
});
201-
202-
try {
186+
try (Options options = RocksDbSettings.getOptionsByDbName(dataBaseName);
187+
) {
188+
options.setLogger(new Logger(options) {
189+
@Override
190+
protected void log(InfoLogLevel infoLogLevel, String logMsg) {
191+
rocksDbLogger.info("{} {}", dataBaseName, logMsg);
192+
}
193+
});
203194
logger.debug("Opening database {}.", dataBaseName);
204195
final Path dbPath = getDbPath();
205196

@@ -420,9 +411,9 @@ public void backup(String dir) throws RocksDBException {
420411
}
421412

422413
private RocksIterator getRocksIterator() {
423-
try ( ReadOptions readOptions = new ReadOptions().setFillCache(false)) {
414+
try (ReadOptions readOptions = new ReadOptions().setFillCache(false)) {
424415
throwIfNotAlive();
425-
return database.newIterator(readOptions);
416+
return database.newIterator(readOptions);
426417
}
427418
}
428419

@@ -432,8 +423,7 @@ public boolean deleteDbBakPath(String dir) {
432423

433424
@Override
434425
public RocksDbDataSourceImpl newInstance() {
435-
return new RocksDbDataSourceImpl(parentPath, dataBaseName,
436-
this.options);
426+
return new RocksDbDataSourceImpl(parentPath, dataBaseName);
437427
}
438428

439429

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
import javax.annotation.PostConstruct;
99
import lombok.Getter;
1010
import lombok.extern.slf4j.Slf4j;
11-
import org.iq80.leveldb.WriteOptions;
1211
import org.springframework.beans.factory.annotation.Autowired;
1312
import org.tron.common.parameter.CommonParameter;
14-
import org.tron.common.storage.OptionsPicker;
1513
import org.tron.common.storage.WriteOptionsWrapper;
1614
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
1715
import org.tron.common.storage.metric.DbStatService;
@@ -24,7 +22,7 @@
2422
import org.tron.core.exception.ItemNotFoundException;
2523

2624
@Slf4j(topic = "DB")
27-
public abstract class TronDatabase<T> extends OptionsPicker implements ITronChainBase<T> {
25+
public abstract class TronDatabase<T> implements ITronChainBase<T> {
2826

2927
protected DbSourceInter<byte[]> dbSource;
3028
@Getter
@@ -40,18 +38,12 @@ protected TronDatabase(String dbName) {
4038

4139
if ("LEVELDB".equals(CommonParameter.getInstance().getStorage()
4240
.getDbEngine().toUpperCase())) {
43-
dbSource =
44-
new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(dbName),
45-
dbName,
46-
getOptionsByDbNameForLevelDB(dbName),
47-
new WriteOptions().sync(CommonParameter.getInstance()
48-
.getStorage().isDbSync()));
41+
dbSource = new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(dbName), dbName);
4942
} else if ("ROCKSDB".equals(CommonParameter.getInstance()
5043
.getStorage().getDbEngine().toUpperCase())) {
5144
String parentName = Paths.get(StorageUtils.getOutputDirectoryByDbName(dbName),
5245
CommonParameter.getInstance().getStorage().getDbDirectory()).toString();
53-
dbSource =
54-
new RocksDbDataSourceImpl(parentName, dbName, getOptionsByDbNameForRocksDB(dbName));
46+
dbSource = new RocksDbDataSourceImpl(parentName, dbName);
5547
}
5648

5749
dbSource.initDB();

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
import javax.annotation.PostConstruct;
1616
import lombok.Getter;
1717
import lombok.extern.slf4j.Slf4j;
18-
import org.iq80.leveldb.WriteOptions;
1918
import org.springframework.beans.factory.annotation.Autowired;
2019
import org.tron.common.parameter.CommonParameter;
21-
import org.tron.common.storage.OptionsPicker;
2220
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
2321
import org.tron.common.storage.metric.DbStatService;
2422
import org.tron.common.storage.rocksdb.RocksDbDataSourceImpl;
@@ -38,7 +36,7 @@
3836

3937

4038
@Slf4j(topic = "DB")
41-
public abstract class TronStoreWithRevoking<T extends ProtoCapsule> extends OptionsPicker implements ITronChainBase<T> {
39+
public abstract class TronStoreWithRevoking<T extends ProtoCapsule> implements ITronChainBase<T> {
4240

4341
@Getter // only for unit test
4442
protected IRevokingDB revokingDB;
@@ -58,18 +56,12 @@ protected TronStoreWithRevoking(String dbName) {
5856
String dbEngine = CommonParameter.getInstance().getStorage().getDbEngine();
5957
if ("LEVELDB".equals(dbEngine.toUpperCase())) {
6058
this.db = new LevelDB(
61-
new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(dbName),
62-
dbName,
63-
getOptionsByDbNameForLevelDB(dbName),
64-
new WriteOptions().sync(CommonParameter.getInstance()
65-
.getStorage().isDbSync())));
59+
new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(dbName), dbName));
6660
} else if ("ROCKSDB".equals(dbEngine.toUpperCase())) {
6761
String parentPath = Paths
6862
.get(StorageUtils.getOutputDirectoryByDbName(dbName), CommonParameter
6963
.getInstance().getStorage().getDbDirectory()).toString();
70-
this.db = new RocksDB(
71-
new RocksDbDataSourceImpl(parentPath,
72-
dbName, getOptionsByDbNameForRocksDB(dbName)));
64+
this.db = new RocksDB(new RocksDbDataSourceImpl(parentPath, dbName));
7365
} else {
7466
throw new RuntimeException(String.format("db engine %s is error", dbEngine));
7567
}

chainbase/src/main/java/org/tron/core/db2/common/TxCacheDB.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@
3131
import lombok.extern.slf4j.Slf4j;
3232
import org.apache.commons.lang3.ArrayUtils;
3333
import org.bouncycastle.util.encoders.Hex;
34-
import org.iq80.leveldb.WriteOptions;
3534
import org.tron.common.parameter.CommonParameter;
3635
import org.tron.common.prometheus.MetricKeys;
3736
import org.tron.common.prometheus.Metrics;
38-
import org.tron.common.storage.OptionsPicker;
3937
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
4038
import org.tron.common.storage.rocksdb.RocksDbDataSourceImpl;
4139
import org.tron.common.utils.ByteArray;
@@ -49,7 +47,7 @@
4947
import org.tron.core.store.DynamicPropertiesStore;
5048

5149
@Slf4j(topic = "DB")
52-
public class TxCacheDB extends OptionsPicker implements DB<byte[], byte[]>, Flusher {
50+
public class TxCacheDB implements DB<byte[], byte[]>, Flusher {
5351

5452
// > 65_536(= 2^16) blocks, that is the number of the reference block
5553
private static final long MAX_BLOCK_SIZE = 65536;
@@ -106,17 +104,13 @@ public TxCacheDB(String name, RecentTransactionStore recentTransactionStore,
106104
String dbEngine = CommonParameter.getInstance().getStorage().getDbEngine();
107105
if ("LEVELDB".equals(dbEngine.toUpperCase())) {
108106
this.persistentStore = new LevelDB(
109-
new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(name),
110-
name, getOptionsByDbNameForLevelDB(name),
111-
new WriteOptions().sync(CommonParameter.getInstance()
112-
.getStorage().isDbSync())));
107+
new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(name), name));
113108
} else if ("ROCKSDB".equals(dbEngine.toUpperCase())) {
114109
String parentPath = Paths
115110
.get(StorageUtils.getOutputDirectoryByDbName(name), CommonParameter
116111
.getInstance().getStorage().getDbDirectory()).toString();
117112

118-
this.persistentStore = new RocksDB(
119-
new RocksDbDataSourceImpl(parentPath, name, getOptionsByDbNameForRocksDB(name)));
113+
this.persistentStore = new RocksDB(new RocksDbDataSourceImpl(parentPath, name));
120114
} else {
121115
throw new RuntimeException(String.format("db type: %s is not supported", dbEngine));
122116
}

0 commit comments

Comments
 (0)