Skip to content
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

Use weighted random score #231

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 20 additions & 27 deletions packages/network-support/src/orderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export enum ScoreType {
}

const scoresDelta = {
[ScoreType.GRAPHQL]: 50,
[ScoreType.NETWORK]: 10,
[ScoreType.RPC]: 10,
[ScoreType.GRAPHQL]: 0.5,
[ScoreType.NETWORK]: 0.8,
[ScoreType.RPC]: 0.8,
};

function tokenToAuthHeader(token: string) {
Expand All @@ -53,9 +53,6 @@ function tokenToAuthHeader(token: string) {

export class OrderManager {
private projectType: ProjectType;
private nextAgreementIndex: number | undefined;

private nextPlanIndex: number | undefined;
private _agreements: ServiceAgreementOrder[] = [];
private _plans: FlexPlanOrder[] = [];

Expand Down Expand Up @@ -166,8 +163,10 @@ export class OrderManager {
return score;
}

private filterOrdersByScore(orders: Order[]) {
return orders.filter(({ indexer }) => this.getScore(indexer) > this.minScore);
private addScoresToOrders(orders: Order[]): { score: number; order: Order }[] {
return orders
.map((order) => ({ score: this.getScore(order.indexer), order }))
.filter((order) => order.score > this.minScore);
}

private getNextOrderIndex(total: number, currentIndex: number) {
Expand Down Expand Up @@ -306,19 +305,16 @@ export class OrderManager {

if (!this.agreements) return;

const agreements = this.filterOrdersByScore(this.agreements) as ServiceAgreementOrder[];
this.logger?.debug(`available agreements count: ${agreements.length}`);

if (!this.healthy || !agreements.length) return;
const possibleAgreements = this.addScoresToOrders(this.agreements);
this.logger?.debug(`Available agreements count: ${possibleAgreements.length}`);

if (this.nextAgreementIndex === undefined) {
this.nextAgreementIndex = this.getRandomStartIndex(agreements.length);
}
if (!this.healthy || !possibleAgreements.length || possibleAgreements?.length < 1) return;

const agreement = agreements[this.nextAgreementIndex];
this.nextAgreementIndex = this.getNextOrderIndex(agreements.length, this.nextAgreementIndex);
const agreement = possibleAgreements
.map((a) => ({ weighted: Math.random() * a.score, ...a }))
.sort((a, b) => (a.weighted < b.weighted ? -1 : 1))[0].order as ServiceAgreementOrder;

this.logger?.debug(`next agreement: ${JSON.stringify(agreement.indexer)}`);
this.logger?.debug(`Next agreement: ${JSON.stringify(agreement.indexer)}`);

return agreement;
}
Expand All @@ -328,15 +324,12 @@ export class OrderManager {

if (!this.plans) return;

const plans = this.filterOrdersByScore(this.plans);
if (!this.healthy || !plans?.length) return;

if (this.nextPlanIndex === undefined) {
this.nextPlanIndex = this.getRandomStartIndex(plans.length);
}
const possiblePlans = this.addScoresToOrders(this.plans);
if (!this.healthy || !possiblePlans?.length || possiblePlans?.length < 1) return;

const plan = plans[this.nextPlanIndex];
this.nextPlanIndex = this.getNextOrderIndex(plans.length, this.nextPlanIndex);
const plan = possiblePlans
.map((a) => ({ weighted: Math.random() * a.score, ...a }))
.sort((a, b) => (a.weighted < b.weighted ? -1 : 1))[0].order;

return plan;
}
Expand Down Expand Up @@ -367,7 +360,7 @@ export class OrderManager {
const score = this.scoreStore.get<number>(key) ?? 100;

const delta = scoresDelta[errorType];
const newScore = Math.max(score - delta, 0);
const newScore = Math.max(score * delta, 0);

this.scoreStore.set(key, newScore);
}
Expand Down
Loading