All Products
Search
Document Center

Tablestore:Error handling

Last Updated:Apr 24, 2024

Tablestore SDK for Go handles errors as exceptions. This topic describes the error handling methods and retry policies of Tablestore SDK for Go.

Methods

Tablestore SDK for Go handles errors as exceptions. If an operation does not throw an exception, the operation is successful. If an operation throws an exception, the operation fails.

Note

Batch operations such as BatchGetRow and BatchWriteRow are successful only when the system verifies that no exception is returned and the status of each row is successful.

Exceptions

When you use Tablestore SDK for Go, the err parameter is returned as the second parameter in the response. You must check whether a value is returned for the err parameter before you obtain the operation results.

If an error occurred at the server side, requestId is returned for the err parameter. requestId indicates the UUID that is used to identify the request. If the issue persists, record the value of the requestId parameter and submit a ticket.

The following sample code provides an error handling example:

client := tablestore.NewClient(endpoint, instanceName, accessKeyId, accessKeySecret)
listTables, err := client.ListTable()
if err != nil {
    // Handle exceptions. 
    fmt.Println(err.Error())
} else {
    // No exceptions. 
    for _, table := range listTables.TableNames {
        fmt.Println("TableName: ", table)
    }
}

Retries

Tablestore SDK for Go provides the default retry policy. You can also create custom retry policies.

Default retry policy

If throttling errors or internal server errors related to write operations occur, Tablestore SDK for Go automatically performs retries up to 10 times within 5 seconds with exponential backoff. You can configure tablestore.TableStoreConfig to change the default retry parameters. The following table describes the parameters.

Parameter

Description

Default value

RetryTimes

The maximum number of retries.

10

MaxRetryTime

The maximum time that the retried operation is allowed to run.

5s

DefaultRetryInterval

The jitter value of the exponential backoff retry policy. This prevents simultaneous retry requests sent from multiple clients.

10 ms

MaxRetryInterval

The maximum interval between two retries.

320 ms

Transport

The transport attribute for HTTP clients. Default value: nil.

If you specify this parameter, the HTTPTimeout.ConnectionTimeout, MaxIdleConnections, and IdleConnTimeout parameters do not take effect.

nil

HTTPTimeout.ConnectionTimeout

The maximum time for an HTTP connection to be established.

15s

HTTPTimeout.RequestTimeout

The time to wait for a response to come back from a server.

30s

MaxIdleConnections

The maximum number of idle connections.

2000

IdleConnTimeout

The maximum period of time during which a host remains idle in the connection pool.

25s

Custom retry logic

You can configure the following parameters in TableStoreClient to change the default retry logic or create custom retry logic. The following table describes the parameters.

Parameter

Description

Default value

CustomizedRetryFunc

If you specify CustomizedRetryFunc, the system determines whether to perform a retry operation based on the CustomizedRetryFunc configuration.

  • If you do not specify CustomizedRetryFunc, the default value nil is used. In this case, the system uses the default retry logic.

  • If you specify a value that indicates retry allowed for CustomizedRetryFunc, the system performs a retry operation based on the custom logic that you specified.

  • If you specify a value that indicates no retry for CustomizedRetryFunc, the system determines whether to perform a retry operation based on the value of KeepDefaultRetryStrategyWhileUsingCustomizedRetryFunc.

    • If you set KeepDefaultRetryStrategyWhileUsingCustomizedRetryFunc to false, no retry is performed.

    • If you set KeepDefaultRetryStrategyWhileUsingCustomizedRetryFunc to true, the system determines whether to perform a retry operation based on the default retry logic.

nil

KeepDefaultRetryStrategyWhileUsingCustomizedRetryFunc

true

Sample custom retry logic:

Retry all failed requests

Sample code for retrying all failed requests:

func alwaysRetry(errorCode string, errorMsg string, action string, httpStatus int) bool {
	return true
}
func main() {
    client := tablestore.NewClient(endpoint, instanceName, accessKeyId, accessKeySecret)
  	client.CustomizedRetryFunc = alwaysRetry
    // do something
}

Retry none of the failed requests

Sample code for retrying none of the failed requests:

func alwaysNotRetry(errorCode string, errorMsg string, action string, httpStatus int) bool {
	return false
}
func main() {
    client := tablestore.NewClient(endpoint, instanceName, accessKeyId, accessKeySecret)
  	client.CustomizedRetryFunc = alwaysNotRetry
    client.KeepDefaultRetryStrategyWhileUsingCustomizedRetryFunc = false
    // do something
}

Configure retry callbacks

To perform predefined operations during a retry, you can configure the following parameter in TableStoreClient. The following table describes the parameter.

Parameter

Description

Default value

RetryNotify

The callback that is triggered during a retry.

nil

The following sample code provides an example on how to set a traceID for each request and return the traceID upon a retry.

func userRetryNotify(traceId, requestId string, err error, action string, backoffDuration time.Duration) {
    // Configure the retry callback. 
    fmt.Println("Retry for traceId: " + traceId + ", timestamp: " + strconv.FormatInt(time.Now().UnixNano(), 10))
}

func alwaysRetry(errorCode string, errorMsg string, action string, httpStatus int) bool {
    return true
}

func main() {
    client := tablestore.NewClient(endpoint, instanceName, accessKeyId, accessKeySecret)
    client.CustomizedRetryFunc = alwaysRetry
    client.RetryNotify = userRetryNotify

    request := &tablestore.DescribeTableRequest{TableName: "tableNotExist"}
    // Set the traceID of the request. 
    request.ExtraRequestInfo.SetTraceID("test_TraceId_" + strconv.FormatInt(time.Now().UnixNano(), 10))
    // do something
    res, err := client.DescribeTable(request)

    if err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println(res.ResponseInfo.RequestId)
    }
}

Sample response:

Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097752655394000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097752683437000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097752708603000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097752760519000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097752814590000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097752916539000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097753110943000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097753454311000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097753798531000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097754165411000
OTSObjectNotExist Requested table does not exist. 0006143b-fdd6-5050-10ef-700b045590fc