private JobId jobIdFromTaskPath(final String path) { final String prefix = Paths.configHostJobs(agent) + "/"; return JobId.fromString(path.replaceFirst(prefix, "")); }
public ZooKeeperAgentModel(final ZooKeeperClientProvider provider, final String host, final Path stateDirectory, final TaskHistoryWriter historyWriter, final List<EventSender> eventSenders, final String taskStatusEventTopic) throws IOException, InterruptedException { // TODO(drewc): we're constructing too many heavyweight things in the ctor, these kinds of // things should be passed in/provider'd/etc. final ZooKeeperClient client = provider.get("ZooKeeperAgentModel_ctor"); this.agent = checkNotNull(host); final Path taskConfigFile = stateDirectory.resolve(TASK_CONFIG_FILENAME); this.tasks = client.pathChildrenCache(Paths.configHostJobs(host), taskConfigFile, Json.type(Task.class)); tasks.addListener(new JobsListener()); final Path taskStatusFile = stateDirectory.resolve(TASK_STATUS_FILENAME); this.taskStatuses = ZooKeeperUpdatingPersistentDirectory.create("agent-model-task-statuses", provider, taskStatusFile, Paths.statusHostJobs(host)); this.historyWriter = historyWriter; this.eventSenders = eventSenders; this.taskStatusEventTopic = taskStatusEventTopic; }
private Map<JobId, Deployment> getTasks(final ZooKeeperClient client, final String host) { final Map<JobId, Deployment> jobs = Maps.newHashMap(); try { final String folder = Paths.configHostJobs(host); final List<String> jobIds; try { jobIds = client.getChildren(folder); } catch (KeeperException.NoNodeException e) { log.warn("Unable to get deployment config for {}", host, e); return ImmutableMap.of(); } for (final String jobIdString : jobIds) { final JobId jobId = JobId.fromString(jobIdString); final String containerPath = Paths.configHostJob(host, jobId); try { final byte[] data = client.getData(containerPath); final Task task = parse(data, Task.class); jobs.put(jobId, Deployment.of(jobId, task.getGoal(), task.getDeployerUser(), task.getDeployerMaster(), task.getDeploymentGroupName())); } catch (KeeperException.NoNodeException ignored) { log.debug("deployment config node disappeared: {}", jobIdString); } } } catch (KeeperException | IOException e) { throw new HeliosRuntimeException("getting deployment config failed", e); } return jobs; }
public static void registerHost(final ZooKeeperClient client, final String idPath, final String hostname, final String hostId) throws KeeperException { log.info("registering host: {}", hostname); // This would've been nice to do in a transaction but PathChildrenCache ensures paths // so we can't know what paths already exist so assembling a suitable transaction is too // painful. client.ensurePath(Paths.configHost(hostname)); client.ensurePath(Paths.configHostJobs(hostname)); client.ensurePath(Paths.configHostPorts(hostname)); client.ensurePath(Paths.statusHost(hostname)); client.ensurePath(Paths.statusHostJobs(hostname)); // Finish registration by creating the id node last client.createAndSetData(idPath, hostId.getBytes(UTF_8)); }
final List<String> jobs = safeGetChildren(client, Paths.configHostJobs(host)); operations.add(delete(Paths.configHostJobs(host)));
private JobId jobIdFromTaskPath(final String path) { final String prefix = Paths.configHostJobs(agent) + "/"; return JobId.fromString(path.replaceFirst(prefix, "")); }
public ZooKeeperAgentModel(final ZooKeeperClientProvider provider, final KafkaClientProvider kafkaProvider, final String host, final Path stateDirectory) throws IOException, InterruptedException { // TODO(drewc): we're constructing too many heavyweight things in the ctor, these kinds of // things should be passed in/provider'd/etc. final ZooKeeperClient client = provider.get("ZooKeeperAgentModel_ctor"); this.agent = checkNotNull(host); final Path taskConfigFile = stateDirectory.resolve(TASK_CONFIG_FILENAME); this.tasks = client.pathChildrenCache(Paths.configHostJobs(host), taskConfigFile, Json.type(Task.class)); tasks.addListener(new JobsListener()); final Path taskStatusFile = stateDirectory.resolve(TASK_STATUS_FILENAME); this.taskStatuses = ZooKeeperUpdatingPersistentDirectory.create("agent-model-task-statuses", provider, taskStatusFile, Paths.statusHostJobs(host)); this.historyWriter = new TaskHistoryWriter( host, client, stateDirectory.resolve(TASK_HISTORY_FILENAME)); this.kafkaSender = new KafkaSender( kafkaProvider.getProducer(new StringSerializer(), new ByteArraySerializer())); }
client.ensurePath(Paths.configHostJobs(name)); client.ensurePath(Paths.configHostPorts(name)); client.ensurePath(Paths.statusHost(name));
private Map<JobId, Deployment> getTasks(final ZooKeeperClient client, final String host) { final Map<JobId, Deployment> jobs = Maps.newHashMap(); try { final String folder = Paths.configHostJobs(host); final List<String> jobIds; try { jobIds = client.getChildren(folder); } catch (KeeperException.NoNodeException e) { return null; } for (final String jobIdString : jobIds) { final JobId jobId = JobId.fromString(jobIdString); final String containerPath = Paths.configHostJob(host, jobId); try { final byte[] data = client.getData(containerPath); final Task task = parse(data, Task.class); jobs.put(jobId, Deployment.of(jobId, task.getGoal(), task.getDeployerUser(), task.getDeployerMaster(), task.getDeploymentGroupName())); } catch (KeeperException.NoNodeException ignored) { log.debug("deployment config node disappeared: {}", jobIdString); } } } catch (KeeperException | IOException e) { throw new HeliosRuntimeException("getting deployment config failed", e); } return jobs; }
operations.add(delete(Paths.configHostJobs(host)));
/** * Registers a host into ZooKeeper. The {@code id} is initially generated randomly by the Agent * and persisted on disk. This way, in the event that you have two agents attempting to register * with the same value of @{code host}, the first one will win. */ @Override public void registerHost(final String host, final String id) { log.info("registering host: {}", host); final ZooKeeperClient client = provider.get("registerHost"); try { // TODO (dano): this code is replicated in AgentZooKeeperRegistrar // This would've been nice to do in a transaction but PathChildrenCache ensures paths // so we can't know what paths already exist so assembling a suitable transaction is too // painful. client.ensurePath(Paths.configHost(host)); client.ensurePath(Paths.configHostJobs(host)); client.ensurePath(Paths.configHostPorts(host)); client.ensurePath(Paths.statusHost(host)); client.ensurePath(Paths.statusHostJobs(host)); // Finish registration by creating the id node last client.createAndSetData(Paths.configHostId(host), id.getBytes(UTF_8)); } catch (Exception e) { throw new HeliosRuntimeException("registering host " + host + " failed", e); } }