2023-03-08 15:04:54 +00:00
|
|
|
## Nim-Codex
|
|
|
|
## Copyright (c) 2023 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.
|
|
|
|
|
|
|
|
## Store maintenance module
|
|
|
|
## Looks for and removes expired blocks from blockstores.
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
|
|
import ./repostore
|
|
|
|
import ../utils/timer
|
2023-11-14 12:02:17 +00:00
|
|
|
import ../utils/asynciter
|
2023-03-08 15:04:54 +00:00
|
|
|
import ../clock
|
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 07:35:03 +00:00
|
|
|
import ../logutils
|
2023-03-08 15:04:54 +00:00
|
|
|
import ../systemclock
|
|
|
|
|
|
|
|
const
|
|
|
|
DefaultBlockMaintenanceInterval* = 10.minutes
|
|
|
|
DefaultNumberOfBlocksToMaintainPerInterval* = 1000
|
|
|
|
|
|
|
|
type
|
|
|
|
BlockMaintainer* = ref object of RootObj
|
|
|
|
repoStore: RepoStore
|
|
|
|
interval: Duration
|
|
|
|
timer: Timer
|
|
|
|
clock: Clock
|
|
|
|
numberOfBlocksPerInterval: int
|
|
|
|
offset: int
|
|
|
|
|
2023-06-22 15:11:18 +00:00
|
|
|
proc new*(
|
|
|
|
T: type BlockMaintainer,
|
2023-03-08 15:04:54 +00:00
|
|
|
repoStore: RepoStore,
|
|
|
|
interval: Duration,
|
|
|
|
numberOfBlocksPerInterval = 100,
|
|
|
|
timer = Timer.new(),
|
|
|
|
clock: Clock = SystemClock.new()
|
2023-06-22 15:11:18 +00:00
|
|
|
): BlockMaintainer =
|
|
|
|
## Create new BlockMaintainer instance
|
2023-09-04 09:12:14 +00:00
|
|
|
##
|
2023-06-22 15:11:18 +00:00
|
|
|
## Call `start` to begin looking for for expired blocks
|
2023-09-04 09:12:14 +00:00
|
|
|
##
|
2023-06-22 15:11:18 +00:00
|
|
|
BlockMaintainer(
|
2023-03-08 15:04:54 +00:00
|
|
|
repoStore: repoStore,
|
|
|
|
interval: interval,
|
|
|
|
numberOfBlocksPerInterval: numberOfBlocksPerInterval,
|
|
|
|
timer: timer,
|
|
|
|
clock: clock,
|
|
|
|
offset: 0)
|
|
|
|
|
|
|
|
proc deleteExpiredBlock(self: BlockMaintainer, cid: Cid): Future[void] {.async.} =
|
|
|
|
if isErr (await self.repoStore.delBlock(cid)):
|
|
|
|
trace "Unable to delete block from repoStore"
|
|
|
|
|
|
|
|
proc processBlockExpiration(self: BlockMaintainer, be: BlockExpiration): Future[void] {.async} =
|
2024-02-16 22:12:16 +00:00
|
|
|
if be.expiration < self.clock.now:
|
2023-03-08 15:04:54 +00:00
|
|
|
await self.deleteExpiredBlock(be.cid)
|
|
|
|
else:
|
|
|
|
inc self.offset
|
|
|
|
|
|
|
|
proc runBlockCheck(self: BlockMaintainer): Future[void] {.async.} =
|
|
|
|
let expirations = await self.repoStore.getBlockExpirations(
|
|
|
|
maxNumber = self.numberOfBlocksPerInterval,
|
|
|
|
offset = self.offset
|
|
|
|
)
|
|
|
|
|
|
|
|
without iter =? expirations, err:
|
|
|
|
trace "Unable to obtain blockExpirations iterator from repoStore"
|
|
|
|
return
|
|
|
|
|
|
|
|
var numberReceived = 0
|
|
|
|
for maybeBeFuture in iter:
|
|
|
|
if be =? await maybeBeFuture:
|
|
|
|
inc numberReceived
|
|
|
|
await self.processBlockExpiration(be)
|
2023-09-04 09:12:14 +00:00
|
|
|
await sleepAsync(50.millis)
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
# If we received fewer blockExpirations from the iterator than we asked for,
|
|
|
|
# We're at the end of the dataset and should start from 0 next time.
|
|
|
|
if numberReceived < self.numberOfBlocksPerInterval:
|
|
|
|
self.offset = 0
|
|
|
|
|
|
|
|
proc start*(self: BlockMaintainer) =
|
|
|
|
proc onTimer(): Future[void] {.async.} =
|
|
|
|
try:
|
|
|
|
await self.runBlockCheck()
|
2024-05-23 15:29:30 +00:00
|
|
|
except CancelledError as error:
|
|
|
|
raise error
|
2023-03-08 15:04:54 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
error "Unexpected exception in BlockMaintainer.onTimer(): ", msg=exc.msg
|
|
|
|
|
|
|
|
self.timer.start(onTimer, self.interval)
|
|
|
|
|
|
|
|
proc stop*(self: BlockMaintainer): Future[void] {.async.} =
|
|
|
|
await self.timer.stop()
|