io.atomix.protocols.raft.protocol.QueryRequest Java Examples

The following examples show how to use io.atomix.protocols.raft.protocol.QueryRequest. 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: PassiveRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Forwards the query to the leader.
 */
private CompletableFuture<QueryResponse> queryForward(QueryRequest request) {
  if (raft.getLeader() == null) {
    return CompletableFuture.completedFuture(logResponse(QueryResponse.builder()
        .withStatus(RaftResponse.Status.ERROR)
        .withError(RaftError.Type.NO_LEADER)
        .build()));
  }

  log.trace("Forwarding {}", request);
  return forward(request, raft.getProtocol()::query)
      .exceptionally(error -> QueryResponse.builder()
          .withStatus(RaftResponse.Status.ERROR)
          .withError(RaftError.Type.NO_LEADER)
          .build())
      .thenApply(this::logResponse);
}
 
Example #2
Source File: RaftSessionInvokerTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Tests submitting a query to the cluster.
 */
@Test
public void testSubmitQuery() throws Throwable {
  RaftSessionConnection connection = mock(RaftSessionConnection.class);
  when(connection.query(any(QueryRequest.class)))
      .thenReturn(CompletableFuture.completedFuture(QueryResponse.builder()
          .withStatus(RaftResponse.Status.OK)
          .withIndex(10)
          .withResult("Hello world!".getBytes())
          .build()));

  RaftSessionState state = new RaftSessionState("test", SessionId.from(1), UUID.randomUUID().toString(), TestPrimitiveType.instance(), 1000);
  RaftSessionManager manager = mock(RaftSessionManager.class);
  ThreadContext threadContext = new TestContext();

  RaftSessionInvoker submitter = new RaftSessionInvoker(mock(RaftSessionConnection.class), connection, state, new RaftSessionSequencer(state), manager, threadContext);
  assertTrue(Arrays.equals(submitter.invoke(operation(QUERY)).get(), "Hello world!".getBytes()));
  assertEquals(10, state.getResponseIndex());
}
 
Example #3
Source File: RaftSessionConnection.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a query request to the given node.
 *
 * @param request the request to send
 * @return a future to be completed with the response
 */
public CompletableFuture<QueryResponse> query(QueryRequest request) {
  CompletableFuture<QueryResponse> future = new CompletableFuture<>();
  if (context.isCurrentContext()) {
    sendRequest(request, protocol::query, future);
  } else {
    context.execute(() -> sendRequest(request, protocol::query, future));
  }
  return future;
}
 
Example #4
Source File: RaftSessionInvoker.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Submits a query to the cluster.
 */
private void invokeQuery(PrimitiveOperation operation, CompletableFuture<byte[]> future) {
  QueryRequest request = QueryRequest.builder()
      .withSession(state.getSessionId().id())
      .withSequence(state.getCommandRequest())
      .withOperation(operation)
      .withIndex(Math.max(state.getResponseIndex(), state.getEventIndex()))
      .build();
  invokeQuery(request, future);
}
 
Example #5
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> onQuery(QueryRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(QueryResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #6
Source File: RaftSessionInvokerTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Tests skipping over a failed query attempt.
 */
@Test
public void testSkippingOverFailedQuery() throws Throwable {
  CompletableFuture<QueryResponse> future1 = new CompletableFuture<>();
  CompletableFuture<QueryResponse> future2 = new CompletableFuture<>();

  RaftSessionConnection connection = mock(RaftSessionConnection.class);
  Mockito.when(connection.query(any(QueryRequest.class)))
      .thenReturn(future1)
      .thenReturn(future2);

  RaftSessionState state = new RaftSessionState("test", SessionId.from(1), UUID.randomUUID().toString(), TestPrimitiveType.instance(), 1000);
  RaftSessionManager manager = mock(RaftSessionManager.class);
  ThreadContext threadContext = new TestContext();

  RaftSessionInvoker submitter = new RaftSessionInvoker(mock(RaftSessionConnection.class), connection, state, new RaftSessionSequencer(state), manager, threadContext);

  CompletableFuture<byte[]> result1 = submitter.invoke(operation(QUERY));
  CompletableFuture<byte[]> result2 = submitter.invoke(operation(QUERY));

  assertEquals(1, state.getResponseIndex());

  assertFalse(result1.isDone());
  assertFalse(result2.isDone());

  future1.completeExceptionally(new RaftException.QueryFailure("failure"));
  future2.complete(QueryResponse.builder()
      .withStatus(RaftResponse.Status.OK)
      .withIndex(10)
      .withResult("Hello world!".getBytes())
      .build());

  assertTrue(result1.isCompletedExceptionally());
  assertTrue(result2.isDone());
  assertTrue(Arrays.equals(result2.get(), "Hello world!".getBytes()));

  assertEquals(10, state.getResponseIndex());
}
 
Example #7
Source File: RaftSessionInvokerTest.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Tests resequencing a query response.
 */
@Test
public void testResequenceQuery() throws Throwable {
  CompletableFuture<QueryResponse> future1 = new CompletableFuture<>();
  CompletableFuture<QueryResponse> future2 = new CompletableFuture<>();

  RaftSessionConnection connection = mock(RaftSessionConnection.class);
  Mockito.when(connection.query(any(QueryRequest.class)))
      .thenReturn(future1)
      .thenReturn(future2);

  RaftSessionState state = new RaftSessionState("test", SessionId.from(1), UUID.randomUUID().toString(), TestPrimitiveType.instance(), 1000);
  RaftSessionManager manager = mock(RaftSessionManager.class);
  ThreadContext threadContext = new TestContext();

  RaftSessionInvoker submitter = new RaftSessionInvoker(mock(RaftSessionConnection.class), connection, state, new RaftSessionSequencer(state), manager, threadContext);

  CompletableFuture<byte[]> result1 = submitter.invoke(operation(QUERY));
  CompletableFuture<byte[]> result2 = submitter.invoke(operation(QUERY));

  future2.complete(QueryResponse.builder()
      .withStatus(RaftResponse.Status.OK)
      .withIndex(10)
      .withResult("Hello world again!".getBytes())
      .build());

  assertEquals(1, state.getResponseIndex());

  assertFalse(result1.isDone());
  assertFalse(result2.isDone());

  future1.complete(QueryResponse.builder()
      .withStatus(RaftResponse.Status.OK)
      .withIndex(9)
      .withResult("Hello world!".getBytes())
      .build());

  assertTrue(result1.isDone());
  assertTrue(Arrays.equals(result1.get(), "Hello world!".getBytes()));
  assertTrue(result2.isDone());
  assertTrue(Arrays.equals(result2.get(), "Hello world again!".getBytes()));

  assertEquals(10, state.getResponseIndex());
}
 
Example #8
Source File: RaftServerCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return sendAndReceive(context.querySubject, request, memberId);
}
 
Example #9
Source File: RaftServerCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerQueryHandler(Function<QueryRequest, CompletableFuture<QueryResponse>> handler) {
  clusterCommunicator.subscribe(context.querySubject, serializer::decode, handler, serializer::encode);
}
 
Example #10
Source File: RaftClientCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return sendAndReceive(context.querySubject, request, memberId);
}
 
Example #11
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerQueryHandler(Function<QueryRequest, CompletableFuture<QueryResponse>> handler) {
  this.queryHandler = handler;
}
 
Example #12
Source File: RaftSessionInvokerTest.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that the client's session is expired when an UnknownSessionException is received from the cluster.
 */
@Test
public void testExpireSessionOnQueryFailure() throws Throwable {
  CompletableFuture<QueryResponse> future = new CompletableFuture<>();

  RaftSessionConnection connection = mock(RaftSessionConnection.class);
  Mockito.when(connection.query(any(QueryRequest.class)))
      .thenReturn(future);

  RaftSessionState state = new RaftSessionState("test", SessionId.from(1), UUID.randomUUID().toString(), TestPrimitiveType.instance(), 1000);
  RaftSessionManager manager = mock(RaftSessionManager.class);
  ThreadContext threadContext = new TestContext();

  RaftSessionInvoker submitter = new RaftSessionInvoker(mock(RaftSessionConnection.class), connection, state, new RaftSessionSequencer(state), manager, threadContext);

  CompletableFuture<byte[]> result = submitter.invoke(operation(QUERY));

  assertEquals(1, state.getResponseIndex());

  assertFalse(result.isDone());

  future.completeExceptionally(new RaftException.UnknownSession("unknown session"));

  assertTrue(result.isCompletedExceptionally());
}
 
Example #13
Source File: RaftClientMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return sendAndReceive(memberId, "query", request);
}
 
Example #14
Source File: LocalRaftClientProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return getServer(memberId).thenCompose(protocol -> protocol.query(encode(request))).thenApply(this::decode);
}
 
Example #15
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return sendAndReceive(memberId, "query", request);
}
 
Example #16
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerQueryHandler(Function<QueryRequest, CompletableFuture<QueryResponse>> handler) {
  registerHandler("query", handler);
}
 
Example #17
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return getServer(memberId).thenCompose(listener -> listener.query(encode(request))).thenApply(this::decode);
}
 
Example #18
Source File: RaftClientMessagingProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return sendAndReceive(memberId, "query", request);
}
 
Example #19
Source File: RaftSessionInvoker.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
protected OperationAttempt<QueryRequest, QueryResponse> next() {
  return new QueryAttempt(sequence, this.attempt + 1, request, future);
}
 
Example #20
Source File: RaftSessionInvoker.java    From atomix with Apache License 2.0 4 votes vote down vote up
QueryAttempt(long sequence, int attempt, QueryRequest request, CompletableFuture<byte[]> future) {
  super(sequence, attempt, request, future);
}
 
Example #21
Source File: RaftSessionInvoker.java    From atomix with Apache License 2.0 4 votes vote down vote up
QueryAttempt(long sequence, QueryRequest request, CompletableFuture<byte[]> future) {
  super(sequence, 1, request, future);
}
 
Example #22
Source File: RaftSessionInvoker.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Submits a query request to the cluster.
 */
private void invokeQuery(QueryRequest request, CompletableFuture<byte[]> future) {
  invoke(new QueryAttempt(sequencer.nextRequest(), request, future));
}
 
Example #23
Source File: LocalRaftServerProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void registerQueryHandler(Function<QueryRequest,
    CompletableFuture<QueryResponse>> handler) {
  this.queryHandler = handler;
}
 
Example #24
Source File: LocalRaftServerProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return getServer(memberId).thenCompose(listener ->
      listener.query(encode(request))).thenApply(this::decode);
}
 
Example #25
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void registerQueryHandler(Function<QueryRequest,
    CompletableFuture<QueryResponse>> handler) {
  registerHandler("query", handler);
}
 
Example #26
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return sendAndReceive(memberId, "query", request);
}
 
Example #27
Source File: LocalRaftClientProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return getServer(memberId).thenCompose(protocol ->
      protocol.query(encode(request))).thenApply(this::decode);
}
 
Example #28
Source File: RaftClientMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return sendAndReceive(memberId, "query", request);
}
 
Example #29
Source File: LocalRaftServerProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public void registerQueryHandler(Function<QueryRequest,
    CompletableFuture<QueryResponse>> handler) {
  this.queryHandler = handler;
}
 
Example #30
Source File: LocalRaftServerProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return getServer(memberId).thenCompose(listener ->
      listener.query(encode(request))).thenApply(this::decode);
}