-
Notifications
You must be signed in to change notification settings - Fork 251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BN, add timeout for sync workers which are waiting in queue. #5831
base: unstable
Are you sure you want to change the base?
Changes from 3 commits
bb3269e
662c365
642be87
423a8bc
727b3f6
f2b3c70
d02376a
7843d8f
0a4d3ac
99f657e
79dd6c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,7 @@ type | |
readyQueue: HeapQueue[SyncResult[T]] | ||
rewind: Option[RewindPoint] | ||
blockVerifier: BlockVerifier | ||
pendingWorkerBlockWaitTime: Duration | ||
ident*: string | ||
|
||
chronicles.formatIt SyncQueueKind: toLowerAscii($it) | ||
|
@@ -120,8 +121,9 @@ proc getShortMap*[T](req: SyncRequest[T], | |
res.add('|') | ||
continue | ||
if slot == data[cur].signed_block_header.message.slot: | ||
for k in cur..<cur+MAX_BLOBS_PER_BLOCK: | ||
if k >= lenu64(data) or slot != data[k].signed_block_header.message.slot: | ||
for k in cur ..< cur + MAX_BLOBS_PER_BLOCK: | ||
if k >= lenu64(data) or | ||
slot != data[k].signed_block_header.message.slot: | ||
res.add('|') | ||
break | ||
else: | ||
|
@@ -197,7 +199,8 @@ proc init*[T](t1: typedesc[SyncQueue], t2: typedesc[T], | |
getSafeSlotCb: GetSlotCallback, | ||
blockVerifier: BlockVerifier, | ||
syncQueueSize: int = -1, | ||
ident: string = "main"): SyncQueue[T] = | ||
ident: string = "main", | ||
pendingWorkerWaitTime = InfiniteDuration): SyncQueue[T] = | ||
## Create new synchronization queue with parameters | ||
## | ||
## ``start`` and ``final`` are starting and final Slots. | ||
|
@@ -267,6 +270,7 @@ proc init*[T](t1: typedesc[SyncQueue], t2: typedesc[T], | |
inpSlot: start, | ||
outSlot: start, | ||
blockVerifier: blockVerifier, | ||
pendingWorkerBlockWaitTime: pendingWorkerWaitTime, | ||
ident: ident | ||
) | ||
|
||
|
@@ -311,18 +315,22 @@ proc wakeupWaiters[T](sq: SyncQueue[T], reset = false) = | |
if not(item.future.finished()): | ||
item.future.complete() | ||
|
||
proc waitForChanges[T](sq: SyncQueue[T]): Future[bool] {.async: (raises: [CancelledError]).} = | ||
proc waitForChanges[T](sq: SyncQueue[T]): Future[bool] {. | ||
async: (raises: [CancelledError]).} = | ||
## Create new waiter and wait for completion from `wakeupWaiters()`. | ||
let waitfut = Future[void].Raising([CancelledError]).init("SyncQueue.waitForChanges") | ||
let waititem = SyncWaiter(future: waitfut) | ||
let | ||
waitfut = | ||
Future[void].Raising([CancelledError]).init("SyncQueue.waitForChanges") | ||
waititem = SyncWaiter(future: waitfut) | ||
sq.waiters.add(waititem) | ||
try: | ||
await waitfut | ||
return waititem.reset | ||
finally: | ||
sq.waiters.delete(sq.waiters.find(waititem)) | ||
|
||
proc wakeupAndWaitWaiters[T](sq: SyncQueue[T]) {.async: (raises: [CancelledError]).} = | ||
proc wakeupAndWaitWaiters[T](sq: SyncQueue[T]) {. | ||
async: (raises: [CancelledError]).} = | ||
## This procedure will perform wakeupWaiters(true) and blocks until last | ||
## waiter will be awakened. | ||
var waitChanges = sq.waitForChanges() | ||
|
@@ -333,7 +341,8 @@ proc clearAndWakeup*[T](sq: SyncQueue[T]) = | |
sq.pending.clear() | ||
sq.wakeupWaiters(true) | ||
|
||
proc resetWait*[T](sq: SyncQueue[T], toSlot: Option[Slot]) {.async: (raises: [CancelledError]).} = | ||
proc resetWait*[T](sq: SyncQueue[T], toSlot: Option[Slot]) {. | ||
async: (raises: [CancelledError]).} = | ||
## Perform reset of all the blocked waiters in SyncQueue. | ||
## | ||
## We adding one more waiter to the waiters sequence and | ||
|
@@ -546,8 +555,9 @@ func getOpt(blobs: Opt[seq[BlobSidecars]], i: int): Opt[BlobSidecars] = | |
else: | ||
Opt.none(BlobSidecars) | ||
|
||
iterator blocks[T](sq: SyncQueue[T], | ||
sr: SyncResult[T]): (ref ForkedSignedBeaconBlock, Opt[BlobSidecars]) = | ||
iterator blocks[T]( | ||
sq: SyncQueue[T], | ||
sr: SyncResult[T]): (ref ForkedSignedBeaconBlock, Opt[BlobSidecars]) = | ||
case sq.kind | ||
of SyncQueueKind.Forward: | ||
for i in countup(0, len(sr.data) - 1): | ||
|
@@ -577,6 +587,24 @@ proc notInRange[T](sq: SyncQueue[T], sr: SyncRequest[T]): bool = | |
of SyncQueueKind.Backward: | ||
(sq.queueSize > 0) and (sr.lastSlot < sq.outSlot) | ||
|
||
func chunksCount*[T](sq: SyncQueue[T], sr: SyncRequest[T]): uint64 = | ||
## Returns number of chunks between current queue position and request's | ||
## position. | ||
case sq.kind | ||
of SyncQueueKind.Forward: | ||
(sr.slot - sq.outSlot) div sq.chunkSize | ||
of SyncQueueKind.Backward: | ||
(sq.outSlot - sr.lastSlot) div sq.chunkSize | ||
|
||
func expectedTime[T](sq: SyncQueue[T], sr: SyncRequest[T]): Duration = | ||
## Returns expected wait time of specific pending request. | ||
## SR3 SR2 SR1 SR0 <CURRENT QUEUE POSITION> | ||
## SR3 expected pending time would be | ||
## <CHUNK SIZE> * <SR2 .. SR0 CHUNKS COUNT> * <BLOCK WAIT TIME> | ||
nanoseconds( | ||
int64(sq.chunkSize * sq.chunksCount(sr)) * | ||
sq.pendingWorkerBlockWaitTime.nanoseconds) | ||
Comment on lines
+605
to
+607
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the value if every single request takes the maximum time to complete a single time.
I wonder if a simpler mechanism with a static, e.g., 30sec timeout, could also mitigate the risk of getting stuck. It would take a bit longer to unstuck than the current solution, but is simpler to reason about. Alternatively, to get it fully correct may involve having to re-schedule the timeouts whenever a prior request completes. |
||
|
||
func numAlreadyKnownSlots[T](sq: SyncQueue[T], sr: SyncRequest[T]): uint64 = | ||
## Compute the number of slots covered by a given `SyncRequest` that are | ||
## already known and, hence, no longer relevant for sync progression. | ||
|
@@ -606,11 +634,14 @@ func numAlreadyKnownSlots[T](sq: SyncQueue[T], sr: SyncRequest[T]): uint64 = | |
# Entire request is still relevant. | ||
0 | ||
|
||
proc push*[T](sq: SyncQueue[T], sr: SyncRequest[T], | ||
data: seq[ref ForkedSignedBeaconBlock], | ||
blobs: Opt[seq[BlobSidecars]], | ||
maybeFinalized: bool = false, | ||
processingCb: ProcessingCallback = nil) {.async: (raises: [CancelledError]).} = | ||
proc push*[T]( | ||
sq: SyncQueue[T], | ||
sr: SyncRequest[T], | ||
data: seq[ref ForkedSignedBeaconBlock], | ||
blobs: Opt[seq[BlobSidecars]], | ||
maybeFinalized: bool = false, | ||
processingCb: ProcessingCallback = nil | ||
) {.async: (raises: [CancelledError]).} = | ||
logScope: | ||
sync_ident = sq.ident | ||
topics = "syncman" | ||
|
@@ -631,8 +662,18 @@ proc push*[T](sq: SyncQueue[T], sr: SyncRequest[T], | |
# all pending `push` requests if `request.slot` not in range. | ||
while true: | ||
if sq.notInRange(sr): | ||
let reset = await sq.waitForChanges() | ||
if reset: | ||
let | ||
resetWorker = | ||
if sq.pendingWorkerBlockWaitTime == InfiniteDuration: | ||
await sq.waitForChanges() | ||
else: | ||
try: | ||
await sq.waitForChanges().wait(sq.expectedTime(sr)) | ||
except AsyncTimeoutError: | ||
# Moving request to debts queue without checking pending list. | ||
sq.debtsQueue.push(sr) | ||
return | ||
if resetWorker: | ||
# SyncQueue reset happens. We are exiting to wake up sync-worker. | ||
return | ||
else: | ||
|
@@ -685,10 +726,8 @@ proc push*[T](sq: SyncQueue[T], sr: SyncRequest[T], | |
# Nim versions, remove workaround and move `res` into for loop | ||
res: Result[void, VerifierError] | ||
|
||
var i=0 | ||
for blk, blb in sq.blocks(item): | ||
res = await sq.blockVerifier(blk[], blb, maybeFinalized) | ||
inc(i) | ||
|
||
if res.isOk(): | ||
goodBlock = some(blk[].slot) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
|
||
import std/[strutils, sequtils] | ||
import unittest2 | ||
import chronos | ||
import chronos, chronos/unittest2/asynctests | ||
import ../beacon_chain/gossip_processing/block_processor, | ||
../beacon_chain/sync/sync_manager, | ||
../beacon_chain/spec/forks | ||
|
@@ -1134,8 +1134,6 @@ suite "SyncManager test suite": | |
check: | ||
groupedRes3.isErr() | ||
|
||
|
||
|
||
test "[SyncQueue#Forward] getRewindPoint() test": | ||
let aq = newAsyncQueue[BlockEntry]() | ||
block: | ||
|
@@ -1207,3 +1205,69 @@ suite "SyncManager test suite": | |
let safeSlot = getSafeSlot() | ||
for i in countdown(1023, 0): | ||
check queue.getRewindPoint(Slot(i), safeSlot) == safeSlot | ||
|
||
template workerTimeoutTest(startSlot, finishSlot: Slot, | ||
kind: SyncQueueKind) = | ||
let aq = newAsyncQueue[BlockEntry]() | ||
var | ||
chain = createChain(startSlot, finishSlot) | ||
queue = | ||
case kind | ||
of SyncQueueKind.Forward: | ||
SyncQueue.init(SomeTPeer, kind, startSlot, finishSlot, | ||
1'u64, getStaticSlotCb(startSlot), | ||
collector(aq), 1, "worker-timeout-test", | ||
100.milliseconds) | ||
of SyncQueueKind.Backward: | ||
SyncQueue.init(SomeTPeer, kind, finishSlot, startSlot, | ||
1'u64, getStaticSlotCb(finishSlot), | ||
collector(aq), 1, "worker-timeout-test", | ||
100.milliseconds) | ||
|
||
let | ||
p1 = SomeTPeer() | ||
p2 = SomeTPeer() | ||
p3 = SomeTPeer() | ||
p4 = SomeTPeer() | ||
r11 {.used.} = queue.pop(finishSlot, p1) | ||
r12 = queue.pop(finishSlot, p2) | ||
r13 = queue.pop(finishSlot, p3) | ||
r14 = queue.pop(finishSlot, p4) | ||
f12 = queue.push( | ||
r12, chain.getSlice(Slot(0), r12), Opt.none(seq[BlobSidecars])) | ||
f13 = queue.push( | ||
r13, chain.getSlice(Slot(0), r13), Opt.none(seq[BlobSidecars])) | ||
f14 = queue.push( | ||
r14, chain.getSlice(Slot(0), r14), Opt.none(seq[BlobSidecars])) | ||
|
||
check: | ||
f12.finished() == false | ||
f13.finished() == false | ||
f14.finished() == false | ||
|
||
# Current timeout value is 100.milliseconds per block. | ||
await sleepAsync(400.milliseconds) | ||
|
||
check: | ||
f12.finished() == true | ||
f13.finished() == true | ||
f14.finished() == true | ||
|
||
let | ||
r22 = queue.pop(finishSlot, p2) | ||
r23 = queue.pop(finishSlot, p3) | ||
r24 = queue.pop(finishSlot, p4) | ||
|
||
check: | ||
r22.slot == r12.slot | ||
r22.count == r12.count | ||
r23.slot == r13.slot | ||
r23.count == r13.count | ||
r24.slot == r14.slot | ||
r24.count == r14.count | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and, also, if otherwise, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep in old version |
||
|
||
asyncTest "[SyncQueue#Forward] SyncQueue worker timeout test": | ||
workerTimeoutTest(Slot(0), Slot(200), SyncQueueKind.Forward) | ||
|
||
asyncTest "[SyncQueue#Backward] SyncQueue worker timeout test": | ||
workerTimeoutTest(Slot(0), Slot(200), SyncQueueKind.Backward) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1s intuitively feels a bit short, I could see false positives if a block is stuck in P for a while (for example, the occasional multi-second state replay). The underlying problem only occurs in low-peer scenario with bad peers failing to provide data, I think it's okay if it takes a bit longer to recover in that edge case, if it means that the happy case is a bit more reliable.