mirror of
https://github.com/codex-storage/nim-codex.git
synced 2025-01-10 04:55:40 +00:00
570a1f7b67
## Problem When Availabilities are created, the amount of bytes in the Availability are reserved in the repo, so those bytes on disk cannot be written to otherwise. When a request for storage is received by a node, if a previously created Availability is matched, an attempt will be made to fill a slot in the request (more accurately, the request's slots are added to the SlotQueue, and eventually those slots will be processed). During download, bytes that were reserved for the Availability were released (as they were written to disk). To prevent more bytes from being released than were reserved in the Availability, the Availability was marked as used during the download, so that no other requests would match the Availability, and therefore no new downloads (and byte releases) would begin. The unfortunate downside to this, is that the number of Availabilities a node has determines the download concurrency capacity. If, for example, a node creates a single Availability that covers all available disk space the operator is willing to use, that single Availability would mean that only one download could occur at a time, meaning the node could potentially miss out on storage opportunities. ## Solution To alleviate the concurrency issue, each time a slot is processed, a Reservation is created, which takes size (aka reserved bytes) away from the Availability and stores them in the Reservation object. This can be done as many times as needed as long as there are enough bytes remaining in the Availability. Therefore, concurrent downloads are no longer limited by the number of Availabilities. Instead, they would more likely be limited to the SlotQueue's `maxWorkers`. From a database design perspective, an Availability has zero or more Reservations. Reservations are persisted in the RepoStore's metadata, along with Availabilities. The metadata store key path for Reservations is ` meta / sales / reservations / <availabilityId> / <reservationId>`, while Availabilities are stored one level up, eg `meta / sales / reservations / <availabilityId> `, allowing all Reservations for an Availability to be queried (this is not currently needed, but may be useful when work to restore Availability size is implemented, more on this later). ### Lifecycle When a reservation is created, its size is deducted from the Availability, and when a reservation is deleted, any remaining size (bytes not written to disk) is returned to the Availability. If the request finishes, is cancelled (expired), or an error occurs, the Reservation is deleted (and any undownloaded bytes returned to the Availability). In addition, when the Sales module starts, any Reservations that are not actively being used in a filled slot, are deleted. Having a Reservation persisted until after a storage request is completed, will allow for the originally set Availability size to be reclaimed once a request contract has been completed. This is a feature that is yet to be implemented, however the work in this PR is a step in the direction towards enabling this. ### Unknowns Reservation size is determined by the `StorageAsk.slotSize`. If during download, more bytes than `slotSize` are attempted to be downloaded than this, then the Reservation update will fail, and the state machine will move to a `SaleErrored` state, deleting the Reservation. This will likely prevent the slot from being filled. ### Notes Based on #514
112 lines
2.9 KiB
Nim
112 lines
2.9 KiB
Nim
import std/sugar
|
|
import pkg/questionable
|
|
import pkg/chronos
|
|
import pkg/chronicles
|
|
import pkg/upraises
|
|
import ./trackedfutures
|
|
import ./then
|
|
|
|
push: {.upraises:[].}
|
|
|
|
type
|
|
Machine* = ref object of RootObj
|
|
state: State
|
|
running: Future[void]
|
|
scheduled: AsyncQueue[Event]
|
|
started: bool
|
|
trackedFutures: TrackedFutures
|
|
State* = ref object of RootObj
|
|
Query*[T] = proc(state: State): T
|
|
Event* = proc(state: State): ?State {.gcsafe, upraises:[].}
|
|
|
|
logScope:
|
|
topics = "statemachine"
|
|
|
|
proc new*[T: Machine](_: type T): T =
|
|
T(trackedFutures: TrackedFutures.new())
|
|
|
|
method `$`*(state: State): string {.base.} =
|
|
raiseAssert "not implemented"
|
|
|
|
proc transition(_: type Event, previous, next: State): Event =
|
|
return proc (state: State): ?State =
|
|
if state == previous:
|
|
return some next
|
|
|
|
proc query*[T](machine: Machine, query: Query[T]): ?T =
|
|
if machine.state == nil:
|
|
none T
|
|
else:
|
|
some query(machine.state)
|
|
|
|
proc schedule*(machine: Machine, event: Event) =
|
|
if not machine.started:
|
|
return
|
|
|
|
try:
|
|
machine.scheduled.putNoWait(event)
|
|
except AsyncQueueFullError:
|
|
raiseAssert "unlimited queue is full?!"
|
|
|
|
method run*(state: State, machine: Machine): Future[?State] {.base, async.} =
|
|
discard
|
|
|
|
method onError*(state: State, error: ref CatchableError): ?State {.base.} =
|
|
raise (ref Defect)(msg: "error in state machine: " & error.msg, parent: error)
|
|
|
|
proc onError(machine: Machine, error: ref CatchableError): Event =
|
|
return proc (state: State): ?State =
|
|
state.onError(error)
|
|
|
|
proc run(machine: Machine, state: State) {.async.} =
|
|
try:
|
|
if next =? await state.run(machine):
|
|
machine.schedule(Event.transition(state, next))
|
|
except CancelledError:
|
|
discard
|
|
|
|
proc scheduler(machine: Machine) {.async.} =
|
|
var running: Future[void]
|
|
try:
|
|
while machine.started:
|
|
let event = await machine.scheduled.get().track(machine)
|
|
if next =? event(machine.state):
|
|
if not running.isNil and not running.finished:
|
|
await running.cancelAndWait()
|
|
machine.state = next
|
|
debug "enter state", state = machine.state
|
|
running = machine.run(machine.state)
|
|
running
|
|
.track(machine)
|
|
.catch((err: ref CatchableError) =>
|
|
machine.schedule(machine.onError(err))
|
|
)
|
|
except CancelledError:
|
|
discard
|
|
|
|
proc start*(machine: Machine, initialState: State) =
|
|
if machine.started:
|
|
return
|
|
|
|
if machine.scheduled.isNil:
|
|
machine.scheduled = newAsyncQueue[Event]()
|
|
|
|
machine.started = true
|
|
machine.scheduler()
|
|
.track(machine)
|
|
.catch((err: ref CatchableError) =>
|
|
error("Error in scheduler", error = err.msg)
|
|
)
|
|
machine.schedule(Event.transition(machine.state, initialState))
|
|
|
|
proc stop*(machine: Machine) {.async.} =
|
|
if not machine.started:
|
|
return
|
|
|
|
trace "stopping state machine"
|
|
|
|
machine.started = false
|
|
await machine.trackedFutures.cancelTracked()
|
|
|
|
machine.state = nil
|