Fix startedAtMs in traceContentLookup to use system clock (#2891)

startedAtMs is the time passed since UNIX epoch in milliseconds.
Cannot use a monotime clock for that. As we want to keep mono
for other Moment.now() usage, imported epochTime from std/times
instead.
This commit is contained in:
Kim De Mey 2024-11-29 01:05:15 +07:00 committed by GitHub
parent e74d5c3f22
commit c0199e8944
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 7 deletions

View File

@ -27,6 +27,8 @@ import
"."/[portal_stream, portal_protocol_config], "."/[portal_stream, portal_protocol_config],
./messages ./messages
from std/times import epochTime # For system timestamp in traceContentLookup
export messages, routing_table, protocol export messages, routing_table, protocol
declareCounter portal_message_requests_incoming, declareCounter portal_message_requests_incoming,
@ -1249,12 +1251,15 @@ proc traceContentLookup*(
## target. Maximum value for n is `BUCKET_SIZE`. ## target. Maximum value for n is `BUCKET_SIZE`.
# `closestNodes` holds the k closest nodes to target found, sorted by distance # `closestNodes` holds the k closest nodes to target found, sorted by distance
# Unvalidated nodes are used for requests as a form of validation. # Unvalidated nodes are used for requests as a form of validation.
let startedAt = Moment.now()
# Need to use a system clock and not the mono clock for this.
let startedAtMs = int64(times.epochTime() * 1000)
var closestNodes = p.routingTable.neighbours(targetId, BUCKET_SIZE, seenOnly = false) var closestNodes = p.routingTable.neighbours(targetId, BUCKET_SIZE, seenOnly = false)
# Shuffling the order of the nodes in order to not always hit the same node # Shuffling the order of the nodes in order to not always hit the same node
# first for the same request. # first for the same request.
p.baseProtocol.rng[].shuffle(closestNodes) p.baseProtocol.rng[].shuffle(closestNodes)
let ts = now(chronos.Moment)
var responses = Table[string, TraceResponse]() var responses = Table[string, TraceResponse]()
var metadata = Table[string, NodeMetadata]() var metadata = Table[string, NodeMetadata]()
@ -1324,7 +1329,7 @@ proc traceContentLookup*(
case content.kind case content.kind
of Nodes: of Nodes:
let duration = chronos.milliseconds(now(chronos.Moment) - ts) let duration = chronos.milliseconds(Moment.now() - startedAt)
let maybeRadius = p.radiusCache.get(content.src.id) let maybeRadius = p.radiusCache.get(content.src.id)
if maybeRadius.isSome() and if maybeRadius.isSome() and
@ -1365,7 +1370,7 @@ proc traceContentLookup*(
metadata["0x" & $content.src.id] = metadata["0x" & $content.src.id] =
NodeMetadata(enr: content.src.record, distance: distance) NodeMetadata(enr: content.src.record, distance: distance)
of Content: of Content:
let duration = chronos.milliseconds(now(chronos.Moment) - ts) let duration = chronos.milliseconds(Moment.now() - startedAt)
# cancel any pending queries as the content has been found # cancel any pending queries as the content has been found
for f in pendingQueries: for f in pendingQueries:
@ -1399,8 +1404,7 @@ proc traceContentLookup*(
responses: responses, responses: responses,
metadata: metadata, metadata: metadata,
cancelled: pendingNodeIds, cancelled: pendingNodeIds,
startedAtMs: chronos.epochNanoSeconds(ts) div 1_000_000, startedAtMs: startedAtMs,
# nanoseconds to milliseconds
), ),
) )
else: else:
@ -1419,8 +1423,7 @@ proc traceContentLookup*(
responses: responses, responses: responses,
metadata: metadata, metadata: metadata,
cancelled: newSeq[NodeId](), cancelled: newSeq[NodeId](),
startedAtMs: chronos.epochNanoSeconds(ts) div 1_000_000, startedAtMs: startedAtMs,
# nanoseconds to milliseconds
), ),
) )