2023-09-12 18:44:45 +00:00
|
|
|
# nimbus-eth1
|
2024-03-14 22:17:43 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-09-12 18:44:45 +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.
|
|
|
|
|
|
|
|
## Rocksdb backend for Kvt DB
|
|
|
|
## ==========================
|
|
|
|
##
|
|
|
|
## The iterators provided here are currently available only by direct
|
|
|
|
## backend access
|
|
|
|
## ::
|
|
|
|
## import
|
|
|
|
## kvt/kvt_init,
|
|
|
|
## kvt/kvt_init/kvt_rocksdb
|
|
|
|
##
|
|
|
|
## let rc = KvtDb.init(BackendRocksDB, "/var/tmp")
|
|
|
|
## if rc.isOk:
|
|
|
|
## let be = rc.value.to(RdbBackendRef)
|
|
|
|
## for (n, key, vtx) in be.walkVtx:
|
|
|
|
## ...
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-06-13 18:15:11 +00:00
|
|
|
chronicles,
|
2023-09-12 18:44:45 +00:00
|
|
|
rocksdb,
|
|
|
|
results,
|
2024-06-13 18:15:11 +00:00
|
|
|
../../aristo/aristo_init/persistent,
|
|
|
|
../../opts,
|
2023-09-12 18:44:45 +00:00
|
|
|
../kvt_desc,
|
|
|
|
../kvt_desc/desc_backend,
|
2024-06-13 18:15:11 +00:00
|
|
|
../kvt_tx/tx_stow,
|
2023-09-12 18:44:45 +00:00
|
|
|
./init_common,
|
|
|
|
./rocks_db/[rdb_desc, rdb_get, rdb_init, rdb_put, rdb_walk]
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
const
|
|
|
|
extraTraceMessages = false or true
|
|
|
|
## Enabled additional logging noise
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
RdbBackendRef* = ref object of TypedBackendRef
|
|
|
|
rdb: RdbInst ## Allows low level access to database
|
|
|
|
|
|
|
|
RdbPutHdlRef = ref object of TypedPutHdlRef
|
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
logScope:
|
|
|
|
topics = "kvt-backend"
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template logTxt(info: static[string]): static[string] =
|
|
|
|
"RocksDB " & info
|
|
|
|
|
|
|
|
|
|
|
|
proc newSession(db: RdbBackendRef): RdbPutHdlRef =
|
|
|
|
new result
|
|
|
|
result.TypedPutHdlRef.beginSession db
|
|
|
|
|
|
|
|
proc getSession(hdl: PutHdlRef; db: RdbBackendRef): RdbPutHdlRef =
|
|
|
|
hdl.TypedPutHdlRef.verifySession db
|
|
|
|
hdl.RdbPutHdlRef
|
|
|
|
|
|
|
|
proc endSession(hdl: PutHdlRef; db: RdbBackendRef): RdbPutHdlRef =
|
|
|
|
hdl.TypedPutHdlRef.finishSession db
|
|
|
|
hdl.RdbPutHdlRef
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2024-06-13 18:15:11 +00:00
|
|
|
# Private functions: standard interface
|
2023-09-12 18:44:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc getKvpFn(db: RdbBackendRef): GetKvpFn =
|
|
|
|
result =
|
2024-10-01 21:03:10 +00:00
|
|
|
proc(key: openArray[byte]): Result[seq[byte],KvtError] =
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
# Get data record
|
2024-05-24 09:27:17 +00:00
|
|
|
var data = db.rdb.get(key).valueOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "getKvpFn() failed", key, error=error[0], info=error[1]
|
|
|
|
return err(error[0])
|
|
|
|
|
|
|
|
# Return if non-empty
|
|
|
|
if 0 < data.len:
|
2024-05-24 09:27:17 +00:00
|
|
|
return ok(move(data))
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
err(GetNotFound)
|
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
proc lenKvpFn(db: RdbBackendRef): LenKvpFn =
|
|
|
|
result =
|
|
|
|
proc(key: openArray[byte]): Result[int,KvtError] =
|
|
|
|
|
|
|
|
# Get data record
|
|
|
|
var len = db.rdb.len(key).valueOr:
|
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "lenKvpFn() failed", key, error=error[0], info=error[1]
|
|
|
|
return err(error[0])
|
|
|
|
|
|
|
|
# Return if non-empty
|
|
|
|
if 0 < len:
|
|
|
|
return ok(len)
|
|
|
|
|
|
|
|
err(GetNotFound)
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
# -------------
|
|
|
|
|
|
|
|
proc putBegFn(db: RdbBackendRef): PutBegFn =
|
|
|
|
result =
|
2024-06-13 18:15:11 +00:00
|
|
|
proc(): Result[PutHdlRef,KvtError] =
|
2024-04-16 20:39:11 +00:00
|
|
|
db.rdb.begin()
|
2024-06-13 18:15:11 +00:00
|
|
|
ok db.newSession()
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
proc putKvpFn(db: RdbBackendRef): PutKvpFn =
|
|
|
|
result =
|
2024-07-26 05:32:01 +00:00
|
|
|
proc(hdl: PutHdlRef; k, v: openArray[byte]) =
|
2023-09-12 18:44:45 +00:00
|
|
|
let hdl = hdl.getSession db
|
|
|
|
if hdl.error == KvtError(0):
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
# Collect batch session arguments
|
2024-07-26 05:32:01 +00:00
|
|
|
db.rdb.put(k, v).isOkOr:
|
|
|
|
hdl.error = error[0]
|
|
|
|
hdl.info = error[1]
|
2024-04-16 20:39:11 +00:00
|
|
|
return
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
proc putEndFn(db: RdbBackendRef): PutEndFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef): Result[void,KvtError] =
|
|
|
|
let hdl = hdl.endSession db
|
|
|
|
if hdl.error != KvtError(0):
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "putEndFn: failed", error=hdl.error, info=hdl.info
|
2024-06-13 18:15:11 +00:00
|
|
|
db.rdb.rollback()
|
2023-09-12 18:44:45 +00:00
|
|
|
return err(hdl.error)
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
# Commit session
|
|
|
|
db.rdb.commit().isOkOr:
|
2023-09-12 18:44:45 +00:00
|
|
|
when extraTraceMessages:
|
2024-04-16 20:39:11 +00:00
|
|
|
trace logTxt "putEndFn: failed", error=($error[0]), info=error[1]
|
|
|
|
return err(error[0])
|
2023-09-12 18:44:45 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
|
|
|
|
proc closeFn(db: RdbBackendRef): CloseFn =
|
|
|
|
result =
|
2024-06-14 11:19:48 +00:00
|
|
|
proc(eradicate: bool) =
|
|
|
|
db.rdb.destroy(eradicate)
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
proc setWrReqFn(db: RdbBackendRef): SetWrReqFn =
|
|
|
|
result =
|
|
|
|
proc(kvt: RootRef): Result[void,KvtError] =
|
|
|
|
err(RdbBeHostNotApplicable)
|
2024-04-16 20:39:11 +00:00
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions: triggered interface changes
|
|
|
|
# ------------------------------------------------------------------------------
|
2024-04-16 20:39:11 +00:00
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
proc putBegTriggeredFn(db: RdbBackendRef): PutBegFn =
|
|
|
|
## Variant of `putBegFn()` for piggyback write batch
|
|
|
|
result =
|
|
|
|
proc(): Result[PutHdlRef,KvtError] =
|
|
|
|
# Check whether somebody else initiated the rocksdb write batch/session
|
|
|
|
if db.rdb.session.isNil:
|
|
|
|
const error = RdbBeDelayedNotReady
|
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "putBegTriggeredFn: failed", error
|
|
|
|
return err(error)
|
|
|
|
ok db.newSession()
|
|
|
|
|
|
|
|
proc putEndTriggeredFn(db: RdbBackendRef): PutEndFn =
|
|
|
|
## Variant of `putEndFn()` for piggyback write batch
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef): Result[void,KvtError] =
|
|
|
|
|
|
|
|
# There is no commit()/rollback() here as we do not own the backend.
|
|
|
|
let hdl = hdl.endSession db
|
|
|
|
|
|
|
|
if hdl.error != KvtError(0):
|
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "putEndTriggeredFn: failed",
|
|
|
|
error=hdl.error, info=hdl.info
|
2024-12-18 16:03:51 +00:00
|
|
|
# The error return code will signal a problem to the `txPersist()`
|
2024-06-13 18:15:11 +00:00
|
|
|
# function which was called by `writeEvCb()` below.
|
|
|
|
return err(hdl.error)
|
|
|
|
|
2024-12-18 16:03:51 +00:00
|
|
|
# Commit the session. This will be acknowledged by the `txPersist()`
|
2024-06-13 18:15:11 +00:00
|
|
|
# function which was called by `writeEvCb()` below.
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc closeTriggeredFn(db: RdbBackendRef): CloseFn =
|
|
|
|
## Variant of `closeFn()` for piggyback write batch
|
|
|
|
result =
|
2024-06-14 11:19:48 +00:00
|
|
|
proc(eradicate: bool) =
|
2024-06-13 18:15:11 +00:00
|
|
|
# Nothing to do here as we do not own the backend
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc setWrReqTriggeredFn(db: RdbBackendRef): SetWrReqFn =
|
|
|
|
result =
|
|
|
|
proc(kvt: RootRef): Result[void,KvtError] =
|
|
|
|
if db.rdb.delayedPersist.isNil:
|
|
|
|
db.rdb.delayedPersist = KvtDbRef(kvt)
|
|
|
|
ok()
|
|
|
|
else:
|
|
|
|
err(RdbBeDelayedAlreadyRegistered)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private function: trigger handler
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc writeEvCb(db: RdbBackendRef): RdbWriteEventCb =
|
|
|
|
## Write session event handler
|
|
|
|
result =
|
|
|
|
proc(ws: WriteBatchRef): bool =
|
|
|
|
|
|
|
|
# Only do something if a write session request was queued
|
|
|
|
if not db.rdb.delayedPersist.isNil:
|
|
|
|
defer:
|
|
|
|
# Clear session environment when leaving. This makes sure that the
|
|
|
|
# same session can only be run once.
|
|
|
|
db.rdb.session = WriteBatchRef(nil)
|
|
|
|
db.rdb.delayedPersist = KvtDbRef(nil)
|
|
|
|
|
|
|
|
# Publish session argument
|
|
|
|
db.rdb.session = ws
|
|
|
|
|
2024-12-18 16:03:51 +00:00
|
|
|
# Execute delayed session. Note the the `txPersist()` function is located
|
2024-06-13 18:15:11 +00:00
|
|
|
# in `tx_stow.nim`. This module `tx_stow.nim` is also imported by
|
|
|
|
# `kvt_tx.nim` which contains `persist() `. So the logic goes:
|
|
|
|
# ::
|
|
|
|
# kvt_tx.persist() --> registers a delayed write request rather
|
2024-12-18 16:03:51 +00:00
|
|
|
# than excuting tx_stow.txPersist()
|
2024-06-13 18:15:11 +00:00
|
|
|
#
|
|
|
|
# // the backend owner (i.e. Aristo) will start a write cycle and
|
|
|
|
# // invoke the envent handler rocks_db.writeEvCb()
|
2024-12-18 16:03:51 +00:00
|
|
|
# rocks_db.writeEvCb() --> calls tx_stow.txPersist()
|
2024-06-13 18:15:11 +00:00
|
|
|
#
|
2024-12-18 16:03:51 +00:00
|
|
|
# tx_stow.txPersist() --> calls rocks_db.putBegTriggeredFn()
|
2024-06-13 18:15:11 +00:00
|
|
|
# calls rocks_db.putKvpFn()
|
|
|
|
# calls rocks_db.putEndTriggeredFn()
|
|
|
|
#
|
2024-12-18 16:03:51 +00:00
|
|
|
let rc = db.rdb.delayedPersist.txPersist()
|
2024-06-13 18:15:11 +00:00
|
|
|
if rc.isErr:
|
|
|
|
error "writeEventCb(): persist() failed", error=rc.error
|
|
|
|
return false
|
|
|
|
true
|
2024-04-16 20:39:11 +00:00
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-03 20:10:35 +00:00
|
|
|
proc rocksDbKvtBackend*(
|
|
|
|
path: string;
|
2024-06-19 08:55:57 +00:00
|
|
|
dbOpts: DbOptionsRef;
|
|
|
|
cfOpts: ColFamilyOptionsRef;
|
2024-06-13 18:15:11 +00:00
|
|
|
): Result[BackendRef,(KvtError,string)] =
|
2023-09-12 18:44:45 +00:00
|
|
|
let db = RdbBackendRef(
|
|
|
|
beKind: BackendRocksDB)
|
|
|
|
|
|
|
|
# Initialise RocksDB
|
2024-06-19 08:55:57 +00:00
|
|
|
db.rdb.init(path, dbOpts, cfOpts).isOkOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "constructor failed", error=error[0], info=error[1]
|
2024-06-13 18:15:11 +00:00
|
|
|
return err(error)
|
|
|
|
|
|
|
|
db.getKvpFn = getKvpFn db
|
2024-06-21 07:44:10 +00:00
|
|
|
db.lenKvpFn = lenKvpFn db
|
2024-06-13 18:15:11 +00:00
|
|
|
|
|
|
|
db.putBegFn = putBegFn db
|
|
|
|
db.putKvpFn = putKvpFn db
|
|
|
|
db.putEndFn = putEndFn db
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
db.closeFn = closeFn db
|
|
|
|
db.setWrReqFn = setWrReqFn db
|
2024-04-16 20:39:11 +00:00
|
|
|
ok db
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
|
|
|
|
proc rocksDbKvtTriggeredBackend*(
|
|
|
|
adb: AristoDbRef;
|
2024-06-19 08:55:57 +00:00
|
|
|
oCfs: openArray[ColFamilyReadWrite];
|
2024-06-13 18:15:11 +00:00
|
|
|
): Result[BackendRef,(KvtError,string)] =
|
2024-04-16 20:39:11 +00:00
|
|
|
let db = RdbBackendRef(
|
2024-06-13 18:15:11 +00:00
|
|
|
beKind: BackendRdbTriggered)
|
|
|
|
|
|
|
|
# Initialise RocksDB piggy-backed on `Aristo` backend.
|
2024-06-19 08:55:57 +00:00
|
|
|
db.rdb.init(oCfs).isOkOr:
|
2024-06-13 18:15:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "constructor failed", error=error[0], info=error[1]
|
|
|
|
return err(error)
|
|
|
|
|
|
|
|
# Register write session event handler
|
|
|
|
adb.activateWrTrigger(db.writeEvCb()).isOkOr:
|
|
|
|
return err((RdbBeHostError,$error))
|
|
|
|
|
|
|
|
db.getKvpFn = getKvpFn db
|
2024-06-21 07:44:10 +00:00
|
|
|
db.lenKvpFn = lenKvpFn db
|
2024-06-13 18:15:11 +00:00
|
|
|
|
|
|
|
db.putBegFn = putBegTriggeredFn db
|
|
|
|
db.putKvpFn = putKvpFn db
|
|
|
|
db.putEndFn = putEndTriggeredFn db
|
|
|
|
|
|
|
|
db.closeFn = closeTriggeredFn db
|
|
|
|
db.setWrReqFn = setWrReqTriggeredFn db
|
2023-09-12 18:44:45 +00:00
|
|
|
ok db
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public iterators (needs direct backend access)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
iterator walk*(
|
|
|
|
be: RdbBackendRef;
|
2024-10-01 21:03:10 +00:00
|
|
|
): tuple[key: seq[byte], data: seq[byte]] =
|
2023-09-12 18:44:45 +00:00
|
|
|
## Walk over all key-value pairs of the database.
|
|
|
|
##
|
|
|
|
for (k,v) in be.rdb.walk:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (k,v)
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|