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

Hook into continual dqlite role probes #301

Merged
merged 3 commits into from
Jul 9, 2024
Merged
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
19 changes: 16 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func New(dir string, options ...Option) (app *App, err error) {
}()
}

go app.run(ctx, o.RolesAdjustmentFrequency, joinFileExists)
go app.run(ctx, o, joinFileExists)

return app, nil
}
Expand Down Expand Up @@ -525,7 +525,7 @@ func (a *App) proxy() {

// Run background tasks. The join flag is true if the node is a brand new one
// and should join the cluster.
func (a *App) run(ctx context.Context, frequency time.Duration, join bool) {
func (a *App) run(ctx context.Context, options *options, join bool) {
defer close(a.runCh)

delay := time.Duration(0)
Expand Down Expand Up @@ -584,7 +584,7 @@ func (a *App) run(ctx context.Context, frequency time.Duration, join bool) {
continue
}
ready = true
delay = frequency
delay = options.RolesAdjustmentFrequency
close(a.readyCh)
cli.Close()
continue
Expand All @@ -595,6 +595,19 @@ func (a *App) run(ctx context.Context, frequency time.Duration, join bool) {
if err := a.maybeAdjustRoles(ctx, cli); err != nil {
a.warn("adjust roles: %v", err)
}

leader, err := cli.Leader(ctx)
if err != nil {
a.error("fetch leader info: %v", err)
cli.Close()
continue
}

err = options.OnRolesAdjustment(*leader, servers)
if err != nil {
a.warn("roles adjustment hook: %v", err)
}

cli.Close()
}
}
Expand Down
11 changes: 11 additions & 0 deletions app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ func WithRolesAdjustmentFrequency(frequency time.Duration) Option {
}
}

// WithRolesAdjustmentHook will be run each time the roles are adjusted, as
// controlled by WithRolesAdjustmentFrequency. Provides the current raft leader information
// as well as the most up to date list of cluster members and their roles.
func WithRolesAdjustmentHook(hook func(leader client.NodeInfo, cluster []client.NodeInfo) error) Option {
return func(o *options) {
o.OnRolesAdjustment = hook
}
}

// WithLogFunc sets a custom log function.
func WithLogFunc(log client.LogFunc) Option {
return func(options *options) {
Expand Down Expand Up @@ -223,6 +232,7 @@ type options struct {
Voters int
StandBys int
RolesAdjustmentFrequency time.Duration
OnRolesAdjustment func(client.NodeInfo, []client.NodeInfo) error
FailureDomain uint64
NetworkLatency time.Duration
UnixSocket string
Expand All @@ -239,6 +249,7 @@ func defaultOptions() *options {
Voters: 3,
StandBys: 3,
RolesAdjustmentFrequency: 30 * time.Second,
OnRolesAdjustment: func(client.NodeInfo, []client.NodeInfo) error { return nil },
DiskMode: false, // Be explicit about not enabling disk-mode by default.
AutoRecovery: true,
}
Expand Down
Loading