Search in sources :

Example 1 with ExecuteRequest

use of io.atomix.protocols.backup.protocol.ExecuteRequest in project atomix by atomix.

the class PrimaryBackupProxy method execute.

private void execute(PrimitiveOperation operation, ComposableFuture<byte[]> future) {
    ExecuteRequest request = ExecuteRequest.request(descriptor, sessionId.id(), clusterService.getLocalNode().id(), operation);
    log.trace("Sending {} to {}", request, term.primary());
    PrimaryTerm term = this.term;
    if (term.primary() != null) {
        protocol.execute(term.primary().nodeId(), request).whenCompleteAsync((response, error) -> {
            if (error == null) {
                log.trace("Received {}", response);
                if (response.status() == Status.OK) {
                    future.complete(response.result());
                } else {
                    if (this.term.term() > term.term()) {
                        execute(operation).whenComplete(future);
                    } else {
                        primaryElection.getTerm().whenComplete((newTerm, termError) -> {
                            if (termError == null) {
                                if (newTerm.term() > term.term() && newTerm.primary() != null) {
                                    execute(operation).whenComplete(future);
                                } else {
                                    future.completeExceptionally(new PrimitiveException.Unavailable());
                                }
                            } else {
                                future.completeExceptionally(new PrimitiveException.Unavailable());
                            }
                        });
                    }
                }
            } else {
                future.completeExceptionally(error);
            }
        }, threadContext);
    } else {
        future.completeExceptionally(new ConnectException());
    }
}
Also used : ExecuteRequest(io.atomix.protocols.backup.protocol.ExecuteRequest) PrimaryTerm(io.atomix.primitive.partition.PrimaryTerm) PrimitiveException(io.atomix.primitive.PrimitiveException) ConnectException(java.net.ConnectException)

Aggregations

PrimitiveException (io.atomix.primitive.PrimitiveException)1 PrimaryTerm (io.atomix.primitive.partition.PrimaryTerm)1 ExecuteRequest (io.atomix.protocols.backup.protocol.ExecuteRequest)1 ConnectException (java.net.ConnectException)1