org.apache.ratis.util.StringUtils Java Examples

The following examples show how to use org.apache.ratis.util.StringUtils. 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: ServerRestartTests.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
static void assertCorruptedLogHeader(RaftPeerId id, File openLogFile, int partialLength,
    MiniRaftCluster cluster, Logger LOG) throws Exception {
  Preconditions.assertTrue(partialLength < SegmentedRaftLogFormat.getHeaderLength());
  try(final RandomAccessFile raf = new RandomAccessFile(openLogFile, "rw")) {
    SegmentedRaftLogFormat.applyHeaderTo(header -> {
      LOG.info("header    = {}", StringUtils.bytes2HexString(header));
      final byte[] corrupted = new byte[header.length];
      System.arraycopy(header, 0, corrupted, 0, partialLength);
      LOG.info("corrupted = {}", StringUtils.bytes2HexString(corrupted));
      raf.write(corrupted);
      return null;
    });
  }
  final RaftServerImpl server = cluster.restartServer(id, false);
  server.getProxy().close();
}
 
Example #2
Source File: ServerRestartTests.java    From ratis with Apache License 2.0 6 votes vote down vote up
static void assertCorruptedLogHeader(RaftPeerId id, File openLogFile, int partialLength,
    MiniRaftCluster cluster, Logger LOG) throws Exception {
  Preconditions.assertTrue(partialLength < SegmentedRaftLogFormat.getHeaderLength());
  try(final RandomAccessFile raf = new RandomAccessFile(openLogFile, "rw")) {
    SegmentedRaftLogFormat.applyHeaderTo(header -> {
      LOG.info("header    = {}", StringUtils.bytes2HexString(header));
      final byte[] corrupted = new byte[header.length];
      System.arraycopy(header, 0, corrupted, 0, partialLength);
      LOG.info("corrupted = {}", StringUtils.bytes2HexString(corrupted));
      raf.write(corrupted);
      return null;
    });
  }
  final RaftServerImpl server = cluster.restartServer(id, false);
  server.getProxy().close();
}
 
Example #3
Source File: LogReader.java    From ratis with Apache License 2.0 6 votes vote down vote up
/**
 * Read header from the log file:
 *
 * (1) The header in file is verified successfully.
 *     Then, return true.
 *
 * (2) The header in file is partially written.
 *     Then, return false.
 *
 * (3) The header in file is corrupted or there is some other {@link IOException}.
 *     Then, throw an exception.
 */
boolean verifyHeader() throws IOException {
  final int headerLength = SegmentedRaftLogFormat.getHeaderLength();
  final int readLength = in.read(temp, 0, headerLength);
  Preconditions.assertTrue(readLength <= headerLength);
  final int matchLength = SegmentedRaftLogFormat.matchHeader(temp, 0, readLength);
  Preconditions.assertTrue(matchLength <= readLength);

  if (readLength == headerLength && matchLength == readLength) {
    // The header is matched successfully
    return true;
  } else if (SegmentedRaftLogFormat.isTerminator(temp, matchLength, readLength - matchLength)) {
    // The header is partially written
    return false;
  }
  // The header is corrupted
  throw new CorruptedFileException(file, "Log header mismatched: expected header length="
      + SegmentedRaftLogFormat.getHeaderLength() + ", read length=" + readLength + ", match length=" + matchLength
      + ", header in file=" + StringUtils.bytes2HexString(temp, 0, readLength)
      + ", expected header=" + SegmentedRaftLogFormat.applyHeaderTo(StringUtils::bytes2HexString));
}
 
Example #4
Source File: OutputStreamBaseTest.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private void checkLog(RaftLog raftLog, long expectedCommittedIndex,
    Supplier<byte[]> s) throws IOException {
  long committedIndex = raftLog.getLastCommittedIndex();
  Assert.assertTrue(committedIndex >= expectedCommittedIndex);
  // check the log content
  TermIndex[] entries = raftLog.getEntries(0, Long.MAX_VALUE);
  int count = 0;
  for (TermIndex entry : entries) {
    LogEntryProto log  = raftLog.get(entry.getIndex());
    if (!log.hasStateMachineLogEntry()) {
      continue;
    }
    byte[] logData = log.getStateMachineLogEntry().getLogData().toByteArray();
    byte[] expected = s.get();
    final String message = "log " + entry + " " + log.getLogEntryBodyCase()
        + " " + StringUtils.bytes2HexString(logData)
        + ", expected=" + StringUtils.bytes2HexString(expected);
    LOG.info(message);
    Assert.assertArrayEquals(message, expected, logData);
    count++;
  }
  Assert.assertEquals(expectedCommittedIndex, count);
}
 
Example #5
Source File: SegmentedRaftLogReader.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
/**
 * Read header from the log file:
 *
 * (1) The header in file is verified successfully.
 *     Then, return true.
 *
 * (2) The header in file is partially written.
 *     Then, return false.
 *
 * (3) The header in file is corrupted or there is some other {@link IOException}.
 *     Then, throw an exception.
 */
boolean verifyHeader() throws IOException {
  final int headerLength = SegmentedRaftLogFormat.getHeaderLength();
  final int readLength = in.read(temp, 0, headerLength);
  Preconditions.assertTrue(readLength <= headerLength);
  final int matchLength = SegmentedRaftLogFormat.matchHeader(temp, 0, readLength);
  Preconditions.assertTrue(matchLength <= readLength);

  if (readLength == headerLength && matchLength == readLength) {
    // The header is matched successfully
    return true;
  } else if (SegmentedRaftLogFormat.isTerminator(temp, matchLength, readLength - matchLength)) {
    // The header is partially written
    return false;
  }
  // The header is corrupted
  throw new CorruptedFileException(file, "Log header mismatched: expected header length="
      + SegmentedRaftLogFormat.getHeaderLength() + ", read length=" + readLength + ", match length=" + matchLength
      + ", header in file=" + StringUtils.bytes2HexString(temp, 0, readLength)
      + ", expected header=" + SegmentedRaftLogFormat.applyHeaderTo(StringUtils::bytes2HexString));
}
 
Example #6
Source File: RaftLogIndex.java    From ratis with Apache License 2.0 5 votes vote down vote up
public boolean updateIncreasingly(long newIndex, Consumer<Object> log) {
  final long old = index.getAndSet(newIndex);
  Preconditions.assertTrue(old <= newIndex,
      () -> "Failed to updateIncreasingly for " + name + ": " + old + " -> " + newIndex);
  log.accept(StringUtils.stringSupplierAsObject(() -> name + ": updateIncreasingly " + old + " -> " + newIndex));
  return old != newIndex;
}
 
Example #7
Source File: RaftLogIndex.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public boolean updateIncreasingly(long newIndex, Consumer<Object> log) {
  final long old = index.getAndSet(newIndex);
  Preconditions.assertTrue(old <= newIndex,
      () -> "Failed to updateIncreasingly for " + name + ": " + old + " -> " + newIndex);
  log.accept(StringUtils.stringSupplierAsObject(() -> name + ": updateIncreasingly " + old + " -> " + newIndex));
  return old != newIndex;
}
 
Example #8
Source File: RaftLogIndex.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public boolean updateToMax(long newIndex, Consumer<Object> log) {
  final long old = index.getAndUpdate(oldIndex -> Math.max(oldIndex, newIndex));
  final boolean updated = old < newIndex;
  log.accept(StringUtils.stringSupplierAsObject(
      () -> name + ": updateToMax old=" + old + ", new=" + newIndex + ", updated? " + updated));
  return updated;
}
 
Example #9
Source File: FileStoreBaseTest.java    From ratis with Apache License 2.0 5 votes vote down vote up
static void assertBuffers(int offset, int length, ByteBuffer expected, ByteBuffer computed) {
  try {
    Assert.assertEquals(expected, computed);
  } catch(AssertionError e) {
    LOG.error("Buffer mismatched at offset=" + offset + ", length=" + length
        + "\n  expected = " + StringUtils.bytes2HexString(expected)
        + "\n  computed = " + StringUtils.bytes2HexString(computed), e);
    throw e;
  }
}
 
Example #10
Source File: FileStoreBaseTest.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
static void assertBuffers(int offset, int length, ByteBuffer expected, ByteBuffer computed) {
  try {
    Assert.assertEquals(expected, computed);
  } catch(AssertionError e) {
    LOG.error("Buffer mismatched at offset=" + offset + ", length=" + length
        + "\n  expected = " + StringUtils.bytes2HexString(expected)
        + "\n  computed = " + StringUtils.bytes2HexString(computed), e);
    throw e;
  }
}
 
Example #11
Source File: TestRaftStream.java    From ratis with Apache License 2.0 5 votes vote down vote up
private void checkLog(RaftLog raftLog, long expectedCommittedIndex,
    Supplier<byte[]> s) throws IOException {
  long committedIndex = raftLog.getLastCommittedIndex();
  Assert.assertEquals(expectedCommittedIndex, committedIndex);
  // check the log content
  TermIndex[] entries = raftLog.getEntries(1, expectedCommittedIndex + 1);
  for (TermIndex entry : entries) {
    RaftProtos.LogEntryProto log  = raftLog.get(entry.getIndex());
    byte[] logData = log.getStateMachineLogEntry().getLogData().toByteArray();
    byte[] expected = s.get();
    LOG.info("log " + entry + " " + log.getLogEntryBodyCase() + " " + StringUtils.bytes2HexString(logData));
    Assert.assertEquals(expected.length, logData.length);
    Assert.assertArrayEquals(expected, logData);
  }
}
 
Example #12
Source File: OzoneManagerRatisServer.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
/**
 * Process the raftClientReply and return OMResponse.
 * @param omRequest
 * @param reply
 * @return OMResponse - response which is returned to client.
 * @throws ServiceException
 */
private OMResponse processReply(OMRequest omRequest, RaftClientReply reply)
    throws ServiceException {
  // NotLeader exception is thrown only when the raft server to which the
  // request is submitted is not the leader. This can happen first time
  // when client is submitting request to OM.

  if (!reply.isSuccess()) {
    NotLeaderException notLeaderException = reply.getNotLeaderException();
    if (notLeaderException != null) {
      throw new ServiceException(
          OMNotLeaderException.convertToOMNotLeaderException(
                notLeaderException, getRaftPeerId()));
    }

    LeaderNotReadyException leaderNotReadyException =
        reply.getLeaderNotReadyException();
    if (leaderNotReadyException != null) {
      throw new ServiceException(new OMLeaderNotReadyException(
          leaderNotReadyException.getMessage()));
    }

    StateMachineException stateMachineException =
        reply.getStateMachineException();
    if (stateMachineException != null) {
      OMResponse.Builder omResponse = OMResponse.newBuilder()
          .setCmdType(omRequest.getCmdType())
          .setSuccess(false)
          .setTraceID(omRequest.getTraceID());
      if (stateMachineException.getCause() != null) {
        omResponse.setMessage(stateMachineException.getCause().getMessage());
        omResponse.setStatus(
            exceptionToResponseStatus(stateMachineException.getCause()));
      } else {
        // Current Ratis is setting cause, this is an safer side check.
        LOG.error("StateMachine exception cause is not set");
        omResponse.setStatus(
            OzoneManagerProtocolProtos.Status.INTERNAL_ERROR);
        omResponse.setMessage(
            StringUtils.stringifyException(stateMachineException));
      }

      if (LOG.isDebugEnabled()) {
        LOG.debug("Error while executing ratis request. " +
            "stateMachineException: ", stateMachineException);
      }
      return omResponse.build();
    }
  }

  try {
    return OMRatisHelper.getOMResponseFromRaftClientReply(reply);
  } catch (InvalidProtocolBufferException ex) {
    if (ex.getMessage() != null) {
      throw new ServiceException(ex.getMessage(), ex);
    } else {
      throw new ServiceException(ex);
    }
  }

  // TODO: Still need to handle RaftRetry failure exception and
  //  NotReplicated exception.
}
 
Example #13
Source File: FileStore.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
FileMap(Supplier<String> name) {
  this.name = StringUtils.stringSupplierAsObject(name);
}
 
Example #14
Source File: RaftLogSequentialOps.java    From ratis with Apache License 2.0 4 votes vote down vote up
Runner(Supplier<String> name) {
  this.name = StringUtils.stringSupplierAsObject(name);
}
 
Example #15
Source File: RaftLogIndex.java    From ratis with Apache License 2.0 4 votes vote down vote up
public boolean updateToMax(long newIndex, Consumer<Object> log) {
  final long old = index.getAndUpdate(oldIndex -> Math.max(oldIndex, newIndex));
  log.accept(StringUtils.stringSupplierAsObject(() -> name + ": updateToMax " + old + " -> " + newIndex));
  return old != newIndex;
}
 
Example #16
Source File: RaftLogIndex.java    From ratis with Apache License 2.0 4 votes vote down vote up
public boolean updateUnconditionally(LongUnaryOperator update, Consumer<Object> log) {
  final long old = index.getAndUpdate(update);
  final long newIndex = update.applyAsLong(old);
  log.accept(StringUtils.stringSupplierAsObject(() -> name + ": updateUnconditionally " + old + " -> " + newIndex));
  return old != newIndex;
}
 
Example #17
Source File: RaftLogIndex.java    From ratis with Apache License 2.0 4 votes vote down vote up
public boolean setUnconditionally(long newIndex, Consumer<Object> log) {
  final long old = index.getAndSet(newIndex);
  log.accept(StringUtils.stringSupplierAsObject(() -> name + ": setUnconditionally " + old + " -> " + newIndex));
  return old != newIndex;
}
 
Example #18
Source File: ConfigurationManager.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized String toString() {
  return getClass().getSimpleName() + ", init=" + initialConf + ", confs=" + StringUtils.map2String(configurations);
}
 
Example #19
Source File: Message.java    From ratis with Apache License 2.0 4 votes vote down vote up
static Message valueOf(ByteString bytes) {
  return valueOf(bytes, () -> "Message:" + StringUtils.bytes2HexShortString(bytes));
}
 
Example #20
Source File: FileStore.java    From ratis with Apache License 2.0 4 votes vote down vote up
FileMap(Supplier<String> name) {
  this.name = StringUtils.stringSupplierAsObject(name);
}
 
Example #21
Source File: ConfigurationManager.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized String toString() {
  return getClass().getSimpleName() + ", init=" + initialConf + ", confs=" + StringUtils.map2String(configurations);
}
 
Example #22
Source File: RaftLogSequentialOps.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
Runner(Supplier<String> name) {
  this.name = StringUtils.stringSupplierAsObject(name);
}
 
Example #23
Source File: RaftLogIndex.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public long incrementAndGet(Consumer<Object> log) {
  final long newIndex = index.incrementAndGet();
  log.accept(StringUtils.stringSupplierAsObject(
      () -> name + ": incrementAndGet " + (newIndex-1) + " -> " + newIndex));
  return newIndex;
}
 
Example #24
Source File: RaftLogIndex.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public boolean updateUnconditionally(LongUnaryOperator update, Consumer<Object> log) {
  final long old = index.getAndUpdate(update);
  final long newIndex = update.applyAsLong(old);
  log.accept(StringUtils.stringSupplierAsObject(() -> name + ": updateUnconditionally " + old + " -> " + newIndex));
  return old != newIndex;
}
 
Example #25
Source File: RaftLogIndex.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public boolean setUnconditionally(long newIndex, Consumer<Object> log) {
  final long old = index.getAndSet(newIndex);
  log.accept(StringUtils.stringSupplierAsObject(() -> name + ": setUnconditionally " + old + " -> " + newIndex));
  return old != newIndex;
}
 
Example #26
Source File: SegmentedRaftLog.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
final void completeFuture() {
  final boolean completed = future.complete(getEndIndex());
  Preconditions.assertTrue(completed,
      () -> this + " is already " + StringUtils.completableFuture2String(future, false));
}
 
Example #27
Source File: SegmentedRaftLogReader.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
/**
 * Decode the log entry "frame". This includes reading the log entry, and
 * validating the checksum.
 *
 * The input stream will be advanced to the end of the op at the end of this
 * function.
 *
 * @return The log entry, or null if we hit EOF.
 */
private LogEntryProto decodeEntry() throws IOException {
  limiter.setLimit(MAX_OP_SIZE);
  in.mark(MAX_OP_SIZE);

  byte nextByte;
  try {
    nextByte = in.readByte();
  } catch (EOFException eof) {
    // EOF at an opcode boundary is expected.
    return null;
  }
  // Each log entry starts with a var-int. Thus a valid entry's first byte
  // should not be 0. So if the terminate byte is 0, we should hit the end
  // of the segment.
  if (SegmentedRaftLogFormat.isTerminator(nextByte)) {
    verifyTerminator();
    return null;
  }

  // Here, we verify that the Op size makes sense and that the
  // data matches its checksum before attempting to construct an Op.
  int entryLength = CodedInputStream.readRawVarint32(nextByte, in);
  if (entryLength > MAX_OP_SIZE) {
    throw new IOException("Entry has size " + entryLength
        + ", but MAX_OP_SIZE = " + MAX_OP_SIZE);
  }

  final int varintLength = CodedOutputStream.computeUInt32SizeNoTag(
      entryLength);
  final int totalLength = varintLength + entryLength;
  checkBufferSize(totalLength);
  in.reset();
  in.mark(MAX_OP_SIZE);
  IOUtils.readFully(in, temp, 0, totalLength);

  // verify checksum
  checksum.reset();
  checksum.update(temp, 0, totalLength);
  int expectedChecksum = in.readInt();
  int calculatedChecksum = (int) checksum.getValue();
  if (expectedChecksum != calculatedChecksum) {
    final String s = StringUtils.format("Log entry corrupted: Calculated checksum is %08X but read checksum is %08X.",
        calculatedChecksum, expectedChecksum);
    throw new ChecksumException(s, limiter.markPos);
  }

  // parse the buffer
  return LogEntryProto.parseFrom(
      CodedInputStream.newInstance(temp, varintLength, entryLength));
}
 
Example #28
Source File: Message.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
static Message valueOf(ByteString bytes) {
  return valueOf(bytes, () -> "Message:" + StringUtils.bytes2HexShortString(bytes));
}
 
Example #29
Source File: RaftProperties.java    From ratis with Apache License 2.0 2 votes vote down vote up
/**
 * Get the value of the <code>name</code> property as a <code>boolean</code>.
 * If no such property is specified, or if the specified value is not a valid
 * <code>boolean</code>, then <code>defaultValue</code> is returned.
 *
 * @param name property name.
 * @param defaultValue default value.
 * @return property value as a <code>boolean</code>,
 *         or <code>defaultValue</code>.
 */
public boolean getBoolean(String name, boolean defaultValue) {
  String valueString = getTrimmed(name);
  return StringUtils.string2boolean(valueString, defaultValue);
}
 
Example #30
Source File: RaftProperties.java    From ratis with Apache License 2.0 2 votes vote down vote up
/**
 * Get the comma delimited values of the <code>name</code> property as
 * an array of <code>String</code>s, trimmed of the leading and trailing whitespace.
 * If no such property is specified then an empty array is returned.
 *
 * @param name property name.
 * @return property value as an array of trimmed <code>String</code>s,
 *         or empty array.
 */
public String[] getTrimmedStrings(String name) {
  String valueString = get(name);
  return StringUtils.getTrimmedStrings(valueString);
}