github.com/KinWaiYuen/client-go/v2@v2.5.4/internal/mockstore/mocktikv/errors.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/mockstore/mocktikv/errors.go
    19  //
    20  
    21  // Copyright 2016 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 mocktikv
    36  
    37  import (
    38  	"encoding/hex"
    39  	"fmt"
    40  
    41  	"github.com/pingcap/kvproto/pkg/kvrpcpb"
    42  )
    43  
    44  // ErrLocked is returned when trying to Read/Write on a locked key. Client should
    45  // backoff or cleanup the lock then retry.
    46  type ErrLocked struct {
    47  	Key         MvccKey
    48  	Primary     []byte
    49  	StartTS     uint64
    50  	ForUpdateTS uint64
    51  	TTL         uint64
    52  	TxnSize     uint64
    53  	LockType    kvrpcpb.Op
    54  }
    55  
    56  // Error formats the lock to a string.
    57  func (e *ErrLocked) Error() string {
    58  	return fmt.Sprintf("key is locked, key: %q, primary: %q, txnStartTS: %v, forUpdateTs: %v, LockType: %v",
    59  		e.Key, e.Primary, e.StartTS, e.ForUpdateTS, e.LockType)
    60  }
    61  
    62  // ErrKeyAlreadyExist is returned when key exists but this key has a constraint that
    63  // it should not exist. Client should return duplicated entry error.
    64  type ErrKeyAlreadyExist struct {
    65  	Key []byte
    66  }
    67  
    68  func (e *ErrKeyAlreadyExist) Error() string {
    69  	return fmt.Sprintf("key already exist, key: %q", e.Key)
    70  }
    71  
    72  // ErrRetryable suggests that client may restart the txn.
    73  type ErrRetryable string
    74  
    75  func (e ErrRetryable) Error() string {
    76  	return fmt.Sprintf("retryable: %s", string(e))
    77  }
    78  
    79  // ErrAbort means something is wrong and client should abort the txn.
    80  type ErrAbort string
    81  
    82  func (e ErrAbort) Error() string {
    83  	return fmt.Sprintf("abort: %s", string(e))
    84  }
    85  
    86  // ErrAlreadyCommitted is returned specially when client tries to rollback a
    87  // committed lock.
    88  type ErrAlreadyCommitted uint64
    89  
    90  func (e ErrAlreadyCommitted) Error() string {
    91  	return "txn already committed"
    92  }
    93  
    94  // ErrAlreadyRollbacked is returned when lock operation meets rollback write record
    95  type ErrAlreadyRollbacked struct {
    96  	startTS uint64
    97  	key     []byte
    98  }
    99  
   100  func (e *ErrAlreadyRollbacked) Error() string {
   101  	return fmt.Sprintf("txn=%v on key=%s is already rolled back", e.startTS, hex.EncodeToString(e.key))
   102  }
   103  
   104  // ErrConflict is returned when the commitTS of key in the DB is greater than startTS.
   105  type ErrConflict struct {
   106  	StartTS          uint64
   107  	ConflictTS       uint64
   108  	ConflictCommitTS uint64
   109  	Key              []byte
   110  }
   111  
   112  func (e *ErrConflict) Error() string {
   113  	return "write conflict"
   114  }
   115  
   116  // ErrDeadlock is returned when deadlock error is detected.
   117  type ErrDeadlock struct {
   118  	LockTS         uint64
   119  	LockKey        []byte
   120  	DealockKeyHash uint64
   121  }
   122  
   123  func (e *ErrDeadlock) Error() string {
   124  	return "deadlock"
   125  }
   126  
   127  // ErrCommitTSExpired is returned when commit.CommitTS < lock.MinCommitTS
   128  type ErrCommitTSExpired struct {
   129  	kvrpcpb.CommitTsExpired
   130  }
   131  
   132  func (e *ErrCommitTSExpired) Error() string {
   133  	return "commit ts expired"
   134  }
   135  
   136  // ErrTxnNotFound is returned when the primary lock of the txn is not found.
   137  type ErrTxnNotFound struct {
   138  	kvrpcpb.TxnNotFound
   139  }
   140  
   141  func (e *ErrTxnNotFound) Error() string {
   142  	return "txn not found"
   143  }