-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sentinel): detect failover from +switch-master messages (#1328)
- Loading branch information
Showing
8 changed files
with
525 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Debug } from "../../utils"; | ||
import SentinelConnector from "./index"; | ||
import { ISentinel } from "./types"; | ||
|
||
const debug = Debug("FailoverDetector"); | ||
|
||
const CHANNEL_NAME = "+switch-master"; | ||
|
||
export class FailoverDetector { | ||
private connector: SentinelConnector; | ||
private sentinels: ISentinel[]; | ||
private isDisconnected = false; | ||
|
||
// sentinels can't be used for regular commands after this | ||
constructor(connector: SentinelConnector, sentinels: ISentinel[]) { | ||
this.connector = connector; | ||
this.sentinels = sentinels; | ||
} | ||
|
||
public cleanup() { | ||
this.isDisconnected = true; | ||
|
||
for (const sentinel of this.sentinels) { | ||
sentinel.client.disconnect(); | ||
} | ||
} | ||
|
||
public async subscribe() { | ||
debug("Starting FailoverDetector"); | ||
|
||
const promises: Promise<unknown>[] = []; | ||
|
||
for (const sentinel of this.sentinels) { | ||
const promise = sentinel.client.subscribe(CHANNEL_NAME).catch((err) => { | ||
debug( | ||
"Failed to subscribe to failover messages on sentinel %s:%s (%s)", | ||
sentinel.address.host || "127.0.0.1", | ||
sentinel.address.port || 26739, | ||
err.message | ||
); | ||
}); | ||
|
||
promises.push(promise); | ||
|
||
sentinel.client.on("message", (channel: string) => { | ||
if (!this.isDisconnected && channel === CHANNEL_NAME) { | ||
this.disconnect(); | ||
} | ||
}); | ||
} | ||
|
||
await Promise.all(promises); | ||
} | ||
|
||
private disconnect() { | ||
// Avoid disconnecting more than once per failover. | ||
// A new FailoverDetector will be created after reconnecting. | ||
this.isDisconnected = true; | ||
|
||
debug("Failover detected, disconnecting"); | ||
|
||
// Will call this.cleanup() | ||
this.connector.disconnect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,31 @@ | ||
import { IRedisOptions } from "../../redis/RedisOptions"; | ||
|
||
export interface ISentinelAddress { | ||
port: number; | ||
host: string; | ||
family?: number; | ||
} | ||
|
||
// TODO: A proper typedef. This one only declares a small subset of all the members. | ||
export interface IRedisClient { | ||
options: IRedisOptions; | ||
sentinel(subcommand: "sentinels", name: string): Promise<string[]>; | ||
sentinel( | ||
subcommand: "get-master-addr-by-name", | ||
name: string | ||
): Promise<string[]>; | ||
sentinel(subcommand: "slaves", name: string): Promise<string[]>; | ||
subscribe(...channelNames: string[]): Promise<number>; | ||
on( | ||
event: "message", | ||
callback: (channel: string, message: string) => void | ||
): void; | ||
on(event: "error", callback: (error: Error) => void): void; | ||
on(event: "reconnecting", callback: () => void): void; | ||
disconnect(): void; | ||
} | ||
|
||
export interface ISentinel { | ||
address: Partial<ISentinelAddress>; | ||
client: IRedisClient; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.