Java Code Examples for org.elasticsearch.rest.RestStatus#CONFLICT

The following examples show how to use org.elasticsearch.rest.RestStatus#CONFLICT . 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: ElasticsearchLockProvider.java    From ShedLock with Apache License 2.0 5 votes vote down vote up
@Override
@NonNull
public Optional<SimpleLock> lock(@NonNull LockConfiguration lockConfiguration) {
    try {
        Map<String, Object> lockObject = lockObject(lockConfiguration.getName(),
            lockConfiguration.getLockAtMostUntil(),
            now());
        UpdateRequest ur = new UpdateRequest()
            .index(index)
            .type(type)
            .id(lockConfiguration.getName())
            .script(new Script(ScriptType.INLINE,
                "painless",
                UPDATE_SCRIPT,
                lockObject)
            )
            .upsert(lockObject)
            .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        UpdateResponse res = highLevelClient.update(ur, RequestOptions.DEFAULT);
        if (res.getResult() != DocWriteResponse.Result.NOOP) {
            return Optional.of(new ElasticsearchSimpleLock(lockConfiguration));
        } else {
            return Optional.empty();
        }
    } catch (IOException | ElasticsearchException e) {
        if (e instanceof ElasticsearchException && ((ElasticsearchException) e).status() == RestStatus.CONFLICT) {
            return Optional.empty();
        } else {
            throw new LockException("Unexpected exception occurred", e);
        }
    }
}
 
Example 2
Source File: TransportLeaderShardIngestAction.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
private void performOnLeader(final ShardRouting shard, int quorum, ClusterState clusterState) {
    try {
        LeaderOperationRequest leaderOperationRequest = new LeaderOperationRequest(System.currentTimeMillis(), shard.id(), request);
        IngestLeaderShardResponse response = shardOperationOnLeader(clusterState, quorum, leaderOperationRequest);
        listener.onResponse(response);
    } catch (Throwable e) {
        logger.error(e.getMessage(), e);
        if (retryLeaderException(e)) {
            leaderOperationStarted.set(false);
            if (logger.isTraceEnabled()) {
                logger.trace("error while performing operation on leader {}, scheduling a retry", e);
            }
            retry(e);
            return;
        }
        if (e instanceof ElasticsearchException && ((ElasticsearchException) e).status() == RestStatus.CONFLICT) {
            if (logger.isTraceEnabled()) {
                logger.trace(shard.shortSummary() + ": failed to execute [" + request + "]", e);
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug(shard.shortSummary() + ": failed to execute [" + request + "]", e);
            }
        }
        listener.onFailure(e);
    }
}
 
Example 3
Source File: TransportBaseSQLAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Create a {@link io.crate.action.sql.SQLActionException} out of a {@link java.lang.Throwable}.
 * If concrete {@link org.elasticsearch.ElasticsearchException} is found, first transform it
 * to a {@link io.crate.exceptions.CrateException}
 */
private SQLActionException buildSQLActionException(Throwable e) {
    logger.error("errors while processing sql", e);
    if (e instanceof SQLActionException) {
        return (SQLActionException) e;
    }
    e = esToCrateException(e);

    int errorCode = 5000;
    RestStatus restStatus = RestStatus.INTERNAL_SERVER_ERROR;
    if (e instanceof CrateException) {
        CrateException crateException = (CrateException) e;
        if (e instanceof ValidationException) {
            errorCode = 4000 + crateException.errorCode();
            restStatus = RestStatus.BAD_REQUEST;
        } else if (e instanceof NoPermissionException) {
            errorCode = 4000 + crateException.errorCode();
            restStatus = RestStatus.UNAUTHORIZED;
            e.setStackTrace(new StackTraceElement[0]);
        } else if (e instanceof ForbiddenException) {
            errorCode = 4030 + crateException.errorCode();
            restStatus = RestStatus.FORBIDDEN;
        } else if (e instanceof ResourceUnknownException) {
            errorCode = 4040 + crateException.errorCode();
            restStatus = RestStatus.NOT_FOUND;
        } else if (e instanceof ConflictException) {
            errorCode = 4090 + crateException.errorCode();
            restStatus = RestStatus.CONFLICT;
        } else if (e instanceof UnhandledServerException) {
            errorCode = 5000 + crateException.errorCode();
        }
    } else if (e instanceof ParsingException) {
        errorCode = 4000;
        restStatus = RestStatus.BAD_REQUEST;
    } else if (e instanceof MapperParsingException) {
        errorCode = 4000;
        restStatus = RestStatus.BAD_REQUEST;
    }

    String message = e.getMessage();
    if (message == null) {
        if (e instanceof CrateException && e.getCause() != null) {
            e = e.getCause();   // use cause because it contains a more meaningful error in most cases
        }
        StackTraceElement[] stackTraceElements = e.getStackTrace();
        if (stackTraceElements.length > 0) {
            message = String.format(Locale.ENGLISH, "%s in %s", e.getClass().getSimpleName(), stackTraceElements[0]);
        } else {
            message = "Error in " + e.getClass().getSimpleName();
        }
    } else {
        message = e.getClass().getSimpleName() + ": " + message;
    }
    return new SQLActionException(message, errorCode, restStatus, e.getStackTrace());
}
 
Example 4
Source File: DocumentAlreadyExistsException.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public RestStatus status() {
    return RestStatus.CONFLICT;
}
 
Example 5
Source File: VersionConflictEngineException.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public RestStatus status() {
    return RestStatus.CONFLICT;
}
 
Example 6
Source File: SyncedFlushResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public RestStatus restStatus() {
    return failedShards() == 0 ? RestStatus.OK : RestStatus.CONFLICT;
}
 
Example 7
Source File: VersionConflictEngineException.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public RestStatus status() {
    return RestStatus.CONFLICT;
}