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

The following examples show how to use org.apache.zookeeper.data.Stat#getVersion() . 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: ZKHelixDataAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public List<HelixProperty.Stat> getPropertyStats(List<PropertyKey> keys) {
  if (keys == null || keys.size() == 0) {
    return Collections.emptyList();
  }

  List<HelixProperty.Stat> propertyStats = new ArrayList<>(keys.size());
  List<String> paths = new ArrayList<>(keys.size());
  for (PropertyKey key : keys) {
    paths.add(key.getPath());
  }
  Stat[] zkStats = _baseDataAccessor.getStats(paths, 0);

  for (int i = 0; i < keys.size(); i++) {
    Stat zkStat = zkStats[i];
    HelixProperty.Stat propertyStat = null;
    if (zkStat != null) {
      propertyStat =
          new HelixProperty.Stat(zkStat.getVersion(), zkStat.getCtime(), zkStat.getMtime(), zkStat.getEphemeralOwner());
    }
    propertyStats.add(propertyStat);
  }

  return propertyStats;
}
 
Example 2
Source File: MockAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public List<HelixProperty.Stat> getPropertyStats(List<PropertyKey> keys) {
  if (keys == null || keys.size() == 0) {
    return Collections.emptyList();
  }

  List<HelixProperty.Stat> propertyStats = new ArrayList<>(keys.size());
  List<String> paths = new ArrayList<>(keys.size());
  for (PropertyKey key : keys) {
    paths.add(key.getPath());
  }
  Stat[] zkStats = _baseDataAccessor.getStats(paths, 0);

  for (int i = 0; i < keys.size(); i++) {
    Stat zkStat = zkStats[i];
    HelixProperty.Stat propertyStat = null;
    if (zkStat != null) {
      propertyStat =
          new HelixProperty.Stat(zkStat.getVersion(), zkStat.getCtime(), zkStat.getMtime(),
              zkStat.getEphemeralOwner());
    }
    propertyStats.add(propertyStat);
  }

  return propertyStats;
}
 
Example 3
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public HelixProperty.Stat getPropertyStat(PropertyKey key) {
  PropertyType type = key.getType();
  String path = key.getPath();
  int options = constructOptions(type);
  try {
    Stat stat = _baseDataAccessor.getStat(path, options);
    if (stat != null) {
      return new HelixProperty.Stat(stat.getVersion(), stat.getCtime(), stat.getMtime(), stat.getEphemeralOwner());
    }
  } catch (ZkNoNodeException e) {

  }

  return null;
}
 
Example 4
Source File: PathChildrenCache.java    From xian with Apache License 2.0 6 votes vote down vote up
private void applyNewData(String fullPath, int resultCode, Stat stat, byte[] bytes)
{
    if ( resultCode == KeeperException.Code.OK.intValue() ) // otherwise - node must have dropped or something - we should be getting another event
    {
        ChildData data = new ChildData(fullPath, stat, bytes);
        ChildData previousData = currentData.put(fullPath, data);
        if ( previousData == null ) // i.e. new
        {
            offerOperation(new EventOperation(this, new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CHILD_ADDED, data)));
        }
        else if ( previousData.getStat().getVersion() != stat.getVersion() )
        {
            offerOperation(new EventOperation(this, new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CHILD_UPDATED, data)));
        }
        updateInitialSet(ZKPaths.getNodeFromPath(fullPath), data);
    }
}
 
Example 5
Source File: MockAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public HelixProperty.Stat getPropertyStat(PropertyKey key) {
  PropertyType type = key.getType();
  String path = key.getPath();
  try {
    Stat stat = _baseDataAccessor.getStat(path, 0);
    if (stat != null) {
      return new HelixProperty.Stat(stat.getVersion(), stat.getCtime(), stat.getMtime(),
          stat.getEphemeralOwner());
    }
  } catch (ZkNoNodeException e) {

  }

  return null;
}
 
Example 6
Source File: ZkUtils.java    From samza with Apache License 2.0 6 votes vote down vote up
/**
 * publish the version number of the next JobModel
 * @param oldVersion - used to validate, that no one has changed the version in the meanwhile.
 * @param newVersion - new version.
 */
public void publishJobModelVersion(String oldVersion, String newVersion) {
  Stat stat = new Stat();
  String currentVersion = zkClient.readData(keyBuilder.getJobModelVersionPath(), stat);
  metrics.reads.inc();
  LOG.info("publishing new version: " + newVersion + "; oldVersion = " + oldVersion + "(" + stat
      .getVersion() + ")");

  if (currentVersion != null && !currentVersion.equals(oldVersion)) {
    throw new SamzaException(
        "Someone changed JobModelVersion while the leader was generating one: expected" + oldVersion + ", got " + currentVersion);
  }
  // data version is the ZK version of the data from the ZK.
  int dataVersion = stat.getVersion();
  try {
    stat = zkClient.writeDataReturnStat(keyBuilder.getJobModelVersionPath(), newVersion, dataVersion);
    metrics.writes.inc();
  } catch (Exception e) {
    String msg = "publish job model version failed for new version = " + newVersion + "; old version = " + oldVersion;
    LOG.error(msg, e);
    throw new SamzaException(msg, e);
  }
  LOG.info("published new version: " + newVersion + "; expected data version = " + (dataVersion + 1) +
      "(actual data version after update = " + stat.getVersion() + ")");
}
 
Example 7
Source File: ZooKeeperStateHandleStore.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the version of the node if it exists or <code>-1</code> if it doesn't.
 *
 * @param pathInZooKeeper Path in ZooKeeper to check
 * @return Version of the ZNode if the path exists, <code>-1</code> otherwise.
 * @throws Exception If the ZooKeeper operation fails
 */
public int exists(String pathInZooKeeper) throws Exception {
	checkNotNull(pathInZooKeeper, "Path in ZooKeeper");

	final String path = normalizePath(pathInZooKeeper);

	Stat stat = client.checkExists().forPath(path);

	if (stat != null) {
		return stat.getVersion();
	}

	return -1;
}
 
Example 8
Source File: ZkSplitLogWorkerCoordination.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Try to own the task by transitioning the zk node data from UNASSIGNED to OWNED.
 * <p>
 * This method is also used to periodically heartbeat the task progress by transitioning the node
 * from OWNED to OWNED.
 * <p>
 * @param isFirstTime shows whther it's the first attempt.
 * @param zkw zk wathcer
 * @param server name
 * @param task to own
 * @param taskZKVersion version of the task in zk
 * @return non-negative integer value when task can be owned by current region server otherwise -1
 */
protected static int attemptToOwnTask(boolean isFirstTime, ZKWatcher zkw,
    ServerName server, String task, int taskZKVersion) {
  int latestZKVersion = FAILED_TO_OWN_TASK;
  try {
    SplitLogTask slt = new SplitLogTask.Owned(server);
    Stat stat = zkw.getRecoverableZooKeeper().setData(task, slt.toByteArray(), taskZKVersion);
    if (stat == null) {
      LOG.warn("zk.setData() returned null for path " + task);
      SplitLogCounters.tot_wkr_task_heartbeat_failed.increment();
      return FAILED_TO_OWN_TASK;
    }
    latestZKVersion = stat.getVersion();
    SplitLogCounters.tot_wkr_task_heartbeat.increment();
    return latestZKVersion;
  } catch (KeeperException e) {
    if (!isFirstTime) {
      if (e.code().equals(KeeperException.Code.NONODE)) {
        LOG.warn("NONODE failed to assert ownership for " + task, e);
      } else if (e.code().equals(KeeperException.Code.BADVERSION)) {
        LOG.warn("BADVERSION failed to assert ownership for " + task, e);
      } else {
        LOG.warn("failed to assert ownership for " + task, e);
      }
    }
  } catch (InterruptedException e1) {
    LOG.warn("Interrupted while trying to assert ownership of " + task + " "
        + StringUtils.stringifyException(e1));
    Thread.currentThread().interrupt();
  }
  SplitLogCounters.tot_wkr_task_heartbeat_failed.increment();
  return FAILED_TO_OWN_TASK;
}
 
Example 9
Source File: ZKSignerSecretProvider.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Pulls data from ZooKeeper.  If isInit is false, it will only parse the
 * next secret and version.  If isInit is true, it will also parse the current
 * and previous secrets, and the next rollover date; it will also init the
 * secrets.  Hence, isInit should only be true on startup.
 * @param isInit  see description above
 */
private synchronized void pullFromZK(boolean isInit) {
  try {
    Stat stat = new Stat();
    byte[] bytes = client.getData().storingStatIn(stat).forPath(path);
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    int dataVersion = bb.getInt();
    if (dataVersion > DATA_VERSION) {
      throw new IllegalStateException("Cannot load data from ZooKeeper; it"
              + "was written with a newer version");
    }
    int nextSecretLength = bb.getInt();
    byte[] nextSecret = new byte[nextSecretLength];
    bb.get(nextSecret);
    this.nextSecret = nextSecret;
    zkVersion = stat.getVersion();
    if (isInit) {
      int currentSecretLength = bb.getInt();
      byte[] currentSecret = new byte[currentSecretLength];
      bb.get(currentSecret);
      int previousSecretLength = bb.getInt();
      byte[] previousSecret = null;
      if (previousSecretLength > 0) {
        previousSecret = new byte[previousSecretLength];
        bb.get(previousSecret);
      }
      super.initSecrets(currentSecret, previousSecret);
      nextRolloverDate = bb.getLong();
    }
  } catch (Exception ex) {
    LOG.error("An unexpected exception occurred while pulling data from"
            + "ZooKeeper", ex);
  }
}
 
Example 10
Source File: LeaseManager.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
@Override
protected Scheduler scheduler() {

    final long guardLeasePeriodInMs = leasePeriodInMs / 4;

    return new AbstractScheduledService.CustomScheduler() {

        @Override
        protected Schedule getNextSchedule() throws Exception {
            if (!haveLease()) {
                // Get the current node version...
                Stat stat = zkClient.checkExists().forPath(leasePath);
                leaseNodeVersion = stat.getVersion();
                LOG.trace("{} will try to get lease (with Ver. {}) in {}ms", tsoHostAndPort, leaseNodeVersion,
                          leasePeriodInMs);
                // ...and wait the lease period
                return new Schedule(leasePeriodInMs, TimeUnit.MILLISECONDS);
            } else {
                long waitTimeInMs = getEndLeaseInMs() - System.currentTimeMillis() - guardLeasePeriodInMs;
                LOG.trace("{} will try to renew lease (with Ver. {}) in {}ms", tsoHostAndPort,
                          leaseNodeVersion, waitTimeInMs);
                return new Schedule(waitTimeInMs, TimeUnit.MILLISECONDS);
            }
        }
    };

}
 
Example 11
Source File: ZKSignerSecretProvider.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Pulls data from ZooKeeper.  If isInit is false, it will only parse the
 * next secret and version.  If isInit is true, it will also parse the current
 * and previous secrets, and the next rollover date; it will also init the
 * secrets.  Hence, isInit should only be true on startup.
 * @param isInit  see description above
 */
private synchronized void pullFromZK(boolean isInit) {
  try {
    Stat stat = new Stat();
    byte[] bytes = client.getData().storingStatIn(stat).forPath(path);
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    int dataVersion = bb.getInt();
    if (dataVersion > DATA_VERSION) {
      throw new IllegalStateException("Cannot load data from ZooKeeper; it"
              + "was written with a newer version");
    }
    int nextSecretLength = bb.getInt();
    byte[] nextSecret = new byte[nextSecretLength];
    bb.get(nextSecret);
    this.nextSecret = nextSecret;
    zkVersion = stat.getVersion();
    if (isInit) {
      int currentSecretLength = bb.getInt();
      byte[] currentSecret = new byte[currentSecretLength];
      bb.get(currentSecret);
      int previousSecretLength = bb.getInt();
      byte[] previousSecret = null;
      if (previousSecretLength > 0) {
        previousSecret = new byte[previousSecretLength];
        bb.get(previousSecret);
      }
      super.initSecrets(currentSecret, previousSecret);
      nextRolloverDate = bb.getLong();
    }
  } catch (Exception ex) {
    LOG.error("An unexpected exception occurred while pulling data from"
            + "ZooKeeper", ex);
  }
}
 
Example 12
Source File: TestLogSegmentsZK.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private static MaxLogSegmentSequenceNo getMaxLogSegmentSequenceNo(ZooKeeperClient zkc, URI uri, String streamName,
                                                                  DistributedLogConfiguration conf) throws Exception {
    Stat stat = new Stat();
    String logSegmentsPath = ZKLogMetadata.getLogSegmentsPath(
            uri, streamName, conf.getUnpartitionedStreamName());
    byte[] data = zkc.get().getData(logSegmentsPath, false, stat);
    Versioned<byte[]> maxLSSNData = new Versioned<byte[]>(data, new ZkVersion(stat.getVersion()));
    return new MaxLogSegmentSequenceNo(maxLSSNData);
}
 
Example 13
Source File: ZkCallbackCache.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void update(String path, T data, Stat stat) {
  String parentPath = HelixUtil.getZkParentPath(path);
  String childName = HelixUtil.getZkName(path);

  addToParentChildSet(parentPath, childName);
  ZNode znode = _cache.get(path);
  if (znode == null) {
    _cache.put(path, new ZNode(path, data, stat));
    fireEvents(path, EventType.NodeCreated);
  } else {
    Stat oldStat = znode.getStat();

    znode.setData(data);
    znode.setStat(stat);
    // System.out.println("\t\t--setData. path: " + path + ", data: " + data);

    if (oldStat.getCzxid() != stat.getCzxid()) {
      fireEvents(path, EventType.NodeDeleted);
      fireEvents(path, EventType.NodeCreated);
    } else if (oldStat.getVersion() != stat.getVersion()) {
      // System.out.println("\t--fireNodeChanged: " + path + ", oldVersion: " +
      // oldStat.getVersion() + ", newVersion: " + stat.getVersion());
      fireEvents(path, EventType.NodeDataChanged);
    }
  }
}
 
Example 14
Source File: ScheduleDataManager4ZK.java    From tbschedule with Apache License 2.0 5 votes vote down vote up
@Override
public long updateReloadTaskItemFlag(String taskType) throws Exception {
    String baseTaskType = ScheduleUtil.splitBaseTaskTypeFromTaskType(taskType);
    String zkPath = this.PATH_BaseTaskType + "/" + baseTaskType + "/" + taskType + "/" + this.PATH_Server;
    Stat stat = this.getZooKeeper().setData(zkPath, "reload=true".getBytes(), -1);
    return stat.getVersion();

}
 
Example 15
Source File: ScheduleDataManager4ZK.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
public long getReloadTaskItemFlag(String taskType) throws Exception{
  	String baseTaskType = ScheduleUtil.splitBaseTaskTypeFromTaskType(taskType);
String zkPath = this.PATH_BaseTaskType + "/" + baseTaskType 
        + "/" + taskType + "/" + this.PATH_Server;
Stat stat = new Stat();
this.getZooKeeper().getData(zkPath, false, stat);
  	return stat.getVersion();
  }
 
Example 16
Source File: HelixExternalViewBasedQueryQuotaManager.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
/**
 * Create a rate limiter for a table.
 * @param tableNameWithType table name with table type.
 * @param brokerResource broker resource which stores all the broker states of each table.
 * @param quotaConfig quota config of the table.
 */
private void createRateLimiter(String tableNameWithType, ExternalView brokerResource, QuotaConfig quotaConfig) {
  if (quotaConfig == null || quotaConfig.getMaxQueriesPerSecond() == null) {
    LOGGER.info("No qps config specified for table: {}", tableNameWithType);
    return;
  }

  if (brokerResource == null) {
    LOGGER.warn("Failed to init qps quota for table {}. No broker resource connected!", tableNameWithType);
    return;
  }

  Map<String, String> stateMap = brokerResource.getStateMap(tableNameWithType);
  int otherOnlineBrokerCount = 0;

  // If stateMap is null, that means this broker is the first broker for this table.
  if (stateMap != null) {
    for (Map.Entry<String, String> state : stateMap.entrySet()) {
      if (!_helixManager.getInstanceName().equals(state.getKey()) && state.getValue()
          .equals(CommonConstants.Helix.StateModel.SegmentStateModel.ONLINE)) {
        otherOnlineBrokerCount++;
      }
    }
  }

  int onlineCount = otherOnlineBrokerCount + 1;
  LOGGER.info("The number of online brokers for table {} is {}", tableNameWithType, onlineCount);

  // Get the dynamic rate
  double overallRate = quotaConfig.getMaxQPS();

  // Get stat from property store
  String tableConfigPath = constructTableConfigPath(tableNameWithType);
  Stat stat = _propertyStore.getStat(tableConfigPath, AccessOption.PERSISTENT);

  double perBrokerRate = overallRate / onlineCount;
  QueryQuotaEntity queryQuotaEntity =
      new QueryQuotaEntity(RateLimiter.create(perBrokerRate), new HitCounter(TIME_RANGE_IN_SECOND), onlineCount,
          overallRate, stat.getVersion());
  _rateLimiterMap.put(tableNameWithType, queryQuotaEntity);
  LOGGER.info(
      "Rate limiter for table: {} has been initialized. Overall rate: {}. Per-broker rate: {}. Number of online broker instances: {}. Table config stat version: {}",
      tableNameWithType, overallRate, perBrokerRate, onlineCount, stat.getVersion());
}
 
Example 17
Source File: HelixExternalViewBasedQueryQuotaManager.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
/**
 * Process query quota change when number of online brokers has changed.
 */
public void processQueryQuotaChange(ExternalView currentBrokerResource) {
  LOGGER.info("Start processing qps quota change.");
  long startTime = System.currentTimeMillis();

  if (currentBrokerResource == null) {
    LOGGER.warn("Finish processing qps quota change: external view for broker resource is null!");
    return;
  }
  int currentVersionNumber = currentBrokerResource.getRecord().getVersion();
  if (currentVersionNumber == _lastKnownBrokerResourceVersion.get()) {
    LOGGER.info("No qps quota change: external view for broker resource remains the same.");
    return;
  }

  int numRebuilt = 0;
  for (Iterator<Map.Entry<String, QueryQuotaEntity>> it = _rateLimiterMap.entrySet().iterator(); it.hasNext(); ) {
    Map.Entry<String, QueryQuotaEntity> entry = it.next();
    String tableNameWithType = entry.getKey();
    QueryQuotaEntity queryQuotaEntity = entry.getValue();

    // Get number of online brokers.
    Map<String, String> stateMap = currentBrokerResource.getStateMap(tableNameWithType);
    if (stateMap == null) {
      LOGGER.info("No broker resource for Table {}. Removing its rate limit.", tableNameWithType);
      it.remove();
      continue;
    }
    int otherOnlineBrokerCount = 0;
    for (Map.Entry<String, String> state : stateMap.entrySet()) {
      if (!_helixManager.getInstanceName().equals(state.getKey()) && state.getValue()
          .equals(CommonConstants.Helix.StateModel.SegmentStateModel.ONLINE)) {
        otherOnlineBrokerCount++;
      }
    }
    int onlineBrokerCount = otherOnlineBrokerCount + 1;

    // Get stat from property store
    String tableConfigPath = constructTableConfigPath(tableNameWithType);
    Stat stat = _propertyStore.getStat(tableConfigPath, AccessOption.PERSISTENT);
    if (stat == null) {
      LOGGER.info("Table {} has been deleted from property store. Removing its rate limit.", tableNameWithType);
      it.remove();
      continue;
    }

    // If number of online brokers and table config don't change, there is no need to re-calculate the dynamic rate.
    if (onlineBrokerCount == queryQuotaEntity.getNumOnlineBrokers() && stat.getVersion() == queryQuotaEntity
        .getTableConfigStatVersion()) {
      continue;
    }

    double overallRate;
    // Get latest quota config only if stat don't match.
    if (stat.getVersion() != queryQuotaEntity.getTableConfigStatVersion()) {
      QuotaConfig quotaConfig = getQuotaConfigFromPropertyStore(tableNameWithType);
      if (quotaConfig == null || quotaConfig.getMaxQueriesPerSecond() == null) {
        LOGGER.info("No query quota config or the config is invalid for Table {}. Removing its rate limit.",
            tableNameWithType);
        it.remove();
        continue;
      }
      overallRate = quotaConfig.getMaxQPS();
    } else {
      overallRate = queryQuotaEntity.getOverallRate();
    }
    double latestRate = overallRate / onlineBrokerCount;
    double previousRate = queryQuotaEntity.getRateLimiter().getRate();
    if (Math.abs(latestRate - previousRate) > 0.001) {
      queryQuotaEntity.getRateLimiter().setRate(latestRate);
      queryQuotaEntity.setNumOnlineBrokers(onlineBrokerCount);
      queryQuotaEntity.setOverallRate(overallRate);
      queryQuotaEntity.setTableConfigStatVersion(stat.getVersion());
      LOGGER.info(
          "Rate limiter for table: {} has been updated. Overall rate: {}. Previous per-broker rate: {}. New per-broker rate: {}. Number of online broker instances: {}",
          tableNameWithType, overallRate, previousRate, latestRate, onlineBrokerCount);
      numRebuilt++;
    }
  }
  _lastKnownBrokerResourceVersion.set(currentVersionNumber);
  long endTime = System.currentTimeMillis();
  LOGGER
      .info("Processed query quota change in {}ms, {} out of {} query quota configs rebuilt.", (endTime - startTime),
          numRebuilt, _rateLimiterMap.size());
}
 
Example 18
Source File: ZookeeperMetadataStorageManager.java    From herddb with Apache License 2.0 4 votes vote down vote up
private TableSpaceList listTablesSpaces() throws KeeperException, InterruptedException, IOException {
    Stat stat = new Stat();
    List<String> children = ensureZooKeeper().getChildren(tableSpacesPath, mainWatcher, stat);
    return new TableSpaceList(stat.getVersion(), children);
}
 
Example 19
Source File: CuratorCrud.java    From BigData-In-Practice with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    // 创建节点
    String nodePath = "/testZK"; // 节点路径
    byte[] data = "this is a test data".getBytes(); // 节点数据
    byte[] newData = "new test data".getBytes(); // 节点数据

    // 设置重连策略ExponentialBackoffRetry, baseSleepTimeMs:初始sleep的时间,maxRetries:最大重试次数,maxSleepMs:最大重试时间
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(10000, 5);
    //(推荐)curator链接zookeeper的策略:RetryNTimes n:重试的次数 sleepMsBetweenRetries:每次重试间隔的时间
    // RetryPolicy retryPolicy = new RetryNTimes(3, 5000);
    // (不推荐) curator链接zookeeper的策略:RetryOneTime sleepMsBetweenRetry:每次重试间隔的时间,这个策略只会重试一次
    // RetryPolicy retryPolicy2 = new RetryOneTime(3000);
    // 永远重试,不推荐使用
    // RetryPolicy retryPolicy3 = new RetryForever(retryIntervalMs)
    // curator链接zookeeper的策略:RetryUntilElapsed maxElapsedTimeMs:最大重试时间 sleepMsBetweenRetries:每次重试间隔 重试时间超过maxElapsedTimeMs后,就不再重试
    // RetryPolicy retryPolicy4 = new RetryUntilElapsed(2000, 3000);

    // Curator客户端
    CuratorFramework client = null;
    // 实例化Curator客户端,Curator的编程风格可以让我们使用方法链的形式完成客户端的实例化
    client = CuratorFrameworkFactory.builder()  // 使用工厂类来建造客户端的实例对象
            .connectString(zkServerIps) // 放入zookeeper服务器ip
            .sessionTimeoutMs(10000).retryPolicy(retryPolicy)  // 设定会话时间以及重连策略
            // .namespace("testApp")    // 隔离的命名空间
            .build(); // 建立连接通道
    // 启动Curator客户端
    client.start();

    boolean isZkCuratorStarted = client.getState().equals(CuratorFrameworkState.STARTED);
    System.out.println("当前客户端的状态:" + (isZkCuratorStarted ? "连接中..." : "已关闭..."));
    try {
        // 检查节点是否存在
        Stat s = client.checkExists().forPath(nodePath);
        if (s == null) {
            System.out.println("节点不存在,创建节点");
            // 创建节点
            String result = client.create()
                    .creatingParentsIfNeeded()    // 创建父节点,也就是会递归创建
                    .withMode(CreateMode.PERSISTENT) // 节点类型,PERSISTENT是持久节点
                    .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE) // 节点的ACL权限
                    .forPath(nodePath, data);
            System.out.println(result + "节点,创建成功...");
        } else {
            System.out.println("节点已存在," + s);
        }
        getData(client, nodePath);  // 输出节点信息

        // 更新指定节点的数据
        int version = s == null ? 0 : s.getVersion();  // 版本不一致时的异常:KeeperErrorCode = BadVersion
        Stat resultStat = client.setData().withVersion(version)   // 指定数据版本
                .forPath(nodePath, newData);    // 需要修改的节点路径以及新数据
        System.out.println("更新节点数据成功");
        getData(client, nodePath);  // 输出节点信息

        // 删除节点
        client.delete().guaranteed()    // 如果删除失败,那么在后端还是会继续删除,直到成功
                .deletingChildrenIfNeeded() // 子节点也一并删除,也就是会递归删除
                .withVersion(resultStat.getVersion())
                .forPath(nodePath);
        System.out.println("删除节点:" + nodePath);
        Thread.sleep(1000);
    } finally {
        // 关闭客户端
        if (!client.getState().equals(CuratorFrameworkState.STOPPED)) {
            System.out.println("关闭客户端.....");
            client.close();
        }
        isZkCuratorStarted = client.getState().equals(CuratorFrameworkState.STARTED);
        System.out.println("当前客户端的状态:" + (isZkCuratorStarted ? "连接中..." : "已关闭..."));
    }
}
 
Example 20
Source File: TestLedgerAllocator.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 60000)
public void testBadVersionOnTwoAllocators() throws Exception {
    String allocationPath = "/allocation-bad-version";
    zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Stat stat = new Stat();
    byte[] data = zkc.get().getData(allocationPath, false, stat);
    Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new LongVersion(stat.getVersion()));

    SimpleLedgerAllocator allocator1 =
            new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    SimpleLedgerAllocator allocator2 =
            new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator1.allocate();
    // wait until allocated
    ZKTransaction txn1 = newTxn();
    LedgerHandle lh = Utils.ioResult(allocator1.tryObtain(txn1, NULL_LISTENER));
    allocator2.allocate();
    ZKTransaction txn2 = newTxn();
    try {
        Utils.ioResult(allocator2.tryObtain(txn2, NULL_LISTENER));
        fail("Should fail allocating on second allocator as allocator1 is starting allocating something.");
    } catch (ZKException ke) {
        assertEquals(KeeperException.Code.BADVERSION, ke.getKeeperExceptionCode());
    }
    Utils.ioResult(txn1.execute());
    Utils.close(allocator1);
    Utils.close(allocator2);

    long eid = lh.addEntry("hello world".getBytes());
    lh.close();
    LedgerHandle readLh = bkc.get().openLedger(lh.getId(),
            BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
    Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
    int i = 0;
    while (entries.hasMoreElements()) {
        LedgerEntry entry = entries.nextElement();
        assertEquals("hello world", new String(entry.getEntry(), UTF_8));
        ++i;
    }
    assertEquals(1, i);
}