Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
checkpoint: fix empty map become nil after unmarshall (#237)
Browse files Browse the repository at this point in the history
* checkpoint: fix empty map become nil after unmarshall

* checkpoint: unify code style

* checkpoint: remove hard-coded path
  • Loading branch information
lance6716 committed Sep 6, 2019
1 parent ab23466 commit efb95cb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lightning/checkpoints/checkpoints.go
Expand Up @@ -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),
Expand Down
15 changes: 15 additions & 0 deletions lightning/checkpoints/checkpoints_test.go
@@ -1,6 +1,7 @@
package checkpoints

import (
"path/filepath"
"testing"

. "github.com/pingcap/check"
Expand Down Expand Up @@ -288,3 +289,17 @@ func (s *checkpointSuite) TestApplyDiff(c *C) {
},
})
}

func (s *checkpointSuite) 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)
}

0 comments on commit efb95cb

Please sign in to comment.