/** * 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); } }
/** * Create an ephemeral path * @param path path * @return the path created */ public String createEphemeral(String path) { return createEphemeral(path, (byte[]) null); }
private void getServersOnce() { List<String> servers = zk.gets(ZkPaths.SERVERS); if (servers.isEmpty()){ log.warn("there are no available servers, please check the environment."); return; } for (String server: servers){ client.addHttpServer(server); } }
/** * 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); } }
/** * Create an persistent path * @param path path * @param data string data * @return the path created */ public String create(String path, String data){ try { return create(path, data.getBytes("UTF-8")); } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
@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);
/** * Update the job fire time info * @param appName the app name * @param jobClass the job class * @param jobFireTime the job fire time * @return return true if update successfully, or false */ public Boolean updateJobFireTime(String appName, String jobClass, JobFireTime jobFireTime) { String jobFireTimeNode = ZkPaths.pathOfJobFireTime(appName, jobClass); zk.client().mkdirs(jobFireTimeNode); return zk.client().update(jobFireTimeNode, JSON.toJSONString(jobFireTime)); }
@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);
public AppClientCluster(AntaresZkClient zk, String appName){ this.appName = appName; this.zk = zk; // get alive clients once String appClientsPath = ZkPaths.pathOfAppClients(appName); zk.client().mkdirs(appClientsPath); List<String> clients = zk.client().gets(appClientsPath); if (!CollectionUtil.isNullOrEmpty(clients)){ alives.addAll(clients); } }
/** * 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); }
public Boolean update(String path){ return update(path, (byte[])null); }
/** * new a watcher of path child * @param path the parent path * @param listener a listener * NOTE: * Only watch first level children, not recursive */ public ChildWatcher newChildWatcher(String path, ChildListener listener) { return newChildWatcher(path, listener, Boolean.TRUE); }
/** * 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); } }
/** * 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 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)); }
/** * 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); }
@Override public Response<List<ServerInfo>> listServers() { try { List<String> servers = zk.client().gets(ZkPaths.SERVERS); if (CollectionUtil.isNullOrEmpty(servers)){ return Response.ok(Collections.<ServerInfo>emptyList()); } String leader = zk.client().getString(ZkPaths.LEADER); List<ServerInfo> serverInfos = Lists.newArrayListWithExpectedSize(servers.size()); ServerInfo serverInfo; for (String server: servers){ serverInfo = new ServerInfo(); if (Objects.equal(server, leader)){ serverInfo.setLeader(true); } serverInfo.setServer(server); serverInfo.setJobCount(jobServerDao.countJobsByServer(server).intValue()); serverInfos.add(serverInfo); } return Response.ok(serverInfos); } catch (Exception e){ Logs.error("failed to list servers, cause: {}", Throwables.getStackTraceAsString(e)); return Response.notOk("server.list.failed"); } }
/** * Create a client instancclientAppPathExiste * @param hosts host strings: zk01:2181,zk02:2181,zk03:2181 * @param namespace path root, such as app name */ public static ZkClient newClient(String hosts, String namespace){ return newClient(hosts, namespace, DEFAULT_RETRY_STRATEGY); }
@Override public void doStart() { String jobInstancesLockPath = Lock.PREFIX + ZkPaths.JOB_INSTANCES; zk.client().mkdirs(jobInstancesLockPath); emptyChildCleaner = new ChildReaper(zk.client().client(), jobInstancesLockPath, Reaper.Mode.REAP_INDEFINITELY); try { String serversFailover = Lock.PREFIX + ZkPaths.SERVER_FAILOVER; zk.client().mkdirs(serversFailover); emptyChildCleaner.addPath(serversFailover); emptyChildCleaner.start(); } catch (Exception e) { throw new ZkException(e); } }
/** * Make the job instances node * @param appName the app name * @param jobClass the job class * @return return true if make successfully, or false */ public Boolean mkJobInstances(String appName, String jobClass) { return zk.client().mkdirs(ZkPaths.pathOfJobInstances(appName, jobClass)); }