Skip to content

Commit

Permalink
Make UpgradeV0toV1 and DowngradeV1toV0 more robust.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevina committed Jan 19, 2017
1 parent f91286d commit 05f0240
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
9 changes: 8 additions & 1 deletion convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ func UpgradeV0toV1(path string, prefixLen int) error {
}

func DowngradeV1toV0(path string) error {
err := os.Remove(filepath.Join(path, SHARDING_FN))
fun, err := ReadShardFunc(path)
if err != nil {
return err
} else if fun.funName != "prefix" {
return fmt.Errorf("%s: can only downgrade datastore that use the 'prefix' sharding function", path)
}

err = os.Remove(filepath.Join(path, SHARDING_FN))
if err != nil {
return err
}
Expand Down
24 changes: 18 additions & 6 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestMoveRestart(t *testing.T) {

// there should be nothing left in the new datastore
rmEmptyDatastore(t, v2dir)

// try the move again, again should fail
createDatastore(t, v2dir, flatfs.NextToLast(2))
err = flatfs.Move(v1dir, v2dir, nil)
Expand Down Expand Up @@ -139,12 +139,12 @@ func TestUpgradeDownload(t *testing.T) {
keys, blocks := populateDatastore(t, tempdir)
checkKeys(t, tempdir, keys, blocks)

//err := flatfs.UpgradeV0toV1(tempdir, 3)
//if err == nil {
// t.Fatalf("UpgradeV0toV1 on already v1 should fail.")
//}
err := flatfs.UpgradeV0toV1(tempdir, 3)
if err == nil {
t.Fatalf("UpgradeV0toV1 on already v1 should fail.")
}

err := flatfs.DowngradeV1toV0(tempdir)
err = flatfs.DowngradeV1toV0(tempdir)
if err != nil {
t.Fatalf("DowngradeV1toV0 fail: %v\n", err)
}
Expand All @@ -165,6 +165,18 @@ func TestUpgradeDownload(t *testing.T) {
checkKeys(t, tempdir, keys, blocks)
}

func TestDownloadNonPrefix(t *testing.T) {
tempdir, cleanup := tempdir(t)
defer cleanup()

createDatastore(t, tempdir, flatfs.NextToLast(2))

err := flatfs.DowngradeV1toV0(tempdir)
if err == nil {
t.Fatalf("DowngradeV1toV0 should have failed", err)
}
}

func createDatastore(t *testing.T, dir string, fun *flatfs.ShardIdV1) {
err := flatfs.Create(dir, fun)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func ReadShardFunc(dir string) (*ShardIdV1, error) {
}

func WriteShardFunc(dir string, id *ShardIdV1) error {
file, err := os.Create(filepath.Join(dir, SHARDING_FN))
file, err := os.OpenFile(filepath.Join(dir, SHARDING_FN), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
return err
}
Expand Down

0 comments on commit 05f0240

Please sign in to comment.