List of usage examples for org.apache.zookeeper.recipes.lock WriteLock lock
public synchronized boolean lock() throws KeeperException, InterruptedException
From source file:com.jointhegrid.ironcount.manager.WorkloadManager.java
License:Apache License
public void considerStartingWorkload(Workload w) { logger.debug("considert starting " + w); if (w.active == false) { logger.debug(w.name + " is non active"); return;/*w w w. ja v a2 s . c o m*/ } WriteLock l = null; try { l = new WriteLock(zk, "/ironcount/workloads/" + w.name, null); l.lock(); List<String> children = zk.getChildren("/ironcount/workloads/" + w.name, false); if (children.size() <= w.maxWorkers) { WorkerThread wt = new WorkerThread(this, w); this.executor.submit(wt); this.workerThreads.put(wt, new Object()); logger.debug("Started worker thread " + wt + " " + w); } } catch (KeeperException ex) { throw new RuntimeException(ex); } catch (InterruptedException ex) { throw new RuntimeException(ex); } catch (Throwable t) { t.printStackTrace(System.err); logger.error(t); throw new RuntimeException(t); } finally { l.unlock(); } }
From source file:io.teknek.daemon.TeknekDaemon.java
License:Apache License
private void considerStarting(String child) throws WorkerDaoException { Plan plan = WorkerDao.findPlanByName(reKeeper.getZooKeeper(), child); if (plan == null) { logger.warn(String.format("Did not find a valid plan under node name %s", child)); return;// ww w . j a v a 2 s .com } if (!child.equals(plan.getName())) { logger.warn(String.format("Node name %s is not the same is the json value %s will not start", child, plan.getName())); return; } List<String> workerUuidsWorkingOnPlan = WorkerDao.findWorkersWorkingOnPlan(reKeeper.getZooKeeper(), plan); if (alreadyAtMaxWorkersPerNode(plan, workerUuidsWorkingOnPlan, workerThreads.get(plan))) { return; } if (!isPlanSane(plan)) { return; } logger.debug("trying to acqure lock on " + WorkerDao.LOCKS_ZK + "/" + plan.getName()); WorkerDao.maybeCreatePlanLockDir(reKeeper.getZooKeeper(), plan); final CountDownLatch c = new CountDownLatch(1); WriteLock l = new WriteLock(reKeeper.getZooKeeper(), WorkerDao.LOCKS_ZK + "/" + plan.getName(), null); l.setLockListener(new LockListener() { @Override public void lockAcquired() { logger.debug(myId + " counting down"); c.countDown(); } @Override public void lockReleased() { logger.debug(myId + " released"); } }); boolean hasLatch = false; try { boolean gotLock = l.lock(); if (!gotLock) { logger.debug("did not get lock"); return; } hasLatch = c.await(3000, TimeUnit.MILLISECONDS); if (hasLatch) { plan = WorkerDao.findPlanByName(reKeeper.getZooKeeper(), child); if (plan.isDisabled()) { logger.debug("disabled " + plan.getName()); return; } List<String> workerUuids = WorkerDao.findWorkersWorkingOnPlan(reKeeper.getZooKeeper(), plan); if (workerUuids.size() >= plan.getMaxWorkers()) { logger.debug("already running max children:" + workerUuids.size() + " planmax:" + plan.getMaxWorkers() + " running:" + workerUuids); return; } logger.debug("starting worker"); try { Worker worker = new Worker(plan, workerUuids, this); worker.init(); worker.start(); addWorkerToList(plan, worker); } catch (RuntimeException e) { throw new WorkerStartException(e); } } } catch (KeeperException | InterruptedException | WorkerDaoException | WorkerStartException e) { logger.warn("getting lock", e); } finally { try { l.unlock(); } catch (RuntimeException ex) { logger.warn("Unable to unlock/cleanup. hadlock?" + hasLatch, ex); } } }