Search in sources :

Example 1 with StoreAction

use of com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreAction in project incubator-hugegraph by apache.

the class StoreCommand method fromBytes.

public static StoreCommand fromBytes(byte[] bytes) {
    StoreType type = StoreType.valueOf(bytes[0]);
    StoreAction action = StoreAction.valueOf(bytes[1]);
    return new StoreCommand(type, action, bytes);
}
Also used : StoreType(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreType) StoreAction(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreAction)

Example 2 with StoreAction

use of com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreAction in project incubator-hugegraph by apache.

the class StoreStateMachine method onApplyFollower.

private Future<?> onApplyFollower(ByteBuffer data) {
    // Follower need to read mutation data
    byte[] bytes = data.array();
    // Let the backend thread do it directly
    return this.context.backendExecutor().submit(() -> {
        BytesBuffer buffer = LZ4Util.decompress(bytes, RaftSharedContext.BLOCK_SIZE);
        buffer.forReadWritten();
        StoreType type = StoreType.valueOf(buffer.read());
        StoreAction action = StoreAction.valueOf(buffer.read());
        try {
            return this.applyCommand(type, action, buffer, false);
        } catch (Throwable e) {
            String title = "Failed to execute backend command";
            LOG.error("{}: {}", title, action, e);
            throw new BackendException(title, e);
        }
    });
}
Also used : StoreType(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreType) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) StoreAction(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreAction) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 3 with StoreAction

use of com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreAction in project incubator-hugegraph by apache.

the class StoreCommandProcessor method parseStoreCommand.

private StoreCommand parseStoreCommand(StoreCommandRequest request) {
    StoreType type = request.getType();
    StoreAction action = request.getAction();
    byte[] data = request.getData().toByteArray();
    return new StoreCommand(type, action, data, true);
}
Also used : StoreType(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreType) StoreCommand(com.baidu.hugegraph.backend.store.raft.StoreCommand) StoreAction(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreAction)

Example 4 with StoreAction

use of com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreAction in project incubator-hugegraph by apache.

the class StoreStateMachine method onApplyLeader.

private Future<?> onApplyLeader(RaftStoreClosure closure) {
    // Leader just take the command out from the closure
    StoreCommand command = closure.command();
    BytesBuffer buffer = BytesBuffer.wrap(command.data());
    // The first two bytes are StoreType and StoreAction
    StoreType type = StoreType.valueOf(buffer.read());
    StoreAction action = StoreAction.valueOf(buffer.read());
    boolean forwarded = command.forwarded();
    // Let the producer thread to handle it, and wait for it
    CompletableFuture<Object> future = new CompletableFuture<>();
    closure.complete(Status.OK(), () -> {
        Object result;
        try {
            result = this.applyCommand(type, action, buffer, forwarded);
        } catch (Throwable e) {
            future.completeExceptionally(e);
            throw e;
        }
        future.complete(result);
        return result;
    });
    return future;
}
Also used : StoreType(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreType) CompletableFuture(java.util.concurrent.CompletableFuture) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) StoreAction(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreAction)

Aggregations

StoreAction (com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreAction)4 StoreType (com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreType)4 BytesBuffer (com.baidu.hugegraph.backend.serializer.BytesBuffer)2 BackendException (com.baidu.hugegraph.backend.BackendException)1 StoreCommand (com.baidu.hugegraph.backend.store.raft.StoreCommand)1 CompletableFuture (java.util.concurrent.CompletableFuture)1