From 3a26d4c17652053e2338fd8605646535eab92c55 Mon Sep 17 00:00:00 2001 From: lance6716 Date: Fri, 6 Sep 2019 15:00:12 +0800 Subject: [PATCH 1/3] checkpoint: fix empty map become nil after unmarshall --- lightning/checkpoints/checkpoints.go | 20 +++++++++++++++++++- lightning/checkpoints/checkpoints_test.go | 14 ++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lightning/checkpoints/checkpoints.go b/lightning/checkpoints/checkpoints.go index 8e6f06f19..1f842a06f 100644 --- a/lightning/checkpoints/checkpoints.go +++ b/lightning/checkpoints/checkpoints.go @@ -741,7 +741,25 @@ func NewFileCheckpointsDB(path string) *FileCheckpointsDB { // ignore all errors -- file maybe not created yet (and it is fine). content, err := ioutil.ReadFile(path) if err == nil { - cpdb.checkpoints.Unmarshal(content) + err2 := cpdb.checkpoints.Unmarshal(content) + if err2 != nil { + log.L().Error("checkpoint file is broken", zap.String("path", path), zap.Error(err2)) + } + // FIXME: patch for empty map may need initialize manually, because currently + // FIXME: a map of zero size -> marshall -> unmarshall -> become nil, see checkpoint_test.go + if cpdb.checkpoints.Checkpoints == nil { + cpdb.checkpoints.Checkpoints = map[string]*TableCheckpointModel{} + } + for _, table := range cpdb.checkpoints.Checkpoints { + if (*table).Engines == nil { + (*table).Engines = map[int32]*EngineCheckpointModel{} + } + for _, engine := range (*table).Engines { + if (*engine).Chunks == nil { + (*engine).Chunks = map[string]*ChunkCheckpointModel{} + } + } + } } else { log.L().Info("open checkpoint file failed, going to create a new one", zap.String("path", path), diff --git a/lightning/checkpoints/checkpoints_test.go b/lightning/checkpoints/checkpoints_test.go index 593085146..8c11ff97f 100644 --- a/lightning/checkpoints/checkpoints_test.go +++ b/lightning/checkpoints/checkpoints_test.go @@ -288,3 +288,17 @@ func (s *checkpointSuite) TestApplyDiff(c *C) { }, }) } + +func (s *checkpointSuite) TestCheckpointMarshallUnmarshall(c *C) { + path := "/tmp/test-chkp" + fileChkp := NewFileCheckpointsDB(path) + fileChkp.checkpoints.Checkpoints["a"] = &TableCheckpointModel{ + Status: uint32(CheckpointStatusLoaded), + Engines: map[int32]*EngineCheckpointModel{}, + } + fileChkp.Close() + + fileChkp2 := NewFileCheckpointsDB(path) + // if not recover empty map explicitly, it will become nil + c.Assert(fileChkp2.checkpoints.Checkpoints["a"].Engines, NotNil) +} \ No newline at end of file From 7c1d9317c95aa824f83293ed6c56a64df3c789bb Mon Sep 17 00:00:00 2001 From: lance6716 Date: Fri, 6 Sep 2019 15:40:33 +0800 Subject: [PATCH 2/3] checkpoint: unify code style --- lightning/checkpoints/checkpoints.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lightning/checkpoints/checkpoints.go b/lightning/checkpoints/checkpoints.go index 1f842a06f..f1b66bab2 100644 --- a/lightning/checkpoints/checkpoints.go +++ b/lightning/checkpoints/checkpoints.go @@ -751,12 +751,12 @@ func NewFileCheckpointsDB(path string) *FileCheckpointsDB { cpdb.checkpoints.Checkpoints = map[string]*TableCheckpointModel{} } for _, table := range cpdb.checkpoints.Checkpoints { - if (*table).Engines == nil { - (*table).Engines = map[int32]*EngineCheckpointModel{} + if table.Engines == nil { + table.Engines = map[int32]*EngineCheckpointModel{} } - for _, engine := range (*table).Engines { - if (*engine).Chunks == nil { - (*engine).Chunks = map[string]*ChunkCheckpointModel{} + for _, engine := range table.Engines { + if engine.Chunks == nil { + engine.Chunks = map[string]*ChunkCheckpointModel{} } } } From ead6a23337bbf77c025cda969bbbfafaaea96458 Mon Sep 17 00:00:00 2001 From: lance6716 Date: Fri, 6 Sep 2019 15:56:23 +0800 Subject: [PATCH 3/3] checkpoint: remove hard-coded path --- lightning/checkpoints/checkpoints_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lightning/checkpoints/checkpoints_test.go b/lightning/checkpoints/checkpoints_test.go index 8c11ff97f..95e1a6574 100644 --- a/lightning/checkpoints/checkpoints_test.go +++ b/lightning/checkpoints/checkpoints_test.go @@ -1,6 +1,7 @@ package checkpoints import ( + "path/filepath" "testing" . "github.com/pingcap/check" @@ -290,7 +291,7 @@ func (s *checkpointSuite) TestApplyDiff(c *C) { } func (s *checkpointSuite) TestCheckpointMarshallUnmarshall(c *C) { - path := "/tmp/test-chkp" + path := filepath.Join(c.MkDir(), "filecheckpoint") fileChkp := NewFileCheckpointsDB(path) fileChkp.checkpoints.Checkpoints["a"] = &TableCheckpointModel{ Status: uint32(CheckpointStatusLoaded),