-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: weighted gateway peers for data retrievals
* adding 'peer' label to prometheus data retrival metrics * add lib helper 'random-weighted-choices' for returning weighted random list * add retry mechanism, retrying over random and unique gateway peers
- Loading branch information
Showing
3 changed files
with
190 additions
and
57 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,95 @@ | ||
/** | ||
* AR.IO Gateway | ||
* Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
// note: this file is based on the library random-weighted-choice | ||
// but it differs in that it allows for multiple choices to be made at once | ||
|
||
export type WeightedElement<T> = { | ||
id: T; | ||
weight: number; | ||
}; | ||
|
||
type RandomWeightedChoicesArgs<T> = { | ||
table: WeightedElement<T>[]; | ||
count?: number; // Number of IDs to return, default is 1 | ||
temperature?: number; // Default is 50 | ||
randomFunction?: () => number; // Default is Math.random | ||
influence?: number; // Default is 2 | ||
}; | ||
|
||
export const randomWeightedChoices = <T>({ | ||
table, | ||
count = 1, | ||
temperature = 50, | ||
randomFunction = Math.random, | ||
influence = 2, | ||
}: RandomWeightedChoicesArgs<T>): T[] => { | ||
const T = (temperature - 50) / 50; | ||
const nb = table.length; | ||
if (!nb) { | ||
return []; | ||
} | ||
|
||
const total = table.reduce( | ||
(previousTotal, element) => previousTotal + element.weight, | ||
0, | ||
); | ||
|
||
const avg = total / nb; | ||
|
||
// Compute amplified urgencies (depending on temperature) | ||
const urgencies: Record<string, number> = {}; | ||
const urgencySum = table.reduce((previousSum, element) => { | ||
const { id, weight } = element; | ||
let urgency = weight + T * influence * (avg - weight); | ||
if (urgency < 0) urgency = 0; | ||
urgencies[id as string] = (urgencies[id as string] || 0) + urgency; | ||
return previousSum + urgency; | ||
}, 0); | ||
|
||
let currentUrgency = 0; | ||
const cumulatedUrgencies: Record<string, number> = {}; | ||
Object.keys(urgencies).forEach((id) => { | ||
currentUrgency += urgencies[id]; | ||
cumulatedUrgencies[id] = currentUrgency; | ||
}); | ||
|
||
if (urgencySum <= 0) { | ||
return []; // No weight given | ||
} | ||
|
||
// Choose | ||
const results: T[] = []; | ||
for (let i = 0; i < count; i++) { | ||
const choice = randomFunction() * urgencySum; | ||
const ids = Object.keys(cumulatedUrgencies).filter( | ||
// prevent duplicates | ||
(id) => !results.includes(id as unknown as T), | ||
); | ||
for (let j = 0; j < ids.length; j++) { | ||
const id = ids[j]; | ||
const urgency = cumulatedUrgencies[id]; | ||
if (choice <= urgency) { | ||
results.push(id as unknown as T); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return results; | ||
}; |
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