multi: add reset closure to kvdb.Update

Similarly as with kvdb.View this commits adds a reset closure to the
kvdb.Update call in order to be able to reset external state if the
underlying db backend needs to retry the transaction.
This commit is contained in:
Andras Banki-Horvath
2020-10-26 14:06:32 +01:00
parent 2a358327f4
commit d89f51d1d0
44 changed files with 208 additions and 162 deletions

View File

@@ -146,7 +146,7 @@ func OpenClientDB(dbPath string) (*ClientDB, error) {
// initialized. This allows us to assume their presence throughout all
// operations. If an known top-level bucket is expected to exist but is
// missing, this will trigger a ErrUninitializedDB error.
err = kvdb.Update(clientDB.db, initClientDBBuckets)
err = kvdb.Update(clientDB.db, initClientDBBuckets, func() {})
if err != nil {
bdb.Close()
return nil, err
@@ -293,6 +293,8 @@ func (c *ClientDB) CreateTower(lnAddr *lnwire.NetAddress) (*Tower, error) {
// Store the new or updated tower under its tower id.
return putTower(towers, tower)
}, func() {
tower = nil
})
if err != nil {
return nil, err
@@ -379,7 +381,7 @@ func (c *ClientDB) RemoveTower(pubKey *btcec.PublicKey, addr net.Addr) error {
}
return nil
})
}, func() {})
}
// LoadTowerByID retrieves a tower by its tower ID.
@@ -506,6 +508,8 @@ func (c *ClientDB) NextSessionKeyIndex(towerID TowerID) (uint32, error) {
// Record the reserved session key index under this tower's id.
return keyIndex.Put(towerIDBytes, indexBuf[:])
}, func() {
index = 0
})
if err != nil {
return 0, err
@@ -558,7 +562,7 @@ func (c *ClientDB) CreateClientSession(session *ClientSession) error {
// Finally, write the client session's body in the sessions
// bucket.
return putClientSessionBody(sessions, session)
})
}, func() {})
}
// ListClientSessions returns the set of all client sessions known to the db. An
@@ -686,7 +690,7 @@ func (c *ClientDB) RegisterChannel(chanID lnwire.ChannelID,
}
return putChanSummary(chanSummaries, chanID, &summary)
})
}, func() {})
}
// MarkBackupIneligible records that the state identified by the (channel id,
@@ -794,6 +798,8 @@ func (c *ClientDB) CommitUpdate(id *SessionID,
return nil
}, func() {
lastApplied = 0
})
if err != nil {
return 0, err
@@ -899,7 +905,7 @@ func (c *ClientDB) AckUpdate(id *SessionID, seqNum uint16,
// Finally, insert the ack into the sessionAcks sub-bucket.
return sessionAcks.Put(seqNumBuf[:], b.Bytes())
})
}, func() {})
}
// getClientSessionBody loads the body of a ClientSession from the sessions

View File

@@ -88,7 +88,7 @@ func OpenTowerDB(dbPath string) (*TowerDB, error) {
// initialized. This allows us to assume their presence throughout all
// operations. If an known top-level bucket is expected to exist but is
// missing, this will trigger a ErrUninitializedDB error.
err = kvdb.Update(towerDB.db, initTowerDBBuckets)
err = kvdb.Update(towerDB.db, initTowerDBBuckets, func() {})
if err != nil {
bdb.Close()
return nil, err
@@ -214,7 +214,7 @@ func (t *TowerDB) InsertSessionInfo(session *SessionInfo) error {
// be deleted without needing to iterate over the entire
// database.
return touchSessionHintBkt(updateIndex, &session.ID)
})
}, func() {})
}
// InsertStateUpdate stores an update sent by the client after validating that
@@ -296,6 +296,8 @@ func (t *TowerDB) InsertStateUpdate(update *SessionStateUpdate) (uint16, error)
// hint under its session id. This will allow us to delete the
// entries efficiently if the session is ever removed.
return putHintForSession(updateIndex, &update.ID, update.Hint)
}, func() {
lastApplied = 0
})
if err != nil {
return 0, err
@@ -385,7 +387,7 @@ func (t *TowerDB) DeleteSession(target SessionID) error {
// Finally, remove this session from the update index, which
// also removes any of the indexed hints beneath it.
return removeSessionHintBkt(updateIndex, &target)
})
}, func() {})
}
// QueryMatches searches against all known state updates for any that match the
@@ -484,7 +486,7 @@ func (t *TowerDB) SetLookoutTip(epoch *chainntnfs.BlockEpoch) error {
}
return putLookoutEpoch(lookoutTip, epoch)
})
}, func() {})
}
// GetLookoutTip retrieves the current lookout tip block epoch from the tower

View File

@@ -107,7 +107,7 @@ func initOrSyncVersions(db versionedDB, init bool, versions []version) error {
if init {
return kvdb.Update(db.bdb(), func(tx kvdb.RwTx) error {
return initDBVersion(tx, getLatestDBVersion(versions))
})
}, func() {})
}
// Otherwise, ensure that any migrations are applied to ensure the data
@@ -159,5 +159,5 @@ func syncVersions(db versionedDB, versions []version) error {
}
return putDBVersion(tx, latestVersion)
})
}, func() {})
}