github.com/KinWaiYuen/client-go/v2@v2.5.4/txnkv/transaction/cleanup.go (about) 1 // Copyright 2021 TiKV Authors 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // NOTE: The code in this file is based on code from the 16 // TiDB project, licensed under the Apache License v 2.0 17 // 18 // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/cleanup.go 19 // 20 21 // Copyright 2020 PingCAP, Inc. 22 // 23 // Licensed under the Apache License, Version 2.0 (the "License"); 24 // you may not use this file except in compliance with the License. 25 // You may obtain a copy of the License at 26 // 27 // http://www.apache.org/licenses/LICENSE-2.0 28 // 29 // Unless required by applicable law or agreed to in writing, software 30 // distributed under the License is distributed on an "AS IS" BASIS, 31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 // See the License for the specific language governing permissions and 33 // limitations under the License. 34 35 package transaction 36 37 import ( 38 "github.com/KinWaiYuen/client-go/v2/internal/client" 39 "github.com/KinWaiYuen/client-go/v2/internal/logutil" 40 "github.com/KinWaiYuen/client-go/v2/internal/retry" 41 "github.com/KinWaiYuen/client-go/v2/metrics" 42 "github.com/KinWaiYuen/client-go/v2/tikvrpc" 43 "github.com/pingcap/errors" 44 "github.com/pingcap/kvproto/pkg/kvrpcpb" 45 "github.com/prometheus/client_golang/prometheus" 46 "go.uber.org/zap" 47 ) 48 49 type actionCleanup struct{} 50 51 var _ twoPhaseCommitAction = actionCleanup{} 52 53 func (actionCleanup) String() string { 54 return "cleanup" 55 } 56 57 func (actionCleanup) tiKVTxnRegionsNumHistogram() prometheus.Observer { 58 return metrics.TxnRegionsNumHistogramCleanup 59 } 60 61 func (actionCleanup) handleSingleBatch(c *twoPhaseCommitter, bo *retry.Backoffer, batch batchMutations) error { 62 req := tikvrpc.NewRequest(tikvrpc.CmdBatchRollback, &kvrpcpb.BatchRollbackRequest{ 63 Keys: batch.mutations.GetKeys(), 64 StartVersion: c.startTS, 65 }, kvrpcpb.Context{Priority: c.priority, SyncLog: c.syncLog, ResourceGroupTag: c.resourceGroupTag, 66 MaxExecutionDurationMs: uint64(client.MaxWriteExecutionTime.Milliseconds())}) 67 resp, err := c.store.SendReq(bo, req, batch.region, client.ReadTimeoutShort) 68 if err != nil { 69 return errors.Trace(err) 70 } 71 regionErr, err := resp.GetRegionError() 72 if err != nil { 73 return errors.Trace(err) 74 } 75 if regionErr != nil { 76 err = bo.Backoff(retry.BoRegionMiss, errors.New(regionErr.String())) 77 if err != nil { 78 return errors.Trace(err) 79 } 80 err = c.cleanupMutations(bo, batch.mutations) 81 return errors.Trace(err) 82 } 83 if keyErr := resp.Resp.(*kvrpcpb.BatchRollbackResponse).GetError(); keyErr != nil { 84 err = errors.Errorf("session %d 2PC cleanup failed: %s", c.sessionID, keyErr) 85 logutil.BgLogger().Debug("2PC failed cleanup key", 86 zap.Error(err), 87 zap.Uint64("txnStartTS", c.startTS)) 88 return errors.Trace(err) 89 } 90 return nil 91 } 92 93 func (c *twoPhaseCommitter) cleanupMutations(bo *retry.Backoffer, mutations CommitterMutations) error { 94 return c.doActionOnMutations(bo, actionCleanup{}, mutations) 95 }