-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Detect failover from +switch-master messages #1328
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this name, could be confusing.
But I'm planning to submit a PR that documents all the sentinel options after this gets merged, so that might help.