github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/util/gc_service_test.go (about)

     1  // Copyright 2020 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 util
    15  
    16  import (
    17  	"context"
    18  	"math"
    19  
    20  	"github.com/pingcap/check"
    21  	"github.com/pingcap/errors"
    22  	"github.com/pingcap/ticdc/pkg/util/testleak"
    23  	pd "github.com/tikv/pd/client"
    24  )
    25  
    26  type gcServiceSuite struct {
    27  	pdCli *mockPdClientForServiceGCSafePoint
    28  }
    29  
    30  var _ = check.Suite(&gcServiceSuite{
    31  	&mockPdClientForServiceGCSafePoint{serviceSafePoint: make(map[string]uint64)},
    32  })
    33  
    34  func (s *gcServiceSuite) TestCheckSafetyOfStartTs(c *check.C) {
    35  	defer testleak.AfterTest(c)()
    36  	ctx := context.Background()
    37  
    38  	// assume no pd leader switch
    39  	s.pdCli.UpdateServiceGCSafePoint(ctx, "service1", 10, 60) //nolint:errcheck
    40  	err := CheckSafetyOfStartTs(ctx, s.pdCli, "changefeed1", 50)
    41  	c.Assert(err.Error(), check.Equals, "[CDC:ErrStartTsBeforeGC]fail to create changefeed because start-ts 50 is earlier than GC safepoint at 60")
    42  	s.pdCli.UpdateServiceGCSafePoint(ctx, "service2", 10, 80) //nolint:errcheck
    43  	s.pdCli.UpdateServiceGCSafePoint(ctx, "service3", 10, 70) //nolint:errcheck
    44  	err = CheckSafetyOfStartTs(ctx, s.pdCli, "changefeed2", 65)
    45  	c.Assert(err, check.IsNil)
    46  	c.Assert(s.pdCli.serviceSafePoint, check.DeepEquals, map[string]uint64{
    47  		"service1":                   60,
    48  		"service2":                   80,
    49  		"service3":                   70,
    50  		"ticdc-creating-changefeed2": 65,
    51  	})
    52  
    53  	s.pdCli.enableLeaderSwitch = true
    54  
    55  	s.pdCli.retryThresh = 1
    56  	s.pdCli.retryCount = 0
    57  	err = CheckSafetyOfStartTs(ctx, s.pdCli, "changefeed2", 65)
    58  	c.Assert(err, check.IsNil)
    59  
    60  	s.pdCli.retryThresh = 8
    61  	s.pdCli.retryCount = 0
    62  	err = CheckSafetyOfStartTs(ctx, s.pdCli, "changefeed2", 65)
    63  	c.Assert(err, check.NotNil)
    64  	c.Assert(err.Error(), check.Equals, "[CDC:ErrReachMaxTry]reach maximum try: 8")
    65  
    66  	s.pdCli.retryThresh = 3
    67  	s.pdCli.retryCount = 0
    68  	err = CheckSafetyOfStartTs(ctx, s.pdCli, "changefeed1", 50)
    69  	c.Assert(err.Error(), check.Equals, "[CDC:ErrStartTsBeforeGC]fail to create changefeed because start-ts 50 is earlier than GC safepoint at 60")
    70  }
    71  
    72  type mockPdClientForServiceGCSafePoint struct {
    73  	pd.Client
    74  	serviceSafePoint   map[string]uint64
    75  	enableLeaderSwitch bool
    76  	retryCount         int
    77  	retryThresh        int
    78  }
    79  
    80  func (m *mockPdClientForServiceGCSafePoint) UpdateServiceGCSafePoint(ctx context.Context, serviceID string, ttl int64, safePoint uint64) (uint64, error) {
    81  	defer func() { m.retryCount++ }()
    82  	minSafePoint := uint64(math.MaxUint64)
    83  	if m.enableLeaderSwitch && m.retryCount < m.retryThresh {
    84  		// simulate pd leader switch error
    85  		return minSafePoint, errors.New("not pd leader")
    86  	}
    87  
    88  	for _, safePoint := range m.serviceSafePoint {
    89  		if minSafePoint > safePoint {
    90  			minSafePoint = safePoint
    91  		}
    92  	}
    93  	if safePoint < minSafePoint && len(m.serviceSafePoint) != 0 {
    94  		return minSafePoint, nil
    95  	}
    96  	m.serviceSafePoint[serviceID] = safePoint
    97  	return minSafePoint, nil
    98  }