github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/kv/error.go (about)

     1  // Copyright 2015 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 kv
    15  
    16  import (
    17  	"strings"
    18  
    19  	"github.com/insionng/yougam/libraries/pingcap/go-themis"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/mysql"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/terror"
    22  )
    23  
    24  // KV error codes.
    25  const (
    26  	codeClosed                     terror.ErrCode = 1
    27  	codeNotExist                                  = 2
    28  	codeCondithinNotMatch                         = 3
    29  	codeLockConfilict                             = 4
    30  	codeLazyConditionPairsNotMatch                = 5
    31  	codeRetryable                                 = 6
    32  	codeCantSetNilValue                           = 7
    33  	codeInvalidTxn                                = 8
    34  	codeNotCommitted                              = 9
    35  	codeNotImplemented                            = 10
    36  
    37  	codeKeyExists = 1062
    38  )
    39  
    40  var (
    41  	// ErrClosed is used when close an already closed txn.
    42  	ErrClosed = terror.ClassKV.New(codeClosed, "Error: Transaction already closed")
    43  	// ErrNotExist is used when try to get an entry with an unexist key from KV store.
    44  	ErrNotExist = terror.ClassKV.New(codeNotExist, "Error: key not exist")
    45  	// ErrConditionNotMatch is used when condition is not met.
    46  	ErrConditionNotMatch = terror.ClassKV.New(codeCondithinNotMatch, "Error: Condition not match")
    47  	// ErrLockConflict is used when try to lock an already locked key.
    48  	ErrLockConflict = terror.ClassKV.New(codeLockConfilict, "Error: Lock conflict")
    49  	// ErrLazyConditionPairsNotMatch is used when value in store differs from expect pairs.
    50  	ErrLazyConditionPairsNotMatch = terror.ClassKV.New(codeLazyConditionPairsNotMatch, "Error: Lazy condition pairs not match")
    51  	// ErrRetryable is used when KV store occurs RPC error or some other
    52  	// errors which SQL layer can safely retry.
    53  	ErrRetryable = terror.ClassKV.New(codeRetryable, "Error: KV error safe to retry")
    54  	// ErrCannotSetNilValue is the error when sets an empty value.
    55  	ErrCannotSetNilValue = terror.ClassKV.New(codeCantSetNilValue, "can not set nil value")
    56  	// ErrInvalidTxn is the error when commits or rollbacks in an invalid transaction.
    57  	ErrInvalidTxn = terror.ClassKV.New(codeInvalidTxn, "invalid transaction")
    58  
    59  	// ErrNotCommitted is the error returned by CommitVersion when this
    60  	// transaction is not committed.
    61  	ErrNotCommitted = terror.ClassKV.New(codeNotCommitted, "this transaction has not committed")
    62  
    63  	// ErrKeyExists returns when key is already exist.
    64  	ErrKeyExists = terror.ClassKV.New(codeKeyExists, "key already exist")
    65  	// ErrNotImplemented returns when a function is not implemented yet.
    66  	ErrNotImplemented = terror.ClassKV.New(codeNotImplemented, "not implemented")
    67  )
    68  
    69  func init() {
    70  	kvMySQLErrCodes := map[terror.ErrCode]uint16{
    71  		codeKeyExists: mysql.ErrDupEntry,
    72  	}
    73  	terror.ErrClassToMySQLCodes[terror.ClassKV] = kvMySQLErrCodes
    74  }
    75  
    76  // IsRetryableError checks if the err is a fatal error and the under going operation is worth to retry.
    77  func IsRetryableError(err error) bool {
    78  	if err == nil {
    79  		return false
    80  	}
    81  
    82  	if terror.ErrorEqual(err, ErrRetryable) ||
    83  		terror.ErrorEqual(err, ErrLockConflict) ||
    84  		terror.ErrorEqual(err, ErrConditionNotMatch) ||
    85  		terror.ErrorEqual(err, themis.ErrRetryable) ||
    86  		// HBase exception message will tell you if you should retry or not
    87  		strings.Contains(err.Error(), "try again later") {
    88  		return true
    89  	}
    90  
    91  	return false
    92  }
    93  
    94  // IsErrNotFound checks if err is a kind of NotFound error.
    95  func IsErrNotFound(err error) bool {
    96  	if terror.ErrorEqual(err, ErrNotExist) {
    97  		return true
    98  	}
    99  
   100  	return false
   101  }