github.com/matrixorigin/matrixone@v0.7.0/pkg/common/moerr/error.go (about) 1 // Copyright 2021 - 2022 Matrix Origin 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 package moerr 16 17 import ( 18 "bytes" 19 "context" 20 "encoding" 21 "encoding/gob" 22 "encoding/hex" 23 "fmt" 24 "io" 25 "sync/atomic" 26 27 "github.com/matrixorigin/matrixone/pkg/util/errutil" 28 "github.com/matrixorigin/matrixone/pkg/util/stack" 29 ) 30 31 const MySQLDefaultSqlState = "HY000" 32 33 const ( 34 // 0 - 99 is OK. They do not contain info, and are special handled 35 // using a static instance, no alloc. 36 Ok uint16 = 0 37 OkStopCurrRecur uint16 = 1 38 OkExpectedEOF uint16 = 2 // Expected End Of File 39 OkExpectedEOB uint16 = 3 // Expected End of Batch 40 OkExpectedDup uint16 = 4 // Expected Duplicate 41 42 OkExpectedPossibleDup uint16 = 5 // Expected Possible Duplicate 43 44 OkMax uint16 = 99 45 46 // 100 - 200 is Info 47 ErrInfo uint16 = 100 48 ErrLoadInfo uint16 = 101 49 50 // 100 - 200 is WARNING 51 ErrWarn uint16 = 200 52 // In some cases, for example, varchar(N), truncated is a warning instead error. 53 ErrWarnDataTruncated uint16 = 201 54 55 // Group 1: Internal errors 56 ErrStart uint16 = 20100 57 ErrInternal uint16 = 20101 58 ErrNYI uint16 = 20102 59 ErrOOM uint16 = 20103 60 ErrQueryInterrupted uint16 = 20104 61 ErrNotSupported uint16 = 20105 62 63 // Group 2: numeric and functions 64 ErrDivByZero uint16 = 20200 65 ErrOutOfRange uint16 = 20201 66 ErrDataTruncated uint16 = 20202 67 ErrInvalidArg uint16 = 20203 68 ErrTruncatedWrongValueForField uint16 = 20204 69 70 // Group 3: invalid input 71 ErrBadConfig uint16 = 20300 72 ErrInvalidInput uint16 = 20301 73 ErrSyntaxError uint16 = 20302 74 ErrParseError uint16 = 20303 75 ErrConstraintViolation uint16 = 20304 76 ErrDuplicate uint16 = 20305 77 ErrRoleGrantedToSelf uint16 = 20306 78 ErrDuplicateEntry uint16 = 20307 79 80 // Group 4: unexpected state and io errors 81 ErrInvalidState uint16 = 20400 82 ErrLogServiceNotReady uint16 = 20401 83 ErrBadDB uint16 = 20402 84 ErrNoSuchTable uint16 = 20403 85 ErrEmptyVector uint16 = 20404 86 ErrFileNotFound uint16 = 20405 87 ErrFileAlreadyExists uint16 = 20406 88 ErrUnexpectedEOF uint16 = 20407 89 ErrEmptyRange uint16 = 20408 90 ErrSizeNotMatch uint16 = 20409 91 ErrNoProgress uint16 = 20410 92 ErrInvalidPath uint16 = 20411 93 ErrShortWrite uint16 = 20412 94 ErrInvalidWrite uint16 = 20413 95 ErrShortBuffer uint16 = 20414 96 ErrNoDB uint16 = 20415 97 ErrNoWorkingStore uint16 = 20416 98 ErrNoHAKeeper uint16 = 20417 99 ErrInvalidTruncateLsn uint16 = 20418 100 ErrNotLeaseHolder uint16 = 20419 101 ErrDBAlreadyExists uint16 = 20420 102 ErrTableAlreadyExists uint16 = 20421 103 ErrNoService uint16 = 20422 104 ErrDupServiceName uint16 = 20423 105 ErrWrongService uint16 = 20424 106 ErrBadS3Config uint16 = 20425 107 ErrBadView uint16 = 20426 108 ErrInvalidTask uint16 = 20427 109 ErrInvalidServiceIndex uint16 = 20428 110 ErrDragonboatTimeout uint16 = 20429 111 ErrDragonboatTimeoutTooSmall uint16 = 20430 112 ErrDragonboatInvalidDeadline uint16 = 20431 113 ErrDragonboatRejected uint16 = 20432 114 ErrDragonboatInvalidPayloadSize uint16 = 20433 115 ErrDragonboatShardNotReady uint16 = 20434 116 ErrDragonboatSystemClosed uint16 = 20435 117 ErrDragonboatInvalidRange uint16 = 20436 118 ErrDragonboatShardNotFound uint16 = 20437 119 ErrDragonboatOtherSystemError uint16 = 20438 120 ErrDropNonExistsDB uint16 = 20439 121 ErrQueryIdNotFound uint16 = 20440 122 ErrFunctionAlreadyExists uint16 = 20441 123 ErrDropNonExistsFunction uint16 = 20442 124 ErrNoConfig uint16 = 20443 125 126 // Group 5: rpc timeout 127 // ErrRPCTimeout rpc timeout 128 ErrRPCTimeout uint16 = 20500 129 // ErrClientClosed rpc client closed 130 ErrClientClosed uint16 = 20501 131 // ErrBackendClosed backend closed 132 ErrBackendClosed uint16 = 20502 133 // ErrStreamClosed rpc stream closed 134 ErrStreamClosed uint16 = 20503 135 // ErrNoAvailableBackend no available backend 136 ErrNoAvailableBackend uint16 = 20504 137 138 // Group 6: txn 139 // ErrTxnAborted read and write a transaction that has been rolled back. 140 ErrTxnClosed uint16 = 20600 141 // ErrTxnWriteConflict write conflict error for concurrent transactions 142 ErrTxnWriteConflict uint16 = 20601 143 // ErrMissingTxn missing transaction error 144 ErrMissingTxn uint16 = 20602 145 // ErrUnresolvedConflict read transaction encounters unresolved data 146 ErrUnresolvedConflict uint16 = 20603 147 // ErrTxnError TxnError wrapper 148 ErrTxnError uint16 = 20604 149 // ErrDNShardNotFound DNShard not found, need to get the latest DN list from HAKeeper 150 ErrDNShardNotFound uint16 = 20605 151 ErrShardNotReported uint16 = 20606 152 // Generic TAE error 153 ErrTAEError uint16 = 20607 154 ErrTAERead uint16 = 20608 155 ErrRpcError uint16 = 20609 156 ErrWaitTxn uint16 = 20610 157 ErrTxnNotFound uint16 = 20611 158 ErrTxnNotActive uint16 = 20612 159 ErrTAEWrite uint16 = 20613 160 ErrTAECommit uint16 = 20614 161 ErrTAERollback uint16 = 20615 162 ErrTAEPrepare uint16 = 20616 163 ErrTAEPossibleDuplicate uint16 = 20617 164 ErrTxnRWConflict uint16 = 20618 165 ErrTxnWWConflict uint16 = 20619 166 ErrNotFound uint16 = 20620 167 ErrTxnInternal uint16 = 20621 168 ErrTxnReadConflict uint16 = 20622 169 ErrPrimaryKeyDuplicated uint16 = 20623 170 ErrAppendableSegmentNotFound uint16 = 20624 171 ErrAppendableBlockNotFound uint16 = 20625 172 ErrTAEDebug uint16 = 20626 173 ErrDuplicateKey uint16 = 20626 174 175 // Group 7: lock service 176 // ErrDeadLockDetected lockservice has detected a deadlock and should abort the transaction if it receives this error 177 ErrDeadLockDetected uint16 = 20701 178 179 // ErrEnd, the max value of MOErrorCode 180 ErrEnd uint16 = 65535 181 ) 182 183 type moErrorMsgItem struct { 184 mysqlCode uint16 185 sqlStates []string 186 errorMsgOrFormat string 187 } 188 189 var errorMsgRefer = map[uint16]moErrorMsgItem{ 190 // OK code not in this table. They do not have a mysql code, as 191 // they are OK -- should not leak back to client. 192 193 // Info 194 ErrInfo: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "info: %s"}, 195 ErrLoadInfo: {ER_LOAD_INFO, []string{MySQLDefaultSqlState}, "load info: %d, %d, %d, %d, %d"}, 196 197 // Warn 198 ErrWarn: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "warning: %s"}, 199 ErrWarnDataTruncated: {WARN_DATA_TRUNCATED, []string{MySQLDefaultSqlState}, "warning: data truncated"}, 200 201 // Group 1: Internal errors 202 ErrStart: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "internal error: error code start"}, 203 ErrInternal: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "internal error: %s"}, 204 ErrNYI: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "%s is not yet implemented"}, 205 ErrOOM: {ER_ENGINE_OUT_OF_MEMORY, []string{MySQLDefaultSqlState}, "error: out of memory"}, 206 ErrQueryInterrupted: {ER_QUERY_INTERRUPTED, []string{MySQLDefaultSqlState}, "query interrupted"}, 207 ErrNotSupported: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "not supported: %s"}, 208 209 // Group 2: numeric 210 ErrDivByZero: {ER_DIVISION_BY_ZERO, []string{MySQLDefaultSqlState}, "division by zero"}, 211 ErrOutOfRange: {ER_DATA_OUT_OF_RANGE, []string{MySQLDefaultSqlState}, "data out of range: data type %s, %s"}, 212 ErrDataTruncated: {ER_DATA_TOO_LONG, []string{MySQLDefaultSqlState}, "data truncated: data type %s, %s"}, 213 ErrInvalidArg: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "invalid argument %s, bad value %s"}, 214 ErrTruncatedWrongValueForField: {ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, []string{MySQLDefaultSqlState}, "truncated type %s value %s for column %s, %d"}, 215 216 // Group 3: invalid input 217 ErrBadConfig: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "invalid configuration: %s"}, 218 ErrInvalidInput: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "invalid input: %s"}, 219 ErrSyntaxError: {ER_SYNTAX_ERROR, []string{MySQLDefaultSqlState}, "SQL syntax error: %s"}, 220 ErrParseError: {ER_PARSE_ERROR, []string{MySQLDefaultSqlState}, "SQL parser error: %s"}, 221 ErrConstraintViolation: {ER_CHECK_CONSTRAINT_VIOLATED, []string{MySQLDefaultSqlState}, "constraint violation: %s"}, 222 ErrDuplicate: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "tae data: duplicate"}, 223 ErrRoleGrantedToSelf: {ER_ROLE_GRANTED_TO_ITSELF, []string{MySQLDefaultSqlState}, "cannot grant role %s to %s"}, 224 ErrDuplicateEntry: {ER_DUP_ENTRY, []string{MySQLDefaultSqlState}, "Duplicate entry '%s' for key '%s'"}, 225 226 // Group 4: unexpected state or file io error 227 ErrInvalidState: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "invalid state %s"}, 228 ErrLogServiceNotReady: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "log service not ready"}, 229 ErrBadDB: {ER_BAD_DB_ERROR, []string{MySQLDefaultSqlState}, "invalid database %s"}, 230 ErrNoSuchTable: {ER_NO_SUCH_TABLE, []string{MySQLDefaultSqlState}, "no such table %s.%s"}, 231 ErrEmptyVector: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "empty vector"}, 232 ErrFileNotFound: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "file %s is not found"}, 233 ErrFileAlreadyExists: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "file %s already exists"}, 234 ErrUnexpectedEOF: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "unexpected end of file %s"}, 235 ErrEmptyRange: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "empty range of file %s"}, 236 ErrSizeNotMatch: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "file %s size does not match"}, 237 ErrNoProgress: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "file %s has no io progress"}, 238 ErrInvalidPath: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "invalid file path %s"}, 239 ErrShortWrite: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "file %s io short write"}, 240 ErrInvalidWrite: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "file %s io invalid write"}, 241 ErrShortBuffer: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "file %s io short buffer"}, 242 ErrNoDB: {ER_NO_DB_ERROR, []string{MySQLDefaultSqlState}, "not connect to a database"}, 243 ErrNoWorkingStore: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "no working store"}, 244 ErrNoHAKeeper: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "cannot locate ha keeper"}, 245 ErrInvalidTruncateLsn: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "invalid truncate lsn, shard %d already truncated to %d"}, 246 ErrNotLeaseHolder: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "not lease holder, current lease holder ID %d"}, 247 ErrDBAlreadyExists: {ER_DB_CREATE_EXISTS, []string{MySQLDefaultSqlState}, "database %s already exists"}, 248 ErrTableAlreadyExists: {ER_TABLE_EXISTS_ERROR, []string{MySQLDefaultSqlState}, "table %s already exists"}, 249 ErrFunctionAlreadyExists: {ER_UDF_ALREADY_EXISTS, []string{MySQLDefaultSqlState}, "function %s already exists"}, 250 ErrDropNonExistsFunction: {ER_CANT_FIND_UDF, []string{MySQLDefaultSqlState}, "function %s doesn't exist"}, 251 ErrNoService: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "service %s not found"}, 252 ErrDupServiceName: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "duplicate service name %s"}, 253 ErrWrongService: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "wrong service, expecting %s, got %s"}, 254 ErrBadS3Config: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "bad s3 config: %s"}, 255 ErrBadView: {ER_VIEW_INVALID, []string{MySQLDefaultSqlState}, "invalid view '%s.%s'"}, 256 ErrInvalidTask: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "invalid task, task runner %s, id %d"}, 257 ErrInvalidServiceIndex: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "invalid service idx %d"}, 258 ErrDragonboatTimeout: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "%s"}, 259 ErrDragonboatTimeoutTooSmall: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "%s"}, 260 ErrDragonboatInvalidDeadline: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "%s"}, 261 ErrDragonboatRejected: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "%s"}, 262 ErrDragonboatInvalidPayloadSize: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "%s"}, 263 ErrDragonboatShardNotReady: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "%s"}, 264 ErrDragonboatSystemClosed: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "%s"}, 265 ErrDragonboatInvalidRange: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "%s"}, 266 ErrDragonboatShardNotFound: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "%s"}, 267 ErrDragonboatOtherSystemError: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "%s"}, 268 ErrDropNonExistsDB: {ER_DB_DROP_EXISTS, []string{MySQLDefaultSqlState}, "Can't drop database '%s'; database doesn't exist"}, 269 ErrQueryIdNotFound: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "query id %s is not found, or invalid tenant"}, 270 ErrNoConfig: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "no configure: %s"}, 271 // Group 5: rpc timeout 272 ErrRPCTimeout: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "rpc timeout"}, 273 ErrClientClosed: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "client closed"}, 274 ErrBackendClosed: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "backend closed"}, 275 ErrStreamClosed: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "stream closed"}, 276 ErrNoAvailableBackend: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "no available backend"}, 277 278 // Group 6: txn 279 ErrTxnClosed: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "the transaction %s has been committed or aborted"}, 280 ErrTxnWriteConflict: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "txn write conflict %s"}, 281 ErrMissingTxn: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "missing txn"}, 282 ErrUnresolvedConflict: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "unresolved conflict"}, 283 ErrTxnError: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "transaction error: %s"}, 284 ErrDNShardNotFound: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "dn shard uuid %s, id %d not found"}, 285 ErrShardNotReported: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "dn shard uuid %s, id %d not reported"}, 286 ErrTAEError: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "tae error %s"}, 287 ErrTAERead: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "tae read error"}, 288 ErrRpcError: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "rpc error"}, 289 ErrWaitTxn: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "txn wait error"}, 290 ErrTxnNotFound: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "txn not found"}, 291 ErrTxnNotActive: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "txn not active, state %s"}, 292 ErrTAEWrite: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "tae write error"}, 293 ErrTAECommit: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "tae commit error %s"}, 294 ErrTAERollback: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "tae rollback error %s"}, 295 ErrTAEPrepare: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "tae prepare error %s"}, 296 ErrTAEPossibleDuplicate: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "tae possible duplicate"}, 297 ErrTxnRWConflict: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "r-w conflict"}, 298 ErrTxnWWConflict: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "w-w conflict"}, 299 ErrNotFound: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "not found"}, 300 ErrTxnInternal: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "txn internal error"}, 301 ErrTxnReadConflict: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "txn read conflict %s"}, 302 ErrPrimaryKeyDuplicated: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "duplicated primary key %v"}, 303 ErrAppendableSegmentNotFound: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "appendable segment not found"}, 304 ErrAppendableBlockNotFound: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "appendable block not found"}, 305 ErrDuplicateKey: {ER_DUP_KEYNAME, []string{MySQLDefaultSqlState}, "duplicate key name '%s'"}, 306 307 // Group 7: lock service 308 ErrDeadLockDetected: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "deadlock detected"}, 309 310 // Group End: max value of MOErrorCode 311 ErrEnd: {ER_UNKNOWN_ERROR, []string{MySQLDefaultSqlState}, "internal error: end of errcode code"}, 312 } 313 314 func newError(ctx context.Context, code uint16, args ...any) *Error { 315 var err *Error 316 item, has := errorMsgRefer[code] 317 if !has { 318 panic(NewInternalError(ctx, "not exist MOErrorCode: %d", code)) 319 } 320 if len(args) == 0 { 321 err = &Error{ 322 code: code, 323 mysqlCode: item.mysqlCode, 324 message: item.errorMsgOrFormat, 325 sqlState: item.sqlStates[0], 326 } 327 } else { 328 err = &Error{ 329 code: code, 330 mysqlCode: item.mysqlCode, 331 message: fmt.Sprintf(item.errorMsgOrFormat, args...), 332 sqlState: item.sqlStates[0], 333 } 334 } 335 _ = errutil.WithContextWithDepth(ctx, err, 2) 336 return err 337 } 338 339 type Error struct { 340 code uint16 341 mysqlCode uint16 342 message string 343 sqlState string 344 } 345 346 func (e *Error) Error() string { 347 return e.message 348 } 349 350 func (e *Error) ErrorCode() uint16 { 351 return e.code 352 } 353 354 func (e *Error) MySQLCode() uint16 { 355 return e.mysqlCode 356 } 357 358 func (e *Error) SqlState() string { 359 return e.sqlState 360 } 361 362 type encodingErr struct { 363 Code uint16 364 MysqlCode uint16 365 Message string 366 SqlState string 367 } 368 369 var _ encoding.BinaryMarshaler = new(Error) 370 371 func (e *Error) MarshalBinary() ([]byte, error) { 372 ee := encodingErr{ 373 Code: e.code, 374 MysqlCode: e.mysqlCode, 375 Message: e.message, 376 SqlState: e.sqlState, 377 } 378 buf := new(bytes.Buffer) 379 if err := gob.NewEncoder(buf).Encode(ee); err != nil { 380 return nil, ConvertGoError(Context(), err) 381 } 382 return buf.Bytes(), nil 383 } 384 385 var _ encoding.BinaryUnmarshaler = new(Error) 386 387 func (e *Error) UnmarshalBinary(data []byte) error { 388 var ee encodingErr 389 if err := gob.NewDecoder(bytes.NewReader(data)).Decode(&ee); err != nil { 390 return ConvertGoError(Context(), err) 391 } 392 e.code = ee.Code 393 e.mysqlCode = ee.MysqlCode 394 e.message = ee.Message 395 e.sqlState = ee.SqlState 396 return nil 397 } 398 399 func IsMoErrCode(e error, rc uint16) bool { 400 if e == nil { 401 return rc == Ok 402 } 403 404 me, ok := e.(*Error) 405 if !ok { 406 // This is not a moerr 407 return false 408 } 409 return me.code == rc 410 } 411 412 // ConvertPanicError converts a runtime panic to internal error. 413 func ConvertPanicError(ctx context.Context, v interface{}) *Error { 414 if e, ok := v.(*Error); ok { 415 return e 416 } 417 return newError(ctx, ErrInternal, fmt.Sprintf("panic %v: %+v", v, stack.Callers(3))) 418 } 419 420 // ConvertGoError converts a go error into mo error. 421 // Note here we must return error, because nil error 422 // is the same as nil *Error -- Go strangeness. 423 func ConvertGoError(ctx context.Context, err error) error { 424 // nil is nil 425 if err == nil { 426 return err 427 } 428 429 // already a moerr, return it as is 430 if _, ok := err.(*Error); ok { 431 return err 432 } 433 434 // Convert a few well known os/go error. 435 if err == io.EOF || err == io.ErrUnexpectedEOF { 436 // if io.EOF reaches here, we believe it is not expected. 437 return NewUnexpectedEOF(ctx, err.Error()) 438 } 439 440 return NewInternalError(ctx, "convert go error to mo error %v", err) 441 } 442 443 func (e *Error) Succeeded() bool { 444 return e.code < OkMax 445 } 446 447 // Special handling of OK code. This code are not errors, but used to 448 // signal different success conditions. One user is StopCurrRecur. 449 // TAE use it to loop over memory data structures. They are tight, 450 // performance critical loops, so we cannot afford to new an Error and 451 // definitely not construct call stack and do logging. Note that exactly 452 // because of these, Ok code does not have any contextual info. It is 453 // just a code. 454 // 455 // For these, we have a local var, and caller can use GetOkXXX() to get 456 // *Error. The returned *Error can be tested with either 457 // 458 // if err == GetOkXXX() 459 // or if moerr.IsMoErrCode(err, moerr.OkXXX) 460 // 461 // They are both fast, one with less typing and the other is consistent 462 // with other error code checking. 463 var errOkStopCurrRecur = Error{OkStopCurrRecur, 0, "StopCurrRecur", "00000"} 464 var errOkExpectedEOF = Error{OkExpectedEOF, 0, "ExpectedEOF", "00000"} 465 var errOkExpectedEOB = Error{OkExpectedEOB, 0, "ExpectedEOB", "00000"} 466 var errOkExpectedDup = Error{OkExpectedDup, 0, "ExpectedDup", "00000"} 467 var errOkExpectedPossibleDup = Error{OkExpectedPossibleDup, 0, "OkExpectedPossibleDup", "00000"} 468 469 /* 470 GetOk is useless in general, should just use nil. 471 472 var errOk = Error{Ok, 0, "Succeeded", "00000"} 473 func GetOk() *Error { 474 return &errOk 475 } 476 */ 477 478 func GetOkStopCurrRecur() *Error { 479 return &errOkStopCurrRecur 480 } 481 482 func GetOkExpectedEOF() *Error { 483 return &errOkExpectedEOF 484 } 485 486 func GetOkExpectedEOB() *Error { 487 return &errOkExpectedEOB 488 } 489 490 func GetOkExpectedDup() *Error { 491 return &errOkExpectedDup 492 } 493 494 func GetOkExpectedPossibleDup() *Error { 495 return &errOkExpectedPossibleDup 496 } 497 498 func NewInfo(ctx context.Context, msg string) *Error { 499 return newError(ctx, ErrInfo, msg) 500 } 501 502 func NewLoadInfo(ctx context.Context, rec, del, skip, warn, writeTimeOut uint64) *Error { 503 return newError(ctx, ErrLoadInfo, rec, del, skip, warn, writeTimeOut) 504 } 505 506 func NewWarn(ctx context.Context, msg string) *Error { 507 return newError(ctx, ErrWarn, msg) 508 } 509 510 func NewBadS3Config(ctx context.Context, msg string) *Error { 511 return newError(ctx, ErrBadS3Config, msg) 512 } 513 514 func NewInternalError(ctx context.Context, msg string, args ...any) *Error { 515 xmsg := fmt.Sprintf(msg, args...) 516 return newError(ctx, ErrInternal, xmsg) 517 } 518 519 func NewNYI(ctx context.Context, msg string, args ...any) *Error { 520 xmsg := fmt.Sprintf(msg, args...) 521 return newError(ctx, ErrNYI, xmsg) 522 } 523 524 func NewNotSupported(ctx context.Context, msg string, args ...any) *Error { 525 xmsg := fmt.Sprintf(msg, args...) 526 return newError(ctx, ErrNotSupported, xmsg) 527 } 528 529 func NewOOM(ctx context.Context) *Error { 530 return newError(ctx, ErrOOM) 531 } 532 533 func NewQueryInterrupted(ctx context.Context) *Error { 534 return newError(ctx, ErrQueryInterrupted) 535 } 536 537 func NewDivByZero(ctx context.Context) *Error { 538 return newError(ctx, ErrDivByZero) 539 } 540 541 func NewOutOfRange(ctx context.Context, typ string, msg string, args ...any) *Error { 542 xmsg := fmt.Sprintf(msg, args...) 543 return newError(ctx, ErrOutOfRange, typ, xmsg) 544 } 545 546 func NewDataTruncated(ctx context.Context, typ string, msg string, args ...any) *Error { 547 xmsg := fmt.Sprintf(msg, args...) 548 return newError(ctx, ErrDataTruncated, typ, xmsg) 549 } 550 551 func NewInvalidArg(ctx context.Context, arg string, val any) *Error { 552 return newError(ctx, ErrInvalidArg, arg, fmt.Sprintf("%v", val)) 553 } 554 555 func NewTruncatedValueForField(ctx context.Context, t, v, c string, idx int) *Error { 556 return newError(ctx, ErrTruncatedWrongValueForField, t, v, c, idx) 557 } 558 559 func NewBadConfig(ctx context.Context, msg string, args ...any) *Error { 560 xmsg := fmt.Sprintf(msg, args...) 561 return newError(ctx, ErrBadConfig, xmsg) 562 } 563 564 func NewInvalidInput(ctx context.Context, msg string, args ...any) *Error { 565 xmsg := fmt.Sprintf(msg, args...) 566 return newError(ctx, ErrInvalidInput, xmsg) 567 } 568 569 func NewSyntaxError(ctx context.Context, msg string, args ...any) *Error { 570 xmsg := fmt.Sprintf(msg, args...) 571 return newError(ctx, ErrSyntaxError, xmsg) 572 } 573 574 func NewParseError(ctx context.Context, msg string, args ...any) *Error { 575 xmsg := fmt.Sprintf(msg, args...) 576 return newError(ctx, ErrParseError, xmsg) 577 } 578 579 func NewConstraintViolation(ctx context.Context, msg string, args ...any) *Error { 580 xmsg := fmt.Sprintf(msg, args...) 581 return newError(ctx, ErrConstraintViolation, xmsg) 582 } 583 584 func NewEmptyVector(ctx context.Context) *Error { 585 return newError(ctx, ErrEmptyVector) 586 } 587 588 func NewFileNotFound(ctx context.Context, f string) *Error { 589 return newError(ctx, ErrFileNotFound, f) 590 } 591 592 func NewQueryIdNotFound(ctx context.Context, f string) *Error { 593 return newError(ctx, ErrQueryIdNotFound, f) 594 } 595 596 func NewNoConfig(ctx context.Context, f string) *Error { 597 return newError(ctx, ErrNoConfig, f) 598 } 599 600 func NewFileAlreadyExists(ctx context.Context, f string) *Error { 601 return newError(ctx, ErrFileAlreadyExists, f) 602 } 603 604 func NewDBAlreadyExists(ctx context.Context, db string) *Error { 605 return newError(ctx, ErrDBAlreadyExists, db) 606 } 607 608 func NewTableAlreadyExists(ctx context.Context, t string) *Error { 609 return newError(ctx, ErrTableAlreadyExists, t) 610 } 611 612 func NewUnexpectedEOF(ctx context.Context, f string) *Error { 613 return newError(ctx, ErrUnexpectedEOF, f) 614 } 615 616 func NewEmptyRange(ctx context.Context, f string) *Error { 617 return newError(ctx, ErrEmptyRange, f) 618 } 619 620 func NewSizeNotMatch(ctx context.Context, f string) *Error { 621 return newError(ctx, ErrSizeNotMatch, f) 622 } 623 624 func NewNoProgress(ctx context.Context, f string) *Error { 625 return newError(ctx, ErrNoProgress, f) 626 } 627 628 func NewInvalidPath(ctx context.Context, f string) *Error { 629 return newError(ctx, ErrInvalidPath, f) 630 } 631 632 func NewInvalidState(ctx context.Context, msg string, args ...any) *Error { 633 xmsg := fmt.Sprintf(msg, args...) 634 return newError(ctx, ErrInvalidState, xmsg) 635 } 636 637 func NewInvalidTask(ctx context.Context, runner string, id uint64) *Error { 638 return newError(ctx, ErrInvalidTask, runner, id) 639 } 640 641 func NewInvalidServiceIndex(ctx context.Context, idx int) *Error { 642 return newError(ctx, ErrInvalidServiceIndex, idx) 643 } 644 645 func NewLogServiceNotReady(ctx context.Context) *Error { 646 return newError(ctx, ErrLogServiceNotReady) 647 } 648 649 func NewBadDB(ctx context.Context, name string) *Error { 650 return newError(ctx, ErrBadDB, name) 651 } 652 653 func NewNoDB(ctx context.Context) *Error { 654 return newError(ctx, ErrNoDB) 655 } 656 657 func NewNoWorkingStore(ctx context.Context) *Error { 658 return newError(ctx, ErrNoWorkingStore) 659 } 660 661 func NewNoService(ctx context.Context, name string) *Error { 662 return newError(ctx, ErrNoService, name) 663 } 664 665 func NewDupServiceName(ctx context.Context, name string) *Error { 666 return newError(ctx, ErrDupServiceName, name) 667 } 668 669 func NewWrongService(ctx context.Context, exp, got string) *Error { 670 return newError(ctx, ErrWrongService, exp, got) 671 } 672 673 func NewNoHAKeeper(ctx context.Context) *Error { 674 return newError(ctx, ErrNoHAKeeper) 675 } 676 677 func NewInvalidTruncateLsn(ctx context.Context, shardId, idx uint64) *Error { 678 return newError(ctx, ErrInvalidTruncateLsn, shardId, idx) 679 } 680 681 func NewNotLeaseHolder(ctx context.Context, holderId uint64) *Error { 682 return newError(ctx, ErrNotLeaseHolder, holderId) 683 } 684 685 func NewNoSuchTable(ctx context.Context, db, tbl string) *Error { 686 return newError(ctx, ErrNoSuchTable, db, tbl) 687 } 688 689 func NewBadView(ctx context.Context, db, v string) *Error { 690 return newError(ctx, ErrBadView, db, v) 691 } 692 693 func NewRPCTimeout(ctx context.Context) *Error { 694 return newError(ctx, ErrRPCTimeout) 695 } 696 697 func NewClientClosed(ctx context.Context) *Error { 698 return newError(ctx, ErrClientClosed) 699 } 700 701 func NewBackendClosed(ctx context.Context) *Error { 702 return newError(ctx, ErrBackendClosed) 703 } 704 705 func NewStreamClosed(ctx context.Context) *Error { 706 return newError(ctx, ErrStreamClosed) 707 } 708 709 func NewNoAvailableBackend(ctx context.Context) *Error { 710 return newError(ctx, ErrNoAvailableBackend) 711 } 712 713 func NewTxnClosed(ctx context.Context, txnID []byte) *Error { 714 id := "unknown" 715 if len(txnID) > 0 { 716 id = hex.EncodeToString(txnID) 717 } 718 return newError(ctx, ErrTxnClosed, id) 719 } 720 721 func NewTxnWriteConflict(ctx context.Context, msg string, args ...any) *Error { 722 xmsg := fmt.Sprintf(msg, args...) 723 return newError(ctx, ErrTxnWriteConflict, xmsg) 724 } 725 726 func NewMissingTxn(ctx context.Context) *Error { 727 return newError(ctx, ErrMissingTxn) 728 } 729 730 func NewUnresolvedConflict(ctx context.Context) *Error { 731 return newError(ctx, ErrUnresolvedConflict) 732 } 733 734 func NewTxnError(ctx context.Context, msg string, args ...any) *Error { 735 xmsg := fmt.Sprintf(msg, args...) 736 return newError(ctx, ErrTxnError, xmsg) 737 } 738 739 func NewTAEError(ctx context.Context, msg string, args ...any) *Error { 740 xmsg := fmt.Sprintf(msg, args...) 741 return newError(ctx, ErrTAEError, xmsg) 742 } 743 744 func NewDNShardNotFound(ctx context.Context, uuid string, id uint64) *Error { 745 return newError(ctx, ErrDNShardNotFound, uuid, id) 746 } 747 748 func NewShardNotReported(ctx context.Context, uuid string, id uint64) *Error { 749 return newError(ctx, ErrShardNotReported, uuid, id) 750 } 751 752 func NewDragonboatTimeout(ctx context.Context, msg string, args ...any) *Error { 753 xmsg := fmt.Sprintf(msg, args...) 754 return newError(ctx, ErrDragonboatTimeout, xmsg) 755 } 756 757 func NewDragonboatTimeoutTooSmall(ctx context.Context, msg string, args ...any) *Error { 758 xmsg := fmt.Sprintf(msg, args...) 759 return newError(ctx, ErrDragonboatTimeoutTooSmall, xmsg) 760 } 761 762 func NewDragonboatInvalidDeadline(ctx context.Context, msg string, args ...any) *Error { 763 xmsg := fmt.Sprintf(msg, args...) 764 return newError(ctx, ErrDragonboatInvalidDeadline, xmsg) 765 } 766 767 func NewDragonboatRejected(ctx context.Context, msg string, args ...any) *Error { 768 xmsg := fmt.Sprintf(msg, args...) 769 return newError(ctx, ErrDragonboatRejected, xmsg) 770 } 771 772 func NewDragonboatInvalidPayloadSize(ctx context.Context, msg string, args ...any) *Error { 773 xmsg := fmt.Sprintf(msg, args...) 774 return newError(ctx, ErrDragonboatInvalidPayloadSize, xmsg) 775 } 776 777 func NewDragonboatShardNotReady(ctx context.Context, msg string, args ...any) *Error { 778 xmsg := fmt.Sprintf(msg, args...) 779 return newError(ctx, ErrDragonboatShardNotReady, xmsg) 780 } 781 782 func NewDragonboatSystemClosed(ctx context.Context, msg string, args ...any) *Error { 783 xmsg := fmt.Sprintf(msg, args...) 784 return newError(ctx, ErrDragonboatSystemClosed, xmsg) 785 } 786 787 func NewDragonboatInvalidRange(ctx context.Context, msg string, args ...any) *Error { 788 xmsg := fmt.Sprintf(msg, args...) 789 return newError(ctx, ErrDragonboatInvalidRange, xmsg) 790 } 791 792 func NewDragonboatShardNotFound(ctx context.Context, msg string, args ...any) *Error { 793 xmsg := fmt.Sprintf(msg, args...) 794 return newError(ctx, ErrDragonboatShardNotFound, xmsg) 795 } 796 797 func NewDragonboatOtherSystemError(ctx context.Context, msg string, args ...any) *Error { 798 xmsg := fmt.Sprintf(msg, args...) 799 return newError(ctx, ErrDragonboatOtherSystemError, xmsg) 800 } 801 802 func NewErrDropNonExistsDB(ctx context.Context, name string) *Error { 803 return newError(ctx, ErrDropNonExistsDB, name) 804 } 805 806 func NewTAERead(ctx context.Context) *Error { 807 return newError(ctx, ErrTAERead) 808 } 809 810 func NewRpcError(ctx context.Context, msg string) *Error { 811 return newError(ctx, ErrRpcError, msg) 812 } 813 814 func NewWaitTxn(ctx context.Context) *Error { 815 return newError(ctx, ErrWaitTxn) 816 } 817 818 func NewTxnNotFound(ctx context.Context) *Error { 819 return newError(ctx, ErrTxnNotFound) 820 } 821 822 func NewTxnNotActive(ctx context.Context, st string) *Error { 823 return newError(ctx, ErrTxnNotActive, st) 824 } 825 826 func NewTAEWrite(ctx context.Context) *Error { 827 return newError(ctx, ErrTAEWrite) 828 } 829 830 func NewTAECommit(ctx context.Context, msg string, args ...any) *Error { 831 xmsg := fmt.Sprintf(msg, args...) 832 return newError(ctx, ErrTAECommit, xmsg) 833 } 834 835 func NewTAERollback(ctx context.Context, msg string, args ...any) *Error { 836 xmsg := fmt.Sprintf(msg, args...) 837 return newError(ctx, ErrTAERollback, xmsg) 838 } 839 840 func NewTAEPrepare(ctx context.Context, msg string, args ...any) *Error { 841 xmsg := fmt.Sprintf(msg, args...) 842 return newError(ctx, ErrTAEPrepare, xmsg) 843 } 844 845 func NewTAEPossibleDuplicate(ctx context.Context) *Error { 846 return newError(ctx, ErrTAEPossibleDuplicate) 847 } 848 849 func NewTxnRWConflict(ctx context.Context) *Error { 850 return newError(ctx, ErrTxnRWConflict) 851 } 852 853 func NewTxnWWConflict(ctx context.Context) *Error { 854 return newError(ctx, ErrTxnWWConflict) 855 } 856 857 func NewNotFound(ctx context.Context) *Error { 858 return newError(ctx, ErrNotFound) 859 } 860 861 func NewDuplicate(ctx context.Context) *Error { 862 return newError(ctx, ErrDuplicate) 863 } 864 865 func NewDuplicateEntry(ctx context.Context, entry string, key string) *Error { 866 return newError(ctx, ErrDuplicateEntry, entry, key) 867 } 868 869 func NewRoleGrantedToSelf(ctx context.Context, from, to string) *Error { 870 return newError(ctx, ErrRoleGrantedToSelf, from, to) 871 } 872 873 func NewTxnInternal(ctx context.Context) *Error { 874 return newError(ctx, ErrTxnInternal) 875 } 876 877 func NewTxnReadConflict(ctx context.Context, msg string, args ...any) *Error { 878 xmsg := fmt.Sprintf(msg, args...) 879 return newError(ctx, ErrTxnReadConflict, xmsg) 880 } 881 882 func NewPrimaryKeyDuplicated(ctx context.Context, k any) *Error { 883 return newError(ctx, ErrPrimaryKeyDuplicated, k) 884 } 885 886 func NewDuplicateKey(ctx context.Context, k string) *Error { 887 return newError(ctx, ErrDuplicateKey, k) 888 } 889 890 func NewAppendableSegmentNotFound(ctx context.Context) *Error { 891 return newError(ctx, ErrAppendableSegmentNotFound) 892 } 893 894 func NewAppendableBlockNotFound(ctx context.Context) *Error { 895 return newError(ctx, ErrAppendableBlockNotFound) 896 } 897 898 func NewDeadLockDetected(ctx context.Context) *Error { 899 return newError(ctx, ErrDeadLockDetected) 900 } 901 902 var contextFunc atomic.Value 903 904 func SetContextFunc(f func() context.Context) { 905 contextFunc.Store(f) 906 } 907 908 // Context should be trace.DefaultContext 909 func Context() context.Context { 910 return contextFunc.Load().(func() context.Context)() 911 } 912 913 func init() { 914 SetContextFunc(func() context.Context { return context.Background() }) 915 }