-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore database CLI command (#8061)
* restore beacon node db * revert image name * move restore out of the kv folder * remove files from kv folder * go mod tidy * Remove usage of prometheus testutil * add yes/no to prompt text * restore slasher db * organize imports * go mod tidy * restore validator db * close slasher db * defer close backup db in tests * simplify function literal
- Loading branch information
Showing
27 changed files
with
541 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package db | ||
|
||
import ( | ||
"github.com/prysmaticlabs/prysm/shared/cmd" | ||
"github.com/prysmaticlabs/prysm/shared/tos" | ||
"github.com/sirupsen/logrus" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// DatabaseCommands for Prysm beacon node. | ||
var DatabaseCommands = &cli.Command{ | ||
Name: "db", | ||
Category: "db", | ||
Usage: "defines commands for interacting with eth2 beacon node database", | ||
Subcommands: []*cli.Command{ | ||
{ | ||
Name: "restore", | ||
Description: `restores a database from a backup file`, | ||
Flags: cmd.WrapFlags([]cli.Flag{ | ||
cmd.RestoreSourceFileFlag, | ||
cmd.RestoreTargetDirFlag, | ||
}), | ||
Before: tos.VerifyTosAcceptedOrPrompt, | ||
Action: func(cliCtx *cli.Context) error { | ||
if err := restore(cliCtx); err != nil { | ||
logrus.Fatalf("Could not restore database: %v", err) | ||
} | ||
return nil | ||
}, | ||
}, | ||
}, | ||
} |
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,46 @@ | ||
package db | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/db/kv" | ||
"github.com/prysmaticlabs/prysm/shared/cmd" | ||
"github.com/prysmaticlabs/prysm/shared/fileutil" | ||
"github.com/prysmaticlabs/prysm/shared/promptutil" | ||
"github.com/sirupsen/logrus" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
const dbExistsYesNoPrompt = "A database file already exists in the target directory. " + | ||
"Are you sure that you want to overwrite it? [y/n]" | ||
|
||
func restore(cliCtx *cli.Context) error { | ||
sourceFile := cliCtx.String(cmd.RestoreSourceFileFlag.Name) | ||
targetDir := cliCtx.String(cmd.RestoreTargetDirFlag.Name) | ||
|
||
restoreDir := path.Join(targetDir, kv.BeaconNodeDbDirName) | ||
if fileutil.FileExists(path.Join(restoreDir, kv.DatabaseFileName)) { | ||
resp, err := promptutil.ValidatePrompt( | ||
os.Stdin, dbExistsYesNoPrompt, promptutil.ValidateYesOrNo, | ||
) | ||
if err != nil { | ||
return errors.Wrap(err, "could not validate choice") | ||
} | ||
if strings.ToLower(resp) == "n" { | ||
logrus.Info("Restore aborted") | ||
return nil | ||
} | ||
} | ||
if err := fileutil.MkdirAll(restoreDir); err != nil { | ||
return err | ||
} | ||
if err := fileutil.CopyFile(sourceFile, path.Join(restoreDir, kv.DatabaseFileName)); err != nil { | ||
return err | ||
} | ||
|
||
logrus.Info("Restore completed successfully") | ||
return nil | ||
} |
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,71 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/beacon-chain/cache" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/db/kv" | ||
"github.com/prysmaticlabs/prysm/shared/cmd" | ||
"github.com/prysmaticlabs/prysm/shared/testutil" | ||
"github.com/prysmaticlabs/prysm/shared/testutil/assert" | ||
"github.com/prysmaticlabs/prysm/shared/testutil/require" | ||
logTest "github.com/sirupsen/logrus/hooks/test" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func TestRestore(t *testing.T) { | ||
logHook := logTest.NewGlobal() | ||
ctx := context.Background() | ||
|
||
backupDb, err := kv.NewKVStore(t.TempDir(), cache.NewStateSummaryCache()) | ||
defer func() { | ||
require.NoError(t, backupDb.Close()) | ||
}() | ||
require.NoError(t, err) | ||
head := testutil.NewBeaconBlock() | ||
head.Block.Slot = 5000 | ||
require.NoError(t, backupDb.SaveBlock(ctx, head)) | ||
root, err := head.Block.HashTreeRoot() | ||
require.NoError(t, err) | ||
st := testutil.NewBeaconState() | ||
require.NoError(t, backupDb.SaveState(ctx, st, root)) | ||
require.NoError(t, backupDb.SaveHeadBlockRoot(ctx, root)) | ||
require.NoError(t, err) | ||
require.NoError(t, backupDb.Close()) | ||
// We rename the backup file so that we can later verify | ||
// whether the restored db has been renamed correctly. | ||
require.NoError(t, os.Rename( | ||
path.Join(backupDb.DatabasePath(), kv.DatabaseFileName), | ||
path.Join(backupDb.DatabasePath(), "backup.db"))) | ||
|
||
restoreDir := t.TempDir() | ||
app := cli.App{} | ||
set := flag.NewFlagSet("test", 0) | ||
set.String(cmd.RestoreSourceFileFlag.Name, "", "") | ||
set.String(cmd.RestoreTargetDirFlag.Name, "", "") | ||
require.NoError(t, set.Set(cmd.RestoreSourceFileFlag.Name, path.Join(backupDb.DatabasePath(), "backup.db"))) | ||
require.NoError(t, set.Set(cmd.RestoreTargetDirFlag.Name, restoreDir)) | ||
cliCtx := cli.NewContext(&app, set, nil) | ||
|
||
assert.NoError(t, restore(cliCtx)) | ||
|
||
files, err := ioutil.ReadDir(path.Join(restoreDir, kv.BeaconNodeDbDirName)) | ||
require.NoError(t, err) | ||
assert.Equal(t, 1, len(files)) | ||
assert.Equal(t, kv.DatabaseFileName, files[0].Name()) | ||
restoredDb, err := kv.NewKVStore(path.Join(restoreDir, kv.BeaconNodeDbDirName), nil) | ||
defer func() { | ||
require.NoError(t, restoredDb.Close()) | ||
}() | ||
require.NoError(t, err) | ||
headBlock, err := restoredDb.HeadBlock(ctx) | ||
require.NoError(t, err) | ||
assert.Equal(t, uint64(5000), headBlock.Block.Slot, "Restored database has incorrect data") | ||
assert.LogsContain(t, logHook, "Restore completed successfully") | ||
|
||
} |
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
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,32 @@ | ||
package db | ||
|
||
import ( | ||
"github.com/prysmaticlabs/prysm/shared/cmd" | ||
"github.com/prysmaticlabs/prysm/shared/tos" | ||
"github.com/sirupsen/logrus" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// DatabaseCommands for Prysm slasher. | ||
var DatabaseCommands = &cli.Command{ | ||
Name: "db", | ||
Category: "db", | ||
Usage: "defines commands for interacting with eth2 slasher database", | ||
Subcommands: []*cli.Command{ | ||
{ | ||
Name: "restore", | ||
Description: `restores a database from a backup file`, | ||
Flags: cmd.WrapFlags([]cli.Flag{ | ||
cmd.RestoreSourceFileFlag, | ||
cmd.RestoreTargetDirFlag, | ||
}), | ||
Before: tos.VerifyTosAcceptedOrPrompt, | ||
Action: func(cliCtx *cli.Context) error { | ||
if err := restore(cliCtx); err != nil { | ||
logrus.Fatalf("Could not restore database: %v", err) | ||
} | ||
return nil | ||
}, | ||
}, | ||
}, | ||
} |
Oops, something went wrong.