Java Code Examples for org.apache.ratis.util.Preconditions#assertNull()

The following examples show how to use org.apache.ratis.util.Preconditions#assertNull() . 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: TransactionContext.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
public TransactionContext build() {
  Objects.requireNonNull(serverRole, "serverRole == null");
  Objects.requireNonNull(stateMachine, "stateMachine == null");
  if (clientRequest != null) {
    Preconditions.assertTrue(serverRole == RaftPeerRole.LEADER,
        () -> "serverRole MUST be LEADER since clientRequest != null, serverRole is " + serverRole);
    Preconditions.assertNull(logEntry, () -> "logEntry MUST be null since clientRequest != null");
    if (stateMachineLogEntry == null) {
      stateMachineLogEntry = ServerProtoUtils.toStateMachineLogEntryProto(clientRequest, logData, stateMachineData);
    }
    return new TransactionContextImpl(stateMachine, clientRequest, stateMachineLogEntry, stateMachineContext);
  } else {
    Objects.requireNonNull(logEntry, "logEntry MUST NOT be null since clientRequest == null");
    Preconditions.assertTrue(logEntry.hasStateMachineLogEntry(),
        () -> "Unexpected logEntry: stateMachineLogEntry not found, logEntry=" + logEntry);
    return new TransactionContextImpl(serverRole, stateMachine, logEntry);
  }
}
 
Example 2
Source File: RaftServerProxy.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
synchronized CompletableFuture<RaftServerImpl> addNew(RaftGroup group) {
  if (isClosed) {
    return JavaUtils.completeExceptionally(new AlreadyClosedException(
        getId() + ": Failed to add " + group + " since the server is already closed"));
  }
  if (containsGroup(group.getGroupId())) {
    return JavaUtils.completeExceptionally(new AlreadyExistsException(
        getId() + ": Failed to add " + group + " since the group already exists in the map."));
  }
  final RaftGroupId groupId = group.getGroupId();
  final CompletableFuture<RaftServerImpl> newImpl = newRaftServerImpl(group);
  final CompletableFuture<RaftServerImpl> previous = map.put(groupId, newImpl);
  Preconditions.assertNull(previous, "previous");
  LOG.info("{}: addNew {} returns {}", getId(), group, toString(groupId, newImpl));
  return newImpl;
}
 
Example 3
Source File: TransactionContext.java    From ratis with Apache License 2.0 6 votes vote down vote up
public TransactionContext build() {
  Objects.requireNonNull(serverRole, "serverRole == null");
  Objects.requireNonNull(stateMachine, "stateMachine == null");
  if (clientRequest != null) {
    Preconditions.assertTrue(serverRole == RaftPeerRole.LEADER,
        () -> "serverRole MUST be LEADER since clientRequest != null, serverRole is " + serverRole);
    Preconditions.assertNull(logEntry, () -> "logEntry MUST be null since clientRequest != null");
    if (stateMachineLogEntry == null) {
      stateMachineLogEntry = ServerProtoUtils.toStateMachineLogEntryProto(clientRequest, logData, stateMachineData);
    }
    return new TransactionContextImpl(stateMachine, clientRequest, stateMachineLogEntry, stateMachineContext);
  } else {
    Objects.requireNonNull(logEntry, "logEntry MUST NOT be null since clientRequest == null");
    Preconditions.assertTrue(logEntry.hasStateMachineLogEntry(),
        () -> "Unexpected logEntry: stateMachineLogEntry not found, logEntry=" + logEntry);
    return new TransactionContextImpl(serverRole, stateMachine, logEntry);
  }
}
 
Example 4
Source File: RaftServerProxy.java    From ratis with Apache License 2.0 6 votes vote down vote up
synchronized CompletableFuture<RaftServerImpl> addNew(RaftGroup group) {
  if (isClosed) {
    return JavaUtils.completeExceptionally(new AlreadyClosedException(
        getId() + ": Failed to add " + group + " since the server is already closed"));
  }
  if (containsGroup(group.getGroupId())) {
    return JavaUtils.completeExceptionally(new AlreadyExistsException(
        getId() + ": Failed to add " + group + " since the group already exists in the map."));
  }
  final RaftGroupId groupId = group.getGroupId();
  final CompletableFuture<RaftServerImpl> newImpl = newRaftServerImpl(group);
  final CompletableFuture<RaftServerImpl> previous = map.put(groupId, newImpl);
  Preconditions.assertNull(previous, "previous");
  LOG.info("{}: addNew {} returns {}", getId(), group, toString(groupId, newImpl));
  return newImpl;
}
 
Example 5
Source File: ExceptionDependentRetry.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public Builder setExceptionToPolicy(Class<? extends Throwable> exception,
    RetryPolicy retryPolicy) {
  Preconditions.assertTrue(retryPolicy != null, "Exception to policy should not be null");
  final RetryPolicy previous = exceptionNameToPolicyMap.put(exception.getName(), retryPolicy);
  Preconditions.assertNull(previous, () -> "The exception " + exception + " is already set to " + previous);
  return this;
}
 
Example 6
Source File: TransactionContextImpl.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public LogEntryProto initLogEntry(long term, long index) {
  Preconditions.assertTrue(serverRole == RaftPeerRole.LEADER);
  Preconditions.assertNull(logEntry, "logEntry");
  Objects.requireNonNull(smLogEntryProto, "smLogEntryProto == null");
  return logEntry = ServerProtoUtils.toLogEntryProto(smLogEntryProto, term, index);
}
 
Example 7
Source File: SimpleStateMachine4Testing.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void put(LogEntryProto entry) {
  final LogEntryProto previous = indexMap.put(entry.getIndex(), entry);
  Preconditions.assertNull(previous, "previous");
  final String s = entry.getStateMachineLogEntry().getLogData().toStringUtf8();
  dataMap.put(s, entry);
  LOG.info("{}: put {}, {} -> {}", getId(), entry.getIndex(),
      s.length() <= 10? s: s.substring(0, 10) + "...",
      ServerProtoUtils.toLogEntryString(entry));
}
 
Example 8
Source File: TransactionContextImpl.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public LogEntryProto initLogEntry(long term, long index) {
  Preconditions.assertTrue(serverRole == RaftPeerRole.LEADER);
  Preconditions.assertNull(logEntry, "logEntry");
  Objects.requireNonNull(smLogEntryProto, "smLogEntryProto == null");
  return logEntry = ServerProtoUtils.toLogEntryProto(smLogEntryProto, term, index);
}
 
Example 9
Source File: SimpleStateMachine4Testing.java    From ratis with Apache License 2.0 5 votes vote down vote up
private void put(LogEntryProto entry) {
  final LogEntryProto previous = indexMap.put(entry.getIndex(), entry);
  Preconditions.assertNull(previous, "previous");
  final String s = entry.getStateMachineLogEntry().getLogData().toStringUtf8();
  dataMap.put(s, entry);
  LOG.info("{}: put {}, {} -> {}", getId(), entry.getIndex(),
      s.length() <= 10? s: s.substring(0, 10) + "...",
      ServerProtoUtils.toLogEntryString(entry));
}
 
Example 10
Source File: RequestTypeDependentRetryPolicy.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
/** Set the given policy for the given type. */
public Builder setRetryPolicy(RaftProtos.RaftClientRequestProto.TypeCase type, RetryPolicy policy) {
  final RetryPolicy previous = retryPolicyMap.put(type, policy);
  Preconditions.assertNull(previous, () -> "The retryPolicy for type " + type + " is already set to " + previous);
  return this;
}
 
Example 11
Source File: RequestTypeDependentRetryPolicy.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public Builder setTimeout(RaftProtos.RaftClientRequestProto.TypeCase type, TimeDuration timeout) {
  final TimeDuration previous = timeoutMap.put(type, timeout);
  Preconditions.assertNull(previous, () -> "The timeout for type " + type + " is already set to " + previous);
  return this;
}
 
Example 12
Source File: SimpleStateMachine4Testing.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public BlockingQueue<Runnable> enable(Type type) {
  final BlockingQueue<Runnable> q = new LinkedBlockingQueue<>();
  final BlockingQueue<Runnable> previous = map.put(type, q);
  Preconditions.assertNull(previous, "previous");
  return q;
}
 
Example 13
Source File: SimpleStateMachine4Testing.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
void block(Type type) {
  LOG.info("block {}", type);
  final CompletableFuture<Void> future = new CompletableFuture<>();
  final CompletableFuture<Void> previous = maps.putIfAbsent(type, future);
  Preconditions.assertNull(previous, "previous");
}
 
Example 14
Source File: SimpleStateMachine4Testing.java    From ratis with Apache License 2.0 4 votes vote down vote up
void block(Type type) {
  LOG.info("block {}", type);
  final CompletableFuture<Void> future = new CompletableFuture<>();
  final CompletableFuture<Void> previous = maps.putIfAbsent(type, future);
  Preconditions.assertNull(previous, "previous");
}