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

app: node: Add raft snapshot parameter options #143

Merged
merged 2 commits into from
Jun 23, 2021
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
2 changes: 2 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:
go get github.com/go-playground/overalls

- name: Build & Test
env:
CGO_LDFLAGS_ALLOW: "-Wl,-z,now"
run: |
go get -t -tags libsqlite3 ./...
go vet -tags libsqlite3 ./...
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ system, along with its dependencies. You then need to pass the ```-tags```
argument to the Go tools when building or testing your packages, for example:

```bash
export CGO_LDFLAGS_ALLOW="-Wl,-z,now"
go build -tags libsqlite3
go test -tags libsqlite3
```
Expand Down
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func New(dir string, options ...Option) (app *App, err error) {
dqlite.WithDialFunc(nodeDial),
dqlite.WithFailureDomain(o.FailureDomain),
dqlite.WithNetworkLatency(o.NetworkLatency),
dqlite.WithSnapshotParams(o.SnapshotParams),
)
if err != nil {
return nil, fmt.Errorf("create node: %w", err)
Expand Down
12 changes: 12 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/canonical/go-dqlite/app"
"github.com/canonical/go-dqlite/client"
"github.com/canonical/go-dqlite/internal/bindings"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -865,6 +866,17 @@ func TestOpen(t *testing.T) {
assert.NoError(t, err)
}

// Test some setup options
func TestOptions(t *testing.T) {
options := []app.Option{
app.WithNetworkLatency(20 * time.Millisecond),
app.WithSnapshotParams(bindings.SnapshotParams{Threshold: 1024, Trailing: 1024}),
}
app, cleanup := newApp(t, options...)
defer cleanup()
require.NotNil(t, app)
}

// Test client connections dropping uncleanly.
func TestProxy_Error(t *testing.T) {
cert, pool := loadCert(t)
Expand Down
9 changes: 9 additions & 0 deletions app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/canonical/go-dqlite/client"
"github.com/canonical/go-dqlite/internal/bindings"
)

// Option can be used to tweak app parameters.
Expand Down Expand Up @@ -141,6 +142,13 @@ func WithNetworkLatency(latency time.Duration) Option {
}
}

// WithSnapshotParams sets the raft snapshot parameters.
func WithSnapshotParams(params bindings.SnapshotParams) Option {
return func(options *options) {
options.SnapshotParams = params
}
}

type tlsSetup struct {
Listen *tls.Config
Dial *tls.Config
Expand All @@ -156,6 +164,7 @@ type options struct {
RolesAdjustmentFrequency time.Duration
FailureDomain uint64
NetworkLatency time.Duration
SnapshotParams bindings.SnapshotParams
}

// Create a options object with sane defaults.
Expand Down
2 changes: 1 addition & 1 deletion internal/bindings/build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package bindings

/*
#cgo linux LDFLAGS: -lsqlite3 -lraft -ldqlite
#cgo linux LDFLAGS: -lsqlite3 -lraft -ldqlite -Wl,-z,now
*/
import "C"
15 changes: 15 additions & 0 deletions internal/bindings/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ import (

type Node C.dqlite_node

type SnapshotParams struct {
Threshold uint64
Trailing uint64
}

// Initializes state.
func init() {
// FIXME: ignore SIGPIPE, see https://github.com/joyent/libuv/issues/1254
Expand Down Expand Up @@ -154,6 +159,16 @@ func (s *Node) SetNetworkLatency(nanoseconds uint64) error {
return nil
}

func (s *Node) SetSnapshotParams(params SnapshotParams) error {
server := (*C.dqlite_node)(unsafe.Pointer(s))
cthreshold := C.unsigned(params.Threshold)
ctrailing := C.unsigned(params.Trailing)
if rc := C.dqlite_node_set_snapshot_params(server, cthreshold, ctrailing); rc != 0 {
return fmt.Errorf("failed to set snapshot params")
}
return nil
}

func (s *Node) SetFailureDomain(code uint64) error {
server := (*C.dqlite_node)(unsafe.Pointer(s))
ccode := C.failure_domain_t(code)
Expand Down
13 changes: 13 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ func WithFailureDomain(code uint64) Option {
}
}

// WithSnapshotParams sets the snapshot parameters of the node.
func WithSnapshotParams(params bindings.SnapshotParams) Option {
return func(options *options) {
options.SnapshotParams = params
}
}

// New creates a new Node instance.
func New(id uint64, address string, dir string, options ...Option) (*Node, error) {
o := defaultOptions()
Expand Down Expand Up @@ -84,6 +91,11 @@ func New(id uint64, address string, dir string, options ...Option) (*Node, error
return nil, err
}
}
if o.SnapshotParams.Threshold != 0 || o.SnapshotParams.Trailing != 0 {
if err := server.SetSnapshotParams(o.SnapshotParams); err != nil {
return nil, err
}
}
s := &Node{
server: server,
acceptCh: make(chan error, 1),
Expand Down Expand Up @@ -120,6 +132,7 @@ type options struct {
BindAddress string
NetworkLatency uint64
FailureDomain uint64
SnapshotParams bindings.SnapshotParams
}

// Close the server, releasing all resources it created.
Expand Down