github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/kv/resolvedts_heap_test.go (about)

     1  // Copyright 2021 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package kv
    15  
    16  import (
    17  	"time"
    18  
    19  	"github.com/pingcap/check"
    20  	"github.com/pingcap/ticdc/pkg/util/testleak"
    21  )
    22  
    23  type rtsHeapSuite struct {
    24  }
    25  
    26  var _ = check.Suite(&rtsHeapSuite{})
    27  
    28  func checkRegionTsInfoWithoutEvTime(c *check.C, obtained, expected *regionTsInfo) {
    29  	c.Assert(obtained.regionID, check.Equals, expected.regionID)
    30  	c.Assert(obtained.index, check.Equals, expected.index)
    31  	c.Assert(obtained.ts.resolvedTs, check.Equals, expected.ts.resolvedTs)
    32  	c.Assert(obtained.ts.sortByEvTime, check.IsFalse)
    33  }
    34  
    35  func (s *rtsHeapSuite) TestRegionTsManagerResolvedTs(c *check.C) {
    36  	defer testleak.AfterTest(c)()
    37  	mgr := newRegionTsManager()
    38  	initRegions := []*regionTsInfo{
    39  		{regionID: 102, ts: newResolvedTsItem(1040)},
    40  		{regionID: 100, ts: newResolvedTsItem(1000)},
    41  		{regionID: 101, ts: newResolvedTsItem(1020)},
    42  	}
    43  	for _, rts := range initRegions {
    44  		mgr.Upsert(rts)
    45  	}
    46  	c.Assert(mgr.Len(), check.Equals, 3)
    47  	rts := mgr.Pop()
    48  	checkRegionTsInfoWithoutEvTime(c, rts, &regionTsInfo{regionID: 100, ts: newResolvedTsItem(1000), index: -1})
    49  
    50  	// resolved ts is not updated
    51  	mgr.Upsert(rts)
    52  	rts = mgr.Pop()
    53  	checkRegionTsInfoWithoutEvTime(c, rts, &regionTsInfo{regionID: 100, ts: newResolvedTsItem(1000), index: -1})
    54  
    55  	// resolved ts updated
    56  	rts.ts.resolvedTs = 1001
    57  	mgr.Upsert(rts)
    58  	mgr.Upsert(&regionTsInfo{regionID: 100, ts: newResolvedTsItem(1100)})
    59  
    60  	rts = mgr.Pop()
    61  	checkRegionTsInfoWithoutEvTime(c, rts, &regionTsInfo{regionID: 101, ts: newResolvedTsItem(1020), index: -1})
    62  	rts = mgr.Pop()
    63  	checkRegionTsInfoWithoutEvTime(c, rts, &regionTsInfo{regionID: 102, ts: newResolvedTsItem(1040), index: -1})
    64  	rts = mgr.Pop()
    65  	checkRegionTsInfoWithoutEvTime(c, rts, &regionTsInfo{regionID: 100, ts: newResolvedTsItem(1100), index: -1})
    66  	rts = mgr.Pop()
    67  	c.Assert(rts, check.IsNil)
    68  }
    69  
    70  func (s *rtsHeapSuite) TestRegionTsManagerPenalty(c *check.C) {
    71  	defer testleak.AfterTest(c)()
    72  	mgr := newRegionTsManager()
    73  	initRegions := []*regionTsInfo{
    74  		{regionID: 100, ts: newResolvedTsItem(1000)},
    75  	}
    76  	for _, rts := range initRegions {
    77  		mgr.Upsert(rts)
    78  	}
    79  	c.Assert(mgr.Len(), check.Equals, 1)
    80  
    81  	// test penalty increases if resolved ts keeps unchanged
    82  	for i := 0; i < 6; i++ {
    83  		rts := &regionTsInfo{regionID: 100, ts: newResolvedTsItem(1000)}
    84  		mgr.Upsert(rts)
    85  	}
    86  	rts := mgr.Pop()
    87  	c.Assert(rts.ts.resolvedTs, check.Equals, uint64(1000))
    88  	c.Assert(rts.ts.penalty, check.Equals, 6)
    89  
    90  	// test penalty is cleared to zero if resolved ts is advanced
    91  	mgr.Upsert(rts)
    92  	rtsNew := &regionTsInfo{regionID: 100, ts: newResolvedTsItem(2000)}
    93  	mgr.Upsert(rtsNew)
    94  	rts = mgr.Pop()
    95  	c.Assert(rts.ts.penalty, check.DeepEquals, 0)
    96  	c.Assert(rts.ts.resolvedTs, check.DeepEquals, uint64(2000))
    97  }
    98  
    99  func (s *rtsHeapSuite) TestRegionTsManagerEvTime(c *check.C) {
   100  	defer testleak.AfterTest(c)()
   101  	mgr := newRegionTsManager()
   102  	initRegions := []*regionTsInfo{
   103  		{regionID: 100, ts: newEventTimeItem()},
   104  		{regionID: 101, ts: newEventTimeItem()},
   105  	}
   106  	for _, item := range initRegions {
   107  		mgr.Upsert(item)
   108  	}
   109  	info := mgr.Remove(101)
   110  	c.Assert(info.regionID, check.Equals, uint64(101))
   111  
   112  	ts := time.Now()
   113  	mgr.Upsert(&regionTsInfo{regionID: 100, ts: newEventTimeItem()})
   114  	info = mgr.Pop()
   115  	c.Assert(info.regionID, check.Equals, uint64(100))
   116  	c.Assert(ts.Before(info.ts.eventTime), check.IsTrue)
   117  	c.Assert(time.Now().After(info.ts.eventTime), check.IsTrue)
   118  	info = mgr.Pop()
   119  	c.Assert(info, check.IsNil)
   120  }