2023-11-21 15:16:15 +00:00
|
|
|
# Fluffy
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-09-28 17:58:41 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2021-09-28 17:58:41 +00:00
|
|
|
|
|
|
|
import
|
2022-11-08 17:31:45 +00:00
|
|
|
chronicles,
|
|
|
|
metrics,
|
2021-09-28 17:58:41 +00:00
|
|
|
stint,
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
2023-11-21 15:16:15 +00:00
|
|
|
eth/db/kvstore,
|
|
|
|
eth/db/kvstore_sqlite3,
|
2023-12-01 16:20:52 +00:00
|
|
|
../network/state/state_content,
|
|
|
|
../network/wire/[portal_protocol, portal_protocol_config],
|
2023-11-21 15:16:15 +00:00
|
|
|
./content_db_custom_sql_functions
|
2021-09-28 17:58:41 +00:00
|
|
|
|
|
|
|
export kvstore_sqlite3
|
|
|
|
|
|
|
|
# This version of content db is the most basic, simple solution where data is
|
|
|
|
# stored no matter what content type or content network in the same kvstore with
|
|
|
|
# the content id as key. The content id is derived from the content key, and the
|
|
|
|
# deriviation is different depending on the content type. As we use content id,
|
|
|
|
# this part is currently out of the scope / API of the ContentDB.
|
|
|
|
# In the future it is likely that that either:
|
|
|
|
# 1. More kvstores are added per network, and thus depending on the network a
|
|
|
|
# different kvstore needs to be selected.
|
|
|
|
# 2. Or more kvstores are added per network and per content type, and thus
|
|
|
|
# content key fields are required to access the data.
|
|
|
|
# 3. Or databases are created per network (and kvstores pre content type) and
|
|
|
|
# thus depending on the network the right db needs to be selected.
|
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
declareCounter portal_pruning_counter,
|
2023-11-16 14:27:30 +00:00
|
|
|
"Number of pruning events which occured during the node's uptime",
|
2022-11-08 17:31:45 +00:00
|
|
|
labels = ["protocol_id"]
|
|
|
|
|
|
|
|
declareGauge portal_pruning_deleted_elements,
|
2024-02-28 17:31:45 +00:00
|
|
|
"Number of elements deleted in the last pruning", labels = ["protocol_id"]
|
2022-11-08 17:31:45 +00:00
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
const
|
|
|
|
contentDeletionFraction = 0.05 ## 5% of the content will be deleted when the
|
|
|
|
## storage capacity is hit and radius gets adjusted.
|
|
|
|
|
2021-09-28 17:58:41 +00:00
|
|
|
type
|
2024-02-28 17:31:45 +00:00
|
|
|
RowInfo =
|
|
|
|
tuple[contentId: array[32, byte], payloadLength: int64, distance: array[32, byte]]
|
2022-04-03 13:14:44 +00:00
|
|
|
|
2021-09-28 17:58:41 +00:00
|
|
|
ContentDB* = ref object
|
2023-11-21 15:16:15 +00:00
|
|
|
backend: SqStoreRef
|
2021-09-28 17:58:41 +00:00
|
|
|
kv: KvStoreRef
|
2023-11-21 15:16:15 +00:00
|
|
|
manualCheckpoint: bool
|
2023-11-10 16:16:15 +00:00
|
|
|
storageCapacity*: uint64
|
2022-03-08 13:49:41 +00:00
|
|
|
sizeStmt: SqliteStmt[NoParams, int64]
|
2022-05-26 06:26:08 +00:00
|
|
|
unusedSizeStmt: SqliteStmt[NoParams, int64]
|
2023-11-07 18:46:26 +00:00
|
|
|
vacuumStmt: SqliteStmt[NoParams, void]
|
|
|
|
contentCountStmt: SqliteStmt[NoParams, int64]
|
2022-06-03 11:44:42 +00:00
|
|
|
contentSizeStmt: SqliteStmt[NoParams, int64]
|
|
|
|
getAllOrderedByDistanceStmt: SqliteStmt[array[32, byte], RowInfo]
|
2023-11-21 15:16:15 +00:00
|
|
|
deleteOutOfRadiusStmt: SqliteStmt[(array[32, byte], array[32, byte]), void]
|
2023-11-23 10:44:57 +00:00
|
|
|
largestDistanceStmt: SqliteStmt[array[32, byte], array[32, byte]]
|
2022-04-03 13:14:44 +00:00
|
|
|
|
2022-05-09 15:18:57 +00:00
|
|
|
PutResultType* = enum
|
2024-02-28 17:31:45 +00:00
|
|
|
ContentStored
|
|
|
|
DbPruned
|
2022-05-09 15:18:57 +00:00
|
|
|
|
|
|
|
PutResult* = object
|
|
|
|
case kind*: PutResultType
|
|
|
|
of ContentStored:
|
|
|
|
discard
|
|
|
|
of DbPruned:
|
2023-11-16 14:27:30 +00:00
|
|
|
distanceOfFurthestElement*: UInt256
|
|
|
|
deletedFraction*: float64
|
|
|
|
deletedElements*: int64
|
2022-05-09 15:18:57 +00:00
|
|
|
|
2021-09-28 17:58:41 +00:00
|
|
|
template expectDb(x: auto): untyped =
|
|
|
|
# There's no meaningful error handling implemented for a corrupt database or
|
|
|
|
# full disk - this requires manual intervention, so we'll panic for now
|
|
|
|
x.expect("working database (disk broken/full?)")
|
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
proc new*(
|
2024-02-28 17:31:45 +00:00
|
|
|
T: type ContentDB,
|
|
|
|
path: string,
|
|
|
|
storageCapacity: uint64,
|
|
|
|
inMemory = false,
|
|
|
|
manualCheckpoint = false,
|
|
|
|
): ContentDB =
|
2023-11-10 16:16:15 +00:00
|
|
|
doAssert(storageCapacity <= uint64(int64.high))
|
|
|
|
|
2021-09-28 17:58:41 +00:00
|
|
|
let db =
|
|
|
|
if inMemory:
|
|
|
|
SqStoreRef.init("", "fluffy-test", inMemory = true).expect(
|
2024-02-28 17:31:45 +00:00
|
|
|
"working database (out of memory?)"
|
|
|
|
)
|
2021-09-28 17:58:41 +00:00
|
|
|
else:
|
2023-11-21 15:16:15 +00:00
|
|
|
SqStoreRef.init(path, "fluffy", manualCheckpoint = false).expectDb()
|
|
|
|
|
|
|
|
db.createCustomFunction("xorDistance", 2, xorDistance).expect(
|
2024-02-28 17:31:45 +00:00
|
|
|
"Custom function xorDistance creation OK"
|
|
|
|
)
|
2021-09-28 17:58:41 +00:00
|
|
|
|
2023-11-21 15:16:15 +00:00
|
|
|
db.createCustomFunction("isInRadius", 3, isInRadius).expect(
|
2024-02-28 17:31:45 +00:00
|
|
|
"Custom function isInRadius creation OK"
|
|
|
|
)
|
2022-06-03 11:44:42 +00:00
|
|
|
|
2023-11-07 18:46:26 +00:00
|
|
|
let sizeStmt = db.prepareStmt(
|
2022-03-08 13:49:41 +00:00
|
|
|
"SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();",
|
2024-02-28 17:31:45 +00:00
|
|
|
NoParams, int64,
|
|
|
|
)[]
|
2022-03-08 13:49:41 +00:00
|
|
|
|
2023-11-07 18:46:26 +00:00
|
|
|
let unusedSizeStmt = db.prepareStmt(
|
2022-05-26 06:26:08 +00:00
|
|
|
"SELECT freelist_count * page_size as size FROM pragma_freelist_count(), pragma_page_size();",
|
2024-02-28 17:31:45 +00:00
|
|
|
NoParams, int64,
|
|
|
|
)[]
|
2022-05-26 06:26:08 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let vacuumStmt = db.prepareStmt("VACUUM;", NoParams, void)[]
|
2022-03-08 13:49:41 +00:00
|
|
|
|
2022-04-03 13:14:44 +00:00
|
|
|
let kvStore = kvStore db.openKvStore().expectDb()
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let contentSizeStmt =
|
|
|
|
db.prepareStmt("SELECT SUM(length(value)) FROM kvstore", NoParams, int64)[]
|
2023-11-07 18:46:26 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let contentCountStmt =
|
|
|
|
db.prepareStmt("SELECT COUNT(key) FROM kvstore;", NoParams, int64)[]
|
2022-06-03 11:44:42 +00:00
|
|
|
|
|
|
|
let getAllOrderedByDistanceStmt = db.prepareStmt(
|
|
|
|
"SELECT key, length(value), xorDistance(?, key) as distance FROM kvstore ORDER BY distance DESC",
|
2024-02-28 17:31:45 +00:00
|
|
|
array[32, byte],
|
|
|
|
RowInfo,
|
|
|
|
)[]
|
2022-04-03 13:14:44 +00:00
|
|
|
|
2023-11-21 15:16:15 +00:00
|
|
|
let deleteOutOfRadiusStmt = db.prepareStmt(
|
|
|
|
"DELETE FROM kvstore WHERE isInRadius(?, key, ?) == 0",
|
2024-02-28 17:31:45 +00:00
|
|
|
(array[32, byte], array[32, byte]),
|
|
|
|
void,
|
|
|
|
)[]
|
2023-11-21 15:16:15 +00:00
|
|
|
|
2023-11-23 10:44:57 +00:00
|
|
|
let largestDistanceStmt = db.prepareStmt(
|
2024-02-28 17:31:45 +00:00
|
|
|
"SELECT max(xorDistance(?, key)) FROM kvstore", array[32, byte], array[32, byte]
|
|
|
|
)[]
|
2023-11-23 10:44:57 +00:00
|
|
|
|
2022-04-13 05:56:01 +00:00
|
|
|
ContentDB(
|
2022-05-26 06:26:08 +00:00
|
|
|
kv: kvStore,
|
2023-11-21 15:16:15 +00:00
|
|
|
backend: db,
|
|
|
|
manualCheckpoint: manualCheckpoint,
|
2023-11-10 16:16:15 +00:00
|
|
|
storageCapacity: storageCapacity,
|
2023-11-07 18:46:26 +00:00
|
|
|
sizeStmt: sizeStmt,
|
|
|
|
unusedSizeStmt: unusedSizeStmt,
|
|
|
|
vacuumStmt: vacuumStmt,
|
2022-06-03 11:44:42 +00:00
|
|
|
contentSizeStmt: contentSizeStmt,
|
2023-11-07 18:46:26 +00:00
|
|
|
contentCountStmt: contentCountStmt,
|
2023-11-21 15:16:15 +00:00
|
|
|
getAllOrderedByDistanceStmt: getAllOrderedByDistanceStmt,
|
2023-11-23 10:44:57 +00:00
|
|
|
deleteOutOfRadiusStmt: deleteOutOfRadiusStmt,
|
2024-02-28 17:31:45 +00:00
|
|
|
largestDistanceStmt: largestDistanceStmt,
|
2022-05-26 06:26:08 +00:00
|
|
|
)
|
2022-04-03 13:14:44 +00:00
|
|
|
|
2023-11-21 15:16:15 +00:00
|
|
|
template disposeSafe(s: untyped): untyped =
|
|
|
|
if distinctBase(s) != nil:
|
|
|
|
s.dispose()
|
|
|
|
s = typeof(s)(nil)
|
|
|
|
|
|
|
|
proc close*(db: ContentDB) =
|
|
|
|
db.sizeStmt.disposeSafe()
|
|
|
|
db.unusedSizeStmt.disposeSafe()
|
|
|
|
db.vacuumStmt.disposeSafe()
|
|
|
|
db.contentCountStmt.disposeSafe()
|
|
|
|
db.contentSizeStmt.disposeSafe()
|
|
|
|
db.getAllOrderedByDistanceStmt.disposeSafe()
|
|
|
|
db.deleteOutOfRadiusStmt.disposeSafe()
|
2023-11-23 10:44:57 +00:00
|
|
|
db.largestDistanceStmt.disposeSafe()
|
2023-11-21 15:16:15 +00:00
|
|
|
discard db.kv.close()
|
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
## Private KvStoreRef Calls
|
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
proc get(kv: KvStoreRef, key: openArray[byte]): Opt[seq[byte]] =
|
|
|
|
var res: Opt[seq[byte]]
|
2024-02-28 17:31:45 +00:00
|
|
|
proc onData(data: openArray[byte]) =
|
|
|
|
res = Opt.some(@data)
|
2022-08-01 19:00:21 +00:00
|
|
|
|
|
|
|
discard kv.get(key, onData).expectDb()
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
proc getSszDecoded(kv: KvStoreRef, key: openArray[byte], T: type auto): Opt[T] =
|
2022-08-01 19:00:21 +00:00
|
|
|
let res = kv.get(key)
|
|
|
|
if res.isSome():
|
|
|
|
try:
|
2022-12-09 16:59:36 +00:00
|
|
|
Opt.some(SSZ.decode(res.get(), T))
|
2023-10-17 12:19:50 +00:00
|
|
|
except SerializationError:
|
2022-08-01 19:00:21 +00:00
|
|
|
raiseAssert("Stored data should always be serialized correctly")
|
|
|
|
else:
|
2022-12-09 16:59:36 +00:00
|
|
|
Opt.none(T)
|
2022-08-01 19:00:21 +00:00
|
|
|
|
|
|
|
## Private ContentDB calls
|
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
proc get(db: ContentDB, key: openArray[byte]): Opt[seq[byte]] =
|
2022-08-01 19:00:21 +00:00
|
|
|
db.kv.get(key)
|
|
|
|
|
|
|
|
proc put(db: ContentDB, key, value: openArray[byte]) =
|
|
|
|
db.kv.put(key, value).expectDb()
|
|
|
|
|
|
|
|
proc contains(db: ContentDB, key: openArray[byte]): bool =
|
|
|
|
db.kv.contains(key).expectDb()
|
|
|
|
|
|
|
|
proc del(db: ContentDB, key: openArray[byte]) =
|
2023-01-27 14:57:48 +00:00
|
|
|
# TODO: Do we want to return the bool here too?
|
|
|
|
discard db.kv.del(key).expectDb()
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc getSszDecoded(db: ContentDB, key: openArray[byte], T: type auto): Opt[T] =
|
2022-08-01 19:00:21 +00:00
|
|
|
db.kv.getSszDecoded(key, T)
|
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
## Public ContentId based ContentDB calls
|
|
|
|
|
|
|
|
# TODO: Could also decide to use the ContentKey SSZ bytestring, as this is what
|
|
|
|
# gets send over the network in requests, but that would be a bigger key. Or the
|
|
|
|
# same hashing could be done on it here.
|
|
|
|
# However ContentId itself is already derived through different digests
|
|
|
|
# depending on the content type, and this ContentId typically needs to be
|
|
|
|
# checked with the Radius/distance of the node anyhow. So lets see how we end up
|
|
|
|
# using this mostly in the code.
|
|
|
|
|
|
|
|
proc get*(db: ContentDB, key: ContentId): Opt[seq[byte]] =
|
|
|
|
# TODO: Here it is unfortunate that ContentId is a uint256 instead of Digest256.
|
|
|
|
db.get(key.toBytesBE())
|
|
|
|
|
|
|
|
proc put*(db: ContentDB, key: ContentId, value: openArray[byte]) =
|
|
|
|
db.put(key.toBytesBE(), value)
|
|
|
|
|
|
|
|
proc contains*(db: ContentDB, key: ContentId): bool =
|
|
|
|
db.contains(key.toBytesBE())
|
|
|
|
|
|
|
|
proc del*(db: ContentDB, key: ContentId) =
|
|
|
|
db.del(key.toBytesBE())
|
|
|
|
|
|
|
|
proc getSszDecoded*(db: ContentDB, key: ContentId, T: type auto): Opt[T] =
|
|
|
|
db.getSszDecoded(key.toBytesBE(), T)
|
|
|
|
|
2023-11-21 15:16:15 +00:00
|
|
|
## Public calls to get database size, content size and similar.
|
2022-03-08 13:49:41 +00:00
|
|
|
|
|
|
|
proc size*(db: ContentDB): int64 =
|
2023-11-10 16:16:15 +00:00
|
|
|
## Return current size of DB as product of sqlite page_count and page_size:
|
2022-03-08 13:49:41 +00:00
|
|
|
## https://www.sqlite.org/pragma.html#pragma_page_count
|
|
|
|
## https://www.sqlite.org/pragma.html#pragma_page_size
|
2023-11-10 16:16:15 +00:00
|
|
|
## It returns the total size of db on the disk, i.e both data and metadata
|
|
|
|
## used to store content.
|
|
|
|
## It is worth noting that when deleting content, the size may lag behind due
|
2022-03-08 13:49:41 +00:00
|
|
|
## to the way how deleting works in sqlite.
|
|
|
|
## Good description can be found in: https://www.sqlite.org/lang_vacuum.html
|
|
|
|
var size: int64 = 0
|
2024-02-28 17:31:45 +00:00
|
|
|
discard (
|
|
|
|
db.sizeStmt.exec do(res: int64):
|
2024-07-22 12:22:45 +00:00
|
|
|
size = res).expectDb()
|
2022-03-08 13:49:41 +00:00
|
|
|
return size
|
2021-09-28 17:58:41 +00:00
|
|
|
|
2022-05-26 06:26:08 +00:00
|
|
|
proc unusedSize(db: ContentDB): int64 =
|
|
|
|
## Returns the total size of the pages which are unused by the database,
|
|
|
|
## i.e they can be re-used for new content.
|
|
|
|
var size: int64 = 0
|
2024-02-28 17:31:45 +00:00
|
|
|
discard (
|
|
|
|
db.unusedSizeStmt.exec do(res: int64):
|
2024-07-22 12:22:45 +00:00
|
|
|
size = res).expectDb()
|
2022-05-26 06:26:08 +00:00
|
|
|
return size
|
|
|
|
|
2023-11-07 18:46:26 +00:00
|
|
|
proc usedSize*(db: ContentDB): int64 =
|
2023-11-10 16:16:15 +00:00
|
|
|
## Returns the total size of the database (data + metadata) minus the unused
|
|
|
|
## pages.
|
2022-05-26 06:26:08 +00:00
|
|
|
db.size() - db.unusedSize()
|
|
|
|
|
2023-03-31 16:52:21 +00:00
|
|
|
proc contentSize*(db: ContentDB): int64 =
|
2023-11-10 16:16:15 +00:00
|
|
|
## Returns total size of the content stored in DB.
|
2022-06-03 11:44:42 +00:00
|
|
|
var size: int64 = 0
|
2024-02-28 17:31:45 +00:00
|
|
|
discard (
|
|
|
|
db.contentSizeStmt.exec do(res: int64):
|
2024-07-22 12:22:45 +00:00
|
|
|
size = res).expectDb()
|
2022-06-03 11:44:42 +00:00
|
|
|
return size
|
|
|
|
|
2023-11-07 18:46:26 +00:00
|
|
|
proc contentCount*(db: ContentDB): int64 =
|
|
|
|
var count: int64 = 0
|
2024-02-28 17:31:45 +00:00
|
|
|
discard (
|
|
|
|
db.contentCountStmt.exec do(res: int64):
|
2024-07-22 12:22:45 +00:00
|
|
|
count = res).expectDb()
|
2023-11-07 18:46:26 +00:00
|
|
|
return count
|
|
|
|
|
2023-11-21 15:16:15 +00:00
|
|
|
## Pruning related calls
|
|
|
|
|
2023-11-23 10:44:57 +00:00
|
|
|
proc getLargestDistance*(db: ContentDB, localId: UInt256): UInt256 =
|
|
|
|
var distanceBytes: array[32, byte]
|
2024-02-28 17:31:45 +00:00
|
|
|
discard (
|
|
|
|
db.largestDistanceStmt.exec(
|
|
|
|
localId.toBytesBE(),
|
2023-11-23 10:44:57 +00:00
|
|
|
proc(res: array[32, byte]) =
|
2024-07-22 12:22:45 +00:00
|
|
|
distanceBytes = res,
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
|
|
|
).expectDb()
|
2023-11-23 10:44:57 +00:00
|
|
|
|
|
|
|
return UInt256.fromBytesBE(distanceBytes)
|
|
|
|
|
|
|
|
func estimateNewRadius(
|
2024-02-28 17:31:45 +00:00
|
|
|
currentSize: uint64, storageCapacity: uint64, currentRadius: UInt256
|
|
|
|
): UInt256 =
|
2023-11-23 10:44:57 +00:00
|
|
|
let sizeRatio = currentSize div storageCapacity
|
|
|
|
if sizeRatio > 0:
|
|
|
|
currentRadius div sizeRatio.stuint(256)
|
|
|
|
else:
|
|
|
|
currentRadius
|
|
|
|
|
|
|
|
func estimateNewRadius*(db: ContentDB, currentRadius: UInt256): UInt256 =
|
|
|
|
estimateNewRadius(uint64(db.usedSize()), db.storageCapacity, currentRadius)
|
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
proc deleteContentFraction*(
|
2024-02-28 17:31:45 +00:00
|
|
|
db: ContentDB, target: UInt256, fraction: float64
|
|
|
|
): (UInt256, int64, int64, int64) =
|
2023-11-21 15:16:15 +00:00
|
|
|
## Deletes at most `fraction` percent of content from the database.
|
|
|
|
## The content furthest from the provided `target` is deleted first.
|
2023-11-16 14:27:30 +00:00
|
|
|
# TODO: The usage of `db.contentSize()` for the deletion calculation versus
|
|
|
|
# `db.usedSize()` for the pruning threshold leads sometimes to some unexpected
|
|
|
|
# results of how much content gets up deleted.
|
2024-02-28 17:31:45 +00:00
|
|
|
doAssert(fraction > 0 and fraction < 1, "Deleted fraction should be > 0 and < 1")
|
2022-05-09 15:18:57 +00:00
|
|
|
|
2022-06-03 11:44:42 +00:00
|
|
|
let totalContentSize = db.contentSize()
|
|
|
|
let bytesToDelete = int64(fraction * float64(totalContentSize))
|
2023-11-16 14:27:30 +00:00
|
|
|
var deletedElements: int64 = 0
|
2022-05-24 11:27:22 +00:00
|
|
|
|
2022-06-03 11:44:42 +00:00
|
|
|
var ri: RowInfo
|
2023-11-16 14:27:30 +00:00
|
|
|
var deletedBytes: int64 = 0
|
2023-09-13 02:32:38 +00:00
|
|
|
let targetBytes = target.toBytesBE()
|
2022-06-03 11:44:42 +00:00
|
|
|
for e in db.getAllOrderedByDistanceStmt.exec(targetBytes, ri):
|
2023-11-16 14:27:30 +00:00
|
|
|
if deletedBytes + ri.payloadLength <= bytesToDelete:
|
2022-06-03 11:44:42 +00:00
|
|
|
db.del(ri.contentId)
|
2023-11-16 14:27:30 +00:00
|
|
|
deletedBytes = deletedBytes + ri.payloadLength
|
|
|
|
inc deletedElements
|
2022-05-09 15:18:57 +00:00
|
|
|
else:
|
2022-06-03 11:44:42 +00:00
|
|
|
return (
|
|
|
|
UInt256.fromBytesBE(ri.distance),
|
2023-11-16 14:27:30 +00:00
|
|
|
deletedBytes,
|
2022-08-01 19:00:21 +00:00
|
|
|
totalContentSize,
|
2024-02-28 17:31:45 +00:00
|
|
|
deletedElements,
|
2022-06-03 11:44:42 +00:00
|
|
|
)
|
2022-05-09 15:18:57 +00:00
|
|
|
|
2023-11-21 15:16:15 +00:00
|
|
|
proc reclaimSpace*(db: ContentDB): void =
|
|
|
|
## Runs sqlite VACUUM commands which rebuilds the db, repacking it into a
|
|
|
|
## minimal amount of disk space.
|
|
|
|
## Ideal mode of operation is to run it after several deletes.
|
|
|
|
## Another option would be to run 'PRAGMA auto_vacuum = FULL;' statement at
|
|
|
|
## the start of db to leave it up to sqlite to clean up.
|
|
|
|
db.vacuumStmt.exec().expectDb()
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc deleteContentOutOfRadius*(db: ContentDB, localId: UInt256, radius: UInt256) =
|
2023-11-21 15:16:15 +00:00
|
|
|
## Deletes all content that falls outside of the given radius range.
|
2024-02-28 17:31:45 +00:00
|
|
|
|
|
|
|
db.deleteOutOfRadiusStmt.exec((localId.toBytesBE(), radius.toBytesBE())).expect(
|
|
|
|
"SQL query OK"
|
|
|
|
)
|
2023-11-21 15:16:15 +00:00
|
|
|
|
2024-04-02 18:50:05 +00:00
|
|
|
proc reclaimAndTruncate*(db: ContentDB) =
|
|
|
|
notice "Reclaiming unused pages"
|
|
|
|
db.reclaimSpace()
|
|
|
|
if db.manualCheckpoint:
|
|
|
|
notice "Truncating WAL file"
|
|
|
|
db.backend.checkpoint(SqStoreCheckpointKind.truncate)
|
|
|
|
|
2023-11-21 15:16:15 +00:00
|
|
|
proc forcePrune*(db: ContentDB, localId: UInt256, radius: UInt256) =
|
|
|
|
## Force prune the database to a statically set radius. This will also run
|
|
|
|
## the reclaimSpace (vacuum) to free unused pages. As side effect this will
|
|
|
|
## cause the pruned database size to double in size on disk (wal file will be
|
|
|
|
## approximately the same size as the db). A truncate checkpoint is done to
|
|
|
|
## clean that up. In order to be able do the truncate checkpoint, the db needs
|
|
|
|
## to be initialized in with `manualCheckpoint` on, else this step will be
|
|
|
|
## skipped.
|
|
|
|
notice "Starting the pruning of content"
|
|
|
|
db.deleteContentOutOfRadius(localId, radius)
|
2024-04-02 18:50:05 +00:00
|
|
|
db.reclaimAndTruncate()
|
2023-11-21 15:16:15 +00:00
|
|
|
notice "Finished database pruning"
|
|
|
|
|
2022-05-09 15:18:57 +00:00
|
|
|
proc put*(
|
2024-02-28 17:31:45 +00:00
|
|
|
db: ContentDB, key: ContentId, value: openArray[byte], target: UInt256
|
|
|
|
): PutResult =
|
2022-05-09 15:18:57 +00:00
|
|
|
db.put(key, value)
|
2022-08-01 19:00:21 +00:00
|
|
|
|
2023-11-10 16:16:15 +00:00
|
|
|
# The used size is used as pruning threshold. This means that the database
|
|
|
|
# size will reach the size specified in db.storageCapacity and will stay
|
|
|
|
# around that size throughout the node's lifetime, as after content deletion
|
|
|
|
# due to pruning, the free pages will be re-used.
|
2023-11-23 10:44:57 +00:00
|
|
|
#
|
|
|
|
# Note:
|
|
|
|
# The `forcePrune` call must be used when database storage capacity is lowered
|
|
|
|
# either when setting a lower `storageCapacity` or when lowering a configured
|
|
|
|
# static radius.
|
|
|
|
# When not using the `forcePrune` functionality, pruning to the required
|
|
|
|
# capacity will not be very effictive and free pages will not be returned.
|
2023-11-07 18:46:26 +00:00
|
|
|
let dbSize = db.usedSize()
|
2022-05-24 11:27:22 +00:00
|
|
|
|
2023-11-10 16:16:15 +00:00
|
|
|
if dbSize < int64(db.storageCapacity):
|
2022-05-09 15:18:57 +00:00
|
|
|
return PutResult(kind: ContentStored)
|
|
|
|
else:
|
2023-11-23 10:44:57 +00:00
|
|
|
# Note:
|
|
|
|
# An approach of a deleting a full fraction is chosen here, in an attempt
|
|
|
|
# to not continiously require radius updates, which could have a negative
|
|
|
|
# impact on the network. However this should be further investigated, as
|
|
|
|
# doing a large fraction deletion could cause a temporary node performance
|
|
|
|
# degradation. The `contentDeletionFraction` might need further tuning or
|
|
|
|
# one could opt for a much more granular approach using sql statement
|
|
|
|
# in the trend of:
|
|
|
|
# "SELECT key FROM kvstore ORDER BY xorDistance(?, key) DESC LIMIT 1"
|
|
|
|
# Potential adjusting the LIMIT for how many items require deletion.
|
2024-02-28 17:31:45 +00:00
|
|
|
let (distanceOfFurthestElement, deletedBytes, totalContentSize, deletedElements) =
|
2023-11-16 14:27:30 +00:00
|
|
|
db.deleteContentFraction(target, contentDeletionFraction)
|
2022-05-09 15:18:57 +00:00
|
|
|
|
|
|
|
let deletedFraction = float64(deletedBytes) / float64(totalContentSize)
|
2023-11-16 14:27:30 +00:00
|
|
|
info "Deleted content fraction", deletedBytes, deletedElements, deletedFraction
|
2022-05-09 15:18:57 +00:00
|
|
|
|
|
|
|
return PutResult(
|
|
|
|
kind: DbPruned,
|
2023-11-16 14:27:30 +00:00
|
|
|
distanceOfFurthestElement: distanceOfFurthestElement,
|
|
|
|
deletedFraction: deletedFraction,
|
2024-02-28 17:31:45 +00:00
|
|
|
deletedElements: deletedElements,
|
|
|
|
)
|
2022-11-08 17:31:45 +00:00
|
|
|
|
|
|
|
proc adjustRadius(
|
2024-02-28 17:31:45 +00:00
|
|
|
p: PortalProtocol, deletedFraction: float64, distanceOfFurthestElement: UInt256
|
|
|
|
) =
|
2023-11-16 14:27:30 +00:00
|
|
|
# Invert fraction as the UInt256 implementation does not support
|
2022-11-08 17:31:45 +00:00
|
|
|
# multiplication by float
|
2023-11-16 14:27:30 +00:00
|
|
|
let invertedFractionAsInt = int64(1.0 / deletedFraction)
|
2022-11-08 17:31:45 +00:00
|
|
|
let scaledRadius = p.dataRadius div u256(invertedFractionAsInt)
|
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
# Choose a larger value to avoid the situation where the
|
|
|
|
# `distanceOfFurthestElement is very close to the local id so that the local
|
|
|
|
# radius would end up too small to accept any more data to the database.
|
|
|
|
# If scaledRadius radius will be larger it will still contain all elements.
|
|
|
|
let newRadius = max(scaledRadius, distanceOfFurthestElement)
|
2022-11-08 17:31:45 +00:00
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
info "Database radius adjusted",
|
2024-02-28 17:31:45 +00:00
|
|
|
oldRadius = p.dataRadius, newRadius = newRadius, distanceOfFurthestElement
|
2022-11-08 17:31:45 +00:00
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
# Both scaledRadius and distanceOfFurthestElement are smaller than current
|
|
|
|
# dataRadius, so the radius will constantly decrease through the node its
|
|
|
|
# lifetime.
|
2022-11-08 17:31:45 +00:00
|
|
|
p.dataRadius = newRadius
|
|
|
|
|
|
|
|
proc createGetHandler*(db: ContentDB): DbGetHandler =
|
|
|
|
return (
|
2024-07-17 15:07:27 +00:00
|
|
|
proc(contentKey: ContentKeyByteList, contentId: ContentId): Opt[seq[byte]] =
|
2022-12-09 16:59:36 +00:00
|
|
|
let content = db.get(contentId).valueOr:
|
2022-11-08 17:31:45 +00:00
|
|
|
return Opt.none(seq[byte])
|
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
ok(content)
|
2022-11-08 17:31:45 +00:00
|
|
|
)
|
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
proc createStoreHandler*(
|
2024-02-28 17:31:45 +00:00
|
|
|
db: ContentDB, cfg: RadiusConfig, p: PortalProtocol
|
|
|
|
): DbStoreHandler =
|
|
|
|
return (
|
|
|
|
proc(
|
2024-07-17 15:07:27 +00:00
|
|
|
contentKey: ContentKeyByteList, contentId: ContentId, content: seq[byte]
|
2024-02-28 17:31:45 +00:00
|
|
|
) {.raises: [], gcsafe.} =
|
|
|
|
# always re-check that the key is in the node range to make sure only
|
|
|
|
# content in range is stored.
|
|
|
|
# TODO: current silent assumption is that both ContentDB and PortalProtocol
|
|
|
|
# are using the same xor distance function
|
|
|
|
if p.inRange(contentId):
|
|
|
|
case cfg.kind
|
|
|
|
of Dynamic:
|
|
|
|
# In case of dynamic radius setting we obey storage limits and adjust
|
|
|
|
# radius to store network fraction corresponding to those storage limits.
|
|
|
|
let res = db.put(contentId, content, p.localNode.id)
|
|
|
|
if res.kind == DbPruned:
|
|
|
|
portal_pruning_counter.inc(labelValues = [$p.protocolId])
|
|
|
|
portal_pruning_deleted_elements.set(
|
|
|
|
res.deletedElements.int64, labelValues = [$p.protocolId]
|
|
|
|
)
|
|
|
|
|
|
|
|
if res.deletedFraction > 0.0:
|
|
|
|
p.adjustRadius(res.deletedFraction, res.distanceOfFurthestElement)
|
|
|
|
else:
|
|
|
|
# Note:
|
|
|
|
# This can occur when the furthest content is bigger than the fraction
|
|
|
|
# size. This is unlikely to happen as it would require either very
|
|
|
|
# small storage capacity or a very small `contentDeletionFraction`
|
|
|
|
# combined with some big content.
|
|
|
|
info "Database pruning attempt resulted in no content deleted"
|
|
|
|
return
|
|
|
|
of Static:
|
|
|
|
# If the config is set statically, radius is not adjusted, and is kept
|
|
|
|
# constant thorugh node life time, also database max size is disabled
|
|
|
|
# so we will effectivly store fraction of the network
|
|
|
|
db.put(contentId, content)
|
2022-11-08 17:31:45 +00:00
|
|
|
)
|