github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/lightning/checkpoints/checkpoints_test.go (about)

     1  package checkpoints
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	. "github.com/pingcap/check"
     8  
     9  	"github.com/pingcap/br/pkg/lightning/checkpoints/checkpointspb"
    10  	"github.com/pingcap/br/pkg/lightning/mydump"
    11  	"github.com/pingcap/br/pkg/lightning/verification"
    12  )
    13  
    14  func Test(t *testing.T) {
    15  	TestingT(t)
    16  }
    17  
    18  var _ = Suite(&checkpointSuite{})
    19  
    20  type checkpointSuite struct {
    21  }
    22  
    23  func (s *checkpointSuite) TestMergeStatusCheckpoint(c *C) {
    24  	cpd := NewTableCheckpointDiff()
    25  
    26  	m := StatusCheckpointMerger{EngineID: 0, Status: CheckpointStatusImported}
    27  	m.MergeInto(cpd)
    28  
    29  	c.Assert(cpd, DeepEquals, &TableCheckpointDiff{
    30  		hasStatus: false,
    31  		engines: map[int32]engineCheckpointDiff{
    32  			0: {
    33  				hasStatus: true,
    34  				status:    CheckpointStatusImported,
    35  				chunks:    make(map[ChunkCheckpointKey]chunkCheckpointDiff),
    36  			},
    37  		},
    38  	})
    39  
    40  	m = StatusCheckpointMerger{EngineID: -1, Status: CheckpointStatusLoaded}
    41  	m.MergeInto(cpd)
    42  
    43  	c.Assert(cpd, DeepEquals, &TableCheckpointDiff{
    44  		hasStatus: false,
    45  		engines: map[int32]engineCheckpointDiff{
    46  			0: {
    47  				hasStatus: true,
    48  				status:    CheckpointStatusImported,
    49  				chunks:    make(map[ChunkCheckpointKey]chunkCheckpointDiff),
    50  			},
    51  			-1: {
    52  				hasStatus: true,
    53  				status:    CheckpointStatusLoaded,
    54  				chunks:    make(map[ChunkCheckpointKey]chunkCheckpointDiff),
    55  			},
    56  		},
    57  	})
    58  
    59  	m = StatusCheckpointMerger{EngineID: WholeTableEngineID, Status: CheckpointStatusClosed}
    60  	m.MergeInto(cpd)
    61  
    62  	c.Assert(cpd, DeepEquals, &TableCheckpointDiff{
    63  		hasStatus: true,
    64  		status:    CheckpointStatusClosed,
    65  		engines: map[int32]engineCheckpointDiff{
    66  			0: {
    67  				hasStatus: true,
    68  				status:    CheckpointStatusImported,
    69  				chunks:    make(map[ChunkCheckpointKey]chunkCheckpointDiff),
    70  			},
    71  			-1: {
    72  				hasStatus: true,
    73  				status:    CheckpointStatusLoaded,
    74  				chunks:    make(map[ChunkCheckpointKey]chunkCheckpointDiff),
    75  			},
    76  		},
    77  	})
    78  
    79  	m = StatusCheckpointMerger{EngineID: -1, Status: CheckpointStatusAllWritten}
    80  	m.MergeInto(cpd)
    81  
    82  	c.Assert(cpd, DeepEquals, &TableCheckpointDiff{
    83  		hasStatus: true,
    84  		status:    CheckpointStatusClosed,
    85  		engines: map[int32]engineCheckpointDiff{
    86  			0: {
    87  				hasStatus: true,
    88  				status:    CheckpointStatusImported,
    89  				chunks:    make(map[ChunkCheckpointKey]chunkCheckpointDiff),
    90  			},
    91  			-1: {
    92  				hasStatus: true,
    93  				status:    CheckpointStatusAllWritten,
    94  				chunks:    make(map[ChunkCheckpointKey]chunkCheckpointDiff),
    95  			},
    96  		},
    97  	})
    98  }
    99  
   100  func (s *checkpointSuite) TestMergeInvalidStatusCheckpoint(c *C) {
   101  	cpd := NewTableCheckpointDiff()
   102  
   103  	m := StatusCheckpointMerger{EngineID: 0, Status: CheckpointStatusLoaded}
   104  	m.MergeInto(cpd)
   105  
   106  	m = StatusCheckpointMerger{EngineID: -1, Status: CheckpointStatusAllWritten}
   107  	m.SetInvalid()
   108  	m.MergeInto(cpd)
   109  
   110  	c.Assert(cpd, DeepEquals, &TableCheckpointDiff{
   111  		hasStatus: true,
   112  		status:    CheckpointStatusAllWritten / 10,
   113  		engines: map[int32]engineCheckpointDiff{
   114  			0: {
   115  				hasStatus: true,
   116  				status:    CheckpointStatusLoaded,
   117  				chunks:    make(map[ChunkCheckpointKey]chunkCheckpointDiff),
   118  			},
   119  			-1: {
   120  				hasStatus: true,
   121  				status:    CheckpointStatusAllWritten / 10,
   122  				chunks:    make(map[ChunkCheckpointKey]chunkCheckpointDiff),
   123  			},
   124  		},
   125  	})
   126  }
   127  
   128  func (s *checkpointSuite) TestMergeChunkCheckpoint(c *C) {
   129  	cpd := NewTableCheckpointDiff()
   130  
   131  	key := ChunkCheckpointKey{Path: "/tmp/path/1.sql", Offset: 0}
   132  
   133  	m := ChunkCheckpointMerger{
   134  		EngineID: 2,
   135  		Key:      key,
   136  		Checksum: verification.MakeKVChecksum(700, 15, 1234567890),
   137  		Pos:      1055,
   138  		RowID:    31,
   139  	}
   140  	m.MergeInto(cpd)
   141  
   142  	c.Assert(cpd, DeepEquals, &TableCheckpointDiff{
   143  		engines: map[int32]engineCheckpointDiff{
   144  			2: {
   145  				chunks: map[ChunkCheckpointKey]chunkCheckpointDiff{
   146  					key: {
   147  						pos:      1055,
   148  						rowID:    31,
   149  						checksum: verification.MakeKVChecksum(700, 15, 1234567890),
   150  					},
   151  				},
   152  			},
   153  		},
   154  	})
   155  
   156  	m = ChunkCheckpointMerger{
   157  		EngineID: 2,
   158  		Key:      key,
   159  		Checksum: verification.MakeKVChecksum(800, 20, 1357924680),
   160  		Pos:      1080,
   161  		RowID:    42,
   162  	}
   163  	m.MergeInto(cpd)
   164  
   165  	c.Assert(cpd, DeepEquals, &TableCheckpointDiff{
   166  		engines: map[int32]engineCheckpointDiff{
   167  			2: {
   168  				chunks: map[ChunkCheckpointKey]chunkCheckpointDiff{
   169  					key: {
   170  						pos:      1080,
   171  						rowID:    42,
   172  						checksum: verification.MakeKVChecksum(800, 20, 1357924680),
   173  					},
   174  				},
   175  			},
   176  		},
   177  	})
   178  }
   179  
   180  func (s *checkpointSuite) TestRebaseCheckpoint(c *C) {
   181  	cpd := NewTableCheckpointDiff()
   182  
   183  	m := RebaseCheckpointMerger{AllocBase: 10000}
   184  	m.MergeInto(cpd)
   185  
   186  	c.Assert(cpd, DeepEquals, &TableCheckpointDiff{
   187  		hasRebase: true,
   188  		allocBase: 10000,
   189  		engines:   make(map[int32]engineCheckpointDiff),
   190  	})
   191  }
   192  
   193  func (s *checkpointSuite) TestApplyDiff(c *C) {
   194  	cp := TableCheckpoint{
   195  		Status:    CheckpointStatusLoaded,
   196  		AllocBase: 123,
   197  		Engines: map[int32]*EngineCheckpoint{
   198  			-1: {
   199  				Status: CheckpointStatusLoaded,
   200  			},
   201  			0: {
   202  				Status: CheckpointStatusLoaded,
   203  				Chunks: []*ChunkCheckpoint{
   204  					{
   205  						Key: ChunkCheckpointKey{Path: "/tmp/01.sql"},
   206  						Chunk: mydump.Chunk{
   207  							Offset:       0,
   208  							EndOffset:    20000,
   209  							PrevRowIDMax: 0,
   210  							RowIDMax:     1000,
   211  						},
   212  					},
   213  					{
   214  						Key: ChunkCheckpointKey{Path: "/tmp/04.sql"},
   215  						Chunk: mydump.Chunk{
   216  							Offset:       0,
   217  							EndOffset:    15000,
   218  							PrevRowIDMax: 1000,
   219  							RowIDMax:     1300,
   220  						},
   221  					},
   222  				},
   223  			},
   224  		},
   225  	}
   226  
   227  	cpd := NewTableCheckpointDiff()
   228  	(&StatusCheckpointMerger{EngineID: -1, Status: CheckpointStatusImported}).MergeInto(cpd)
   229  	(&StatusCheckpointMerger{EngineID: WholeTableEngineID, Status: CheckpointStatusAllWritten}).MergeInto(cpd)
   230  	(&StatusCheckpointMerger{EngineID: 1234, Status: CheckpointStatusAnalyzeSkipped}).MergeInto(cpd)
   231  	(&RebaseCheckpointMerger{AllocBase: 11111}).MergeInto(cpd)
   232  	(&ChunkCheckpointMerger{
   233  		EngineID: 0,
   234  		Key:      ChunkCheckpointKey{Path: "/tmp/01.sql"},
   235  		Checksum: verification.MakeKVChecksum(3333, 4444, 5555),
   236  		Pos:      6666,
   237  		RowID:    777,
   238  	}).MergeInto(cpd)
   239  	(&ChunkCheckpointMerger{
   240  		EngineID: 5678,
   241  		Key:      ChunkCheckpointKey{Path: "/tmp/04.sql"},
   242  		Pos:      9999,
   243  		RowID:    888,
   244  	}).MergeInto(cpd)
   245  	(&ChunkCheckpointMerger{
   246  		EngineID: 0,
   247  		Key:      ChunkCheckpointKey{Path: "/tmp/03.sql"},
   248  		Pos:      3636,
   249  		RowID:    2222,
   250  	}).MergeInto(cpd)
   251  	(&ChunkCheckpointMerger{
   252  		EngineID: 0,
   253  		Key:      ChunkCheckpointKey{Path: "/tmp/10.sql"},
   254  		Pos:      4949,
   255  		RowID:    444,
   256  	}).MergeInto(cpd)
   257  
   258  	cp.Apply(cpd)
   259  
   260  	c.Assert(cp, DeepEquals, TableCheckpoint{
   261  		Status:    CheckpointStatusAllWritten,
   262  		AllocBase: 11111,
   263  		Engines: map[int32]*EngineCheckpoint{
   264  			-1: {
   265  				Status: CheckpointStatusImported,
   266  			},
   267  			0: {
   268  				Status: CheckpointStatusLoaded,
   269  				Chunks: []*ChunkCheckpoint{
   270  					{
   271  						Key: ChunkCheckpointKey{Path: "/tmp/01.sql"},
   272  						Chunk: mydump.Chunk{
   273  							Offset:       6666,
   274  							EndOffset:    20000,
   275  							PrevRowIDMax: 777,
   276  							RowIDMax:     1000,
   277  						},
   278  						Checksum: verification.MakeKVChecksum(3333, 4444, 5555),
   279  					},
   280  					{
   281  						Key: ChunkCheckpointKey{Path: "/tmp/04.sql"},
   282  						Chunk: mydump.Chunk{
   283  							Offset:       0,
   284  							EndOffset:    15000,
   285  							PrevRowIDMax: 1000,
   286  							RowIDMax:     1300,
   287  						},
   288  					},
   289  				},
   290  			},
   291  		},
   292  	})
   293  }
   294  
   295  func (s *checkpointSuite) TestCheckpointMarshallUnmarshall(c *C) {
   296  	path := filepath.Join(c.MkDir(), "filecheckpoint")
   297  	fileChkp := NewFileCheckpointsDB(path)
   298  	fileChkp.checkpoints.Checkpoints["a"] = &checkpointspb.TableCheckpointModel{
   299  		Status:  uint32(CheckpointStatusLoaded),
   300  		Engines: map[int32]*checkpointspb.EngineCheckpointModel{},
   301  	}
   302  	fileChkp.Close()
   303  
   304  	fileChkp2 := NewFileCheckpointsDB(path)
   305  	// if not recover empty map explicitly, it will become nil
   306  	c.Assert(fileChkp2.checkpoints.Checkpoints["a"].Engines, NotNil)
   307  }