github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/ekv/error.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 ekv
    15  
    16  import (
    17  	"strings"
    18  
    19  	"github.com/whtcorpsinc/BerolinaSQL/terror"
    20  	allegrosql "github.com/whtcorpsinc/milevadb/errno"
    21  	"github.com/whtcorpsinc/milevadb/soliton/redact"
    22  )
    23  
    24  // TxnRetryableMark is used to uniform the commit error messages which could retry the transaction.
    25  // *WARNING*: changing this string will affect the backward compatibility.
    26  const TxnRetryableMark = "[try again later]"
    27  
    28  var (
    29  	// ErrNotExist is used when try to get an entry with an unexist key from KV causetstore.
    30  	ErrNotExist = terror.ClassKV.New(allegrosql.ErrNotExist, allegrosql.MyALLEGROSQLErrName[allegrosql.ErrNotExist])
    31  	// ErrTxnRetryable is used when KV causetstore occurs retryable error which ALLEGROALLEGROSQL layer can safely retry the transaction.
    32  	// When using EinsteinDB as the storage node, the error is returned ONLY when dagger not found (txnLockNotFound) in Commit,
    33  	// subject to change it in the future.
    34  	ErrTxnRetryable = terror.ClassKV.New(allegrosql.ErrTxnRetryable,
    35  		allegrosql.MyALLEGROSQLErrName[allegrosql.ErrTxnRetryable]+TxnRetryableMark)
    36  	// ErrCannotSetNilValue is the error when sets an empty value.
    37  	ErrCannotSetNilValue = terror.ClassKV.New(allegrosql.ErrCannotSetNilValue, allegrosql.MyALLEGROSQLErrName[allegrosql.ErrCannotSetNilValue])
    38  	// ErrInvalidTxn is the error when commits or rollbacks in an invalid transaction.
    39  	ErrInvalidTxn = terror.ClassKV.New(allegrosql.ErrInvalidTxn, allegrosql.MyALLEGROSQLErrName[allegrosql.ErrInvalidTxn])
    40  	// ErrTxnTooLarge is the error when transaction is too large, dagger time reached the maximum value.
    41  	ErrTxnTooLarge = terror.ClassKV.New(allegrosql.ErrTxnTooLarge, allegrosql.MyALLEGROSQLErrName[allegrosql.ErrTxnTooLarge])
    42  	// ErrEntryTooLarge is the error when a key value entry is too large.
    43  	ErrEntryTooLarge = terror.ClassKV.New(allegrosql.ErrEntryTooLarge, allegrosql.MyALLEGROSQLErrName[allegrosql.ErrEntryTooLarge])
    44  	// ErrKeyExists returns when key is already exist.
    45  	ErrKeyExists = redact.NewRedactError(terror.ClassKV.New(allegrosql.ErrDupEntry, allegrosql.MyALLEGROSQLErrName[allegrosql.ErrDupEntry]), 0, 1)
    46  	// ErrNotImplemented returns when a function is not implemented yet.
    47  	ErrNotImplemented = terror.ClassKV.New(allegrosql.ErrNotImplemented, allegrosql.MyALLEGROSQLErrName[allegrosql.ErrNotImplemented])
    48  	// ErrWriteConflict is the error when the commit meets an write conflict error.
    49  	ErrWriteConflict = terror.ClassKV.New(allegrosql.ErrWriteConflict,
    50  		allegrosql.MyALLEGROSQLErrName[allegrosql.ErrWriteConflict]+" "+TxnRetryableMark)
    51  	// ErrWriteConflictInMilevaDB is the error when the commit meets an write conflict error when local latch is enabled.
    52  	ErrWriteConflictInMilevaDB = terror.ClassKV.New(allegrosql.ErrWriteConflictInMilevaDB,
    53  		allegrosql.MyALLEGROSQLErrName[allegrosql.ErrWriteConflictInMilevaDB]+" "+TxnRetryableMark)
    54  )
    55  
    56  // IsTxnRetryableError checks if the error could safely retry the transaction.
    57  func IsTxnRetryableError(err error) bool {
    58  	if err == nil {
    59  		return false
    60  	}
    61  
    62  	if ErrTxnRetryable.Equal(err) || ErrWriteConflict.Equal(err) || ErrWriteConflictInMilevaDB.Equal(err) {
    63  		return true
    64  	}
    65  
    66  	return false
    67  }
    68  
    69  // IsErrNotFound checks if err is a HoTT of NotFound error.
    70  func IsErrNotFound(err error) bool {
    71  	return ErrNotExist.Equal(err)
    72  }
    73  
    74  // GetDuplicateErrorHandleString is used to concat the handle columns data with '-'.
    75  // This is consistent with MyALLEGROSQL.
    76  func GetDuplicateErrorHandleString(handle Handle) string {
    77  	dt, err := handle.Data()
    78  	if err != nil {
    79  		return err.Error()
    80  	}
    81  	var sb strings.Builder
    82  	for i, d := range dt {
    83  		if i != 0 {
    84  			sb.WriteString("-")
    85  		}
    86  		s, err := d.ToString()
    87  		if err != nil {
    88  			return err.Error()
    89  		}
    90  		sb.WriteString(s)
    91  	}
    92  	return sb.String()
    93  }