Commit 05f0240c authored by Kevin Atkinson's avatar Kevin Atkinson

Make UpgradeV0toV1 and DowngradeV1toV0 more robust.

parent f91286dd
...@@ -28,7 +28,14 @@ func UpgradeV0toV1(path string, prefixLen int) error { ...@@ -28,7 +28,14 @@ func UpgradeV0toV1(path string, prefixLen int) error {
} }
func DowngradeV1toV0(path string) 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 { if err != nil {
return err return err
} }
......
...@@ -96,7 +96,7 @@ func TestMoveRestart(t *testing.T) { ...@@ -96,7 +96,7 @@ func TestMoveRestart(t *testing.T) {
// there should be nothing left in the new datastore // there should be nothing left in the new datastore
rmEmptyDatastore(t, v2dir) rmEmptyDatastore(t, v2dir)
// try the move again, again should fail // try the move again, again should fail
createDatastore(t, v2dir, flatfs.NextToLast(2)) createDatastore(t, v2dir, flatfs.NextToLast(2))
err = flatfs.Move(v1dir, v2dir, nil) err = flatfs.Move(v1dir, v2dir, nil)
...@@ -139,12 +139,12 @@ func TestUpgradeDownload(t *testing.T) { ...@@ -139,12 +139,12 @@ func TestUpgradeDownload(t *testing.T) {
keys, blocks := populateDatastore(t, tempdir) keys, blocks := populateDatastore(t, tempdir)
checkKeys(t, tempdir, keys, blocks) checkKeys(t, tempdir, keys, blocks)
//err := flatfs.UpgradeV0toV1(tempdir, 3) err := flatfs.UpgradeV0toV1(tempdir, 3)
//if err == nil { if err == nil {
// t.Fatalf("UpgradeV0toV1 on already v1 should fail.") t.Fatalf("UpgradeV0toV1 on already v1 should fail.")
//} }
err := flatfs.DowngradeV1toV0(tempdir) err = flatfs.DowngradeV1toV0(tempdir)
if err != nil { if err != nil {
t.Fatalf("DowngradeV1toV0 fail: %v\n", err) t.Fatalf("DowngradeV1toV0 fail: %v\n", err)
} }
...@@ -165,6 +165,18 @@ func TestUpgradeDownload(t *testing.T) { ...@@ -165,6 +165,18 @@ func TestUpgradeDownload(t *testing.T) {
checkKeys(t, tempdir, keys, blocks) 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) { func createDatastore(t *testing.T, dir string, fun *flatfs.ShardIdV1) {
err := flatfs.Create(dir, fun) err := flatfs.Create(dir, fun)
if err != nil { if err != nil {
......
...@@ -121,7 +121,7 @@ func ReadShardFunc(dir string) (*ShardIdV1, error) { ...@@ -121,7 +121,7 @@ func ReadShardFunc(dir string) (*ShardIdV1, error) {
} }
func WriteShardFunc(dir string, id *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 { if err != nil {
return err return err
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment