Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import static org.fusesource.leveldbjni.JniDBFactory.factory;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import com.google.common.primitives.Bytes;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -53,7 +51,6 @@
import org.tron.common.storage.WriteOptionsWrapper;
import org.tron.common.storage.metric.DbStat;
import org.tron.common.utils.FileUtil;
import org.tron.common.utils.PropUtil;
import org.tron.common.utils.StorageUtils;
import org.tron.core.db.common.DbSourceInter;
import org.tron.core.db.common.iterator.StoreIterator;
Expand All @@ -73,9 +70,7 @@ public class LevelDbDataSourceImpl extends DbStat implements DbSourceInter<byte[
private Options options;
private WriteOptions writeOptions;
private ReadWriteLock resetDbLock = new ReentrantReadWriteLock();
private static final String LEVELDB = "LEVELDB";
private static final org.slf4j.Logger innerLogger = LoggerFactory.getLogger(LEVELDB);
private static final String KEY_ENGINE = "ENGINE";
private Logger leveldbLogger = new Logger() {
@Override
public void log(String message) {
Expand Down Expand Up @@ -111,11 +106,6 @@ public LevelDbDataSourceImpl(String parentPath, String dataBaseName) {

@Override
public void initDB() {
if (!checkOrInitEngine()) {
throw new RuntimeException(String.format(
"Cannot open RocksDB database '%s' with LevelDB engine."
+ " Set db.engine=ROCKSDB or use LevelDB database. ", dataBaseName));
}
resetDbLock.writeLock().lock();
try {
logger.debug("Init DB: {}.", dataBaseName);
Expand All @@ -141,28 +131,6 @@ public void initDB() {
}
}

private boolean checkOrInitEngine() {
String dir = getDbPath().toString();
String enginePath = dir + File.separator + "engine.properties";

if (FileUtil.createDirIfNotExists(dir)) {
if (!FileUtil.createFileIfNotExists(enginePath)) {
return false;
}
} else {
return false;
}

// for the first init engine
String engine = PropUtil.readProperty(enginePath, KEY_ENGINE);
if (Strings.isNullOrEmpty(engine) && !PropUtil.writeProperty(enginePath, KEY_ENGINE, LEVELDB)) {
return false;
}
engine = PropUtil.readProperty(enginePath, KEY_ENGINE);

return LEVELDB.equals(engine);
}

private void openDatabase(Options dbOptions) throws IOException {
final Path dbPath = getDbPath();
if (dbPath == null || dbPath.getParent() == null) {
Expand All @@ -172,6 +140,8 @@ private void openDatabase(Options dbOptions) throws IOException {
Files.createDirectories(dbPath.getParent());
}
try {
DbSourceInter.checkOrInitEngine(getEngine(), dbPath.toString(),
TronError.ErrCode.LEVELDB_INIT);
database = factory.open(dbPath.toFile(), dbOptions);
if (!this.getDBName().startsWith("checkpoint")) {
logger
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.tron.common.storage.rocksdb;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import com.google.common.primitives.Bytes;
import java.io.File;
Expand Down Expand Up @@ -39,7 +38,6 @@
import org.tron.common.storage.WriteOptionsWrapper;
import org.tron.common.storage.metric.DbStat;
import org.tron.common.utils.FileUtil;
import org.tron.common.utils.PropUtil;
import org.tron.core.db.common.DbSourceInter;
import org.tron.core.db.common.iterator.RockStoreIterator;
import org.tron.core.db2.common.Instance;
Expand All @@ -58,8 +56,6 @@ public class RocksDbDataSourceImpl extends DbStat implements DbSourceInter<byte[
private volatile boolean alive;
private String parentPath;
private ReadWriteLock resetDbLock = new ReentrantReadWriteLock();
private static final String KEY_ENGINE = "ENGINE";
private static final String ROCKSDB = "ROCKSDB";
private static final org.slf4j.Logger rocksDbLogger = LoggerFactory.getLogger(ROCKSDB);

public RocksDbDataSourceImpl(String parentPath, String name, Options options) {
Expand Down Expand Up @@ -187,39 +183,7 @@ public void setDBName(String name) {
this.dataBaseName = name;
}

public boolean checkOrInitEngine() {
String dir = getDbPath().toString();
String enginePath = dir + File.separator + "engine.properties";
File currentFile = new File(dir, "CURRENT");
if (currentFile.exists() && !Paths.get(enginePath).toFile().exists()) {
// if the CURRENT file exists, but the engine.properties file does not exist, it is LevelDB
logger.error(" You are trying to open a LevelDB database with RocksDB engine.");
return false;
}
if (FileUtil.createDirIfNotExists(dir)) {
if (!FileUtil.createFileIfNotExists(enginePath)) {
return false;
}
} else {
return false;
}

// for the first init engine
String engine = PropUtil.readProperty(enginePath, KEY_ENGINE);
if (Strings.isNullOrEmpty(engine) && !PropUtil.writeProperty(enginePath, KEY_ENGINE, ROCKSDB)) {
return false;
}
engine = PropUtil.readProperty(enginePath, KEY_ENGINE);

return ROCKSDB.equals(engine);
}

public void initDB() {
if (!checkOrInitEngine()) {
throw new RuntimeException(String.format(
"Cannot open LevelDB database '%s' with RocksDB engine."
+ " Set db.engine=LEVELDB or use RocksDB database. ", dataBaseName));
}
resetDbLock.writeLock().lock();
try {
if (isAlive()) {
Expand All @@ -244,6 +208,8 @@ protected void log(InfoLogLevel infoLogLevel, String logMsg) {
}

try {
DbSourceInter.checkOrInitEngine(getEngine(), dbPath.toString(),
TronError.ErrCode.ROCKSDB_INIT);
database = RocksDB.open(options, dbPath.toString());
} catch (RocksDBException e) {
if (Objects.equals(e.getStatus().getCode(), Status.Code.Corruption)) {
Expand Down
42 changes: 42 additions & 0 deletions chainbase/src/main/java/org/tron/core/db/common/DbSourceInter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,28 @@
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/

package org.tron.core.db.common;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import java.io.File;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Set;
import org.tron.common.utils.FileUtil;
import org.tron.common.utils.PropUtil;
import org.tron.core.db2.common.WrappedByteArray;
import org.tron.core.exception.TronError;

public interface DbSourceInter<V> extends BatchSourceInter<byte[], V>,
Iterable<Map.Entry<byte[], V>> {

String ENGINE_KEY = "ENGINE";
String ENGINE_FILE = "engine.properties";
String ROCKSDB = "ROCKSDB";
String LEVELDB = "LEVELDB";

String getDBName();

void setDBName(String name);
Expand Down Expand Up @@ -53,4 +65,34 @@ public interface DbSourceInter<V> extends BatchSourceInter<byte[], V>,

Map<WrappedByteArray, byte[]> prefixQuery(byte[] key);

static void checkOrInitEngine(String expectedEngine, String dir, TronError.ErrCode errCode) {
String engineFile = Paths.get(dir, ENGINE_FILE).toString();
File currentFile = new File(dir, "CURRENT");
if (ROCKSDB.equals(expectedEngine) && currentFile.exists()
&& !Paths.get(engineFile).toFile().exists()) {
// if the CURRENT file exists, but the engine.properties file does not exist, it is LevelDB
// 000003.log CURRENT LOCK MANIFEST-000002
throw new TronError(
String.format("Cannot open %s database with %s engine.", LEVELDB, ROCKSDB), errCode);
}
if (FileUtil.createDirIfNotExists(dir)) {
if (!FileUtil.createFileIfNotExists(engineFile)) {
throw new TronError(String.format("Cannot create file: %s.", engineFile), errCode);
}
} else {
throw new TronError(String.format("Cannot create dir: %s.", dir), errCode);
}
String actualEngine = PropUtil.readProperty(engineFile, ENGINE_KEY);
// engine init
if (Strings.isNullOrEmpty(actualEngine)
&& !PropUtil.writeProperty(engineFile, ENGINE_KEY, expectedEngine)) {
throw new TronError(String.format("Cannot write file: %s.", engineFile), errCode);
}
actualEngine = PropUtil.readProperty(engineFile, ENGINE_KEY);
if (!expectedEngine.equals(actualEngine)) {
throw new TronError(String.format(
"Cannot open %s database with %s engine.",
actualEngine, expectedEngine), errCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private static Map<String, String[]> getOptionGroup() {
* set parameters.
*/
public static void setParam(final String[] args, final String confFileName) {
Arch.throwUnsupportedJavaException();
Arch.throwIfUnsupportedJavaVersion();
clearParam(); // reset all parameters to avoid the influence in test
JCommander.newBuilder().addObject(PARAMETER).build().parse(args);
if (PARAMETER.version) {
Expand Down
Loading