-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minmdns - perform exponental backoff retry for discovering nodes (#9900)
* V1: very hardcoded logic for retries * Move the active resolve attempts class to a separate header/cpp file for better code organization * Restyle fixes * Remove unused include in minmdns resolver implementation * Update code to use Optional for potentially missing values * use a clock base for the active resolve attempts, to allow for testability * Start adding unit tests for the resolve attempt queue * More unit tests * Restyle fixes * Fix signed/unsigned for optional, to make android compile * Code review updates, add a more complex test for scheduling check
- Loading branch information
Showing
6 changed files
with
653 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "ActiveResolveAttempts.h" | ||
|
||
#include <lib/support/logging/CHIPLogging.h> | ||
|
||
#include <algorithm> | ||
|
||
using namespace chip; | ||
|
||
namespace mdns { | ||
namespace Minimal { | ||
|
||
void ActiveResolveAttempts::Reset() | ||
|
||
{ | ||
for (auto & item : mRetryQueue) | ||
{ | ||
item.peerId.SetNodeId(kUndefinedNodeId); | ||
} | ||
} | ||
|
||
void ActiveResolveAttempts::Complete(const PeerId & peerId) | ||
{ | ||
for (auto & item : mRetryQueue) | ||
{ | ||
if (item.peerId == peerId) | ||
{ | ||
item.peerId.SetNodeId(kUndefinedNodeId); | ||
return; | ||
} | ||
} | ||
|
||
// This may happen during boot time adverisements: nodes come online | ||
// and advertise their IP without any explicit queries for them | ||
ChipLogProgress(Discovery, "Discovered node without a pending query"); | ||
} | ||
|
||
void ActiveResolveAttempts::MarkPending(const PeerId & peerId) | ||
{ | ||
// Strategy when picking the peer id to use: | ||
// 1 if a matching peer id is already found, use that one | ||
// 2 if an 'unused' entry is found, use that | ||
// 3 otherwise expire the one with the largest nextRetryDelaySec | ||
// or if equal nextRetryDelaySec, pick the one with the oldest | ||
// queryDueTimeMs | ||
|
||
RetryEntry * entryToUse = &mRetryQueue[0]; | ||
|
||
for (size_t i = 1; i < kRetryQueueSize; i++) | ||
{ | ||
if (entryToUse->peerId == peerId) | ||
{ | ||
break; // best match possible | ||
} | ||
|
||
RetryEntry * entry = mRetryQueue + i; | ||
|
||
// Rule 1: peer id match always matches | ||
if (entry->peerId == peerId) | ||
{ | ||
entryToUse = entry; | ||
continue; | ||
} | ||
|
||
// Rule 2: select unused entries | ||
if ((entryToUse->peerId.GetNodeId() != kUndefinedNodeId) && (entry->peerId.GetNodeId() == kUndefinedNodeId)) | ||
{ | ||
entryToUse = entry; | ||
continue; | ||
} | ||
else if (entryToUse->peerId.GetNodeId() == kUndefinedNodeId) | ||
{ | ||
continue; | ||
} | ||
|
||
// Rule 3: both choices are used (have a defined node id): | ||
// - try to find the one with the largest next delay (oldest request) | ||
// - on same delay, use queryDueTime to determine the oldest request | ||
// (the one with the smallest due time was issued the longest time | ||
// ago) | ||
if (entry->nextRetryDelaySec > entryToUse->nextRetryDelaySec) | ||
{ | ||
entryToUse = entry; | ||
} | ||
else if ((entry->nextRetryDelaySec == entryToUse->nextRetryDelaySec) && | ||
(entry->queryDueTimeMs < entryToUse->queryDueTimeMs)) | ||
{ | ||
entryToUse = entry; | ||
} | ||
} | ||
|
||
if ((entryToUse->peerId.GetNodeId() != kUndefinedNodeId) && (entryToUse->peerId != peerId)) | ||
{ | ||
// TODO: node was evicted here, if/when resolution failures are | ||
// supported this could be a place for error callbacks | ||
// | ||
// Note however that this is NOT an actual 'timeout' it is showing | ||
// a burst of lookups for which we cannot maintain state. A reply may | ||
// still be received for this peer id (query was already sent on the | ||
// network) | ||
ChipLogError(Discovery, "Re-using pending resolve entry before reply was received."); | ||
} | ||
|
||
entryToUse->peerId = peerId; | ||
entryToUse->queryDueTimeMs = mClock->GetMonotonicMilliseconds(); | ||
entryToUse->nextRetryDelaySec = 1; | ||
} | ||
|
||
Optional<uint32_t> ActiveResolveAttempts::GetMsUntilNextExpectedResponse() const | ||
{ | ||
Optional<uint32_t> minDelay = Optional<uint32_t>::Missing(); | ||
|
||
chip::System::Clock::MonotonicMilliseconds nowMs = mClock->GetMonotonicMilliseconds(); | ||
|
||
for (auto & entry : mRetryQueue) | ||
{ | ||
if (entry.peerId.GetNodeId() == kUndefinedNodeId) | ||
{ | ||
continue; | ||
} | ||
|
||
if (nowMs >= entry.queryDueTimeMs) | ||
{ | ||
// found an entry that needs processing right now | ||
return Optional<uint32_t>::Value(0); | ||
} | ||
|
||
uint32_t entryDelay = static_cast<int>(entry.queryDueTimeMs - nowMs); | ||
if (!minDelay.HasValue() || (minDelay.Value() > entryDelay)) | ||
{ | ||
minDelay.SetValue(entryDelay); | ||
} | ||
} | ||
|
||
return minDelay; | ||
} | ||
|
||
Optional<PeerId> ActiveResolveAttempts::NextScheduledPeer() | ||
{ | ||
chip::System::Clock::MonotonicMilliseconds nowMs = mClock->GetMonotonicMilliseconds(); | ||
|
||
for (auto & entry : mRetryQueue) | ||
{ | ||
if (entry.peerId.GetNodeId() == kUndefinedNodeId) | ||
{ | ||
continue; // not a pending item | ||
} | ||
|
||
if (entry.queryDueTimeMs > nowMs) | ||
{ | ||
continue; // not yet due | ||
} | ||
|
||
if (entry.nextRetryDelaySec > kMaxRetryDelaySec) | ||
{ | ||
ChipLogError(Discovery, "Timeout waiting for mDNS resolution."); | ||
entry.peerId.SetNodeId(kUndefinedNodeId); | ||
continue; | ||
} | ||
|
||
entry.queryDueTimeMs = nowMs + entry.nextRetryDelaySec * 1000L; | ||
entry.nextRetryDelaySec *= 2; | ||
|
||
return Optional<PeerId>::Value(entry.peerId); | ||
} | ||
|
||
return Optional<PeerId>::Missing(); | ||
} | ||
|
||
} // namespace Minimal | ||
} // namespace mdns |
Oops, something went wrong.