Skip to content

Commit 7dfaf1a

Browse files
committed
add debug fn
1 parent 2a316fe commit 7dfaf1a

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

packages/client/lib/client/commands-queue.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { TypeMapping, ReplyUnion, RespVersions, RedisArgument } from '../RESP/ty
55
import { ChannelListeners, PubSub, PubSubCommand, PubSubListener, PubSubType, PubSubTypeListeners } from './pub-sub';
66
import { AbortError, ErrorReply, CommandTimeoutDuringMaintananceError, TimeoutError } from '../errors';
77
import { MonitorCallback } from '.';
8+
import { dbgMaintenance } from './enterprise-maintenance-manager';
89

910
export interface CommandOptions<T = TypeMapping> {
1011
chainId?: symbol;
@@ -79,11 +80,14 @@ export default class RedisCommandsQueue {
7980
#maintenanceCommandTimeout: number | undefined
8081

8182
setMaintenanceCommandTimeout(ms: number | undefined) {
83+
dbgMaintenance(`Setting maintenance command timeout to ${ms}`);
8284
// Prevent possible api misuse
8385
if (this.#maintenanceCommandTimeout === ms) return;
8486

8587
this.#maintenanceCommandTimeout = ms;
8688

89+
let counter = 0;
90+
8791
// Overwrite timeouts of all eligible toWrite commands
8892
this.#toWrite.forEachNode(node => {
8993
const command = node.value;
@@ -96,6 +100,7 @@ export default class RedisCommandsQueue {
96100
// if no timeout is given and the command didnt have any timeout before, skip
97101
if (!newTimeout) return;
98102

103+
counter++;
99104

100105
// Overwrite the command's timeout
101106
const signal = AbortSignal.timeout(newTimeout);
@@ -109,6 +114,7 @@ export default class RedisCommandsQueue {
109114
};
110115
signal.addEventListener('abort', command.timeout.listener, { once: true });
111116
});
117+
dbgMaintenance(`Total of ${counter} timeouts reset to ${ms}`);
112118
}
113119

114120
get isPubSubActive() {

packages/client/lib/client/enterprise-maintenance-manager.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import EventEmitter from "events";
22
import { RedisClientOptions } from ".";
33
import RedisCommandsQueue from "./commands-queue";
4-
import RedisSocket, { RedisSocketOptions, RedisTcpSocketOptions } from "./socket";
4+
import RedisSocket from "./socket";
55
import { RedisArgument } from "../..";
66
import { isIP } from "net";
77
import { lookup } from "dns/promises";
@@ -25,6 +25,11 @@ export interface SocketTimeoutUpdate {
2525
timeout?: number;
2626
}
2727

28+
export const dbgMaintenance = (...args: any[]) => {
29+
if (!process.env.DEBUG_MAINTENANCE) return;
30+
return console.log('[MNT]', ...args);
31+
}
32+
2833
export default class EnterpriseMaintenanceManager extends EventEmitter {
2934
#commandsQueue: RedisCommandsQueue;
3035
#options: RedisClientOptions;
@@ -47,16 +52,19 @@ export default class EnterpriseMaintenanceManager extends EventEmitter {
4752
case PN.MOVING: {
4853
const [_, afterMs, url] = push;
4954
const [host, port] = url.toString().split(":");
55+
dbgMaintenance('Received MOVING:', afterMs, host, Number(port));
5056
this.#onMoving(afterMs, host, Number(port));
5157
return true;
5258
}
5359
case PN.MIGRATING:
5460
case PN.FAILING_OVER: {
61+
dbgMaintenance('Received MIGRATING|FAILING_OVER');
5562
this.#onMigrating();
5663
return true;
5764
}
5865
case PN.MIGRATED:
5966
case PN.FAILED_OVER: {
67+
dbgMaintenance('Received MIGRATED|FAILED_OVER');
6068
this.#onMigrated();
6169
return true;
6270
}
@@ -83,6 +91,7 @@ export default class EnterpriseMaintenanceManager extends EventEmitter {
8391
): Promise<void> => {
8492
// 1 [EVENT] MOVING PN received
8593
// 2 [ACTION] Pause writing
94+
dbgMaintenance('Pausing writing of new commands to old socket');
8695
this.emit(MAINTENANCE_EVENTS.PAUSE_WRITING);
8796
this.#onMigrating();
8897

@@ -93,13 +102,18 @@ export default class EnterpriseMaintenanceManager extends EventEmitter {
93102
});
94103
//todo
95104
newSocket.setMaintenanceTimeout();
105+
dbgMaintenance(`Connecting to new socket: ${host}:${port}`);
96106
await newSocket.connect();
107+
dbgMaintenance(`Connected to new socket`);
97108
// 3 [EVENT] New socket connected
98109

110+
dbgMaintenance(`Wait for all in-flight commands to complete`);
99111
await this.#commandsQueue.waitForInflightCommandsToComplete();
112+
dbgMaintenance(`In-flight commands completed`);
100113
// 4 [EVENT] In-flight commands completed
101114

102115
// 5 + 6
116+
dbgMaintenance('Resume writing')
103117
this.emit(MAINTENANCE_EVENTS.RESUME_WRITING, newSocket);
104118
this.#onMigrated();
105119
};

packages/client/lib/client/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { BasicClientSideCache, ClientSideCacheConfig, ClientSideCacheProvider }
2020
import { BasicCommandParser, CommandParser } from './parser';
2121
import SingleEntryCache from '../single-entry-cache';
2222
import { version } from '../../package.json'
23-
import EnterpriseMaintenanceManager, { MAINTENANCE_EVENTS, SocketTimeoutUpdate } from './enterprise-maintenance-manager';
23+
import EnterpriseMaintenanceManager, { MAINTENANCE_EVENTS, SocketTimeoutUpdate, dbgMaintenance } from './enterprise-maintenance-manager';
2424
import assert from 'node:assert';
2525

2626
export interface RedisClientOptions<
@@ -752,7 +752,7 @@ export default class RedisClient<
752752
commands.push({
753753
cmd: await EnterpriseMaintenanceManager.getHandshakeCommand(tls, host),
754754
errorHandler: (err: Error) => {
755-
console.log("Maintenance handshake failed: ", err);
755+
dbgMaintenance("Maintenance handshake failed: ", err);
756756
}
757757
});
758758
}

packages/client/lib/client/socket.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import tls from 'node:tls';
44
import { ConnectionTimeoutError, ClientClosedError, SocketClosedUnexpectedlyError, ReconnectStrategyError, SocketTimeoutError, SocketTimeoutDuringMaintananceError } from '../errors';
55
import { setTimeout } from 'node:timers/promises';
66
import { RedisArgument } from '../RESP/types';
7+
import { dbgMaintenance } from './enterprise-maintenance-manager';
78

89
type NetOptions = {
910
tls?: false;
@@ -243,6 +244,7 @@ export default class RedisSocket extends EventEmitter {
243244
}
244245

245246
setMaintenanceTimeout(ms?: number) {
247+
dbgMaintenance(`Reset socket timeout to ${ms}`);
246248
if (this.#maintenanceTimeout === ms) return;
247249

248250
this.#maintenanceTimeout = ms;

0 commit comments

Comments
 (0)