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:
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('coin: {}'.format(self.coin.NAME))
@ -137,16 +140,6 @@ class DB(object):
self.logger.info('sync time so far: {}'
.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):
if self.coin.STATIC_BLOCK_HEADERS:
return

View File

@ -31,9 +31,11 @@ class History(object):
self.unflushed = defaultdict(partial(array.array, 'I'))
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.read_state()
self.clear_excess(utxo_flush_count)
return self.flush_count
def close_db(self):
self.db.close()
@ -61,11 +63,11 @@ class History(object):
self.logger.error(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
# updated atomically
if self.flush_count <= flush_count:
return self.flush_count
if self.flush_count <= utxo_flush_count:
return
self.logger.info('DB shut down uncleanly. Scanning for '
'excess history flushes...')
@ -73,19 +75,18 @@ class History(object):
keys = []
for key, hist in self.db.iterator(prefix=b''):
flush_id, = unpack('>H', key[-2:])
if flush_id > flush_count:
if flush_id > utxo_flush_count:
keys.append(key)
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:
for key in keys:
batch.delete(key)
self.write_state(batch)
self.logger.info('deleted excess history entries')
return self.flush_count
def write_state(self, batch):
'''Write state to the history DB.'''

View File

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