github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/milevadb-server/einsteindb/cleanup.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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 einsteindb
    15  
    16  import (
    17  	"github.com/prometheus/client_golang/prometheus"
    18  	pb "github.com/whtcorpsinc/ekvproto/pkg/ekvrpcpb"
    19  	"github.com/whtcorpsinc/errors"
    20  	"github.com/whtcorpsinc/milevadb/causetstore/einsteindb/einsteindbrpc"
    21  	"github.com/whtcorpsinc/milevadb/metrics"
    22  	"github.com/whtcorpsinc/milevadb/soliton/logutil"
    23  	"go.uber.org/zap"
    24  )
    25  
    26  type actionCleanup struct{}
    27  
    28  var _ twoPhaseCommitCausetAction = actionCleanup{}
    29  var EinsteinDBTxnRegionsNumHistogramCleanup = metrics.EinsteinDBTxnRegionsNumHistogram.WithLabelValues(metricsTag("cleanup"))
    30  
    31  func (actionCleanup) String() string {
    32  	return "cleanup"
    33  }
    34  
    35  func (actionCleanup) EinsteinDBTxnRegionsNumHistogram() prometheus.Observer {
    36  	return EinsteinDBTxnRegionsNumHistogramCleanup
    37  }
    38  
    39  func (actionCleanup) handleSingleBatch(c *twoPhaseCommitter, bo *Backoffer, batch batchMutations) error {
    40  	req := einsteindbrpc.NewRequest(einsteindbrpc.CmdBatchRollback, &pb.BatchRollbackRequest{
    41  		Keys:         batch.mutations.keys,
    42  		StartVersion: c.startTS,
    43  	}, pb.Context{Priority: c.priority, SyncLog: c.syncLog})
    44  	resp, err := c.causetstore.SendReq(bo, req, batch.region, readTimeoutShort)
    45  	if err != nil {
    46  		return errors.Trace(err)
    47  	}
    48  	regionErr, err := resp.GetRegionError()
    49  	if err != nil {
    50  		return errors.Trace(err)
    51  	}
    52  	if regionErr != nil {
    53  		err = bo.Backoff(BoRegionMiss, errors.New(regionErr.String()))
    54  		if err != nil {
    55  			return errors.Trace(err)
    56  		}
    57  		err = c.cleanupMutations(bo, batch.mutations)
    58  		return errors.Trace(err)
    59  	}
    60  	if keyErr := resp.Resp.(*pb.BatchRollbackResponse).GetError(); keyErr != nil {
    61  		err = errors.Errorf("conn %d 2PC cleanup failed: %s", c.connID, keyErr)
    62  		logutil.BgLogger().Debug("2PC failed cleanup key",
    63  			zap.Error(err),
    64  			zap.Uint64("txnStartTS", c.startTS))
    65  		return errors.Trace(err)
    66  	}
    67  	return nil
    68  }
    69  
    70  func (c *twoPhaseCommitter) cleanupMutations(bo *Backoffer, mutations CommitterMutations) error {
    71  	return c.doCausetActionOnMutations(bo, actionCleanup{}, mutations)
    72  }