All Products
Search
Document Center

MaxCompute:Return error logs

Last Updated:Mar 26, 2026

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 Odps client 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:

ParameterTypeDescription
eThrowableThe error that triggered the retry. Cast to OdpsException to access the request ID.
retryCountlongThe number of retries attempted so far.
retrySleepTimelongThe 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:

  1. Subclass RetryLogger and override onRetryLog.

  2. Register your logger by calling odps.getRestClient().setRetryLogger(...) when initializing the Odps client.

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.