github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/util/gc_service.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  
    19  	"github.com/pingcap/errors"
    20  	"github.com/pingcap/log"
    21  	cerrors "github.com/pingcap/ticdc/pkg/errors"
    22  	"github.com/pingcap/ticdc/pkg/retry"
    23  	pd "github.com/tikv/pd/client"
    24  	"go.uber.org/zap"
    25  )
    26  
    27  const (
    28  	// cdcChangefeedCreatingServiceGCSafePointID is service GC safe point ID
    29  	cdcChangefeedCreatingServiceGCSafePointID = "ticdc-creating-"
    30  	// cdcChangefeedCreatingServiceGCSafePointTTL is service GC safe point TTL
    31  	cdcChangefeedCreatingServiceGCSafePointTTL = 10 * 60 // 10 mins
    32  )
    33  
    34  // CheckSafetyOfStartTs checks if the startTs less than the minimum of Service-GC-Ts
    35  // and this function will update the service GC to startTs
    36  func CheckSafetyOfStartTs(ctx context.Context, pdCli pd.Client, changefeedID string, startTs uint64) (err error) {
    37  	var minServiceGCTs uint64
    38  	// pd leader switch may happen, so just retry it.
    39  	if err := retry.Do(ctx, func() error {
    40  		minServiceGCTs, err = pdCli.UpdateServiceGCSafePoint(ctx, cdcChangefeedCreatingServiceGCSafePointID+changefeedID,
    41  			cdcChangefeedCreatingServiceGCSafePointTTL, startTs)
    42  		if err != nil {
    43  			log.Warn("update GC safepoint failed, retry later", zap.Error(err))
    44  		}
    45  		return err
    46  	},
    47  		retry.WithBackoffBaseDelay(500),
    48  		retry.WithMaxTries(8),
    49  		retry.WithIsRetryableErr(cerrors.IsRetryableError)); err != nil {
    50  		return errors.Trace(err)
    51  	}
    52  	if startTs < minServiceGCTs {
    53  		return cerrors.ErrStartTsBeforeGC.GenWithStackByArgs(startTs, minServiceGCTs)
    54  	}
    55  	return nil
    56  }