Skip to content

Commit

Permalink
Simplify aristo tree deletion functionality (#2563)
Browse files Browse the repository at this point in the history
* Cleaning up, removing cruft and debugging statements

* Make `aristo_delta` fluffy compatible

why:
  A sub-module that uses `chronicles` must import all possible
  modules used by a parent module that imports the sub-module.

* update TODO
  • Loading branch information
mjfh committed Aug 14, 2024
1 parent d148de5 commit cbe5131
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 510 deletions.
9 changes: 1 addition & 8 deletions fluffy/fluffy.nim.cfg
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
#
# For some reason `json[dynamic]` causes problems with subsequent modules from
# `Aristo` when compiling `fluffy`. There might be a `chronicles` inport missing
# but it is not obvious where. -- jordan
#
#-d:"chronicles_sinks=textlines[dynamic],json[dynamic]"

-d:"chronicles_sinks=textlines[dynamic]"
-d:"chronicles_sinks=textlines[dynamic],json[dynamic]"
-d:"chronicles_runtime_filtering=on"
-d:"chronicles_disable_thread_id"

Expand Down
3 changes: 1 addition & 2 deletions fluffy/tools/beacon_lc_bridge/nim.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Use only `secp256k1` public key cryptography as an identity in LibP2P.
-d:"libp2p_pki_schemes=secp256k1"

# See `fluffy.nim.cfg`
#-d:"chronicles_sinks=textlines[dynamic],json[dynamic]"
-d:"chronicles_sinks=textlines[dynamic],json[dynamic]"
-d:"chronicles_sinks=textlines[dynamic]"
3 changes: 1 addition & 2 deletions fluffy/tools/portal_bridge/nim.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# See `fluffy.nim.cfg`
#-d:"chronicles_sinks=textlines[dynamic],json[dynamic]"
-d:"chronicles_sinks=textlines[dynamic],json[dynamic]"
-d:"chronicles_sinks=textlines[dynamic]"
7 changes: 7 additions & 0 deletions nimbus/db/aristo/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@
function mentioned above.

* `aristo_nearby` also qualifies for a re-write, now

* Revisit tree deletion. The idea is to finally use ranges of nodes by
exploiting the root ID prefix of a `RootedVertexID`. The `RocksDb` backend
seems to support this kind of operation, see
https://rocksdb.org/blog/2018/11/21/delete-range.html. For the application
part there are some great ideas floating which need to be followed up
some time.
9 changes: 0 additions & 9 deletions nimbus/db/aristo/aristo_constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ const
## LRU cache size for accounts that have storage, see `.accLeaves` and
## `.stoLeaves` fields of the main descriptor.

DELETE_SUBTREE_VERTICES_MAX* = 25
## Maximum number of vertices for a tree to be deleted instantly. If the
## tree is larger, only the sub-tree root will be deleted immediately and
## subsequent entries will be deleted not until the cache layers are saved
## to the backend.
##
## Set to zero to disable in which case all sub-trees are deleted
## immediately.

static:
# must stay away from `VertexID(1)` and `VertexID(2)`
doAssert 2 < LEAST_FREE_VID
Expand Down
127 changes: 0 additions & 127 deletions nimbus/db/aristo/aristo_delete/delete_debug.nim

This file was deleted.

108 changes: 102 additions & 6 deletions nimbus/db/aristo/aristo_delete/delete_subtree.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,109 @@
# at your option. This file may not be copied, modified, or distributed
# except according to those terms.

import ../aristo_constants
{.push raises: [].}

when DELETE_SUBTREE_VERTICES_MAX == 0:
import ./delete_subtree_now as del_sub
else:
import ./delete_subtree_lazy as del_sub
import
eth/common,
results,
".."/[aristo_desc, aristo_get, aristo_layers],
./delete_helpers

export del_sub
# ------------------------------------------------------------------------------
# Private heplers
# ------------------------------------------------------------------------------

proc collectStoTreeLazily(
db: AristoDbRef; # Database, top layer
rvid: RootedVertexID; # Root vertex
accPath: Hash256; # Accounts cache designator
stoPath: NibblesBuf; # Current storage path
): Result[void,AristoError] =
## Collect vertex/vid and delete cache entries.
let (vtx, _) = db.getVtxRc(rvid).valueOr:
if error == GetVtxNotFound:
return ok()
return err(error)

case vtx.vType
of Branch:
for i in 0..15:
if vtx.bVid[i].isValid:
? db.collectStoTreeLazily(
(rvid.root, vtx.bVid[i]), accPath,
stoPath & vtx.ePfx & NibblesBuf.nibble(byte i))

of Leaf:
let stoPath = Hash256(data: (stoPath & vtx.lPfx).getBytes())
db.layersPutStoLeaf(AccountKey.mixUp(accPath, stoPath), nil)

# There is no useful approach avoiding to walk the whole tree for updating
# the storage data access cache.
#
# The alternative of stopping here and clearing the whole cache did degrade
# performance significantly in some tests on mainnet when importing `era1`.
#
# The cache it was seen
# * filled up to maximum size most of the time
# * at the same time having no `stoPath` hit at all (so there was nothing
# to be cleared.)
#
ok()


proc disposeOfSubTree(
db: AristoDbRef; # Database, top layer
rvid: RootedVertexID; # Root vertex
) =
## Evaluate results from `collectSubTreeLazyImpl()` or ftom
## `collectStoTreeLazyImpl)`.
##
let vtx = db.getVtxRc(rvid).value[0]
if vtx.vType == Branch:
for n in 0..15:
if vtx.bVid[n].isValid:
db.top.delTree.add (rvid.root,vtx.bVid[n])

# Delete top of tree now.
db.disposeOfVtx(rvid)

# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------

proc delSubTreeImpl*(
db: AristoDbRef; # Database, top layer
root: VertexID; # Root vertex
): Result[void,AristoError] =
## Delete all the `subRoots`if there are a few, only. Otherwise
## mark it for deleting later.
discard db.getVtxRc((root,root)).valueOr:
if error == GetVtxNotFound:
return ok()
return err(error)

db.disposeOfSubTree((root,root))

ok()


proc delStoTreeImpl*(
db: AristoDbRef; # Database, top layer
rvid: RootedVertexID; # Root vertex
accPath: Hash256;
): Result[void,AristoError] =
## Collect vertex/vid and cache entry.
discard db.getVtxRc(rvid).valueOr:
if error == GetVtxNotFound:
return ok()
return err(error)

? db.collectStoTreeLazily(rvid, accPath, NibblesBuf())

db.disposeOfSubTree(rvid)

ok()

# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------
Loading

0 comments on commit cbe5131

Please sign in to comment.