2022-05-19 14:56:03 -05:00
|
|
|
## Nim-Codex
|
2021-02-25 18:23:22 -06:00
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-02-25 18:23:22 -06:00
|
|
|
import std/tables
|
2023-08-22 08:35:16 +02:00
|
|
|
import std/monotimes
|
2025-02-24 15:01:23 -06:00
|
|
|
import std/strutils
|
2022-05-12 14:23:05 -06:00
|
|
|
|
2021-02-25 18:23:22 -06:00
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
2023-07-20 09:56:28 +02:00
|
|
|
import pkg/metrics
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
import ../protobuf/blockexc
|
2022-05-19 16:28:53 -06:00
|
|
|
import ../../blocktype
|
feat: create logging proxy (#663)
* implement a logging proxy
The logging proxy:
- prevents the need to import chronicles (as well as export except toJson),
- prevents the need to override `writeValue` or use or import nim-json-seralization elsewhere in the codebase, allowing for sole use of utils/json for de/serialization,
- and handles json formatting correctly in chronicles json sinks
* Rename logging -> logutils to avoid ambiguity with common names
* clean up
* add setProperty for JsonRecord, remove nim-json-serialization conflict
* Allow specifying textlines and json format separately
Not specifying a LogFormat will apply the formatting to both textlines and json sinks.
Specifying a LogFormat will apply the formatting to only that sink.
* remove unneeded usages of std/json
We only need to import utils/json instead of std/json
* move serialization from rest/json to utils/json so it can be shared
* fix NoColors ambiguity
Was causing unit tests to fail on Windows.
* Remove nre usage to fix Windows error
Windows was erroring with `could not load: pcre64.dll`. Instead of fixing that error, remove the pcre usage :)
* Add logutils module doc
* Shorten logutils.formatIt for `NBytes`
Both json and textlines formatIt were not needed, and could be combined into one formatIt
* remove debug integration test config
debug output and logformat of json for integration test logs
* Use ## module doc to support docgen
* bump nim-poseidon2 to export fromBytes
Before the changes in this branch, fromBytes was likely being resolved by nim-stew, or other dependency. With the changes in this branch, that dependency was removed and fromBytes could no longer be resolved. By exporting fromBytes from nim-poseidon, the correct resolution is now happening.
* fixes to get compiling after rebasing master
* Add support for Result types being logged using formatIt
2024-01-23 18:35:03 +11:00
|
|
|
import ../../logutils
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
logScope:
|
2022-11-15 09:46:21 -06:00
|
|
|
topics = "codex pendingblocks"
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2023-11-03 17:21:54 +02:00
|
|
|
declareGauge(
|
|
|
|
codex_block_exchange_pending_block_requests,
|
|
|
|
"codex blockexchange pending block requests",
|
|
|
|
)
|
|
|
|
declareGauge(
|
|
|
|
codex_block_exchange_retrieval_time_us, "codex blockexchange block retrieval time us"
|
2025-01-21 21:54:46 +01:00
|
|
|
)
|
2023-07-20 09:56:28 +02:00
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
const
|
|
|
|
DefaultBlockRetries* = 3000
|
|
|
|
DefaultRetryInterval* = 500.millis
|
2022-05-12 14:09:40 -06:00
|
|
|
|
2021-02-25 18:23:22 -06:00
|
|
|
type
|
2025-02-24 15:01:23 -06:00
|
|
|
RetriesExhaustedError* = object of CatchableError
|
|
|
|
BlockHandle* = Future[Block].Raising([CancelledError, RetriesExhaustedError])
|
|
|
|
|
2022-11-15 00:12:05 -06:00
|
|
|
BlockReq* = object
|
2025-02-24 15:01:23 -06:00
|
|
|
handle*: BlockHandle
|
2022-11-15 00:12:05 -06:00
|
|
|
inFlight*: bool
|
2025-02-24 15:01:23 -06:00
|
|
|
blockRetries*: int
|
2023-08-22 08:35:16 +02:00
|
|
|
startTime*: int64
|
2022-11-15 00:12:05 -06:00
|
|
|
|
2021-02-25 18:23:22 -06:00
|
|
|
PendingBlocksManager* = ref object of RootObj
|
2025-02-24 15:01:23 -06:00
|
|
|
blockRetries*: int = DefaultBlockRetries
|
|
|
|
retryInterval*: Duration = DefaultRetryInterval
|
2023-11-14 13:02:17 +01:00
|
|
|
blocks*: Table[BlockAddress, BlockReq] # pending Block requests
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2023-07-20 09:56:28 +02:00
|
|
|
proc updatePendingBlockGauge(p: PendingBlocksManager) =
|
2023-11-03 17:21:54 +02:00
|
|
|
codex_block_exchange_pending_block_requests.set(p.blocks.len.int64)
|
2023-07-20 09:56:28 +02:00
|
|
|
|
2022-05-12 14:09:40 -06:00
|
|
|
proc getWantHandle*(
|
2025-02-24 15:01:23 -06:00
|
|
|
self: PendingBlocksManager, address: BlockAddress, inFlight = false
|
|
|
|
): Future[Block] {.async: (raw: true, raises: [CancelledError, RetriesExhaustedError]).} =
|
2021-02-25 18:23:22 -06:00
|
|
|
## Add an event for a block
|
|
|
|
##
|
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
self.blocks.withValue(address, blk):
|
|
|
|
return blk[].handle
|
|
|
|
do:
|
|
|
|
let blk = BlockReq(
|
|
|
|
handle: newFuture[Block]("pendingBlocks.getWantHandle"),
|
|
|
|
inFlight: inFlight,
|
|
|
|
blockRetries: self.blockRetries,
|
|
|
|
startTime: getMonoTime().ticks,
|
|
|
|
)
|
|
|
|
self.blocks[address] = blk
|
|
|
|
let handle = blk.handle
|
|
|
|
|
|
|
|
proc cleanUpBlock(data: pointer) {.raises: [].} =
|
|
|
|
self.blocks.del(address)
|
|
|
|
self.updatePendingBlockGauge()
|
|
|
|
|
|
|
|
handle.addCallback(cleanUpBlock)
|
|
|
|
handle.cancelCallback = proc(data: pointer) {.raises: [].} =
|
|
|
|
if not handle.finished:
|
|
|
|
handle.removeCallback(cleanUpBlock)
|
|
|
|
cleanUpBlock(nil)
|
|
|
|
|
|
|
|
self.updatePendingBlockGauge()
|
|
|
|
return handle
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
proc getWantHandle*(
|
2025-02-24 15:01:23 -06:00
|
|
|
self: PendingBlocksManager, cid: Cid, inFlight = false
|
|
|
|
): Future[Block] {.async: (raw: true, raises: [CancelledError, RetriesExhaustedError]).} =
|
|
|
|
self.getWantHandle(BlockAddress.init(cid), inFlight)
|
2023-11-14 13:02:17 +01:00
|
|
|
|
|
|
|
proc resolve*(
|
2025-02-24 15:01:23 -06:00
|
|
|
self: PendingBlocksManager, blocksDelivery: seq[BlockDelivery]
|
2024-03-12 13:10:14 +01:00
|
|
|
) {.gcsafe, raises: [].} =
|
2021-02-25 18:23:22 -06:00
|
|
|
## Resolve pending blocks
|
|
|
|
##
|
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
for bd in blocksDelivery:
|
2025-02-24 15:01:23 -06:00
|
|
|
self.blocks.withValue(bd.address, blockReq):
|
|
|
|
if not blockReq[].handle.finished:
|
|
|
|
trace "Resolving pending block", address = bd.address
|
2023-08-22 08:35:16 +02:00
|
|
|
let
|
2025-02-24 15:01:23 -06:00
|
|
|
startTime = blockReq[].startTime
|
2023-08-22 08:35:16 +02:00
|
|
|
stopTime = getMonoTime().ticks
|
|
|
|
retrievalDurationUs = (stopTime - startTime) div 1000
|
2023-11-14 13:02:17 +01:00
|
|
|
|
|
|
|
blockReq.handle.complete(bd.blk)
|
2023-11-14 11:52:27 -06:00
|
|
|
|
2023-11-03 17:21:54 +02:00
|
|
|
codex_block_exchange_retrieval_time_us.set(retrievalDurationUs)
|
2024-05-16 19:06:12 +02:00
|
|
|
|
|
|
|
if retrievalDurationUs > 500000:
|
|
|
|
warn "High block retrieval time", retrievalDurationUs, address = bd.address
|
2023-11-14 13:02:17 +01:00
|
|
|
else:
|
|
|
|
trace "Block handle already finished", address = bd.address
|
2022-11-15 00:12:05 -06:00
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
func retries*(self: PendingBlocksManager, address: BlockAddress): int =
|
|
|
|
self.blocks.withValue(address, pending):
|
|
|
|
result = pending[].blockRetries
|
|
|
|
do:
|
|
|
|
result = 0
|
|
|
|
|
|
|
|
func decRetries*(self: PendingBlocksManager, address: BlockAddress) =
|
|
|
|
self.blocks.withValue(address, pending):
|
|
|
|
pending[].blockRetries -= 1
|
|
|
|
|
|
|
|
func retriesExhausted*(self: PendingBlocksManager, address: BlockAddress): bool =
|
|
|
|
self.blocks.withValue(address, pending):
|
|
|
|
result = pending[].blockRetries <= 0
|
|
|
|
|
|
|
|
func setInFlight*(self: PendingBlocksManager, address: BlockAddress, inFlight = true) =
|
2024-03-12 13:10:14 +01:00
|
|
|
## Set inflight status for a block
|
|
|
|
##
|
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
self.blocks.withValue(address, pending):
|
2022-11-15 09:46:21 -06:00
|
|
|
pending[].inFlight = inFlight
|
2022-11-15 00:12:05 -06:00
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
func isInFlight*(self: PendingBlocksManager, address: BlockAddress): bool =
|
2024-03-12 13:10:14 +01:00
|
|
|
## Check if a block is in flight
|
|
|
|
##
|
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
self.blocks.withValue(address, pending):
|
2022-11-15 00:12:05 -06:00
|
|
|
result = pending[].inFlight
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
func contains*(self: PendingBlocksManager, cid: Cid): bool =
|
|
|
|
BlockAddress.init(cid) in self.blocks
|
2023-11-14 13:02:17 +01:00
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
func contains*(self: PendingBlocksManager, address: BlockAddress): bool =
|
|
|
|
address in self.blocks
|
2023-11-14 13:02:17 +01:00
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
iterator wantList*(self: PendingBlocksManager): BlockAddress =
|
|
|
|
for a in self.blocks.keys:
|
2023-11-14 13:02:17 +01:00
|
|
|
yield a
|
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
iterator wantListBlockCids*(self: PendingBlocksManager): Cid =
|
|
|
|
for a in self.blocks.keys:
|
2023-11-14 13:02:17 +01:00
|
|
|
if not a.leaf:
|
|
|
|
yield a.cid
|
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
iterator wantListCids*(self: PendingBlocksManager): Cid =
|
2023-11-14 13:02:17 +01:00
|
|
|
var yieldedCids = initHashSet[Cid]()
|
2025-02-24 15:01:23 -06:00
|
|
|
for a in self.blocks.keys:
|
2023-11-14 13:02:17 +01:00
|
|
|
let cid = a.cidOrTreeCid
|
|
|
|
if cid notin yieldedCids:
|
|
|
|
yieldedCids.incl(cid)
|
|
|
|
yield cid
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
iterator wantHandles*(self: PendingBlocksManager): Future[Block] =
|
|
|
|
for v in self.blocks.values:
|
2022-11-15 00:12:05 -06:00
|
|
|
yield v.handle
|
2022-05-12 14:09:40 -06:00
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
proc wantListLen*(self: PendingBlocksManager): int =
|
|
|
|
self.blocks.len
|
2023-11-14 13:02:17 +01:00
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
func len*(self: PendingBlocksManager): int =
|
|
|
|
self.blocks.len
|
2022-05-12 14:09:40 -06:00
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
func new*(
|
|
|
|
T: type PendingBlocksManager,
|
|
|
|
retries = DefaultBlockRetries,
|
|
|
|
interval = DefaultRetryInterval,
|
|
|
|
): PendingBlocksManager =
|
|
|
|
PendingBlocksManager(blockRetries: retries, retryInterval: interval)
|