Java Code Examples for com.aliyun.openservices.log.exception.LogException#GetErrorCode

The following examples show how to use com.aliyun.openservices.log.exception.LogException#GetErrorCode . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SendProducerBatchTask.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
private Attempt buildAttempt(Exception e, long nowMs) {
  if (e instanceof LogException) {
    LogException logException = (LogException) e;
    return new Attempt(
        false,
        logException.GetRequestId(),
        logException.GetErrorCode(),
        logException.GetErrorMessage(),
        nowMs);
  } else {
    return new Attempt(false, "", Errors.PRODUCER_EXCEPTION, e.getMessage(), nowMs);
  }
}
 
Example 2
Source File: ShardConsumer.java    From aliyun-log-flink-connector with Apache License 2.0 4 votes vote down vote up
public void run() {
    try {
        LogstoreShardState state = fetcher.getShardState(subscribedShardStateIndex);
        final LogstoreShardMeta shardMeta = state.getShardMeta();
        final int shardId = shardMeta.getShardId();
        String logstore = shardMeta.getLogstore();
        String cursor = restoreCursorFromStateOrCheckpoint(logstore, state.getOffset(), shardId);
        LOG.info("Starting consumer for shard {} with initial cursor {}", shardId, cursor);
        while (isRunning()) {
            PullLogsResponse response;
            long fetchStartTimeMs = System.currentTimeMillis();
            try {
                response = logClient.pullLogs(logProject, logstore, shardId, cursor, fetchSize);
            } catch (LogException ex) {
                LOG.warn("Failed to pull logs, message: {}, shard: {}", ex.GetErrorMessage(), shardId);
                String errorCode = ex.GetErrorCode();
                if ("ShardNotExist".equals(errorCode)) {
                    // The shard has been deleted
                    LOG.warn("The shard {} already not exist, project {} logstore {}", shardId, logProject, logstore);
                    break;
                }
                if ("InvalidCursor".equalsIgnoreCase(errorCode)
                        && Consts.LOG_FROM_CHECKPOINT.equalsIgnoreCase(initialPosition)) {
                    LOG.warn("Got invalid cursor error, start from default position {}", defaultPosition);
                    cursor = findInitialCursor(logstore, defaultPosition, shardId);
                    continue;
                }
                throw ex;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Fetch request cost {} ms", System.currentTimeMillis() - fetchStartTimeMs);
            }
            if (response != null) {
                long processingTimeMs;
                String nextCursor = response.getNextCursor();
                if (response.getCount() > 0) {
                    long processingStartTimeMs = System.currentTimeMillis();
                    processRecordsAndSaveOffset(response.getLogGroups(), shardMeta, nextCursor);
                    processingTimeMs = System.currentTimeMillis() - processingStartTimeMs;
                    LOG.debug("Processing records of shard {} cost {} ms.", shardId, processingTimeMs);
                    cursor = nextCursor;
                } else {
                    processingTimeMs = 0;
                    LOG.debug("No records of shard {} has been responded.", shardId);
                    if (cursor.equals(nextCursor) && readOnly) {
                        LOG.info("Shard {} is finished", shardId);
                        break;
                    }
                }
                adjustFetchFrequency(response.getRawSize(), processingTimeMs);
            }
        }
    } catch (Throwable t) {
        LOG.error("Unexpected error", t);
        fetcher.stopWithError(t);
    }
}