init: recreate a data-less notarizations DB in place, not aside
Every unreadable notarizations DB was moved aside under a new timestamped notarizations.corrupt-* name. For a failure that persists across starts rather than real corruption (disk full: DB::Open creates the directory and LOCK, then fails), each start moved another empty directory aside, and the copies piled up without limit. When the directory holds no data, i.e. no non-empty .ldb/.sst table or .log write-ahead log, there is nothing to keep: recreate the DB in place instead (falling back to an in-memory DB as before if that fails). A directory that cannot be listed counts as holding data, and any DB with records is still moved aside, never deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+40
-1
@@ -151,6 +151,33 @@ static leveldb::Status ProbeLevelDBLock(const boost::filesystem::path& dbdir)
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does <dbdir> hold any LevelDB data, i.e. a non-empty table (.ldb/.sst) or write-ahead log (.log)?
|
||||
* CURRENT, MANIFEST-*, LOCK and the LOG/LOG.old info logs carry no records. When in doubt (the
|
||||
* directory cannot be listed) say yes, so the caller keeps a copy.
|
||||
*/
|
||||
static bool LevelDBDirHasData(const boost::filesystem::path& dbdir)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
if (!boost::filesystem::exists(dbdir, ec))
|
||||
return ec ? true : false;
|
||||
boost::filesystem::directory_iterator it(dbdir, ec), end;
|
||||
if (ec)
|
||||
return true;
|
||||
for (; it != end; it.increment(ec)) {
|
||||
if (ec)
|
||||
return true;
|
||||
const std::string ext = it->path().extension().string();
|
||||
if (ext != ".ldb" && ext != ".sst" && ext != ".log")
|
||||
continue;
|
||||
boost::system::error_code sizeEc;
|
||||
const uintmax_t size = boost::filesystem::file_size(it->path(), sizeEc);
|
||||
if (sizeEc || size > 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the notarizations (dPoW) DB. It is non-essential and node-regenerable, so nothing here may fail
|
||||
* startup (previously any open failure surfaced as a spurious "Error opening block database"):
|
||||
@@ -159,7 +186,9 @@ static leveldb::Status ProbeLevelDBLock(const boost::filesystem::path& dbdir)
|
||||
* in-memory DB and leave the on-disk one alone.
|
||||
* - otherwise the DB is unreadable (seen: a torn snapshot, 0-byte log -> leveldb "Database I/O error"):
|
||||
* move it aside under a timestamped notarizations.corrupt-* name, never deleting an earlier copy, and
|
||||
* create a fresh one. If even that fails, fall back to an in-memory DB.
|
||||
* create a fresh one. A directory holding no data is recreated in place instead, so a persistent
|
||||
* failure (e.g. disk full) does not add another empty copy on every start. If even that fails, fall
|
||||
* back to an in-memory DB.
|
||||
* The lock is probed only after an open fails, so a holder that exits between the failed open and the
|
||||
* probe makes the probe report "free" for a DB that is fine. The DB is therefore treated as corrupt only
|
||||
* when an open fails although the probe just before that open found the lock free.
|
||||
@@ -210,6 +239,16 @@ static NotarizationDB* OpenNotarizationDB(bool fWipe)
|
||||
fProbedFree = true;
|
||||
}
|
||||
|
||||
if (!LevelDBDirHasData(ndir)) {
|
||||
LogPrintf("%s: notarizations DB failed to open (%s) and holds no data; recreating it in place (non-fatal)\n", __func__, strError);
|
||||
try {
|
||||
return new NotarizationDB(NOTARIZATION_DB_CACHE_BYTES, false, true);
|
||||
} catch (const std::exception& e) {
|
||||
LogPrintf("%s: could not recreate the notarizations DB (%s); using an in-memory notarizations DB for this run\n", __func__, e.what());
|
||||
return new NotarizationDB(NOTARIZATION_DB_CACHE_BYTES, true, false);
|
||||
}
|
||||
}
|
||||
|
||||
LogPrintf("%s: notarizations DB failed to open (%s); moving aside and regenerating (non-fatal)\n", __func__, strError);
|
||||
// Move (do NOT delete) the old DB aside, so a transient open failure (fd exhaustion, disk full,
|
||||
// permissions) cannot permanently destroy notarization history. Earlier copies are kept too.
|
||||
|
||||
Reference in New Issue
Block a user