Skip to content

Commit 2b541fc

Browse files
authored
refactor(sdk): reduce checkpoint termination cooldown to 20ms (#438)
Changed CHECKPOINT_TERMINATION_COOLDOWN_MS from 50ms to 20ms and moved it to a shared constant for consistency across code and tests. Changes: - Add CHECKPOINT_TERMINATION_COOLDOWN_MS constant (20ms) to constants.ts - Update checkpoint-manager to use the constant - Update all tests to use the constant instead of hardcoded values - Adjust test timing to work with the reduced cooldown *Issue #, if available:* #437
1 parent b1e16f9 commit 2b541fc

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

packages/aws-durable-execution-sdk-js/src/utils/checkpoint/checkpoint-central-termination.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { OperationLifecycleState, OperationSubType } from "../../types";
55
import { OperationType } from "@aws-sdk/client-lambda";
66
import { EventEmitter } from "events";
77
import { hashId } from "../step-id-utils/step-id-utils";
8+
import { CHECKPOINT_TERMINATION_COOLDOWN_MS } from "../constants/constants";
89

910
jest.mock("../logger/logger");
1011

@@ -720,7 +721,7 @@ describe("CheckpointManager - Centralized Termination", () => {
720721
);
721722

722723
// Advance past cooldown
723-
jest.advanceTimersByTime(50);
724+
jest.advanceTimersByTime(CHECKPOINT_TERMINATION_COOLDOWN_MS);
724725

725726
expect(mockTerminationManager.terminate).toHaveBeenCalledWith({
726727
reason: TerminationReason.WAIT_SCHEDULED,
@@ -741,7 +742,7 @@ describe("CheckpointManager - Centralized Termination", () => {
741742
);
742743

743744
// Advance partway through cooldown
744-
jest.advanceTimersByTime(25);
745+
jest.advanceTimersByTime(CHECKPOINT_TERMINATION_COOLDOWN_MS / 2);
745746

746747
// Start new operation
747748
checkpointManager.markOperationState(
@@ -757,7 +758,7 @@ describe("CheckpointManager - Centralized Termination", () => {
757758
);
758759

759760
// Advance past original cooldown
760-
jest.advanceTimersByTime(50);
761+
jest.advanceTimersByTime(CHECKPOINT_TERMINATION_COOLDOWN_MS);
761762

762763
// Should not have terminated
763764
expect(mockTerminationManager.terminate).not.toHaveBeenCalled();

packages/aws-durable-execution-sdk-js/src/utils/checkpoint/checkpoint-manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from "../../errors/checkpoint-errors/checkpoint-errors";
1717
import { DurableLogger } from "../../types/durable-logger";
1818
import { Checkpoint } from "./checkpoint-helper";
19+
import { CHECKPOINT_TERMINATION_COOLDOWN_MS } from "../constants/constants";
1920
import {
2021
OperationLifecycleState,
2122
OperationInfo,
@@ -59,7 +60,6 @@ export class CheckpointManager implements Checkpoint {
5960
// Termination cooldown
6061
private terminationTimer: NodeJS.Timeout | null = null;
6162
private terminationReason: TerminationReason | null = null;
62-
private readonly TERMINATION_COOLDOWN_MS = 50;
6363

6464
constructor(
6565
private durableExecutionArn: string,
@@ -702,7 +702,7 @@ export class CheckpointManager implements Checkpoint {
702702
this.terminationReason = reason;
703703
log("⏱️", "Scheduling termination", {
704704
reason,
705-
cooldownMs: this.TERMINATION_COOLDOWN_MS,
705+
cooldownMs: CHECKPOINT_TERMINATION_COOLDOWN_MS,
706706
});
707707

708708
this.terminationTimer = setTimeout(() => {
@@ -715,7 +715,7 @@ export class CheckpointManager implements Checkpoint {
715715
return;
716716
}
717717
this.executeTermination(reason);
718-
}, this.TERMINATION_COOLDOWN_MS);
718+
}, CHECKPOINT_TERMINATION_COOLDOWN_MS);
719719
}
720720

721721
private executeTermination(reason: TerminationReason): void {

packages/aws-durable-execution-sdk-js/src/utils/checkpoint/checkpoint-termination.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { EventEmitter } from "events";
1010
import { createDefaultLogger } from "../logger/default-logger";
1111
import { OperationType } from "@aws-sdk/client-lambda";
1212
import { log } from "../logger/logger";
13+
import { CHECKPOINT_TERMINATION_COOLDOWN_MS } from "../constants/constants";
1314

1415
jest.mock("../logger/logger", () => ({
1516
log: jest.fn(),
@@ -244,7 +245,7 @@ describe("CheckpointManager Termination Behavior", () => {
244245
"Scheduling termination",
245246
expect.objectContaining({
246247
reason: "CALLBACK_PENDING",
247-
cooldownMs: 50,
248+
cooldownMs: CHECKPOINT_TERMINATION_COOLDOWN_MS,
248249
}),
249250
);
250251

@@ -255,8 +256,10 @@ describe("CheckpointManager Termination Behavior", () => {
255256
Type: "CHAINED_INVOKE",
256257
});
257258

258-
// Step 3: Advance time past the termination cooldown (50ms + buffer)
259-
await jest.advanceTimersByTimeAsync(60);
259+
// Step 3: Advance time past the termination cooldown
260+
await jest.advanceTimersByTimeAsync(
261+
CHECKPOINT_TERMINATION_COOLDOWN_MS + 10,
262+
);
260263

261264
// At this point, the timer callback should execute and re-check shouldTerminate()
262265
// It should find isProcessing=true and cancel termination

packages/aws-durable-execution-sdk-js/src/utils/constants/constants.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@
77
* TODO: Accept this as configuration parameter in the future
88
*/
99
export const STORE_STACK_TRACES = false;
10+
11+
/**
12+
* Checkpoint manager termination cooldown in milliseconds
13+
* After the last operation completes, the checkpoint manager waits this duration
14+
* before terminating to allow for any final checkpoint operations
15+
*/
16+
export const CHECKPOINT_TERMINATION_COOLDOWN_MS = 20;

0 commit comments

Comments
 (0)