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

The following examples show how to use io.atomix.protocols.raft.protocol.PublishRequest. 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: RaftSessionSequencerTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Tests sequencing an event that arrives before a command response.
 */
@Test
public void testSequenceEventAbsentCommand() throws Throwable {
  RaftSessionSequencer sequencer = new RaftSessionSequencer(new RaftSessionState("test", SessionId.from(1), UUID.randomUUID().toString(), TestPrimitiveType.instance(), 1000));

  PublishRequest request1 = PublishRequest.builder()
      .withSession(1)
      .withEventIndex(2)
      .withPreviousIndex(0)
      .withEvents(Collections.emptyList())
      .build();

  PublishRequest request2 = PublishRequest.builder()
      .withSession(1)
      .withEventIndex(3)
      .withPreviousIndex(2)
      .withEvents(Collections.emptyList())
      .build();

  AtomicInteger run = new AtomicInteger();
  sequencer.sequenceEvent(request1, () -> assertEquals(0, run.getAndIncrement()));
  sequencer.sequenceEvent(request2, () -> assertEquals(1, run.getAndIncrement()));
  assertEquals(2, run.get());
}
 
Example #2
Source File: RaftSessionSequencerTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Tests sequencing an event that arrives before a command response.
 */
@Test
public void testSequenceEventAtCommand() throws Throwable {
  RaftSessionSequencer sequencer = new RaftSessionSequencer(new RaftSessionState("test", SessionId.from(1), UUID.randomUUID().toString(), TestPrimitiveType.instance(), 1000));
  long sequence = sequencer.nextRequest();

  PublishRequest request = PublishRequest.builder()
      .withSession(1)
      .withEventIndex(2)
      .withPreviousIndex(0)
      .withEvents(Collections.emptyList())
      .build();

  CommandResponse response = CommandResponse.builder()
      .withStatus(RaftResponse.Status.OK)
      .withIndex(2)
      .withEventIndex(2)
      .build();

  AtomicInteger run = new AtomicInteger();
  sequencer.sequenceResponse(sequence, response, () -> assertEquals(1, run.getAndIncrement()));
  sequencer.sequenceEvent(request, () -> assertEquals(0, run.getAndIncrement()));
  assertEquals(2, run.get());
}
 
Example #3
Source File: RaftSession.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Sends an event to the session.
 */
private void sendEvents(EventHolder event) {
  // Only send events to the client if this server is the leader.
  if (server.isLeader()) {
    eventExecutor.execute(() -> {
      PublishRequest request = PublishRequest.builder()
          .withSession(sessionId().id())
          .withEventIndex(event.eventIndex)
          .withPreviousIndex(event.previousIndex)
          .withEvents(event.events)
          .build();

      log.trace("Sending {}", request);
      protocol.publish(memberId(), request);
    });
  }
}
 
Example #4
Source File: RaftSessionSequencerTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Tests sequencing an event that arrives before a command response.
 */
@Test
public void testSequenceEventAfterCommand() throws Throwable {
  RaftSessionSequencer sequencer = new RaftSessionSequencer(new RaftSessionState("test", SessionId.from(1), UUID.randomUUID().toString(), TestPrimitiveType.instance(), 1000));
  long sequence = sequencer.nextRequest();

  PublishRequest request = PublishRequest.builder()
      .withSession(1)
      .withEventIndex(1)
      .withPreviousIndex(0)
      .withEvents(Collections.emptyList())
      .build();

  CommandResponse response = CommandResponse.builder()
      .withStatus(RaftResponse.Status.OK)
      .withIndex(2)
      .withEventIndex(1)
      .build();

  AtomicInteger run = new AtomicInteger();
  sequencer.sequenceResponse(sequence, response, () -> assertEquals(0, run.getAndIncrement()));
  sequencer.sequenceEvent(request, () -> assertEquals(1, run.getAndIncrement()));
  assertEquals(2, run.get());
}
 
Example #5
Source File: RaftSessionSequencerTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Tests sequencing an event that arrives before a command response.
 */
@Test
public void testSequenceEventBeforeCommand() throws Throwable {
  RaftSessionSequencer sequencer = new RaftSessionSequencer(new RaftSessionState("test", SessionId.from(1), UUID.randomUUID().toString(), TestPrimitiveType.instance(), 1000));
  long sequence = sequencer.nextRequest();

  PublishRequest request = PublishRequest.builder()
      .withSession(1)
      .withEventIndex(1)
      .withPreviousIndex(0)
      .withEvents(Collections.emptyList())
      .build();

  CommandResponse response = CommandResponse.builder()
      .withStatus(RaftResponse.Status.OK)
      .withIndex(2)
      .withEventIndex(1)
      .build();

  AtomicInteger run = new AtomicInteger();
  sequencer.sequenceEvent(request, () -> assertEquals(0, run.getAndIncrement()));
  sequencer.sequenceResponse(sequence, response, () -> assertEquals(1, run.getAndIncrement()));
  assertEquals(2, run.get());
}
 
Example #6
Source File: RaftClientMessagingProtocol.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public void registerPublishListener(SessionId sessionId, Consumer<PublishRequest> listener,
                                    Executor executor) {
  messagingService.registerHandler(String.format("publish-%d", sessionId.id()), (e, p) -> {
    listener.accept(serializer.decode(p));
  }, executor);
}
 
Example #7
Source File: RaftSessionSequencerTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Tests sequencing an event that arrives before a command response.
 */
@Test
public void testSequenceEventAfterAllCommands() throws Throwable {
  RaftSessionSequencer sequencer = new RaftSessionSequencer(new RaftSessionState("test", SessionId.from(1), UUID.randomUUID().toString(), TestPrimitiveType.instance(), 1000));
  long sequence = sequencer.nextRequest();

  PublishRequest request1 = PublishRequest.builder()
      .withSession(1)
      .withEventIndex(2)
      .withPreviousIndex(0)
      .withEvents(Collections.emptyList())
      .build();

  PublishRequest request2 = PublishRequest.builder()
      .withSession(1)
      .withEventIndex(3)
      .withPreviousIndex(2)
      .withEvents(Collections.emptyList())
      .build();

  CommandResponse response = CommandResponse.builder()
      .withStatus(RaftResponse.Status.OK)
      .withIndex(2)
      .withEventIndex(2)
      .build();

  AtomicInteger run = new AtomicInteger();
  sequencer.sequenceEvent(request1, () -> assertEquals(0, run.getAndIncrement()));
  sequencer.sequenceEvent(request2, () -> assertEquals(2, run.getAndIncrement()));
  sequencer.sequenceResponse(sequence, response, () -> assertEquals(1, run.getAndIncrement()));
  assertEquals(3, run.get());
}
 
Example #8
Source File: RaftClientMessagingProtocol.java    From submarine with Apache License 2.0 5 votes vote down vote up
@Override
public void registerPublishListener(SessionId sessionId, Consumer<PublishRequest> listener,
                                    Executor executor) {
  messagingService.registerHandler(String.format("publish-%d", sessionId.id()), (e, p) -> {
    listener.accept(serializer.decode(p));
  }, executor);
}
 
Example #9
Source File: RaftSessionListener.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Handles a publish request.
 *
 * @param request The publish request to handle.
 */
@SuppressWarnings("unchecked")
private void handlePublish(PublishRequest request) {
  log.trace("Received {}", request);

  // If the request is for another session ID, this may be a session that was previously opened
  // for this client.
  if (request.session() != state.getSessionId().id()) {
    log.trace("Inconsistent session ID: {}", request.session());
    return;
  }

  // Store eventIndex in a local variable to prevent multiple volatile reads.
  long eventIndex = state.getEventIndex();

  // If the request event index has already been processed, return.
  if (request.eventIndex() <= eventIndex) {
    log.trace("Duplicate event index {}", request.eventIndex());
    return;
  }

  // If the request's previous event index doesn't equal the previous received event index,
  // respond with an undefined error and the last index received. This will cause the cluster
  // to resend events starting at eventIndex + 1.
  if (request.previousIndex() != eventIndex) {
    log.trace("Inconsistent event index: {}", request.previousIndex());
    ResetRequest resetRequest = ResetRequest.builder()
        .withSession(state.getSessionId().id())
        .withIndex(eventIndex)
        .build();
    log.trace("Sending {}", resetRequest);
    protocol.reset(memberSelector.members(), resetRequest);
    return;
  }

  // Store the event index. This will be used to verify that events are received in sequential order.
  state.setEventIndex(request.eventIndex());

  sequencer.sequenceEvent(request, () -> {
    for (PrimitiveEvent event : request.events()) {
      Set<Consumer<PrimitiveEvent>> listeners = eventListeners.get(event.type());
      if (listeners != null) {
        for (Consumer<PrimitiveEvent> listener : listeners) {
          listener.accept(event);
        }
      }
    }
  });
}
 
Example #10
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void publish(MemberId memberId, PublishRequest request) {
  getClient(memberId).thenAccept(protocol -> protocol.publish(request.session(), encode(request)));
}
 
Example #11
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void publish(MemberId memberId, PublishRequest request) {
  sendAsync(memberId, String.format("publish-%d", request.session()), request);
}
 
Example #12
Source File: LocalRaftClientProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerPublishListener(SessionId sessionId, Consumer<PublishRequest> listener, Executor executor) {
  publishListeners.put(sessionId.id(), request -> executor.execute(() -> listener.accept(request)));
}
 
Example #13
Source File: LocalRaftClientProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
void publish(long sessionId, byte[] request) {
  Consumer<PublishRequest> listener = publishListeners.get(sessionId);
  if (listener != null) {
    listener.accept(decode(request));
  }
}
 
Example #14
Source File: RaftClientMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerPublishListener(SessionId sessionId, Consumer<PublishRequest> listener, Executor executor) {
  messagingService.registerHandler(String.format("publish-%d", sessionId.id()), (e, p) -> {
    listener.accept(serializer.decode(p));
  }, executor);
}
 
Example #15
Source File: RaftClientCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerPublishListener(SessionId sessionId, Consumer<PublishRequest> listener, Executor executor) {
  clusterCommunicator.subscribe(context.publishSubject(sessionId.id()), serializer::decode, listener, executor);
}
 
Example #16
Source File: RaftServerCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void publish(MemberId memberId, PublishRequest request) {
  clusterCommunicator.unicast(context.publishSubject(request.session()), request, serializer::encode, memberId);
}
 
Example #17
Source File: RaftSessionSequencer.java    From atomix with Apache License 2.0 4 votes vote down vote up
private EventCallback(PublishRequest request, Runnable callback) {
  this.request = request;
  this.callback = callback;
}
 
Example #18
Source File: LocalRaftServerProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void publish(MemberId memberId, PublishRequest request) {
  getClient(memberId).thenAccept(protocol ->
      protocol.publish(request.session(), encode(request)));
}
 
Example #19
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void publish(MemberId memberId, PublishRequest request) {
  sendAsync(memberId, String.format("publish-%d", request.session()), request);
}
 
Example #20
Source File: LocalRaftClientProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void registerPublishListener(SessionId sessionId,
                                    Consumer<PublishRequest> listener, Executor executor) {
  publishListeners.put(sessionId.id(), request ->
      executor.execute(() -> listener.accept(request)));
}
 
Example #21
Source File: LocalRaftClientProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
void publish(long sessionId, byte[] request) {
  Consumer<PublishRequest> listener = publishListeners.get(sessionId);
  if (listener != null) {
    listener.accept(decode(request));
  }
}
 
Example #22
Source File: LocalRaftServerProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public void publish(MemberId memberId, PublishRequest request) {
  getClient(memberId).thenAccept(protocol ->
      protocol.publish(request.session(), encode(request)));
}
 
Example #23
Source File: RaftServerMessagingProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public void publish(MemberId memberId, PublishRequest request) {
  sendAsync(memberId, String.format("publish-%d", request.session()), request);
}
 
Example #24
Source File: LocalRaftClientProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public void registerPublishListener(SessionId sessionId,
                                    Consumer<PublishRequest> listener, Executor executor) {
  publishListeners.put(sessionId.id(), request ->
      executor.execute(() -> listener.accept(request)));
}
 
Example #25
Source File: LocalRaftClientProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
void publish(long sessionId, byte[] request) {
  Consumer<PublishRequest> listener = publishListeners.get(sessionId);
  if (listener != null) {
    listener.accept(decode(request));
  }
}
 
Example #26
Source File: RaftSessionSequencer.java    From atomix with Apache License 2.0 3 votes vote down vote up
/**
 * Sequences an event.
 * <p>
 * This method relies on the session event protocol to ensure that events are applied in sequential order.
 * When an event is received, if no operations are outstanding, the event is immediately completed since
 * the event could not have occurred concurrently with any other operation. Otherwise, the event is queued
 * and the next response in the sequence of responses is checked to determine whether the event can be
 * completed.
 *
 * @param request  The publish request.
 * @param callback The callback to sequence.
 */
public void sequenceEvent(PublishRequest request, Runnable callback) {
  if (requestSequence == responseSequence) {
    log.trace("Completing {}", request);
    callback.run();
    eventIndex = request.eventIndex();
  } else {
    eventCallbacks.add(new EventCallback(request, callback));
    completeResponses();
  }
}