From 7710c108d9877830fcbbdfcd72acfcdba545f983 Mon Sep 17 00:00:00 2001 From: lance6716 Date: Fri, 6 Sep 2019 16:36:00 +0800 Subject: [PATCH] manually cherry-pick for #237 --- lightning/restore/checkpoint_test.go | 24 ++++++++++++++++++++++++ lightning/restore/checkpoints.go | 20 +++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 lightning/restore/checkpoint_test.go diff --git a/lightning/restore/checkpoint_test.go b/lightning/restore/checkpoint_test.go new file mode 100644 index 000000000..028861312 --- /dev/null +++ b/lightning/restore/checkpoint_test.go @@ -0,0 +1,24 @@ +package restore + +import ( + . "github.com/pingcap/check" + "path/filepath" +) + +var _ = Suite(&checkpointSuite{}) + +type checkpointSuite struct{} + +func (s *splitKVSuite) TestCheckpointMarshallUnmarshall(c *C) { + path := filepath.Join(c.MkDir(), "filecheckpoint") + 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 diff --git a/lightning/restore/checkpoints.go b/lightning/restore/checkpoints.go index c4aadf65b..627aa3686 100644 --- a/lightning/restore/checkpoints.go +++ b/lightning/restore/checkpoints.go @@ -654,7 +654,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 { + common.AppLogger.Errorf("checkpoint file is broken %s, error: %v", path, err) + } + // 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 { common.AppLogger.Warnf("failed to open checkpoint file %s, going to create a new one: %v", path, err) }