Skip to content

Commit

Permalink
Merge pull request #264 from cole-miller/auto-recovery
Browse files Browse the repository at this point in the history
Expose auto-recovery config switch from dqlite
  • Loading branch information
cole-miller authored Aug 1, 2023
2 parents beebd01 + 37b4fa1 commit f54a152
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func New(dir string, options ...Option) (app *App, err error) {
dqlite.WithNetworkLatency(o.NetworkLatency),
dqlite.WithSnapshotParams(o.SnapshotParams),
dqlite.WithDiskMode(o.DiskMode),
dqlite.WithAutoRecovery(o.AutoRecovery),
)
if err != nil {
stop()
Expand Down
16 changes: 16 additions & 0 deletions app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ func WithDiskMode(disk bool) Option {
}
}

// WithAutoRecovery enables or disables auto-recovery of persisted data
// at startup for this node.
//
// When auto-recovery is enabled, raft snapshots and segment files may be
// deleted at startup if they are determined to be corrupt. This helps
// the startup process to succeed in more cases, but can lead to data loss.
//
// Auto-recovery is enabled by default.
func WithAutoRecovery(recovery bool) Option {
return func(options *options) {
options.AutoRecovery = recovery
}
}

type tlsSetup struct {
Listen *tls.Config
Dial *tls.Config
Expand All @@ -214,6 +228,7 @@ type options struct {
UnixSocket string
SnapshotParams dqlite.SnapshotParams
DiskMode bool
AutoRecovery bool
}

// Create a options object with sane defaults.
Expand All @@ -225,6 +240,7 @@ func defaultOptions() *options {
StandBys: 3,
RolesAdjustmentFrequency: 30 * time.Second,
DiskMode: false, // Be explicit about not enabling disk-mode by default.
AutoRecovery: true,
}
}

Expand Down
8 changes: 8 additions & 0 deletions internal/bindings/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ func (s *Node) EnableDiskMode() error {
return nil
}

func (s *Node) SetAutoRecovery(on bool) error {
server := (*C.dqlite_node)(unsafe.Pointer(s.node))
if rc := C.dqlite_node_set_auto_recovery(server, C.bool(on)); rc != 0 {
return fmt.Errorf("failed to set auto-recovery behavior")
}
return nil
}

func (s *Node) GetBindAddress() string {
server := (*C.dqlite_node)(unsafe.Pointer(s.node))
return C.GoString(C.dqlite_node_get_bind_address(server))
Expand Down
20 changes: 20 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ func WithDiskMode(disk bool) Option {
}
}

// WithAutoRecovery enables or disables auto-recovery of persisted data
// at startup for this node.
//
// When auto-recovery is enabled, raft snapshots and segment files may be
// deleted at startup if they are determined to be corrupt. This helps
// the startup process to succeed in more cases, but can lead to data loss.
//
// Auto-recovery is enabled by default.
func WithAutoRecovery(recovery bool) Option {
return func(options *options) {
options.AutoRecovery = recovery
}
}

// New creates a new Node instance.
func New(id uint64, address string, dir string, options ...Option) (*Node, error) {
o := defaultOptions()
Expand Down Expand Up @@ -131,6 +145,10 @@ func New(id uint64, address string, dir string, options ...Option) (*Node, error
return nil, err
}
}
if err := server.SetAutoRecovery(o.AutoRecovery); err != nil {
cancel()
return nil, err
}

s := &Node{
server: server,
Expand Down Expand Up @@ -171,6 +189,7 @@ type options struct {
FailureDomain uint64
SnapshotParams bindings.SnapshotParams
DiskMode bool
AutoRecovery bool
}

// Close the server, releasing all resources it created.
Expand Down Expand Up @@ -232,5 +251,6 @@ func defaultOptions() *options {
return &options{
DialFunc: client.DefaultDialFunc,
DiskMode: false, // Be explicit about not enabling disk-mode by default.
AutoRecovery: true,
}
}

0 comments on commit f54a152

Please sign in to comment.