Java Code Examples for org.apache.ratis.server.protocol.TermIndex#newTermIndex()

The following examples show how to use org.apache.ratis.server.protocol.TermIndex#newTermIndex() . 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: ContainerStateMachine.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
private long loadSnapshot(SingleFileSnapshotInfo snapshot)
    throws IOException {
  if (snapshot == null) {
    TermIndex empty =
        TermIndex.newTermIndex(0, RaftLog.INVALID_LOG_INDEX);
    LOG.info("{}: The snapshot info is null. Setting the last applied index" +
            "to:{}", gid, empty);
    setLastAppliedTermIndex(empty);
    return empty.getIndex();
  }

  final File snapshotFile = snapshot.getFile().getPath().toFile();
  final TermIndex last =
      SimpleStateMachineStorage.getTermIndexFromSnapshotFile(snapshotFile);
  LOG.info("{}: Setting the last applied index to {}", gid, last);
  setLastAppliedTermIndex(last);

  // initialize the dispatcher with snapshot so that it build the missing
  // container list
  buildMissingContainerSet(snapshotFile);
  return last.getIndex();
}
 
Example 2
Source File: BaseStateMachine.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
protected boolean updateLastAppliedTermIndex(long term, long index) {
  final TermIndex newTI = TermIndex.newTermIndex(term, index);
  final TermIndex oldTI = lastAppliedTermIndex.getAndSet(newTI);
  if (!newTI.equals(oldTI)) {
    LOG.trace("{}: update lastAppliedTermIndex from {} to {}", getId(), oldTI, newTI);
    if (oldTI != null) {
      Preconditions.assertTrue(newTI.compareTo(oldTI) >= 0,
          () -> getId() + ": Failed updateLastAppliedTermIndex: newTI = "
              + newTI + " < oldTI = " + oldTI);
    }
    return true;
  }

  synchronized (transactionFutures) {
    for(long i; !transactionFutures.isEmpty() && (i = transactionFutures.firstKey()) <= index; ) {
      transactionFutures.remove(i).complete(null);
    }
  }
  return false;
}
 
Example 3
Source File: BaseStateMachine.java    From ratis with Apache License 2.0 6 votes vote down vote up
protected boolean updateLastAppliedTermIndex(long term, long index) {
  final TermIndex newTI = TermIndex.newTermIndex(term, index);
  final TermIndex oldTI = lastAppliedTermIndex.getAndSet(newTI);
  if (!newTI.equals(oldTI)) {
    LOG.trace("{}: update lastAppliedTermIndex from {} to {}", getId(), oldTI, newTI);
    if (oldTI != null) {
      Preconditions.assertTrue(newTI.compareTo(oldTI) >= 0,
          () -> getId() + ": Failed updateLastAppliedTermIndex: newTI = "
              + newTI + " < oldTI = " + oldTI);
    }
    return true;
  }

  synchronized (transactionFutures) {
    for(long i; !transactionFutures.isEmpty() && (i = transactionFutures.firstKey()) <= index; ) {
      transactionFutures.remove(i).complete(null);
    }
  }
  return false;
}
 
Example 4
Source File: TestOzoneManagerRatisServer.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws Exception {
  conf = new OzoneConfiguration();
  omID = UUID.randomUUID().toString();
  final String path = GenericTestUtils.getTempPath(omID);
  Path metaDirPath = Paths.get(path, "om-meta");
  conf.set(HddsConfigKeys.OZONE_METADATA_DIRS, metaDirPath.toString());
  conf.setTimeDuration(
      OMConfigKeys.OZONE_OM_LEADER_ELECTION_MINIMUM_TIMEOUT_DURATION_KEY,
      LEADER_ELECTION_TIMEOUT, TimeUnit.MILLISECONDS);
  int ratisPort = conf.getInt(
      OMConfigKeys.OZONE_OM_RATIS_PORT_KEY,
      OMConfigKeys.OZONE_OM_RATIS_PORT_DEFAULT);
  InetSocketAddress rpcAddress = new InetSocketAddress(
      InetAddress.getLocalHost(), 0);
  omNodeDetails = new OMNodeDetails.Builder()
      .setRpcAddress(rpcAddress)
      .setRatisPort(ratisPort)
      .setOMNodeId(omID)
      .setOMServiceId(OzoneConsts.OM_SERVICE_ID_DEFAULT)
      .build();
  // Starts a single node Ratis server
  ozoneManager = Mockito.mock(OzoneManager.class);
  OzoneConfiguration ozoneConfiguration = new OzoneConfiguration();
  ozoneConfiguration.set(OMConfigKeys.OZONE_OM_DB_DIRS,
      folder.newFolder().getAbsolutePath());
  omMetadataManager = new OmMetadataManagerImpl(ozoneConfiguration);
  when(ozoneManager.getMetadataManager()).thenReturn(omMetadataManager);
  initialTermIndex = TermIndex.newTermIndex(0, 0);
  OMRatisSnapshotInfo omRatisSnapshotInfo = new OMRatisSnapshotInfo();
  when(ozoneManager.getSnapshotInfo()).thenReturn(omRatisSnapshotInfo);
  omRatisServer = OzoneManagerRatisServer.newOMRatisServer(conf, ozoneManager,
    omNodeDetails, Collections.emptyList());
  omRatisServer.start();
}
 
Example 5
Source File: TestOzoneManagerRatisServer.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadSnapshotInfoOnStart() throws Exception {
  // Stop the Ratis server and manually update the snapshotInfo.
  omRatisServer.getOmStateMachine().loadSnapshotInfoFromDB();
  omRatisServer.stop();

  SnapshotInfo snapshotInfo =
      omRatisServer.getOmStateMachine().getLatestSnapshot();

  TermIndex newSnapshotIndex = TermIndex.newTermIndex(
      snapshotInfo.getTerm(), snapshotInfo.getIndex() + 100);

  omMetadataManager.getTransactionInfoTable().put(TRANSACTION_INFO_KEY,
      new OMTransactionInfo.Builder()
          .setCurrentTerm(snapshotInfo.getTerm())
          .setTransactionIndex(snapshotInfo.getIndex() + 100)
          .build());

  // Start new Ratis server. It should pick up and load the new SnapshotInfo
  omRatisServer = OzoneManagerRatisServer.newOMRatisServer(conf, ozoneManager,
      omNodeDetails, Collections.emptyList());
  omRatisServer.start();
  TermIndex lastAppliedTermIndex =
      omRatisServer.getLastAppliedTermIndex();

  Assert.assertEquals(newSnapshotIndex.getIndex(),
      lastAppliedTermIndex.getIndex());
  Assert.assertEquals(newSnapshotIndex.getTerm(),
      lastAppliedTermIndex.getTerm());
}
 
Example 6
Source File: SimpleStateMachineStorage.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static TermIndex getTermIndexFromSnapshotFile(File file) {
  final String name = file.getName();
  final Matcher m = SNAPSHOT_REGEX.matcher(name);
  if (!m.matches()) {
    throw new IllegalArgumentException("File \"" + file
        + "\" does not match snapshot file name pattern \""
        + SNAPSHOT_REGEX + "\"");
  }
  final long term = Long.parseLong(m.group(1));
  final long index = Long.parseLong(m.group(2));
  return TermIndex.newTermIndex(term, index);
}
 
Example 7
Source File: SimpleStateMachineStorage.java    From ratis with Apache License 2.0 5 votes vote down vote up
public static TermIndex getTermIndexFromSnapshotFile(File file) {
  final String name = file.getName();
  final Matcher m = SNAPSHOT_REGEX.matcher(name);
  if (!m.matches()) {
    throw new IllegalArgumentException("File \"" + file
        + "\" does not match snapshot file name pattern \""
        + SNAPSHOT_REGEX + "\"");
  }
  final long term = Long.parseLong(m.group(1));
  final long index = Long.parseLong(m.group(2));
  return TermIndex.newTermIndex(term, index);
}
 
Example 8
Source File: TestOMRatisSnapshots.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@Test
public void testInstallSnapshot() throws Exception {
  // Get the leader OM
  String leaderOMNodeId = OmFailoverProxyUtil
      .getFailoverProxyProvider(objectStore.getClientProxy())
      .getCurrentProxyOMNodeId();

  OzoneManager leaderOM = cluster.getOzoneManager(leaderOMNodeId);
  OzoneManagerRatisServer leaderRatisServer = leaderOM.getOmRatisServer();

  // Find the inactive OM
  String followerNodeId = leaderOM.getPeerNodes().get(0).getOMNodeId();
  if (cluster.isOMActive(followerNodeId)) {
    followerNodeId = leaderOM.getPeerNodes().get(1).getOMNodeId();
  }
  OzoneManager followerOM = cluster.getOzoneManager(followerNodeId);

  // Do some transactions so that the log index increases
  String userName = "user" + RandomStringUtils.randomNumeric(5);
  String adminName = "admin" + RandomStringUtils.randomNumeric(5);
  String volumeName = "volume" + RandomStringUtils.randomNumeric(5);
  String bucketName = "bucket" + RandomStringUtils.randomNumeric(5);

  VolumeArgs createVolumeArgs = VolumeArgs.newBuilder()
      .setOwner(userName)
      .setAdmin(adminName)
      .build();

  objectStore.createVolume(volumeName, createVolumeArgs);
  OzoneVolume retVolumeinfo = objectStore.getVolume(volumeName);

  retVolumeinfo.createBucket(bucketName);
  OzoneBucket ozoneBucket = retVolumeinfo.getBucket(bucketName);

  long leaderOMappliedLogIndex =
      leaderRatisServer.getLastAppliedTermIndex().getIndex();

  List<String> keys = new ArrayList<>();
  while (leaderOMappliedLogIndex < 2000) {
    keys.add(createKey(ozoneBucket));
    leaderOMappliedLogIndex =
        leaderRatisServer.getLastAppliedTermIndex().getIndex();
  }

  // Get the latest db checkpoint from the leader OM.
  OMTransactionInfo omTransactionInfo =
      OMTransactionInfo.readTransactionInfo(leaderOM.getMetadataManager());
  TermIndex leaderOMTermIndex =
      TermIndex.newTermIndex(omTransactionInfo.getCurrentTerm(),
          omTransactionInfo.getTransactionIndex());
  long leaderOMSnaphsotIndex = leaderOMTermIndex.getIndex();
  long leaderOMSnapshotTermIndex = leaderOMTermIndex.getTerm();

  DBCheckpoint leaderDbCheckpoint =
      leaderOM.getMetadataManager().getStore().getCheckpoint(false);

  // Start the inactive OM
  cluster.startInactiveOM(followerNodeId);

  // The recently started OM should be lagging behind the leader OM.
  long followerOMLastAppliedIndex =
      followerOM.getOmRatisServer().getLastAppliedTermIndex().getIndex();
  Assert.assertTrue(
      followerOMLastAppliedIndex < leaderOMSnaphsotIndex);

  // Install leader OM's db checkpoint on the lagging OM.
  File oldDbLocation = followerOM.getMetadataManager().getStore()
      .getDbLocation();
  followerOM.getOmRatisServer().getOmStateMachine().pause();
  followerOM.getMetadataManager().getStore().close();
  followerOM.replaceOMDBWithCheckpoint(leaderOMSnaphsotIndex, oldDbLocation,
      leaderDbCheckpoint.getCheckpointLocation());

  // Reload the follower OM with new DB checkpoint from the leader OM.
  followerOM.reloadOMState(leaderOMSnaphsotIndex, leaderOMSnapshotTermIndex);
  followerOM.getOmRatisServer().getOmStateMachine().unpause(
      leaderOMSnaphsotIndex, leaderOMSnapshotTermIndex);

  // After the new checkpoint is loaded and state machine is unpaused, the
  // follower OM lastAppliedIndex must match the snapshot index of the
  // checkpoint.
  followerOMLastAppliedIndex = followerOM.getOmRatisServer()
      .getLastAppliedTermIndex().getIndex();
  Assert.assertEquals(leaderOMSnaphsotIndex, followerOMLastAppliedIndex);
  Assert.assertEquals(leaderOMSnapshotTermIndex,
      followerOM.getOmRatisServer().getLastAppliedTermIndex().getTerm());

  // Verify that the follower OM's DB contains the transactions which were
  // made while it was inactive.
  OMMetadataManager followerOMMetaMngr = followerOM.getMetadataManager();
  Assert.assertNotNull(followerOMMetaMngr.getVolumeTable().get(
      followerOMMetaMngr.getVolumeKey(volumeName)));
  Assert.assertNotNull(followerOMMetaMngr.getBucketTable().get(
      followerOMMetaMngr.getBucketKey(volumeName, bucketName)));
  for (String key : keys) {
    Assert.assertNotNull(followerOMMetaMngr.getKeyTable().get(
        followerOMMetaMngr.getOzoneKey(volumeName, bucketName, key)));
  }
}
 
Example 9
Source File: OMRatisSnapshotInfo.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@Override
public TermIndex getTermIndex() {
  return TermIndex.newTermIndex(term, snapshotIndex);
}
 
Example 10
Source File: FileListSnapshotInfo.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public FileListSnapshotInfo(List<FileInfo> files, long term, long index) {
  this.termIndex = TermIndex.newTermIndex(term, index);
  this.files = Collections.unmodifiableList(files);
}
 
Example 11
Source File: ServerProtoUtils.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
static TermIndex toTermIndex(TermIndexProto p) {
  return p == null? null: TermIndex.newTermIndex(p.getTerm(), p.getIndex());
}
 
Example 12
Source File: ServerProtoUtils.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
static TermIndex toTermIndex(LogEntryProto entry) {
  return entry == null ? null :
      TermIndex.newTermIndex(entry.getTerm(), entry.getIndex());
}
 
Example 13
Source File: FileListSnapshotInfo.java    From ratis with Apache License 2.0 4 votes vote down vote up
public FileListSnapshotInfo(List<FileInfo> files, long term, long index) {
  this.termIndex = TermIndex.newTermIndex(term, index);
  this.files = Collections.unmodifiableList(files);
}
 
Example 14
Source File: ServerProtoUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
public static TermIndex toTermIndex(TermIndexProto p) {
  return p == null? null: TermIndex.newTermIndex(p.getTerm(), p.getIndex());
}
 
Example 15
Source File: ServerProtoUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
public static TermIndex toTermIndex(LogEntryProto entry) {
  return entry == null ? null :
      TermIndex.newTermIndex(entry.getTerm(), entry.getIndex());
}
 
Example 16
Source File: LogSegment.java    From ratis with Apache License 2.0 4 votes vote down vote up
LogRecord(long offset, LogEntryProto entry) {
  this.offset = offset;
  termIndex = TermIndex.newTermIndex(entry.getTerm(), entry.getIndex());
}