private synchronized TrackingQueue getQueue() { if (queue == null) { queue = new ElementsTrackingQueue(new ZKElementsTracking(zkClient, zkQueuePath)); } return queue; }
@Override public Element startConsuming(String consumerId) { try { globalLock.get().lock(); try { Entry entry = getNotStartedWithHighestPriority(); if (entry == null) { return null; } entry.consumerId = consumerId; entry.lastProgressReportTs = System.currentTimeMillis(); entry.priority = getCurrentHighestPriority(); queueElements.put(entry.element.getId(), entry); return entry.element; } finally { globalLock.get().unlock(); } } catch (Exception e) { LOG.error("error during start consuming", e); return null; } }
@Override public boolean stopConsumingAndAddBackToQueue(String elementId, String consumerId) { try { globalLock.get().lock(); try { Entry entry = queueElements.get(elementId); if (entry == null || !consumerId.equals(entry.consumerId)) { return false; } stopAndReschedule(entry); } finally { globalLock.get().unlock(); } } catch (Exception e) { LOG.error("error during stop & reschedule", e); // it is OK to leave it now in "in-progress" list - we'll attempt to reschedule it by timeout } // TODO: what if we failed? return true; }
@Override public boolean addToQueue(Element element) { try { // we actually may need no lock here: we just adding new element (it is assumed that test adds unique // elems into queue) globalLock.get().lock(); try { Entry entry = new Entry(element, getCurrentHighestPriority()); queueElements.put(entry.element.getId(), entry); return true; } finally { globalLock.get().unlock(); } } catch (Exception e) { LOG.error("error during adding to queue", e); return false; } }
@Override protected ElementsTrackingQueue getQueue() throws Exception { String queueName = "/tracking-queue"; System.out.println("queue name: " + queueName); queue = new ElementsTrackingQueue(new ZKElementsTracking(zkClient, queueName)); return queue; } }
@Override public void walkThruElementsBeingConsumed(Walker walker) { try { globalLock.get().lock(); try { for (Entry entry : queueElements.values()) { // we are walking thru *all* element items, hence need to skip those not in progress if (NO_CONSUMER_ASSIGNED.equals(entry.consumerId)) { continue; } boolean stopAndReschedule = walker.process(entry.element, entry.consumerId, entry.lastProgressReportTs); if (stopAndReschedule) { stopAndReschedule(entry); } } } finally { globalLock.get().unlock(); } } catch (Exception e) { LOG.error("error during walking", e); // should be OK not do anything: we'll walk thru next time :) } }
@Before public void beforeCliTest() throws Exception { queueName = "/tracking-queue"; queue = new ElementsTrackingQueue(new ZKElementsTracking(zkClient, queueName)); }
ElementsTrackingQueue queue = new ElementsTrackingQueue(new ZKElementsTracking(zkClient, queueName)); try { if (CMD_LIST.equals(command)) {