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

The following examples show how to use org.apache.zookeeper.data.Stat#getMtime() . 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: 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 2
Source File: ScheduleDataManager4ZK.java    From tbschedule with Apache License 2.0 6 votes vote down vote up
@Override
public int clearExpireScheduleServer(String taskType, long expireTime) throws Exception {
    int result = 0;
    String baseTaskType = ScheduleUtil.splitBaseTaskTypeFromTaskType(taskType);
    String zkPath = this.PATH_BaseTaskType + "/" + baseTaskType + "/" + taskType + "/" + this.PATH_Server;
    if (this.getZooKeeper().exists(zkPath, false) == null) {
        String tempPath = this.PATH_BaseTaskType + "/" + baseTaskType + "/" + taskType;
        if (this.getZooKeeper().exists(tempPath, false) == null) {
            this.getZooKeeper().create(tempPath, null, this.zkManager.getAcl(), CreateMode.PERSISTENT);
        }
        this.getZooKeeper().create(zkPath, null, this.zkManager.getAcl(), CreateMode.PERSISTENT);
    }
    for (String name : this.getZooKeeper().getChildren(zkPath, false)) {
        try {
            Stat stat = this.getZooKeeper().exists(zkPath + "/" + name, false);
            if (getSystemTime() - stat.getMtime() > expireTime) {
                ZKTools.deleteTree(this.getZooKeeper(), zkPath + "/" + name);
                result++;
            }
        } catch (Exception e) {
            // 当有多台服务器时,存在并发清理的可能,忽略异常
            result++;
        }
    }
    return result;
}
 
Example 3
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 4
Source File: BookKeeperDataStorageManager.java    From herddb with Apache License 2.0 6 votes vote down vote up
private String getMostRecentCheckPointFile(String dir) throws IOException, KeeperException, InterruptedException {
    String result = null;
    long lastMod = -1;
    List<String> children = ensureZNodeDirectoryAndReturnChildren(dir);

    for (String fullpath : children) {
        if (isTableOrIndexCheckpointsFile(fullpath)) {
            LOGGER.log(Level.INFO, "getMostRecentCheckPointFile on " + dir + " -> ACCEPT " + fullpath);
            Stat stat = new Stat();
            zk.ensureZooKeeper().exists(fullpath, false, null, stat);
            long ts = stat.getMtime();
            if (lastMod < 0 || lastMod < ts) {
                result = fullpath;
                lastMod = ts;
            }
        } else {
            LOGGER.log(Level.INFO, "getMostRecentCheckPointFile on " + dir + " -> SKIP " + fullpath);
        }
    }

    LOGGER.log(Level.INFO, "getMostRecentCheckPointFile on " + dir + " -> " + result);
    return result;
}
 
Example 5
Source File: ScheduleDataManager4ZK.java    From uncode-schedule with GNU General Public License v2.0 6 votes vote down vote up
public void clearExpireScheduleServer() throws Exception{
	 String zkPath = this.pathServer;
	 if(this.getZooKeeper().exists(zkPath,false)== null){
		 this.getZooKeeper().create(zkPath, null, this.zkManager.getAcl(), CreateMode.PERSISTENT);
	 }
	for (String name : this.zkManager.getZooKeeper().getChildren(zkPath, false)) {
		try {
			Stat stat = new Stat();
			this.getZooKeeper().getData(zkPath + "/" + name, null, stat);
			if (getSystemTime() - stat.getMtime() > SERVER_EXPIRE_TIME) {
				ZKTools.deleteTree(this.getZooKeeper(), zkPath + "/" + name);
				LOG.debug("ScheduleServer[" + zkPath + "/" + name + "]过期清除");
			}
		} catch (Exception e) {
			// 当有多台服务器时,存在并发清理的可能,忽略异常
		}
	}
}
 
Example 6
Source File: ScheduleDataManager4ZK.java    From uncode-schedule with Apache License 2.0 6 votes vote down vote up
public void clearExpireScheduleServer() throws Exception {
  String zkPath = this.pathServer;
  if (this.getZooKeeper().exists(zkPath, false) == null) {
    this.getZooKeeper().create(zkPath, null, this.zkManager.getAcl(), CreateMode.PERSISTENT);
  }
  for (String name : this.zkManager.getZooKeeper().getChildren(zkPath, false)) {
    try {
      Stat stat = new Stat();
      this.getZooKeeper().getData(zkPath + "/" + name, null, stat);
      if (getSystemTime() - stat.getMtime() > SERVER_EXPIRE_TIME) {
        ZKTools.deleteTree(this.getZooKeeper(), zkPath + "/" + name);
        LOG.warn("清除过期ScheduleServer[" + zkPath + "/" + name + "]");
      }
    } catch (Exception e) {
      // 当有多台服务器时,存在并发清理的可能,忽略异常
    }
  }
}
 
Example 7
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 8
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 9
Source File: ZKPathDataDumpTask.java    From helix with Apache License 2.0 5 votes vote down vote up
void dump(BaseDataAccessor<ZNRecord> accessor, String ancestorPath, long threshold,
    int maxLeafCount) {
  List<String> leafPaths = scanPath(accessor, ancestorPath);
  if (leafPaths.isEmpty()) {
    return;
  }

  Stat[] stats = accessor.getStats(leafPaths, 0);
  List<String> dumpPaths = Lists.newArrayList();
  long now = System.currentTimeMillis();
  for (int i = 0; i < stats.length; i++) {
    Stat stat = stats[i];
    if ((stats.length > maxLeafCount) || ((now - stat.getMtime()) > threshold)) {
      dumpPaths.add(leafPaths.get(i));
    }
  }

  if (!dumpPaths.isEmpty()) {
    LOG.info("Dump statusUpdates and errors records for paths: " + dumpPaths);
    // No need to fail the batch read operation even it is partial result becuase it is for cleaning up.
    List<ZNRecord> dumpRecords = accessor.get(dumpPaths, null, 0, false);
    for (ZNRecord record : dumpRecords) {
      if (record != null) {
        LOG.info(new String(_jsonSerializer.serialize(record)));
      }
    }

    // clean up
    accessor.remove(dumpPaths, 0);
    LOG.info("Remove statusUpdates and errors records for paths: " + dumpPaths);
  }
}
 
Example 10
Source File: BaragonRequestDatastore.java    From Baragon with Apache License 2.0 5 votes vote down vote up
public long getOldestQueuedRequestAge() {
  long now = System.currentTimeMillis();;
  long oldest = now;
  for (String child : getChildren(REQUEST_QUEUE_FORMAT)) {
    try {
      Stat stat = curatorFramework.checkExists().forPath(ZKPaths.makePath(REQUEST_QUEUE_FORMAT, child));
      if (stat != null && stat.getMtime() < oldest) {
        oldest = stat.getMtime();
      }
    } catch (Exception e) {
      LOG.warn("Could not check exists for queued request id {}", child);
    }
  }
  return now - oldest;
}
 
Example 11
Source File: PinotLLCRealtimeSegmentManager.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if more than {@link PinotLLCRealtimeSegmentManager::MAX_SEGMENT_COMPLETION_TIME_MILLIS} ms have elapsed since segment metadata update
 */
@VisibleForTesting
boolean isExceededMaxSegmentCompletionTime(String realtimeTableName, String segmentName, long currentTimeMs) {
  Stat stat = new Stat();
  getSegmentZKMetadata(realtimeTableName, segmentName, stat);
  if (currentTimeMs > stat.getMtime() + MAX_SEGMENT_COMPLETION_TIME_MILLIS) {
    LOGGER.info("Segment: {} exceeds the max completion time: {}ms, metadata update time: {}, current time: {}",
        segmentName, MAX_SEGMENT_COMPLETION_TIME_MILLIS, stat.getMtime(), currentTimeMs);
    return true;
  } else {
    return false;
  }
}
 
Example 12
Source File: ZKUtils.java    From Decision with Apache License 2.0 5 votes vote down vote up
private int removeOldChildZnodes(String path) throws Exception {

            int counter = 0;
            Iterator<String> children = client.getChildren().forPath(path).iterator();

            while (children.hasNext()) {

                String childrenPath = children.next();
                if (!STREAMING.ZK_HIGH_AVAILABILITY_NODE.equals('/'+childrenPath) && !STREAMING.ZK_PERSISTENCE_NODE.equals('/'+childrenPath)) {
                    if (client.getChildren().forPath(path + "/" + childrenPath).size() > 0) {
                        counter += removeOldChildZnodes(path + "/" + childrenPath);
                    } else {

                        Stat znode = client.checkExists().forPath(path + "/" + childrenPath);
                        Boolean deleteNode = true;
                        // avoid nulls and ephemeral znodes
                        if (znode != null && znode.getEphemeralOwner() == 0) {

                            String parentPath = path.substring(path.lastIndexOf("/") +1);

                            if (STREAM_OPERATIONS.SyncOperations.getAckOperations().contains(parentPath)) {

                                if ( new Date().getTime() - znode.getMtime() < MAX_LIVE_FOR_OPERATION_NODE) {
                                    deleteNode = false;
                                }
                            }

                            if (deleteNode) {
                                client.delete().deletingChildrenIfNeeded().forPath(path + "/" + childrenPath);
                                counter++;
                            }
                        }

                    }
                }
            }

            return counter;
        }
 
Example 13
Source File: TBScheduleManagerStatic.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**判断某个任务对应的线程组是否处于僵尸状态。
 * true 表示有线程组处于僵尸状态。需要告警。
 * @param type
 * @param statMap
 * @return
 * @throws Exception
 */
private boolean isExistZombieServ(String type,Map<String,Stat> statMap) throws Exception{
	boolean exist =false;
	for(String key:statMap.keySet()){
		Stat s  = statMap.get(key);
		if(this.scheduleCenter.getSystemTime() -s.getMtime()>  this.taskTypeInfo.getHeartBeatRate() * 40)
		{
			log.error("zombie serverList exists! serv="+key+" ,type="+type +"超过40次心跳周期未更新");
			exist=true;
		}
	}
	return exist;
	 
}
 
Example 14
Source File: SnapshotBiz.java    From shepher with Apache License 2.0 5 votes vote down vote up
/**
 * Get original snapshot and return id.
 *
 * @param path
 * @param cluster
 * @param creator
 * @param stat
 * @param action
 * @param createIfNotExists
 * @return snapshotId
 * @throws ShepherException
 */
public long getOriginalId(String path, String cluster, String creator, Stat stat, Action action, boolean createIfNotExists)
        throws ShepherException {
    if (StringUtils.isBlank(cluster) || StringUtils.isBlank(path) || stat == null) {
        throw ShepherException.createIllegalParameterException();
    }
    boolean exists = nodeDAO.exists(cluster, path);
    if (!exists) {
        throw ShepherException.createNoSuchNodeException();
    }

    long mtime = stat.getMtime();
    if (mtime == 0) {
        mtime = ReviewUtil.DEFAULT_MTIME;
    }

    Snapshot snapshot = this.getByPathAndZk(path, cluster, mtime, stat.getVersion());
    long snapshotId = 0L;
    if (snapshot != null) {
        snapshotId = snapshot.getId();
    } else {
        if (createIfNotExists) {
            String oldData = nodeDAO.getData(cluster, path);
            snapshotId = this.create(cluster, path, oldData, creator, action, mtime, ReviewStatus.ACCEPTED, stat.getVersion(),
                    ReviewUtil.DEFAULT_REVIEWER).getId();
        }
    }
    return snapshotId;
}
 
Example 15
Source File: ScheduleDataManager4ZK.java    From tbschedule with Apache License 2.0 5 votes vote down vote up
@Override
public void clearExpireTaskTypeRunningInfo(String baseTaskType, String serverUUID, double expireDateInternal)
    throws Exception {
    for (String name : this.getZooKeeper().getChildren(this.PATH_BaseTaskType + "/" + baseTaskType, false)) {
        String zkPath = this.PATH_BaseTaskType + "/" + baseTaskType + "/" + name + "/" + this.PATH_TaskItem;
        Stat stat = this.getZooKeeper().exists(zkPath, false);
        if (stat == null || getSystemTime() - stat.getMtime() > (long) (expireDateInternal * 24 * 3600 * 1000)) {
            ZKTools.deleteTree(this.getZooKeeper(), this.PATH_BaseTaskType + "/" + baseTaskType + "/" + name);
        }
    }
}
 
Example 16
Source File: TBScheduleManagerStatic.java    From tbschedule with Apache License 2.0 5 votes vote down vote up
/**
 * 判断某个任务对应的线程组是否处于僵尸状态。 true 表示有线程组处于僵尸状态。需要告警。
 */
private boolean isExistZombieServ(String type, Map<String, Stat> statMap) {
    boolean exist = false;
    for (String key : statMap.keySet()) {
        Stat s = statMap.get(key);
        if (this.scheduleCenter.getSystemTime() - s.getMtime() > this.taskTypeInfo.getHeartBeatRate() * 40) {
            log.error("zombie serverList exists! serv=" + key + " ,type=" + type + "超过40次心跳周期未更新");
            exist = true;
        }
    }
    return exist;

}
 
Example 17
Source File: ScheduleDataManager4ZK.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public int clearExpireScheduleServer(String taskType,long expireTime) throws Exception {
	 int result =0;
	 String baseTaskType = ScheduleUtil.splitBaseTaskTypeFromTaskType(taskType);
	 String zkPath = this.PATH_BaseTaskType + "/" + baseTaskType 
	        + "/" + taskType + "/" + this.PATH_Server;
	 if(this.getZooKeeper().exists(zkPath,false)== null){
		 String tempPath =this.PATH_BaseTaskType + "/" + baseTaskType   + "/" + taskType;
		 if(this.getZooKeeper().exists(tempPath,false)== null){
			 this.getZooKeeper().create(tempPath, null, this.zkManager.getAcl(), CreateMode.PERSISTENT);
		 }
		 this.getZooKeeper().create(zkPath, null, this.zkManager.getAcl(), CreateMode.PERSISTENT);
	 }
	for (String name : this.getZooKeeper().getChildren(zkPath, false)) {
		try {
			Stat stat = this.getZooKeeper().exists(zkPath + "/" + name,false);
			if (getSystemTime() - stat.getMtime() > expireTime) {
				ZKTools.deleteTree(this.getZooKeeper(), zkPath + "/" + name);
				result++;
			}
		} catch (Exception e) {
			// 当有多台服务器时,存在并发清理的可能,忽略异常
			result++;
		}
	}
	return result;
}
 
Example 18
Source File: ScheduleDataManager4ZK.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
public void clearExpireTaskTypeRunningInfo(String baseTaskType,String serverUUID, double expireDateInternal) throws Exception {
	for (String name : this.getZooKeeper().getChildren(this.PATH_BaseTaskType + "/" + baseTaskType, false)) {
		String zkPath = this.PATH_BaseTaskType+"/"+ baseTaskType +"/" + name +"/" + this.PATH_TaskItem;
		Stat stat = this.getZooKeeper().exists(zkPath, false);
		if (stat == null || getSystemTime() - stat.getMtime() > (long) (expireDateInternal * 24 * 3600 * 1000)) {
			ZKTools.deleteTree(this.getZooKeeper(),this.PATH_BaseTaskType +"/" + baseTaskType + "/" + name);
		}
	}
}
 
Example 19
Source File: AgentZooKeeperRegistrar.java    From helios with Apache License 2.0 4 votes vote down vote up
@Override
public boolean tryToRegister(ZooKeeperClient client)
    throws KeeperException, HostNotFoundException {
  final String idPath = Paths.configHostId(name);
  final String hostInfoPath = Paths.statusHostInfo(name);

  final Stat stat = client.exists(idPath);
  if (stat == null) {
    log.debug("Agent id node not present, registering agent {}: {}", id, name);
    ZooKeeperRegistrarUtil.registerHost(client, idPath, name, id);
  } else {
    final byte[] bytes = client.getData(idPath);
    final String existingId = bytes == null ? "" : new String(bytes, UTF_8);
    if (!id.equals(existingId)) {
      final Stat hostInfoStat = client.stat(hostInfoPath);
      if (hostInfoStat != null) {
        final long mtime = hostInfoStat.getMtime();
        final long diff = clock.now().getMillis() - mtime;
        if (diff < zooKeeperRegistrationTtlMillis) {
          final String message = format(
              "Another agent already registered as '%s' (local=%s remote=%s). "
              + "That agent's registration expires in %d seconds",
              name, id.trim(), existingId.trim(),
              TimeUnit.MILLISECONDS.toSeconds(zooKeeperRegistrationTtlMillis - diff));
          log.warn(message);
          return false;
        }

        log.info("Another agent has already registered as '{}', but its ID node was last "
                 + "updated more than {} milliseconds ago. I\'m deregistering the agent with the"
                 + "old ID of {} and replacing it with this new agent with ID '{}'.",
            name, zooKeeperRegistrationTtlMillis, existingId.trim(), id.trim());
      } else {
        log.info("Another agent has registered as '{}', but it never updated '{}' in ZooKeeper. "
                 + "I'll assume it's dead and deregister it.", name, hostInfoPath);
      }

      ZooKeeperRegistrarUtil.reRegisterHost(client, name, id);
    } else {
      log.info("Matching agent id node already present, not registering agent {}: {}", id, name);
    }
  }

  // Start the up node
  if (upNode == null) {
    final String upPath = Paths.statusHostUp(name);
    log.debug("Creating up node: {}", upPath);
    client.ensurePath(upPath, true);
    upNode = client.persistentEphemeralNode(upPath, EPHEMERAL, EMPTY_BYTES);
    upNode.start();
  }

  log.info("ZooKeeper registration complete");
  return true;
}
 
Example 20
Source File: BackupFileManager.java    From ambry with Apache License 2.0 2 votes vote down vote up
/**
 * Generate the backup filename from the given {@link Stat}. The filename contains version number and modified time
 * from record.
 * @param stat The {@link Stat}.
 * @return The filename.
 */
static String getBackupFilenameFromStat(Stat stat) {
  long mtimeInMs = stat.getMtime();
  String timestamp = LocalDateTime.ofEpochSecond(mtimeInMs / 1000, 0, zoneOffset).format(TIMESTAMP_FORMATTER);
  return stat.getVersion() + SEP + timestamp;
}