2023-12-19 12:39:23 +00:00
|
|
|
# nimbus-eth1
|
Core db update storage root management for sub tries (#1964)
* Aristo: Re-phrase `LayerDelta` and `LayerFinal` as object references
why:
Avoids copying in some cases
* Fix copyright header
* Aristo: Verify `leafTie.root` function argument for `merge()` proc
why:
Zero root will lead to inconsistent DB entry
* Aristo: Update failure condition for hash labels compiler `hashify()`
why:
Node need not be rejected as long as links are on the schedule. In
that case, `redo[]` is to become `wff.base[]` at a later stage.
This amends an earlier fix, part of #1952 by also testing against
the target nodes of the `wff.base[]` sets.
* Aristo: Add storage root glue record to `hashify()` schedule
why:
An account leaf node might refer to a non-resolvable storage root ID.
Storage root node chains will end up at the storage root. So the link
`storage-root->account-leaf` needs an extra item in the schedule.
* Aristo: fix error code returned by `fetchPayload()`
details:
Final error code is implied by the error code form the `hikeUp()`
function.
* CoreDb: Discard `createOk` argument in API `getRoot()` function
why:
Not needed for the legacy DB. For the `Arsto` DB, a lazy approach is
implemented where a stprage root node is created on-the-fly.
* CoreDb: Prevent `$$` logging in some cases
why:
Logging the function `$$` is not useful when it is used for internal
use, i.e. retrieving an an error text for logging.
* CoreDb: Add `tryHashFn()` to API for pretty printing
why:
Pretty printing must not change the hashification status for the
`Aristo` DB. So there is an independent API wrapper for getting the
node hash which never updated the hashes.
* CoreDb: Discard `update` argument in API `hash()` function
why:
When calling the API function `hash()`, the latest state is always
wanted. For a version that uses the current state as-is without checking,
the function `tryHash()` was added to the backend.
* CoreDb: Update opaque vertex ID objects for the `Aristo` backend
why:
For `Aristo`, vID objects encapsulate a numeric `VertexID`
referencing a vertex (rather than a node hash as used on the
legacy backend.) For storage sub-tries, there might be no initial
vertex known when the descriptor is created. So opaque vertex ID
objects are supported without a valid `VertexID` which will be
initalised on-the-fly when the first item is merged.
* CoreDb: Add pretty printer for opaque vertex ID objects
* Cosmetics, printing profiling data
* CoreDb: Fix segfault in `Aristo` backend when creating MPT descriptor
why:
Missing initialisation error
* CoreDb: Allow MPT to inherit shared context on `Aristo` backend
why:
Creates descriptors with different storage roots for the same
shared `Aristo` DB descriptor.
* Cosmetics, update diagnostic message items for `Aristo` backend
* Fix Copyright year
2024-01-11 19:11:38 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-12-19 12:39:23 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-07-18 07:13:56 +00:00
|
|
|
std/[enumerate, sequtils, sets, tables],
|
2023-12-19 12:39:23 +00:00
|
|
|
eth/common,
|
|
|
|
results,
|
|
|
|
./aristo_desc
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
func dup(sTab: Table[RootedVertexID,VertexRef]): Table[RootedVertexID,VertexRef] =
|
2023-12-19 12:39:23 +00:00
|
|
|
## Explicit dup for `VertexRef` values
|
|
|
|
for (k,v) in sTab.pairs:
|
|
|
|
result[k] = v.dup
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public getters: lazy value lookup for read only versions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-04 15:05:13 +00:00
|
|
|
func vTop*(db: AristoDbRef): VertexID =
|
2024-07-18 21:32:32 +00:00
|
|
|
db.top.vTop
|
2023-12-19 12:39:23 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public getters/helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func nLayersVtx*(db: AristoDbRef): int =
|
2024-02-22 08:24:58 +00:00
|
|
|
## Number of vertex ID/vertex entries on the cache layers. This is an upper
|
|
|
|
## bound for the number of effective vertex ID mappings held on the cache
|
|
|
|
## layers as there might be duplicate entries for the same vertex ID on
|
|
|
|
## different layers.
|
2024-02-14 19:11:59 +00:00
|
|
|
##
|
2024-07-18 21:32:32 +00:00
|
|
|
db.stack.mapIt(it.sTab.len).foldl(a + b, db.top.sTab.len)
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-02-14 19:11:59 +00:00
|
|
|
func nLayersKey*(db: AristoDbRef): int =
|
2024-02-22 08:24:58 +00:00
|
|
|
## Number of vertex ID/key entries on the cache layers. This is an upper
|
|
|
|
## bound for the number of effective vertex ID mappingss held on the cache
|
|
|
|
## layers as there might be duplicate entries for the same vertex ID on
|
2024-02-14 19:11:59 +00:00
|
|
|
## different layers.
|
|
|
|
##
|
2024-07-18 21:32:32 +00:00
|
|
|
db.stack.mapIt(it.kMap.len).foldl(a + b, db.top.kMap.len)
|
2023-12-19 12:39:23 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2024-02-22 08:24:58 +00:00
|
|
|
# Public functions: getter variants
|
2023-12-19 12:39:23 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-07-18 07:13:56 +00:00
|
|
|
func layersGetVtx*(db: AristoDbRef; rvid: RootedVertexID): Opt[(VertexRef, int)] =
|
2023-12-19 12:39:23 +00:00
|
|
|
## Find a vertex on the cache layers. An `ok()` result might contain a
|
|
|
|
## `nil` vertex if it is stored on the cache that way.
|
|
|
|
##
|
2024-07-18 21:32:32 +00:00
|
|
|
db.top.sTab.withValue(rvid, item):
|
2024-07-18 07:13:56 +00:00
|
|
|
return Opt.some((item[], 0))
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-07-18 07:13:56 +00:00
|
|
|
for i, w in enumerate(db.rstack):
|
2024-07-18 21:32:32 +00:00
|
|
|
w.sTab.withValue(rvid, item):
|
2024-07-18 07:13:56 +00:00
|
|
|
return Opt.some((item[], i + 1))
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-07-18 07:13:56 +00:00
|
|
|
Opt.none((VertexRef, int))
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-07-18 07:13:56 +00:00
|
|
|
func layersGetKey*(db: AristoDbRef; rvid: RootedVertexID): Opt[(HashKey, int)] =
|
2024-02-14 19:11:59 +00:00
|
|
|
## Find a hash key on the cache layers. An `ok()` result might contain a void
|
|
|
|
## hash key if it is stored on the cache that way.
|
2023-12-19 12:39:23 +00:00
|
|
|
##
|
2024-07-18 21:32:32 +00:00
|
|
|
db.top.kMap.withValue(rvid, item):
|
2024-07-18 07:13:56 +00:00
|
|
|
return Opt.some((item[], 0))
|
2023-12-19 12:39:23 +00:00
|
|
|
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
if rvid in db.top.sTab:
|
|
|
|
return Opt.some((VOID_HASH_KEY, 0))
|
|
|
|
|
2024-07-18 07:13:56 +00:00
|
|
|
for i, w in enumerate(db.rstack):
|
2024-07-18 21:32:32 +00:00
|
|
|
w.kMap.withValue(rvid, item):
|
2024-07-18 07:13:56 +00:00
|
|
|
return ok((item[], i + 1))
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
if rvid in w.sTab:
|
|
|
|
return Opt.some((VOID_HASH_KEY, i + 1))
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-07-18 07:13:56 +00:00
|
|
|
Opt.none((HashKey, int))
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
func layersGetKeyOrVoid*(db: AristoDbRef; rvid: RootedVertexID): HashKey =
|
2024-06-19 12:40:00 +00:00
|
|
|
## Simplified version of `layersGetKey()`
|
2024-07-18 07:13:56 +00:00
|
|
|
(db.layersGetKey(rvid).valueOr (VOID_HASH_KEY, 0))[0]
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-10-01 21:03:10 +00:00
|
|
|
func layersGetAccLeaf*(db: AristoDbRef; accPath: Hash32): Opt[VertexRef] =
|
2024-07-18 21:32:32 +00:00
|
|
|
db.top.accLeaves.withValue(accPath, item):
|
2024-07-03 15:58:25 +00:00
|
|
|
return Opt.some(item[])
|
|
|
|
|
|
|
|
for w in db.rstack:
|
2024-07-18 21:32:32 +00:00
|
|
|
w.accLeaves.withValue(accPath, item):
|
2024-07-03 15:58:25 +00:00
|
|
|
return Opt.some(item[])
|
|
|
|
|
2024-07-14 10:02:05 +00:00
|
|
|
Opt.none(VertexRef)
|
2024-07-12 13:08:26 +00:00
|
|
|
|
2024-10-01 21:03:10 +00:00
|
|
|
func layersGetStoLeaf*(db: AristoDbRef; mixPath: Hash32): Opt[VertexRef] =
|
2024-07-18 21:32:32 +00:00
|
|
|
db.top.stoLeaves.withValue(mixPath, item):
|
2024-07-14 17:12:10 +00:00
|
|
|
return Opt.some(item[])
|
|
|
|
|
|
|
|
for w in db.rstack:
|
2024-07-18 21:32:32 +00:00
|
|
|
w.stoLeaves.withValue(mixPath, item):
|
2024-07-14 17:12:10 +00:00
|
|
|
return Opt.some(item[])
|
|
|
|
|
|
|
|
Opt.none(VertexRef)
|
2024-07-03 15:58:25 +00:00
|
|
|
|
2023-12-19 12:39:23 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2024-02-22 08:24:58 +00:00
|
|
|
# Public functions: setter variants
|
2023-12-19 12:39:23 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-05-25 19:01:28 +00:00
|
|
|
func layersPutVtx*(
|
2024-02-22 08:24:58 +00:00
|
|
|
db: AristoDbRef;
|
2024-07-04 13:46:52 +00:00
|
|
|
rvid: RootedVertexID;
|
2024-02-22 08:24:58 +00:00
|
|
|
vtx: VertexRef;
|
|
|
|
) =
|
2023-12-19 12:39:23 +00:00
|
|
|
## Store a (potentally empty) vertex on the top layer
|
2024-07-18 21:32:32 +00:00
|
|
|
db.top.sTab[rvid] = vtx
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
db.top.kMap.del(rvid)
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-05-25 19:01:28 +00:00
|
|
|
func layersResVtx*(
|
2024-02-22 08:24:58 +00:00
|
|
|
db: AristoDbRef;
|
2024-07-04 13:46:52 +00:00
|
|
|
rvid: RootedVertexID;
|
2024-02-22 08:24:58 +00:00
|
|
|
) =
|
2023-12-19 12:39:23 +00:00
|
|
|
## Shortcut for `db.layersPutVtx(vid, VertexRef(nil))`. It is sort of the
|
|
|
|
## equivalent of a delete function.
|
2024-07-04 13:46:52 +00:00
|
|
|
db.layersPutVtx(rvid, VertexRef(nil))
|
2023-12-19 12:39:23 +00:00
|
|
|
|
|
|
|
|
2024-05-25 19:01:28 +00:00
|
|
|
func layersPutKey*(
|
2024-02-22 08:24:58 +00:00
|
|
|
db: AristoDbRef;
|
2024-07-04 13:46:52 +00:00
|
|
|
rvid: RootedVertexID;
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
vtx: VertexRef,
|
2024-02-22 08:24:58 +00:00
|
|
|
key: HashKey;
|
|
|
|
) =
|
2024-02-14 19:11:59 +00:00
|
|
|
## Store a (potentally void) hash key on the top layer
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
db.top.sTab[rvid] = vtx
|
2024-07-18 21:32:32 +00:00
|
|
|
db.top.kMap[rvid] = key
|
2024-02-22 08:24:58 +00:00
|
|
|
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
func layersResKey*(db: AristoDbRef; rvid: RootedVertexID, vtx: VertexRef) =
|
2024-02-14 19:11:59 +00:00
|
|
|
## Shortcut for `db.layersPutKey(vid, VOID_HASH_KEY)`. It is sort of the
|
2023-12-19 12:39:23 +00:00
|
|
|
## equivalent of a delete function.
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
db.layersPutVtx(rvid, vtx)
|
2024-07-04 13:46:52 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
func layersResKeys*(db: AristoDbRef; hike: Hike) =
|
|
|
|
## Reset all cached keys along the given hike
|
|
|
|
for i in 1..hike.legs.len:
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
db.layersResKey((hike.root, hike.legs[^i].wp.vid), hike.legs[^i].wp.vtx)
|
2024-07-04 13:46:52 +00:00
|
|
|
|
2024-10-01 21:03:10 +00:00
|
|
|
func layersPutAccLeaf*(db: AristoDbRef; accPath: Hash32; leafVtx: VertexRef) =
|
2024-07-18 21:32:32 +00:00
|
|
|
db.top.accLeaves[accPath] = leafVtx
|
2024-07-14 17:12:10 +00:00
|
|
|
|
2024-10-01 21:03:10 +00:00
|
|
|
func layersPutStoLeaf*(db: AristoDbRef; mixPath: Hash32; leafVtx: VertexRef) =
|
2024-07-18 21:32:32 +00:00
|
|
|
db.top.stoLeaves[mixPath] = leafVtx
|
2024-07-03 15:58:25 +00:00
|
|
|
|
2023-12-19 12:39:23 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
func isEmpty*(ly: LayerRef): bool =
|
|
|
|
## Returns `true` if the layer does not contain any changes, i.e. all the
|
|
|
|
## tables are empty. The field `txUid` is ignored, here.
|
|
|
|
ly.sTab.len == 0 and
|
|
|
|
ly.kMap.len == 0 and
|
|
|
|
ly.accLeaves.len == 0 and
|
|
|
|
ly.stoLeaves.len == 0
|
|
|
|
|
|
|
|
|
2024-05-25 19:01:28 +00:00
|
|
|
func layersMergeOnto*(src: LayerRef; trg: var LayerObj) =
|
2023-12-19 12:39:23 +00:00
|
|
|
## Merges the argument `src` into the argument `trg` and returns `trg`. For
|
|
|
|
## the result layer, the `txUid` value set to `0`.
|
2023-12-20 16:19:00 +00:00
|
|
|
##
|
2023-12-19 12:39:23 +00:00
|
|
|
trg.txUid = 0
|
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
for (vid,vtx) in src.sTab.pairs:
|
|
|
|
trg.sTab[vid] = vtx
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
trg.kMap.del vid
|
2024-07-18 21:32:32 +00:00
|
|
|
for (vid,key) in src.kMap.pairs:
|
|
|
|
trg.kMap[vid] = key
|
|
|
|
trg.vTop = src.vTop
|
|
|
|
for (accPath,leafVtx) in src.accLeaves.pairs:
|
|
|
|
trg.accLeaves[accPath] = leafVtx
|
|
|
|
for (mixPath,leafVtx) in src.stoLeaves.pairs:
|
|
|
|
trg.stoLeaves[mixPath] = leafVtx
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2023-12-20 16:19:00 +00:00
|
|
|
func layersCc*(db: AristoDbRef; level = high(int)): LayerRef =
|
2023-12-19 12:39:23 +00:00
|
|
|
## Provide a collapsed copy of layers up to a particular transaction level.
|
|
|
|
## If the `level` argument is too large, the maximum transaction level is
|
|
|
|
## returned. For the result layer, the `txUid` value set to `0`.
|
2023-12-20 16:19:00 +00:00
|
|
|
##
|
|
|
|
let layers = if db.stack.len <= level: db.stack & @[db.top]
|
|
|
|
else: db.stack[0 .. level]
|
|
|
|
|
|
|
|
# Set up initial layer (bottom layer)
|
|
|
|
result = LayerRef(
|
2024-07-18 21:32:32 +00:00
|
|
|
sTab: layers[0].sTab.dup, # explicit dup for ref values
|
|
|
|
kMap: layers[0].kMap,
|
|
|
|
vTop: layers[^1].vTop,
|
|
|
|
accLeaves: layers[0].accLeaves,
|
|
|
|
stoLeaves: layers[0].stoLeaves)
|
2023-12-20 16:19:00 +00:00
|
|
|
|
|
|
|
# Consecutively merge other layers on top
|
|
|
|
for n in 1 ..< layers.len:
|
2024-07-18 21:32:32 +00:00
|
|
|
for (vid,vtx) in layers[n].sTab.pairs:
|
|
|
|
result.sTab[vid] = vtx
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
result.kMap.del vid
|
2024-07-18 21:32:32 +00:00
|
|
|
for (vid,key) in layers[n].kMap.pairs:
|
|
|
|
result.kMap[vid] = key
|
|
|
|
for (accPath,vtx) in layers[n].accLeaves.pairs:
|
|
|
|
result.accLeaves[accPath] = vtx
|
|
|
|
for (mixPath,vtx) in layers[n].stoLeaves.pairs:
|
|
|
|
result.stoLeaves[mixPath] = vtx
|
2023-12-20 16:19:00 +00:00
|
|
|
|
2023-12-19 12:39:23 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public iterators
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
iterator layersWalkVtx*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
seen: var HashSet[VertexID];
|
2024-07-04 13:46:52 +00:00
|
|
|
): tuple[rvid: RootedVertexID, vtx: VertexRef] =
|
2023-12-19 12:39:23 +00:00
|
|
|
## Walk over all `(VertexID,VertexRef)` pairs on the cache layers. Note that
|
|
|
|
## entries are unsorted.
|
|
|
|
##
|
|
|
|
## The argument `seen` collects a set of all visited vertex IDs including
|
|
|
|
## the one with a zero vertex which are othewise skipped by the iterator.
|
|
|
|
## The `seen` argument must not be modified while the iterator is active.
|
|
|
|
##
|
2024-07-18 21:32:32 +00:00
|
|
|
for (rvid,vtx) in db.top.sTab.pairs:
|
2024-07-04 13:46:52 +00:00
|
|
|
yield (rvid,vtx)
|
|
|
|
seen.incl rvid.vid
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-05-23 15:37:51 +00:00
|
|
|
for w in db.rstack:
|
2024-07-18 21:32:32 +00:00
|
|
|
for (rvid,vtx) in w.sTab.pairs:
|
2024-07-04 13:46:52 +00:00
|
|
|
if rvid.vid notin seen:
|
|
|
|
yield (rvid,vtx)
|
|
|
|
seen.incl rvid.vid
|
2023-12-19 12:39:23 +00:00
|
|
|
|
|
|
|
iterator layersWalkVtx*(
|
|
|
|
db: AristoDbRef;
|
2024-07-04 13:46:52 +00:00
|
|
|
): tuple[rvid: RootedVertexID, vtx: VertexRef] =
|
2023-12-19 12:39:23 +00:00
|
|
|
## Variant of `layersWalkVtx()`.
|
|
|
|
var seen: HashSet[VertexID]
|
2024-07-04 13:46:52 +00:00
|
|
|
for (rvid,vtx) in db.layersWalkVtx seen:
|
|
|
|
yield (rvid,vtx)
|
2023-12-19 12:39:23 +00:00
|
|
|
|
|
|
|
|
2024-02-14 19:11:59 +00:00
|
|
|
iterator layersWalkKey*(
|
2023-12-19 12:39:23 +00:00
|
|
|
db: AristoDbRef;
|
2024-07-04 13:46:52 +00:00
|
|
|
): tuple[rvid: RootedVertexID, key: HashKey] =
|
2024-02-14 19:11:59 +00:00
|
|
|
## Walk over all `(VertexID,HashKey)` pairs on the cache layers. Note that
|
2023-12-19 12:39:23 +00:00
|
|
|
## entries are unsorted.
|
|
|
|
var seen: HashSet[VertexID]
|
2024-07-18 21:32:32 +00:00
|
|
|
for (rvid,key) in db.top.kMap.pairs:
|
2024-07-04 13:46:52 +00:00
|
|
|
yield (rvid,key)
|
|
|
|
seen.incl rvid.vid
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-05-23 15:37:51 +00:00
|
|
|
for w in db.rstack:
|
2024-07-18 21:32:32 +00:00
|
|
|
for (rvid,key) in w.kMap.pairs:
|
2024-07-04 13:46:52 +00:00
|
|
|
if rvid.vid notin seen:
|
|
|
|
yield (rvid,key)
|
|
|
|
seen.incl rvid.vid
|
2023-12-19 12:39:23 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|