/** Adds a job into the configuration. */ @Override public void addJob(final Job job) throws JobExistsException { log.info("adding job: {}", job); final JobId id = job.getId(); final UUID operationId = UUID.randomUUID(); final String creationPath = Paths.configJobCreation(id, operationId); final ZooKeeperClient client = provider.get("addJob"); try { try { client.ensurePath(Paths.historyJob(id)); client.transaction( create(Paths.configJob(id), job), create(Paths.configJobRefShort(id), id), create(Paths.configJobHosts(id)), create(creationPath)); } catch (final NodeExistsException e) { if (client.exists(creationPath) != null) { // The job was created, we're done here return; } throw new JobExistsException(id.toString()); } } catch (final KeeperException e) { throw new HeliosRuntimeException("adding job " + job + " failed", e); } }
/** * 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); } }