以太坊MPT樹的持久化層是採用了leveldb數據庫,然而在抽取MPT樹代碼運行過程當中,進行get和write操做時卻發生了錯誤:java
Caused by: org.fusesource.leveldbjni.internal.NativeDB$DBException: IO error: C:\data\trie\000785.sst: Could not create random access file. at org.fusesource.leveldbjni.internal.NativeDB.checkStatus(NativeDB.java:200) at org.fusesource.leveldbjni.internal.NativeDB.get(NativeDB.java:307) at org.fusesource.leveldbjni.internal.NativeDB.get(NativeDB.java:300) at org.fusesource.leveldbjni.internal.NativeDB.get(NativeDB.java:293) at org.fusesource.leveldbjni.internal.JniDB.get(JniDB.java:73)
查了一些資料才把這個問題解決,參考:https://github.com/fusesource/leveldbjni/issues/28git
1. 首先增長maxOpenFiles的數量,經過觀察數據庫sst文件數量的變化發現,文件在50個左右,而原來maxOpenFiles=32,所以改成(datasource/LevelDbDataSource.java line75)github
//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)dom
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)spa
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!!code