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

The following examples show how to use org.apache.zookeeper.data.Stat#setVersion() . 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: MockZooKeeper.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] getData(String path, Watcher watcher, Stat stat) throws KeeperException {
    mutex.lock();
    try {
        maybeThrowProgrammedFailure(Op.GET, path);
        Pair<byte[], Integer> value = tree.get(path);
        if (value == null) {
            throw new KeeperException.NoNodeException(path);
        } else {
            if (watcher != null) {
                watchers.put(path, watcher);
            }
            if (stat != null) {
                stat.setVersion(value.getRight());
            }
            return value.getLeft();
        }
    } finally {
        mutex.unlock();
    }
}
 
Example 2
Source File: MockZooKeeper.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public Stat exists(String path, boolean watch) throws KeeperException, InterruptedException {
    mutex.lock();
    try {
        maybeThrowProgrammedFailure(Op.EXISTS, path);

        if (stopped)
            throw new KeeperException.ConnectionLossException();

        if (tree.containsKey(path)) {
            Stat stat = new Stat();
            stat.setVersion(tree.get(path).getRight());
            return stat;
        } else {
            return null;
        }
    } finally {
        mutex.unlock();
    }
}
 
Example 3
Source File: MockZooKeeper.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public Stat exists(String path, Watcher watcher) throws KeeperException, InterruptedException {
    mutex.lock();
    try {
        maybeThrowProgrammedFailure(Op.EXISTS, path);

        if (stopped)
            throw new KeeperException.ConnectionLossException();

        if (watcher != null) {
            watchers.put(path, watcher);
        }

        if (tree.containsKey(path)) {
            Stat stat = new Stat();
            stat.setVersion(tree.get(path).getRight());
            return stat;
        } else {
            return null;
        }
    } finally {
        mutex.unlock();
    }
}
 
Example 4
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 5
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 6
Source File: MockCurator.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Override
public Stat forPath(String path) throws Exception {
    try {
        Node node = getNode(path, fileSystem.root());
        Stat stat = new Stat();
        stat.setVersion(node.version());
        return stat;
    }
    catch (KeeperException.NoNodeException e) {
        return null;
    }
}
 
Example 7
Source File: PinotLLCRealtimeSegmentManagerTest.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Override
LLCRealtimeSegmentZKMetadata getSegmentZKMetadata(String realtimeTableName, String segmentName,
    @Nullable Stat stat) {
  Preconditions.checkState(_segmentZKMetadataMap.containsKey(segmentName));
  if (stat != null) {
    stat.setVersion(_segmentZKMetadataVersionMap.get(segmentName));
  }
  return new LLCRealtimeSegmentZKMetadata(_segmentZKMetadataMap.get(segmentName).toZNRecord());
}
 
Example 8
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);
  }
}
 
Example 9
Source File: MockZooKeeper.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public Stat setData(final String path, byte[] data, int version) throws KeeperException, InterruptedException {
    mutex.lock();

    final Set<Watcher> toNotify = Sets.newHashSet();
    int newVersion;

    try {
        maybeThrowProgrammedFailure(Op.SET, path);

        if (stopped) {
            throw new KeeperException.ConnectionLossException();
        }

        if (!tree.containsKey(path)) {
            throw new KeeperException.NoNodeException();
        }

        int currentVersion = tree.get(path).getRight();

        // Check version
        if (version != -1 && version != currentVersion) {
            throw new KeeperException.BadVersionException(path);
        }

        newVersion = currentVersion + 1;
        log.debug("[{}] Updating -- current version: {}", path, currentVersion);
        tree.put(path, Pair.of(data, newVersion));

        toNotify.addAll(watchers.get(path));
        watchers.removeAll(path);
    } finally {
        mutex.unlock();
    }

    executor.execute(() -> {
        toNotify.forEach(watcher -> watcher
                .process(new WatchedEvent(EventType.NodeDataChanged, KeeperState.SyncConnected, path)));
    });

    Stat stat = new Stat();
    stat.setVersion(newVersion);
    return stat;
}