以太坊MPT树的持久化层是采用了leveldb数据库,然而在抽取MPT树代码运行过程中,进行get和write操作时却发生了错误:
Caused by: org.fusesource.leveldbjni.internal.NativeDB$DBException: IO error: C:\data\trie\.sst: Could not create random access file.
at org.fusesource.leveldbjni.internal.NativeDB.checkStatus(NativeDB.java:)
at org.fusesource.leveldbjni.internal.NativeDB.get(NativeDB.java:)
at org.fusesource.leveldbjni.internal.NativeDB.get(NativeDB.java:)
at org.fusesource.leveldbjni.internal.NativeDB.get(NativeDB.java:)
at org.fusesource.leveldbjni.internal.JniDB.get(JniDB.java:)
查了一些资料才把这个问题解决,参考:https://github.com/fusesource/leveldbjni/issues/28
1. 首先增加maxOpenFiles的数量,通过观察数据库sst文件数量的变化发现,文件在50个左右,而原来maxOpenFiles=32,因此改为(datasource/LevelDbDataSource.java line75)
//options.maxOpenFiles(32);
options.maxOpenFiles(100);
2. 对get和write方法进行异常处理
异常造成的原因是:由于leveldb内部的compaction algorithm将一个level的sst文件合并到下一个level的sst文件中,因此在get或write方法就会出现找不到当前sst文件的情况,解决方法是重新get或write一次(虽然这种处理方法不太优雅,但是确实有用)。修改代码(datasource/LevelDbDataSource.java line139)
try {
byte[] ret = db.get(key);
if (logger.isTraceEnabled()) logger.trace("<~ LevelDbDataSource.get(): " + name + ", key: " + Hex.toHexString(key) + ", " + (ret == null ? "null" : ret.length));
return ret;
} catch (DBException e) {
logger.error("Exception. Retrying again...", e);
byte[] ret = db.get(key);
if (logger.isTraceEnabled()) logger.trace("<~ LevelDbDataSource.get(): " + name + ", key: " + Hex.toHexString(key) + ", " + (ret == null ? "null" : ret.length));
return ret;
}
以及(datasource/LevelDbDataSource.java line209)
try{//add 08-23 18:32
db.write(batch);
} catch (DBException e) {
logger.error("Exception. Retrying again...", e);
db.write(batch);
}//end add 08-23 18:32
这要就修复了这个bug!!