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

fix: 🐛 should-record-active-request-in-a-duration #90

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions proto/noslated/control-plane.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ service ControlPlane {
message WorkerAdditionalData {
int32 activeRequestCount = 2;
bool trafficOff = 3;
int32 accumulatedRequestCount = 4;
}

message GetFunctionProfileResponse {
Expand Down
10 changes: 10 additions & 0 deletions src/control_plane/capacity_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ export class CapacityManager extends Base {
) {
deltaInstance = broker.activeWorkerCount - broker.reservationCount;
}

// 如果周期内有请求,则不缩容到 0,改为缩容到 1
if (broker.getAccumulatedRequestCount() > 0 && broker.activeWorkerCount - deltaInstance === 0) {
broker.redundantTimes = 0;
if (broker.activeWorkerCount > 1) {
return 1 - broker.activeWorkerCount;
} else {
return 0;
}
}

broker.redundantTimes = 0;
return -deltaInstance;
Expand Down
10 changes: 10 additions & 0 deletions src/control_plane/worker_stats/broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ class Broker {
return a;
}

getAccumulatedRequestCount() {
let a = 0;
for (const worker of this.workers.values()) {
if (!worker.isActive()) continue;
a += worker.data?.accumulatedRequestCount || 0;
}

return a;
}

/**
* Get worker.
* @param processName The process name (worker name).
Expand Down
2 changes: 2 additions & 0 deletions src/control_plane/worker_stats/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ export type WorkerStats = noslated.data.IWorkerStats;

class WorkerAdditionalData {
activeRequestCount;
accumulatedRequestCount;

constructor(data: WorkerStats) {
this.activeRequestCount = data.activeRequestCount;
this.accumulatedRequestCount = data.accumulatedRequestCount;
}
}

Expand Down
21 changes: 17 additions & 4 deletions src/data_plane/worker_broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class PendingRequest extends EventEmitter {

export class Worker extends EventEmitter {
activeRequestCount: number;
accumulatedRequestCount: number;
private logger: PrefixedLogger;
trafficOff: boolean;

Expand All @@ -105,6 +106,7 @@ export class Worker extends EventEmitter {
) {
super();
this.activeRequestCount = 0;
this.accumulatedRequestCount = 0;
this.logger = new PrefixedLogger('worker', this.name);

// + if `trafficOff` is `false`, then traffic may in;
Expand Down Expand Up @@ -140,6 +142,10 @@ export class Worker extends EventEmitter {
return promise;
}

zeroAccumulatedRequestCount() {
this.accumulatedRequestCount = 0;
}

/**
* Pipe input stream to worker process and get response.
*/
Expand Down Expand Up @@ -172,6 +178,7 @@ export class Worker extends EventEmitter {
}

this.activeRequestCount++;
this.accumulatedRequestCount++;
this.logger.info(
'[%s] Dispatching request, activeRequestCount: %s, wait: %sms.',
requestId,
Expand Down Expand Up @@ -473,10 +480,16 @@ export class WorkerBroker extends Base implements DispatcherDelegate {
return {
functionName: this.name,
inspector: this.options.inspect === true,
workers: Array.from(this._workerMap.values()).map(item => ({
name: item.name,
activeRequestCount: item.worker?.activeRequestCount ?? 0,
})),
workers: Array.from(this._workerMap.values()).map(item => {
const data = {
name: item.name,
activeRequestCount: item.worker?.activeRequestCount ?? 0,
accumulatedRequestCount: item.worker?.accumulatedRequestCount ?? 0,
};
// 读出累计数据后直接清空
item.worker?.zeroAccumulatedRequestCount();
return data;
}),
};
}

Expand Down