io.atomix.protocols.raft.ReadConsistency Java Examples

The following examples show how to use io.atomix.protocols.raft.ReadConsistency. 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: YfsConfig.java    From yfs with Apache License 2.0 6 votes vote down vote up
@Bean(name = "gatewayAtomix")
public Atomix getGatewayAtomix() {
    List<Member> ms = gatewayMembers.apply(clusterProperties);
    Atomix atomix = Atomix.builder()
            .withMemberId(clusterProperties.getLocal())
            .withAddress(clusterProperties.getGateway().getIp(), clusterProperties.getGateway().getPort())
            .withMembershipProvider(BootstrapDiscoveryProvider.builder()
                    .withNodes((Collection) ms)
                    .build())
            .withProfiles(Profile.client())
            .withZone(CommonConstant.storeZone)
            .withRack(clusterProperties.getGroup())
            .build();

    atomix.start().join();
    storeInfoMap = atomix.<String, StoreInfo>atomicMapBuilder(CommonConstant.storeInfoMapName)
            .withProtocol(MultiRaftProtocol.builder()
                    .withReadConsistency(ReadConsistency.LINEARIZABLE)
                    .build())
            .withSerializer(CommonConstant.protocolSerializer)
            .build();
    return atomix;
}
 
Example #2
Source File: ClusterConfig.java    From yfs with Apache License 2.0 6 votes vote down vote up
public ClusterConfig() {
    this.clusterProperties = getClusterProperties();
    Member m = gatewayMember.apply(clusterProperties);
    List<Member> ms = gatewayMembers.apply(clusterProperties);
    Atomix atomix = Atomix.builder()
            .withMemberId(clusterProperties.getLocal())
            .withAddress(m.address())
            .withMembershipProvider(BootstrapDiscoveryProvider.builder()
                    .withNodes((Collection) ms)
                    .build())
            .withManagementGroup(gatewayManagementGroup.apply(clusterProperties))
            .withPartitionGroups(gatewayDataGroup.apply(clusterProperties))
            .withZone(CommonConstant.gatewayZone)
            .build();
    atomix.start().join();
    this.atomicMap = atomix.<String, StoreInfo>atomicMapBuilder(CommonConstant.storeInfoMapName)
            .withProtocol(MultiRaftProtocol.builder()
                    .withReadConsistency(ReadConsistency.LINEARIZABLE)
                    .build())
            .withSerializer(CommonConstant.protocolSerializer)
            .build();
    ;
    this.atomix = atomix;
    LOGGER.info("Atomix[{},{}]启动成功", m.id(), m.address().toString());
}
 
Example #3
Source File: RaftSessionRegistryTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
private RaftSession createSession(long sessionId) {
  RaftServiceContext context = mock(RaftServiceContext.class);
  when(context.serviceType()).thenReturn(TestPrimitiveType.instance());
  when(context.serviceName()).thenReturn("test");
  when(context.serviceId()).thenReturn(PrimitiveId.from(1));

  RaftContext server = mock(RaftContext.class);
  when(server.getProtocol()).thenReturn(mock(RaftServerProtocol.class));
  RaftServiceManager manager = mock(RaftServiceManager.class);
  when(manager.executor()).thenReturn(mock(ThreadContext.class));
  when(server.getServiceManager()).thenReturn(manager);

  return new RaftSession(
      SessionId.from(sessionId),
      MemberId.from("1"),
      "test",
      TestPrimitiveType.instance(),
      ReadConsistency.LINEARIZABLE,
      100,
      5000,
      System.currentTimeMillis(),
      Serializer.using(Namespaces.BASIC),
      context,
      server,
      mock(ThreadContextFactory.class));
}
 
Example #4
Source File: OpenSessionEntry.java    From atomix with Apache License 2.0 6 votes vote down vote up
public OpenSessionEntry(
    long term,
    long timestamp,
    String memberId,
    String serviceName,
    String serviceType,
    byte[] serviceConfig,
    ReadConsistency readConsistency,
    long minTimeout,
    long maxTimeout) {
  super(term, timestamp);
  this.memberId = memberId;
  this.serviceName = serviceName;
  this.serviceType = serviceType;
  this.serviceConfig = serviceConfig;
  this.readConsistency = readConsistency;
  this.minTimeout = minTimeout;
  this.maxTimeout = maxTimeout;
}
 
Example #5
Source File: DefaultRaftSessionClient.java    From atomix with Apache License 2.0 6 votes vote down vote up
public DefaultRaftSessionClient(
    String serviceName,
    PrimitiveType primitiveType,
    ServiceConfig serviceConfig,
    PartitionId partitionId,
    RaftClientProtocol protocol,
    MemberSelectorManager selectorManager,
    RaftSessionManager sessionManager,
    ReadConsistency readConsistency,
    CommunicationStrategy communicationStrategy,
    ThreadContext context,
    Duration minTimeout,
    Duration maxTimeout) {
  this.serviceName = checkNotNull(serviceName, "serviceName cannot be null");
  this.primitiveType = checkNotNull(primitiveType, "serviceType cannot be null");
  this.serviceConfig = checkNotNull(serviceConfig, "serviceConfig cannot be null");
  this.partitionId = checkNotNull(partitionId, "partitionId cannot be null");
  this.protocol = checkNotNull(protocol, "protocol cannot be null");
  this.selectorManager = checkNotNull(selectorManager, "selectorManager cannot be null");
  this.readConsistency = checkNotNull(readConsistency, "readConsistency cannot be null");
  this.communicationStrategy = checkNotNull(communicationStrategy, "communicationStrategy cannot be null");
  this.context = checkNotNull(context, "context cannot be null");
  this.minTimeout = checkNotNull(minTimeout, "minTimeout cannot be null");
  this.maxTimeout = checkNotNull(maxTimeout, "maxTimeout cannot be null");
  this.sessionManager = checkNotNull(sessionManager, "sessionManager cannot be null");
}
 
Example #6
Source File: RaftFuzzTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test session.
 */
private SessionClient createProxy(RaftClient client, ReadConsistency consistency) {
  return client.sessionBuilder("raft-fuzz-test", TestPrimitiveType.INSTANCE, new ServiceConfig())
      .withReadConsistency(consistency)
      .withCommunicationStrategy(COMMUNICATION_STRATEGY)
      .build()
      .connect()
      .join();
}
 
Example #7
Source File: ClusterManager.java    From submarine with Apache License 2.0 5 votes vote down vote up
private SessionClient createProxy(RaftClient client) {
  return client.sessionBuilder(ClusterPrimitiveType.PRIMITIVE_NAME,
      ClusterPrimitiveType.INSTANCE, new ServiceConfig())
      .withReadConsistency(ReadConsistency.SEQUENTIAL)
      .withCommunicationStrategy(CommunicationStrategy.LEADER)
      .build()
      .connect()
      .join();
}
 
Example #8
Source File: RaftServiceManagerTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testInstallSnapshotOnApply() throws Exception {
  RaftLogWriter writer = raft.getLogWriter();
  writer.append(new InitializeEntry(1, System.currentTimeMillis()));
  writer.append(new OpenSessionEntry(
      1,
      System.currentTimeMillis(),
      "test-1",
      "test",
      "test",
      null,
      ReadConsistency.LINEARIZABLE,
      100,
      1000));
  writer.commit(2);

  RaftServiceManager manager = raft.getServiceManager();

  manager.apply(2).join();

  Snapshot snapshot = manager.snapshot();
  assertEquals(2, snapshot.index());
  assertTrue(snapshotTaken.get());

  snapshot.complete();

  assertEquals(2, raft.getSnapshotStore().getCurrentSnapshot().index());

  writer.append(new CommandEntry(1, System.currentTimeMillis(), 2, 1, new PrimitiveOperation(RUN, new byte[0])));
  writer.commit(3);

  manager.apply(3).join();
  assertTrue(snapshotInstalled.get());
}
 
Example #9
Source File: RaftServiceManagerTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testSnapshotTakeInstall() throws Exception {
  RaftLogWriter writer = raft.getLogWriter();
  writer.append(new InitializeEntry(1, System.currentTimeMillis()));
  writer.append(new OpenSessionEntry(
      1,
      System.currentTimeMillis(),
      "test-1",
      "test",
      "test",
      null,
      ReadConsistency.LINEARIZABLE,
      100,
      1000));
  writer.commit(2);

  RaftServiceManager manager = raft.getServiceManager();

  manager.apply(2).join();

  Snapshot snapshot = manager.snapshot();
  assertEquals(2, snapshot.index());
  assertTrue(snapshotTaken.get());

  snapshot = snapshot.complete();

  assertEquals(2, raft.getSnapshotStore().getCurrentSnapshot().index());

  manager.install(snapshot);
  assertTrue(snapshotInstalled.get());
}
 
Example #10
Source File: RaftSession.java    From atomix with Apache License 2.0 5 votes vote down vote up
public RaftSession(
    SessionId sessionId,
    MemberId member,
    String name,
    PrimitiveType primitiveType,
    ReadConsistency readConsistency,
    long minTimeout,
    long maxTimeout,
    long lastUpdated,
    Serializer serializer,
    RaftServiceContext context,
    RaftContext server,
    ThreadContextFactory threadContextFactory) {
  super(sessionId, name, primitiveType, member, serializer);
  this.readConsistency = readConsistency;
  this.minTimeout = minTimeout;
  this.maxTimeout = maxTimeout;
  this.lastUpdated = lastUpdated;
  this.eventIndex = sessionId.id();
  this.completeIndex = sessionId.id();
  this.lastApplied = sessionId.id();
  this.protocol = server.getProtocol();
  this.context = context;
  this.server = server;
  this.eventExecutor = threadContextFactory.createContext();
  this.log = ContextualLoggerFactory.getLogger(getClass(), LoggerContext.builder(Session.class)
      .addValue(sessionId)
      .add("type", context.serviceType())
      .add("name", context.serviceName())
      .build());
}
 
Example #11
Source File: OpenSessionRequest.java    From atomix with Apache License 2.0 5 votes vote down vote up
public OpenSessionRequest(String node, String name, String typeName, byte[] config, ReadConsistency readConsistency, long minTimeout, long maxTimeout) {
  this.node = node;
  this.name = name;
  this.typeName = typeName;
  this.config = config;
  this.readConsistency = readConsistency;
  this.minTimeout = minTimeout;
  this.maxTimeout = maxTimeout;
}
 
Example #12
Source File: ClusterManager.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private SessionClient createProxy(RaftClient client) {
  return client.sessionBuilder(ClusterPrimitiveType.PRIMITIVE_NAME,
      ClusterPrimitiveType.INSTANCE, new ServiceConfig())
      .withReadConsistency(ReadConsistency.SEQUENTIAL)
      .withCommunicationStrategy(CommunicationStrategy.LEADER)
      .build()
      .connect()
      .join();
}
 
Example #13
Source File: RaftSessionManager.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Opens a new session.
 *
 * @param serviceName           The session name.
 * @param primitiveType         The session type.
 * @param communicationStrategy The strategy with which to communicate with servers.
 * @param minTimeout            The minimum session timeout.
 * @param maxTimeout            The maximum session timeout.
 * @return A completable future to be completed once the session has been opened.
 */
public CompletableFuture<RaftSessionState> openSession(
    String serviceName,
    PrimitiveType primitiveType,
    ServiceConfig config,
    ReadConsistency readConsistency,
    CommunicationStrategy communicationStrategy,
    Duration minTimeout,
    Duration maxTimeout) {
  checkNotNull(serviceName, "serviceName cannot be null");
  checkNotNull(primitiveType, "serviceType cannot be null");
  checkNotNull(communicationStrategy, "communicationStrategy cannot be null");
  checkNotNull(maxTimeout, "timeout cannot be null");

  log.debug("Opening session; name: {}, type: {}", serviceName, primitiveType);
  OpenSessionRequest request = OpenSessionRequest.builder()
      .withMemberId(memberId)
      .withServiceName(serviceName)
      .withServiceType(primitiveType)
      .withServiceConfig(Serializer.using(primitiveType.namespace()).encode(config))
      .withReadConsistency(readConsistency)
      .withMinTimeout(minTimeout.toMillis())
      .withMaxTimeout(maxTimeout.toMillis())
      .build();

  CompletableFuture<RaftSessionState> future = new CompletableFuture<>();
  ThreadContext proxyContext = threadContextFactory.createContext();
  connection.openSession(request).whenCompleteAsync((response, error) -> {
    if (error == null) {
      if (response.status() == RaftResponse.Status.OK) {
        // Create and store the proxy state.
        RaftSessionState state = new RaftSessionState(
            clientId,
            SessionId.from(response.session()),
            serviceName,
            primitiveType,
            response.timeout());
        sessions.put(state.getSessionId().id(), state);

        state.addStateChangeListener(s -> {
          if (s == PrimitiveState.EXPIRED || s == PrimitiveState.CLOSED) {
            sessions.remove(state.getSessionId().id());
          }
        });

        // Ensure the proxy session info is reset and the session is kept alive.
        keepAliveSessions(System.currentTimeMillis(), state.getSessionTimeout());

        future.complete(state);
      } else {
        future.completeExceptionally(new RaftException.Unavailable(response.error().message()));
      }
    } else {
      future.completeExceptionally(new RaftException.Unavailable(error.getMessage()));
    }
  }, proxyContext);
  return future;
}
 
Example #14
Source File: RaftServiceContext.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Installs a snapshot.
 */
public void installSnapshot(SnapshotReader reader) {
  log.debug("Installing snapshot {}", reader.snapshot().index());
  reader.skip(Bytes.LONG); // Skip the service ID
  PrimitiveType primitiveType;
  try {
    primitiveType = raft.getPrimitiveTypes().getPrimitiveType(reader.readString());
  } catch (ConfigurationException e) {
    log.error(e.getMessage(), e);
    return;
  }

  String serviceName = reader.readString();
  currentIndex = reader.readLong();
  currentTimestamp = reader.readLong();
  timestampDelta = reader.readLong();

  int sessionCount = reader.readInt();
  for (int i = 0; i < sessionCount; i++) {
    SessionId sessionId = SessionId.from(reader.readLong());
    MemberId node = MemberId.from(reader.readString());
    ReadConsistency readConsistency = ReadConsistency.valueOf(reader.readString());
    long minTimeout = reader.readLong();
    long maxTimeout = reader.readLong();
    long sessionTimestamp = reader.readLong();

    // Only create a new session if one does not already exist. This is necessary to ensure only a single session
    // is ever opened and exposed to the state machine.
    RaftSession session = raft.getSessions().addSession(new RaftSession(
        sessionId,
        node,
        serviceName,
        primitiveType,
        readConsistency,
        minTimeout,
        maxTimeout,
        sessionTimestamp,
        service.serializer(),
        this,
        raft,
        threadContextFactory));

    session.setRequestSequence(reader.readLong());
    session.setCommandSequence(reader.readLong());
    session.setEventIndex(reader.readLong());
    session.setLastCompleted(reader.readLong());
    session.setLastApplied(reader.snapshot().index());
    session.setLastUpdated(sessionTimestamp);
    session.open();
    service.register(sessions.addSession(session));
  }
  service.restore(new DefaultBackupInput(reader, service.serializer()));
}
 
Example #15
Source File: RaftFuzzTest.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a random query consistency level.
 */
private ReadConsistency randomConsistency() {
  return ReadConsistency.values()[randomNumber(ReadConsistency.values().length)];
}
 
Example #16
Source File: RaftSession.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the session read consistency.
 *
 * @return the session read consistency
 */
public ReadConsistency readConsistency() {
  return readConsistency;
}
 
Example #17
Source File: RaftSessionClient.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the read consistency level.
 *
 * @param readConsistency the read consistency level
 * @return the Raft protocol builder
 */
public Builder withReadConsistency(ReadConsistency readConsistency) {
  this.readConsistency = checkNotNull(readConsistency, "readConsistency cannot be null");
  return this;
}
 
Example #18
Source File: OpenSessionEntry.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the session read consistency level.
 *
 * @return The session's read consistency level.
 */
public ReadConsistency readConsistency() {
  return readConsistency;
}
 
Example #19
Source File: OpenSessionRequest.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the session read consistency.
 *
 * @param readConsistency the session read consistency
 * @return the session request builder
 * @throws NullPointerException if the {@code readConsistency} is null
 */
public Builder withReadConsistency(ReadConsistency readConsistency) {
  this.readConsistency = checkNotNull(readConsistency, "readConsistency cannot be null");
  return this;
}
 
Example #20
Source File: OpenSessionRequest.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the session read consistency level.
 *
 * @return The session's read consistency.
 */
public ReadConsistency readConsistency() {
  return readConsistency;
}