From 974ec02d249e6a77712e24233692e4e08c8bf31b Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Sun, 16 Sep 2018 21:04:13 -0700 Subject: [PATCH] channeldb/migrations: improve performance of edge update migration This commit modifies the edge update index migration to avoid repetitive calls to Cursor.First(). Instead, we construct a set of all edge update keys to remove, and then do a second pass to remove each from the bucket. Additional log messages are included to notify users on progress, as some have reported long migration times. --- channeldb/migrations.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/channeldb/migrations.go b/channeldb/migrations.go index 948d8249..518271a8 100644 --- a/channeldb/migrations.go +++ b/channeldb/migrations.go @@ -514,19 +514,34 @@ func migratePruneEdgeUpdateIndex(tx *bolt.Tx) error { err) } - // With the existing edge policies gathered, we'll recreate the index - // and populate it with the correct entries. - // - // NOTE: In bolt DB, calling Delete() on an iterator will actually move - // it to the NEXT item. As a result, we call First() here again to go - // back to the front after the item has been deleted. - updateCursor := edgeUpdateIndex.Cursor() - for k, _ := updateCursor.First(); k != nil; k, _ = updateCursor.First() { - if err := updateCursor.Delete(); err != nil { + log.Info("Constructing set of edge update entries to purge.") + + // Build the set of keys that we will remove from the edge update index. + // This will include all keys contained within the bucket. + var updateKeysToRemove [][]byte + err = edgeUpdateIndex.ForEach(func(updKey, _ []byte) error { + updateKeysToRemove = append(updateKeysToRemove, updKey) + return nil + }) + if err != nil { + return fmt.Errorf("unable to gather existing edge updates: %v", + err) + } + + log.Infof("Removing %d entries from edge update index.", + len(updateKeysToRemove)) + + // With the set of keys contained in the edge update index constructed, + // we'll proceed in purging all of them from the index. + for _, updKey := range updateKeysToRemove { + if err := edgeUpdateIndex.Delete(updKey); err != nil { return err } } + log.Infof("Repopulating edge update index with %d valid entries.", + len(edgeKeys)) + // For each edge key, we'll retrieve the policy, deserialize it, and // re-add it to the different buckets. By doing so, we'll ensure that // all existing edge policies are serialized correctly within their