Java Code Examples for org.apache.zookeeper.data.Stat#setMtime()

The following examples show how to use org.apache.zookeeper.data.Stat#setMtime() . 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: BackupFileManagerTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Test disable backup. Every method from {@link BackupFileManager} should return null or have no effect.
 * @throws IOException if I/O error occurs
 */
@Test
public void testDisableBackupDir() throws IOException {
  helixConfigProps.remove(HelixAccountServiceConfig.BACKUP_DIRECTORY_KEY);
  VerifiableProperties vHelixConfigProps = new VerifiableProperties(helixConfigProps);
  HelixAccountServiceConfig config = new HelixAccountServiceConfig(vHelixConfigProps);
  BackupFileManager backup = new BackupFileManager(accountServiceMetrics, config);

  // getLatestAccountMap should return null
  assertNull("Disabled backup shouldn't have any state", backup.getLatestAccountMap(0));

  // persistAccountMap should not create any backup
  Stat stat = new Stat();
  stat.setVersion(1);
  stat.setMtime(System.currentTimeMillis());

  backup.persistAccountMap(new HashMap<String, String>(), stat);
  assertTrue("Disabled backup shouldn't add any backup", backup.isEmpty());
}
 
Example 2
Source File: BackupFileManagerTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Create backup files with version number without any content and return all the filenames.
 * @param backupDir The directory to create files under.
 * @param startVersion The starting version, inclusive.
 * @param endVersion The ending version, inclusive.
 * @param baseModifiedTime The starting version file's modified time.
 * @param interval The interval by which different backup files' modified time increases.
 * @param isTemp True if all the backup files should be temporary files.
 * @return Array of filenames.
 */
private String[] createBackupFilesWithVersion(Path backupDir, int startVersion, int endVersion, long baseModifiedTime,
    long interval, boolean isTemp) {
  String[] filenames = new String[endVersion - startVersion + 1];
  for (int i = startVersion; i <= endVersion; i++) {
    Stat stat = new Stat();
    stat.setVersion(i);
    stat.setMtime((baseModifiedTime + (i - startVersion) * interval) * 1000);
    String filename = BackupFileManager.getBackupFilenameFromStat(stat);
    if (isTemp) {
      filename = filename + BackupFileManager.SEP + BackupFileManager.TEMP_FILE_SUFFIX;
    }
    try {
      Files.createFile(backupDir.resolve(filename));
      filenames[i - startVersion] = filename;
    } catch (IOException e) {
      fail("Fail to create file " + filename);
    }
  }
  return filenames;
}
 
Example 3
Source File: AgentZooKeeperRegistrarTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void recentRegistrationExists_DifferentHostId() throws Exception {
  // hostInfo has been updated for this host name very recently...
  final Stat hostInfo = new Stat();
  hostInfo.setMtime(clock.now().getMillis());
  when(client.stat(hostPath)).thenReturn(hostInfo);

  // ... and the name is claimed by a different ID
  when(client.getData(idPath)).thenReturn("a different host".getBytes());

  final boolean success = registrar.tryToRegister(client);
  assertFalse(success);

  verify(client, never()).createAndSetData(idPath, hostId.getBytes());
}
 
Example 4
Source File: AgentZooKeeperRegistrarTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void oldRegistrationExists_DifferentHostId() throws Exception {
  // the hostname is claimed by a different ID...
  when(client.getData(idPath)).thenReturn("a different host".getBytes());

  // ... but the hostInfo was last updated more than TTL minutes ago
  final Stat hostInfo = new Stat();
  hostInfo.setMtime(clock.now().minus(Duration.standardMinutes(registrationTtl * 2)).getMillis());
  when(client.stat(hostPath)).thenReturn(hostInfo);

  // expect the old host to be deregistered and this registration to succeed
  final boolean success = registrar.tryToRegister(client);
  assertTrue(success);

  // expect a transaction containing a delete of the idpath followed by a create
  // // TODO (mbrown): this should really be in a test of ZooKeeperRegistrarUtil, and
  // AgentZooKeeperRegistrar should not call a static method to do this
  final ArgumentCaptor<List> opsCaptor = ArgumentCaptor.forClass(List.class);
  verify(client).transaction(opsCaptor.capture());

  // note that we are not testing full equality of the list, just that it contains
  // a few notable items
  final List<ZooKeeperOperation> actual = opsCaptor.getValue();
  assertThat(actual, hasItems(ZooKeeperOperations.delete(idPath),
      ZooKeeperOperations.create(idPath, hostId.getBytes())));
}
 
Example 5
Source File: BackupFileManagerTest.java    From ambry with Apache License 2.0 5 votes vote down vote up
/**
 * Test {@link BackupFileManager#persistAccountMap(Map, Stat)} and then recover {@link BackupFileManager} from the same backup directory.
 * @throws IOException if I/O error occurs
 */
@Test
public void testPersistAccountMapAndRecover() throws IOException {
  BackupFileManager backup = new BackupFileManager(accountServiceMetrics, config);
  final int numberAccounts = maxBackupFile * 2;
  final Map<String, String> accounts = new HashMap<>(numberAccounts);
  final Stat stat = new Stat();
  final long interval = 1;
  long modifiedTime = System.currentTimeMillis() / 1000 - numberAccounts * 2;
  for (int i = 0; i < numberAccounts; i++) {
    Account account = createRandomAccount();
    accounts.put(String.valueOf(account.getId()), account.toJson(true).toString());
    stat.setVersion(i + 1);
    stat.setMtime(modifiedTime * 1000);
    backup.persistAccountMap(accounts, stat);
    modifiedTime += interval;
  }

  for (int i = 0; i < 2; i++) {
    assertEquals("Number of backup file mismatch", maxBackupFile, backup.size());
    Map<String, String> obtained = backup.getLatestAccountMap(0);
    assertTwoStringMapsEqual(accounts, obtained);
    File[] remaingingFiles = accountBackupDir.toFile().listFiles();
    assertEquals("Remaining backup mismatch", maxBackupFile, remaingingFiles.length);

    // Recover BackupFileManager from the same backup dir
    backup = new BackupFileManager(accountServiceMetrics, config);
  }
}