Move clean_db() logic to when hist DB is opened

This commit is contained in:
Neil Booth 2018-05-26 09:24:37 +08:00
parent e88b8c0dec
commit e0186d7661
3 changed files with 13 additions and 19 deletions

View File

@ -125,7 +125,10 @@ class DB(object):
if self.first_sync == self.utxo_db.for_sync: if self.first_sync == self.utxo_db.for_sync:
break break
self.history.open_db(self.db_class, for_sync) # Open history DB, clear excess history
self.utxo_flush_count = self.history.open_db(self.db_class, for_sync,
self.utxo_flush_count)
self.clear_excess_undo_info()
self.logger.info('DB version: {:d}'.format(self.db_version)) self.logger.info('DB version: {:d}'.format(self.db_version))
self.logger.info('coin: {}'.format(self.coin.NAME)) self.logger.info('coin: {}'.format(self.coin.NAME))
@ -137,16 +140,6 @@ class DB(object):
self.logger.info('sync time so far: {}' self.logger.info('sync time so far: {}'
.format(util.formatted_time(self.wall_time))) .format(util.formatted_time(self.wall_time)))
def clean_db(self):
'''Clean out stale DB items.
Stale DB items are excess history flushed since the most
recent UTXO flush (only happens on unclean shutdown), and aged
undo information.
'''
self.utxo_flush_count = self.history.clear_excess(self.utxo_flush_count)
self.clear_excess_undo_info()
def fs_update_header_offsets(self, offset_start, height_start, headers): def fs_update_header_offsets(self, offset_start, height_start, headers):
if self.coin.STATIC_BLOCK_HEADERS: if self.coin.STATIC_BLOCK_HEADERS:
return return

View File

@ -31,9 +31,11 @@ class History(object):
self.unflushed = defaultdict(partial(array.array, 'I')) self.unflushed = defaultdict(partial(array.array, 'I'))
self.unflushed_count = 0 self.unflushed_count = 0
def open_db(self, db_class, for_sync): def open_db(self, db_class, for_sync, utxo_flush_count):
self.db = db_class('hist', for_sync) self.db = db_class('hist', for_sync)
self.read_state() self.read_state()
self.clear_excess(utxo_flush_count)
return self.flush_count
def close_db(self): def close_db(self):
self.db.close() self.db.close()
@ -61,11 +63,11 @@ class History(object):
self.logger.error(msg) self.logger.error(msg)
raise RuntimeError(msg) raise RuntimeError(msg)
def clear_excess(self, flush_count): def clear_excess(self, utxo_flush_count):
# < might happen at end of compaction as both DBs cannot be # < might happen at end of compaction as both DBs cannot be
# updated atomically # updated atomically
if self.flush_count <= flush_count: if self.flush_count <= utxo_flush_count:
return self.flush_count return
self.logger.info('DB shut down uncleanly. Scanning for ' self.logger.info('DB shut down uncleanly. Scanning for '
'excess history flushes...') 'excess history flushes...')
@ -73,19 +75,18 @@ class History(object):
keys = [] keys = []
for key, hist in self.db.iterator(prefix=b''): for key, hist in self.db.iterator(prefix=b''):
flush_id, = unpack('>H', key[-2:]) flush_id, = unpack('>H', key[-2:])
if flush_id > flush_count: if flush_id > utxo_flush_count:
keys.append(key) keys.append(key)
self.logger.info('deleting {:,d} history entries'.format(len(keys))) self.logger.info('deleting {:,d} history entries'.format(len(keys)))
self.flush_count = flush_count self.flush_count = utxo_flush_count
with self.db.write_batch() as batch: with self.db.write_batch() as batch:
for key in keys: for key in keys:
batch.delete(key) batch.delete(key)
self.write_state(batch) self.write_state(batch)
self.logger.info('deleted excess history entries') self.logger.info('deleted excess history entries')
return self.flush_count
def write_state(self, batch): def write_state(self, batch):
'''Write state to the history DB.''' '''Write state to the history DB.'''

View File

@ -1 +1 @@
VERSION = 'ElectrumX 1.4.4p1' VERSION = 'ElectrumX 1.4.4p2'