When a REST request fails, MaxCompute SDK for Java automatically retries the request. By default, no retry events are logged. To capture error details such as request IDs, retry counts, and wait intervals for troubleshooting, implement a custom RetryLogger.
Prerequisites
Before you begin, ensure that you have:
MaxCompute SDK for Java added to your project dependencies
A configured
Odpsclient instance
How it works
RetryLogger is an abstract class provided by MaxCompute SDK for Java. Subclass it and override onRetryLog to define your logging logic. RestClient calls onRetryLog each time it retries a failed request.
public static abstract class RetryLogger {
/**
* The callback function that enables RestClient to retry after an error occurs.
* @param e The error.
* @param retryCount The number of retries.
* @param retrySleepTime The retry interval.
*/
public abstract void onRetryLog(Throwable e, long retryCount, long retrySleepTime);
}onRetryLog receives three parameters:
| Parameter | Type | Description |
|---|---|---|
e | Throwable | The error that triggered the retry. Cast to OdpsException to access the request ID. |
retryCount | long | The number of retries attempted so far. |
retrySleepTime | long | The interval in seconds before the next retry. |
For the full API reference, see SDK Java Doc.
Implement a custom retry logger
To log retry events:
Subclass
RetryLoggerand overrideonRetryLog.Register your logger by calling
odps.getRestClient().setRetryLogger(...)when initializing theOdpsclient.
The following example implements UserRetryLogger, which writes a warning to stderr on each retry. When the error is an OdpsException with a request ID, the log includes the request ID for easier tracing.
// Register the custom logger when initializing the MaxCompute client
odps.getRestClient().setRetryLogger(new UserRetryLogger());
public class UserRetryLogger extends RetryLogger {
@Override
public void onRetryLog(Throwable e, long retryCount, long sleepTime) {
if (e != null && e instanceof OdpsException) {
String requestId = ((OdpsException) e).getRequestId();
if (requestId != null) {
System.err.println(String.format(
"Warning: ODPS request failed, requestID:%s, retryCount:%d, will retry in %d seconds.",
requestId, retryCount, sleepTime));
return;
}
}
System.err.println(String.format(
"Warning: ODPS request failed:%s, retryCount:%d, will retry in %d seconds.",
e.getMessage(), retryCount, sleepTime));
}
}The logger writes output to stderr in one of two formats:
When the error has a request ID:
Warning: ODPS request failed, requestID:%s, retryCount:%d, will retry in %d seconds.When no request ID is available:
Warning: ODPS request failed:%s, retryCount:%d, will retry in %d seconds.