Skip to content

Commit

Permalink
Remove vertex ID recycle function
Browse files Browse the repository at this point in the history
why:
  It is not safe in general to recycle vertex IDs while the `RocksDb`
  cache has `VertexID` rather than `RootedVertexID` where the former
  type seems preferable.

  In some fringe cases one might remove a vertex with key `(root1,vid)`
  and insert another vertex with key `(root2,vid)` while re-using the
  vertex ID `vid`. Without knowledge of `root1` and `root2`, the LRU
  cache will return the same vertex for `(root2,vid)` also for
  `(root1,vid)`.
  • Loading branch information
mjfh committed Aug 12, 2024
1 parent 8723a79 commit ec6b095
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
3 changes: 1 addition & 2 deletions nimbus/db/aristo/aristo_delete.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import
eth/common,
results,
"."/[aristo_desc, aristo_fetch, aristo_get, aristo_hike, aristo_layers,
aristo_utils, aristo_vid]
aristo_utils]

# ------------------------------------------------------------------------------
# Private heplers
Expand Down Expand Up @@ -48,7 +48,6 @@ proc disposeOfVtx(
# Remove entry
db.layersResVtx(rvid)
db.layersResKey(rvid)
db.vidDispose rvid.vid # Recycle ID

# ------------------------------------------------------------------------------
# Private functions
Expand Down
14 changes: 14 additions & 0 deletions nimbus/db/aristo/aristo_init/rocks_db/rdb_desc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ type
vtxCol*: ColFamilyReadWrite ## Vertex column family handler
keyCol*: ColFamilyReadWrite ## Hash key column family handler
session*: WriteBatchRef ## For batched `put()`

# Note that the key type `VertexID` for LRU caches requires that there is
# strictly no vertex ID re-use.
#
# Otherwise, in some fringe cases one might remove a vertex with key
# `(root1,vid)` and insert another vertex with key `(root2,vid)` while
# re-using the vertex ID `vid`. Without knowledge of `root1` and `root2`,
# the LRU cache will return the same vertex for `(root2,vid)` also for
# `(root1,vid)`.
#
# The other alternaive would be to use the key type `RootedVertexID` which
# is less memory and time efficient (the latter one due to internal LRU
# handling of the longer key.)
#
rdKeyLru*: KeyedQueue[VertexID,HashKey] ## Read cache
rdVtxLru*: KeyedQueue[VertexID,VertexRef] ## Read cache

Expand Down
4 changes: 1 addition & 3 deletions nimbus/db/aristo/aristo_part/part_ctx.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import
std/sets,
eth/common,
results,
".."/[aristo_desc, aristo_get, aristo_hike, aristo_layers, aristo_utils,
aristo_vid],
".."/[aristo_desc, aristo_get, aristo_hike, aristo_layers, aristo_utils],
#./part_debug,
./part_desc

Expand Down Expand Up @@ -112,7 +111,6 @@ proc ctxAcceptChange(psc: PartStateCtx): Result[bool,AristoError] =
let key = ps.move(psc.fromVid, toVid)
doAssert key.isValid
ps.changed.incl key
db.vidDispose psc.fromVid # typically no action

# Remove parent vertex it it has become complete.
ps.removeCompletedNodes(psc.location)
Expand Down
6 changes: 0 additions & 6 deletions nimbus/db/aristo/aristo_vid.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
{.push raises: [].}

import
std/typetraits,
./aristo_desc

# ------------------------------------------------------------------------------
Expand All @@ -30,11 +29,6 @@ proc vidFetch*(db: AristoDbRef): VertexID =
db.top.vTop.inc
db.top.vTop

proc vidDispose*(db: AristoDbRef; vid: VertexID) =
## Only top vertexIDs are disposed
if vid == db.top.vTop and LEAST_FREE_VID < db.top.vTop.distinctBase:
db.top.vTop.dec

# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------

0 comments on commit ec6b095

Please sign in to comment.