-
Notifications
You must be signed in to change notification settings - Fork 655
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Wei Fu <[email protected]>
- Loading branch information
Showing
4 changed files
with
148 additions
and
2 deletions.
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
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
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,134 @@ | ||
//go:build linux | ||
|
||
package failpoint | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/fuweid/go-dmflakey" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// TestRestartFromPowerFailure is to test data after unexpected power failure. | ||
func TestRestartFromPowerFailure(t *testing.T) { | ||
flakey := initFlakeyDevice(t, "") | ||
root := flakey.rootfs() | ||
|
||
dbPath := filepath.Join(root, "boltdb") | ||
|
||
args := []string{"bbolt", "bench", | ||
"-work", // keep the database | ||
"-path", dbPath, | ||
"-count=1000000000", | ||
"-batch-size=5", // separate total count into multiple truncation | ||
} | ||
cmd := exec.Command(args[0], args[1:]...) | ||
logger := &bytes.Buffer{} | ||
cmd.Stdout = logger | ||
cmd.Stderr = logger | ||
|
||
t.Logf("start %s", strings.Join(args, " ")) | ||
require.NoError(t, cmd.Start(), "args: %v", args) | ||
time.Sleep(3 * time.Second) | ||
|
||
defer func() { | ||
if t.Failed() { | ||
t.Logf("dump log:\n: %s", logger.String()) | ||
} | ||
}() | ||
|
||
t.Logf("simulate power failure") | ||
require.NoError(t, cmd.Process.Kill()) | ||
require.Error(t, cmd.Wait()) | ||
require.NoError(t, flakey.powerFailure("")) | ||
|
||
st, err := os.Stat(dbPath) | ||
require.NoError(t, err) | ||
t.Logf("db size: %d", st.Size()) | ||
|
||
t.Logf("verify data") | ||
output, err := exec.Command("bbolt", "check", dbPath).CombinedOutput() | ||
require.NoError(t, err, "bbolt check output: %s", string(output)) | ||
} | ||
|
||
// initFlakeyDevice inits flakey device with ext4 filesystem. | ||
func initFlakeyDevice(t *testing.T, mntOpt string) *flakeyDevice { | ||
imgDir := t.TempDir() | ||
rootDir := t.TempDir() | ||
fsType := dmflakey.FSTypeEXT4 | ||
|
||
flakey, err := dmflakey.InitFlakey("bbolt-failpoint", imgDir, fsType) | ||
require.NoError(t, err, "init flakey device") | ||
t.Cleanup(func() { | ||
assert.NoError(t, flakey.Teardown()) | ||
}) | ||
|
||
err = unix.Mount(flakey.DevicePath(), rootDir, string(fsType), 0, mntOpt) | ||
require.NoError(t, err, "init rootfs with flakey device") | ||
t.Cleanup(func() { | ||
assert.NoError(t, unmount(rootDir)) | ||
}) | ||
|
||
return &flakeyDevice{ | ||
flakey: flakey, | ||
|
||
rootDir: rootDir, | ||
} | ||
} | ||
|
||
type flakeyDevice struct { | ||
flakey dmflakey.Flakey | ||
|
||
rootDir string | ||
} | ||
|
||
// rootfs returns the rootfs where flakey device mounts. | ||
func (f *flakeyDevice) rootfs() string { | ||
return f.rootDir | ||
} | ||
|
||
// powerFailure drops all the pending writes and remount the rootfs. | ||
func (f *flakeyDevice) powerFailure(mntOpt string) error { | ||
if err := f.flakey.DropWrites(); err != nil { | ||
return fmt.Errorf("failed to drop_writes: %w", err) | ||
} | ||
|
||
if err := unmount(f.rootDir); err != nil { | ||
return fmt.Errorf("failed to unmount rootfs %s: %w", f.rootDir, err) | ||
} | ||
|
||
if err := f.flakey.AllowWrites(); err != nil { | ||
return fmt.Errorf("failed to allow_writes: %w", err) | ||
} | ||
|
||
if err := unix.Mount(f.flakey.DevicePath(), f.rootDir, string(f.flakey.Filesystem()), 0, mntOpt); err != nil { | ||
return fmt.Errorf("failed to mount rootfs %s: %w", f.rootDir, err) | ||
} | ||
return nil | ||
} | ||
|
||
func unmount(target string) error { | ||
for i := 0; i < 50; i++ { | ||
if err := unix.Unmount(target, 0); err != nil { | ||
switch err { | ||
case unix.EBUSY: | ||
time.Sleep(500 * time.Millisecond) | ||
continue | ||
case unix.EINVAL: | ||
default: | ||
return fmt.Errorf("failed to umount %s: %w", target, err) | ||
} | ||
} | ||
return nil | ||
} | ||
return unix.EBUSY | ||
} |