Merge pull request #2061 from nspcc-dev/incremental-dumps

Support incremental dumps
This commit is contained in:
Roman Khimov 2021-07-16 15:17:00 +03:00 committed by GitHub
commit 24d89e4f86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 12 deletions

View file

@ -40,17 +40,14 @@ func TestDBRestore(t *testing.T) {
// First 15 blocks.
e.Run(t, append(baseArgs, "--count", "15")...)
// Invalid skips.
e.RunWithError(t, append(baseArgs, "--count", "10", "--skip", "14")...)
e.RunWithError(t, append(baseArgs, "--count", "10", "--skip", "16")...)
// Big count.
e.RunWithError(t, append(baseArgs, "--count", "1000", "--skip", "15")...)
e.RunWithError(t, append(baseArgs, "--count", "1000")...)
// Continue 15..25
e.Run(t, append(baseArgs, "--count", "10", "--skip", "15")...)
e.Run(t, append(baseArgs, "--count", "10")...)
// Continue till end.
e.Run(t, append(baseArgs, "--skip", "25")...)
e.Run(t, baseArgs...)
// Dump and compare.
dumpPath := path.Join(tmpDir, "testdump.acc")

View file

@ -52,10 +52,6 @@ func NewCommands() []cli.Command {
var cfgCountInFlags = make([]cli.Flag, len(cfgWithCountFlags))
copy(cfgCountInFlags, cfgWithCountFlags)
cfgCountInFlags = append(cfgCountInFlags,
cli.UintFlag{
Name: "skip, s",
Usage: "number of blocks to skip (default: 0)",
},
cli.StringFlag{
Name: "in, i",
Usage: "Input file (stdin if not given)",
@ -64,6 +60,10 @@ func NewCommands() []cli.Command {
Name: "dump",
Usage: "directory for storing JSON dumps",
},
cli.BoolFlag{
Name: "incremental, n",
Usage: "use if dump is incremental",
},
)
return []cli.Command{
{
@ -215,7 +215,6 @@ func restoreDB(ctx *cli.Context) error {
return cli.NewExitError(err, 1)
}
count := uint32(ctx.Uint("count"))
skip := uint32(ctx.Uint("skip"))
var inStream = os.Stdin
if in := ctx.String("in"); in != "" {
@ -240,6 +239,20 @@ func restoreDB(ctx *cli.Context) error {
defer prometheus.ShutDown()
defer pprof.ShutDown()
var start uint32
if ctx.Bool("incremental") {
start = reader.ReadU32LE()
if chain.BlockHeight()+1 < start {
return cli.NewExitError(fmt.Errorf("expected height: %d, dump starts at %d",
chain.BlockHeight()+1, start), 1)
}
}
var skip uint32
if chain.BlockHeight() != 0 {
skip = chain.BlockHeight() + 1 - start
}
var allBlocks = reader.ReadU32LE()
if reader.Err != nil {
return cli.NewExitError(err, 1)
@ -250,6 +263,11 @@ func restoreDB(ctx *cli.Context) error {
if count == 0 {
count = allBlocks - skip
}
log.Info("initialize restore",
zap.Uint32("start", start),
zap.Uint32("height", chain.BlockHeight()),
zap.Uint32("skip", skip),
zap.Uint32("count", count))
gctx := newGraceContext()
var lastIndex uint32

View file

@ -164,7 +164,6 @@ func TestRestoreDB(t *testing.T) {
// and then restore
set.String("in", testDump, "")
set.Int("skip", 0, "")
set.String("dump", saveDump, "")
require.NoError(t, restoreDB(ctx))
}