/** * Get the children of the path * @param path the path * @return the children of the path */ public List<String> gets(String path){ try { if (!checkExists(path)){ return Collections.emptyList(); } return client.getChildren().forPath(path); } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Try to wait server to start * @param server the server * @return return true if server started, or false */ private Boolean tryWaitServerStart(String server) { Sleeps.sleep(serverFailoverWaitTime); // check server register? String serverPath = ZkPaths.pathOfServer(server); return zk.client().checkExists(serverPath); }
/** * Checking the job is scheduling or not * @param appName the app name * @param jobClass the job class * @return return true if the job is scheduling, or false */ public Boolean checkJobScheduling(String appName, String jobClass) { String jobPath = ZkPaths.pathOfJob(appName, jobClass); if(!zk.client().checkExists(jobPath)){ return Boolean.FALSE; } String scheduler = getJobScheduler(appName, jobClass); if(Strings.isNullOrEmpty(scheduler)){ // The scheduler is empty return Boolean.FALSE; } if(!zk.client().checkExists(ZkPaths.pathOfServer(scheduler))){ // The scheduler server offline return Boolean.FALSE; } return Boolean.TRUE; }
/** * Delete the node recursively if the path exists * @param path the node path */ public void deleteRecursivelyIfExists(String path){ try { if(checkExists(path)){ deleteRecursively(path); } } catch (Exception e){ handleConnectionLoss(e); throw new ZkException(e); } }
/** * Delete the node if the node exists * @param path node path */ public void deleteIfExists(String path) { try { if(checkExists(path)){ delete(path); } } catch (Exception e){ handleConnectionLoss(e); throw new ZkException(e); } }
/** * Get the job fire time info * @param appName the app name * @param jobClass the job class * @return the job fire time info */ public JobFireTime getJobFireTime(String appName, String jobClass){ String jobFireTimeNode = ZkPaths.pathOfJobFireTime(appName, jobClass); if (!zk.client().checkExists(jobFireTimeNode)){ return null; } return zk.client().getJson(jobFireTimeNode, JobFireTime.class); }
/** * Get the job scheduler * @param appName the app name * @param jobClass the job class * @return the job scheduler */ public String getJobScheduler(String appName, String jobClass) { String jobSchedulerNode = ZkPaths.pathOfJobScheduler(appName, jobClass); if (!zk.client().checkExists(jobSchedulerNode)){ return null; } return zk.client().getString(jobSchedulerNode); }
@Override public void run() { ZkClient zk = client.getZk(); // register period, prevent client disconnects unexpected String appClientPath = ZkPaths.pathOfAppClient(client.getAppName(), Systems.hostPid()); if (!zk.checkExists(appClientPath)){ zk.createEphemeral(appClientPath); } } }, 1, 10, TimeUnit.SECONDS);
/** * Get the job state * @param appName the app name * @param jobClass the job class * @return the job state */ public JobState getJobState(String appName, String jobClass) { String jobStateNode = ZkPaths.pathOfJobState(appName, jobClass); if (!zk.client().checkExists(jobStateNode)){ return JobState.STOPPED; } return JobState.from(zk.client().getInteger(jobStateNode)); }
@Override public void run() { String server = serverHost.get(); // mkdirs /cluster/servers if necessary zk.client().mkdirs(ZkPaths.SERVERS); // register the server node String serverPath = ZkPaths.pathOfServer(server); if (!zk.client().checkExists(serverPath)){ String result = zk.client().createEphemeral(ZkPaths.pathOfServer(server)); Logs.info("server({}) registered: {}", server, result); } } }, 1, 5, TimeUnit.SECONDS);