Skip to content

Commit

Permalink
Feat/agreement score (#346)
Browse files Browse the repository at this point in the history
* feat: agreement score

* feat: add log

* fix: traceId
  • Loading branch information
cool-firer authored Nov 19, 2024
1 parent c93c975 commit 168d369
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/network-support/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export function createFetch(
scoreType,
});
const extraLog = {
traceId,
requestId,
retry: retries,
error: errorMsg,
Expand Down
19 changes: 14 additions & 5 deletions packages/network-support/src/orderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,11 @@ export class OrderManager {
return plan;
}

private async selectRunner(orders: Order[], type: OrderType): Promise<Order | undefined> {
private async selectRunner(orders: Order[], orderType: OrderType): Promise<Order | undefined> {
if (!orders.length) return;
const scores = await Promise.all(
orders.map((o) =>
this.scoreManager.getAdjustedScore(o.indexer, o.metadata?.proxyVersion, type)
this.scoreManager.getAdjustedScore(o.indexer, o.metadata?.proxyVersion, orderType)
)
);
const random = Math.random() * scores.reduce((a, b) => a + b.score, 0);
Expand All @@ -529,6 +529,7 @@ export class OrderManager {
indexer: orders[i].indexer,
score: scores[i].score,
detail: scores[i].scoreDetail,
orderType,
});
return orders[i];
}
Expand Down Expand Up @@ -568,11 +569,19 @@ export class OrderManager {
this.agreements[index].token = token;
}

async getScore(runner: string) {
const plans = this._plans || [];
async getScore(runner: string, orderType?: OrderType) {
orderType = orderType || OrderType.flexPlan;

let plans = this._plans;
if (orderType === OrderType.agreement) {
plans = this._agreements;
}
plans = plans || [];

const plan = plans.find((p) => p.indexer === runner);
const proxyVersion = plan?.metadata?.proxyVersion || '';
return this.scoreManager.getAdjustedScore(runner, proxyVersion);

return this.scoreManager.getAdjustedScore(runner, proxyVersion, orderType);
}

async updateScore(runner: string, errorType: ScoreType, httpVersion?: number, extraLog?: any) {
Expand Down
8 changes: 4 additions & 4 deletions packages/network-support/src/scoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ export class ScoreManager {
async getAdjustedScore(
runner: string,
proxyVersion?: string,
type?: OrderType
orderType?: OrderType
): Promise<ScoreWithDetail> {
proxyVersion = proxyVersion || '';
type = type || OrderType.flexPlan;
orderType = orderType || OrderType.flexPlan;
const score = await this.getScore(runner);
const base = this.getAvailabilityScore(score);
const http2 = this.getHttpVersionWeight(score);
const manual = await this.getManualScoreWeight(runner, this.projectId);
const multiple = this.getMultipleAuthScoreWeight(proxyVersion);
const block = await getBlockScoreWeight(this.scoreStore, runner, this.projectId);
const latency = await getLatencyScoreWeight(this.scoreStore, runner, this.projectId);
const latency = await getLatencyScoreWeight(this.scoreStore, runner, this.projectId, orderType);
const price = await this.getPriceScoreWeight(runner);
const ratelimitInfo = await this.getRatelimitWeightInfo(runner, type);
const ratelimitInfo = await this.getRatelimitWeightInfo(runner, orderType);
const ratelimitWeight = ratelimitInfo.weight;

this.logger?.debug(
Expand Down
46 changes: 37 additions & 9 deletions packages/network-support/src/utils/score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@

import BigNumber from 'bignumber.js';
import { IStore } from './store';
import { OrderType } from '../types';

const BLOCK_WEIGHT_OUTPUT_RANGE: [number, number] = [0.2, 1];
const LATENCY_WEIGHT_THRESHOLD: [threshold: number, weight: number][] = [
const PLAN_LATENCY_WEIGHT_THRESHOLD: [threshold: number, weight: number][] = [
[2_000, 0.2], // >= 2000ms: 0.2
[1_000, 0.5], // [1000ms, 2000ms): 0.5
[500, 1], // [500ms, 1000ms): 1
[300, 3], // [300ms, 500ms): 3
[0, 6], // [0ms, 300ms): 6
];

const AGREEMENT_LATENCY_WEIGHT_THRESHOLD: [threshold: number, weight: number][] = [
[2_000, 0.2], // >= 2000ms: 0.2
[1_000, 0.5], // [1000ms, 2000ms): 0.5
[500, 1], // [500ms, 1000ms): 1
[300, 6], // [300ms, 500ms): 6
[0, 12], // [0ms, 300ms): 12
];

export enum CurveType {
LINEAR = 1,
QUADRATIC = 2,
Expand Down Expand Up @@ -83,23 +92,39 @@ export async function updateLatencyScoreWeight(
}

for (let i = 0; i < indexerLantency.length; i++) {
let weight = 1;
for (const [threshold, wt] of LATENCY_WEIGHT_THRESHOLD) {
let planWeight = 1;
for (const [threshold, wt] of PLAN_LATENCY_WEIGHT_THRESHOLD) {
if (medians[i] && medians[i] >= threshold) {
weight = wt;
planWeight = wt;
break;
}
}
await scoreStore.set(`${key}:${indexerLantency[i].indexer}_${deploymentId}`, weight);

let agreementWeight = 1;
for (const [threshold, wt] of AGREEMENT_LATENCY_WEIGHT_THRESHOLD) {
if (medians[i] && medians[i] >= threshold) {
agreementWeight = wt;
break;
}
}
await scoreStore.set(
`${key}:${OrderType.flexPlan}:${indexerLantency[i].indexer}_${deploymentId}`,
planWeight
);
await scoreStore.set(
`${key}:${OrderType.agreement}:${indexerLantency[i].indexer}_${deploymentId}`,
agreementWeight
);
logger?.debug(
`updateLatencyScoreWeight: ${indexerLantency[i].indexer} ${deploymentId}(min:${min}, max:${max}) ${medians[i]} => ${weight}`
`updateLatencyScoreWeight: ${indexerLantency[i].indexer} ${deploymentId}(min:${min}, max:${max}) ${medians[i]} => plan:${planWeight}, agreement:${agreementWeight}`
);
logger?.info({
type: 'updateScore',
target: 'latencyWeight',
deploymentId,
indexer: indexerLantency[i].indexer,
to: weight,
toPlan: planWeight,
toAgreement: agreementWeight,
});
}
}
Expand All @@ -117,9 +142,12 @@ export async function getBlockScoreWeight(
export async function getLatencyScoreWeight(
scoreStore: IStore,
runner: string,
deploymentId: string
deploymentId: string,
orderType?: OrderType
) {
const key = `${getLatencyScoreKey()}:${runner}_${deploymentId}`;
orderType = orderType || OrderType.flexPlan;
const orderKey = orderType as string;
const key = `${getLatencyScoreKey()}:${orderKey}:${runner}_${deploymentId}`;
const latencyWeight = await scoreStore.get<number>(key);
return latencyWeight || 1;
}
Expand Down

0 comments on commit 168d369

Please sign in to comment.