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

Exit on WAL commit failure #405

Merged
merged 1 commit into from
Sep 21, 2023
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
6 changes: 5 additions & 1 deletion cmd/litefs/mount_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ type MountCommand struct {

Config Config

OS litefs.OS
OS litefs.OS
Exit func(int)

Store *litefs.Store
Leaser litefs.Leaser
FileSystem *fuse.FileSystem
Expand All @@ -52,6 +54,7 @@ func NewMountCommand() *MountCommand {
execCh: make(chan error),
Config: NewConfig(),
OS: &internal.SystemOS{},
Exit: os.Exit,
}
}

Expand Down Expand Up @@ -351,6 +354,7 @@ func (c *MountCommand) initConsul(ctx context.Context) (err error) {
func (c *MountCommand) initStore(ctx context.Context) error {
c.Store = litefs.NewStore(c.Config.Data.Dir, c.Config.Lease.Candidate)
c.Store.OS = c.OS
c.Store.Exit = c.Exit
c.Store.StrictVerify = c.Config.StrictVerify
c.Store.Compress = c.Config.Data.Compress
c.Store.Retention = c.Config.Data.Retention
Expand Down
55 changes: 55 additions & 0 deletions cmd/litefs/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
main "github.com/superfly/litefs/cmd/litefs"
"github.com/superfly/litefs/internal"
"github.com/superfly/litefs/internal/testingutil"
"github.com/superfly/litefs/mock"
"github.com/superfly/ltx"
"golang.org/x/exp/slog"
"golang.org/x/net/http2"
Expand Down Expand Up @@ -328,6 +329,60 @@ func TestSingleNode_DropDB(t *testing.T) {
}
}

func TestSingleNode_ErrCommitWAL(t *testing.T) {
if !testingutil.IsWALMode() {
t.Skip("test failure only applies to WAL mode, skipping")
}

mos := mock.NewOS()
dir := t.TempDir()
cmd0 := newMountCommand(t, dir, nil)
cmd0.OS = mos
runMountCommand(t, cmd0)
db := testingutil.OpenSQLDB(t, filepath.Join(cmd0.Config.FUSE.Dir, "db"))

if _, err := db.Exec(`CREATE TABLE t (x)`); err != nil {
t.Fatal(err)
} else if _, err := db.Exec(`INSERT INTO t VALUES (100)`); err != nil {
t.Fatal(err)
}

var exitCode int
cmd0.Store.Exit = func(code int) {
exitCode = code
}
mos.OpenFunc = func(op, name string) (*os.File, error) {
if op == "COMMITWAL:WAL" {
return nil, fmt.Errorf("marker")
}
return os.Open(name)
}
if _, err := db.Exec(`INSERT INTO t VALUES (200)`); err != nil {
t.Fatal(err)
}

if got, want := exitCode, 99; got != want {
t.Fatalf("exit=%d, want=%d", got, want)
}
if err := db.Close(); err != nil {
t.Fatal(err)
} else if err := cmd0.Close(); err != nil {
t.Fatal(err)
}

// Restart the mount command.
cmd0 = runMountCommand(t, newMountCommand(t, dir, nil))
db = testingutil.OpenSQLDB(t, filepath.Join(cmd0.Config.FUSE.Dir, "db"))

// Ensure the second insert was not processed.
var x int
if err := db.QueryRow(`SELECT SUM(x) FROM t`).Scan(&x); err != nil {
t.Fatal(err)
} else if got, want := x, 100; got != want {
t.Fatalf("x=%d, want %d", got, want)
}
}

func TestSingleNode_BackupClient(t *testing.T) {
t.Run("OK", func(t *testing.T) {
cmd0 := newMountCommand(t, t.TempDir(), nil)
Expand Down
Loading
Loading