@Override public void configure(Context context) { Integer capacity = context.getInteger("capacity"); keepAlive = context.getInteger("keep-alive"); if (capacity == null) { capacity = defaultCapacity; } if (keepAlive == null) { keepAlive = defaultKeepAlive; } queue = new ArrayBlockingQueue<Event>(capacity); if (channelCounter == null) { channelCounter = new ChannelCounter(getName()); } }
@Override public void put(Event event) { Preconditions.checkState(queue != null, "No queue defined (Did you forget to configure me?"); channelCounter.incrementEventPutAttemptCount(); try { queue.put(event); } catch (InterruptedException ex) { throw new ChannelException("Failed to put(" + event + ")", ex); } channelCounter.addToEventPutSuccessCount(1); channelCounter.setChannelSize(queue.size()); }
@Override public Event take() { Preconditions.checkState(queue != null, "No queue defined (Did you forget to configure me?"); channelCounter.incrementEventTakeAttemptCount(); try { Event e = queue.poll(keepAlive, TimeUnit.SECONDS); channelCounter.addToEventTakeSuccessCount(1); channelCounter.setChannelSize(queue.size()); return e; } catch (InterruptedException ex) { throw new ChannelException("Failed to take()", ex); } }
@Override public double getChannelFillPercentage() { long capacity = getChannelCapacity(); if (capacity != 0L) { return (getChannelSize() / (double)capacity) * 100; } return Double.MAX_VALUE; }
@Override public void stop() { channelCounter.setChannelSize(queue.size()); channelCounter.stop(); super.stop(); }
@Override public void start() { channelCounter.start(); channelCounter.setChannelSize(queue.size()); channelCounter.setChannelSize( Long.valueOf(queue.size() + queue.remainingCapacity())); super.start(); }
@Override public synchronized void start() { channelCounter.start(); channelCounter.setChannelSize(queue.size()); channelCounter.setChannelCapacity(Long.valueOf( queue.size() + queue.remainingCapacity())); super.start(); }
channelCounter.addToEventPutSuccessCount(puts); channelCounter.addToEventTakeSuccessCount(takes); channelCounter.setChannelSize(queue.size());
private void commitPutsToPrimary() { synchronized (queueLock) { for (Event e : putList) { if (!memQueue.offer(e)) { throw new ChannelException("Unable to insert event into memory " + "queue in spite of spare capacity, this is very unexpected"); } } drainOrder.putPrimary(putList.size()); maxMemQueueSize = (memQueue.size() > maxMemQueueSize) ? memQueue.size() : maxMemQueueSize; channelCounter.setChannelSize(memQueue.size() + drainOrder.overflowCounter); } // update counters and semaphores totalStored.release(putList.size()); channelCounter.addToEventPutSuccessCount(putList.size()); }
private void takeCommit() { if (takeCount > largestTakeTxSize) { largestTakeTxSize = takeCount; } synchronized (queueLock) { if (overflowTakeTx != null) { overflowTakeTx.commit(); } double memoryPercentFree = (memoryCapacity == 0) ? 0 : (memoryCapacity - memQueue.size() + takeCount) / (double) memoryCapacity; if (overflowActivated && memoryPercentFree >= overflowDeactivationThreshold) { overflowActivated = false; LOGGER.info("Overflow Deactivated"); } channelCounter.setChannelSize(getTotalStored()); } if (!useOverflow) { memQueRemaining.release(takeCount); bytesRemaining.release(takeListByteCount); } channelCounter.addToEventTakeSuccessCount(takeCount); }
private void commitPutsToOverflow_core(Transaction overflowPutTx) throws InterruptedException { // reattempt only once if overflow is full first time around for (int i = 0; i < 2; ++i) { try { synchronized (queueLock) { overflowPutTx.commit(); drainOrder.putOverflow(putList.size()); channelCounter.setChannelSize(memQueue.size() + drainOrder.overflowCounter); break; } } catch (ChannelFullException e) { // drop lock & reattempt if (i == 0) { Thread.sleep(overflowTimeout * 1000); } else { throw e; } } } }
@Override protected void doPut(Event event) throws InterruptedException { channelCounter.incrementEventPutAttemptCount(); putCalled = true; int eventByteSize = (int) Math.ceil(estimateEventSize(event) / avgEventSize); if (!putList.offer(event)) { throw new ChannelFullException("Put queue in " + getName() + " channel's Transaction having capacity " + putList.size() + " full, consider reducing batch size of sources"); } putListByteCount += eventByteSize; }
@Override protected Event doTake() throws InterruptedException { channelCounter.incrementEventTakeAttemptCount(); if (takeList.remainingCapacity() == 0) { throw new ChannelException("Take list for MemoryTransaction, capacity " + takeList.size() + " full, consider committing more frequently, " + "increasing capacity, or increasing thread count"); } if (!queueStored.tryAcquire(keepAlive, TimeUnit.SECONDS)) { return null; } Event event; synchronized (queueLock) { event = queue.poll(); } Preconditions.checkNotNull(event, "Queue.poll returned NULL despite semaphore " + "signalling existence of entry"); takeList.put(event); int eventByteSize = (int) Math.ceil(estimateEventSize(event) / byteCapacitySlotSize); takeByteCounter += eventByteSize; return event; }
private void commitPutsToOverflow() throws InterruptedException { overflowPutTx = getOverflowTx(); overflowPutTx.begin(); for (Event event : putList) { overflowPutTx.put(event); } commitPutsToOverflow_core(overflowPutTx); totalStored.release(putList.size()); overflowPutCount += putList.size(); channelCounter.addToEventPutSuccessCount(putList.size()); }
@Override public long getNumEvents() { return channelCounterRef.getChannelSize(); } // getNumEvents
@Override public synchronized void start() { super.start(); int overFlowCount = super.getDepth(); if (drainOrder.isEmpty()) { drainOrder.putOverflow(overFlowCount); totalStored.release(overFlowCount); } channelCounter.start(); int totalCount = overFlowCount + memQueue.size(); channelCounter.setChannelCapacity(memoryCapacity + getOverflowCapacity()); channelCounter.setChannelSize(totalCount); }
@Override public synchronized void stop() { if (getLifecycleState() == LifecycleState.STOP) { return; } channelCounter.setChannelSize(memQueue.size() + drainOrder.overflowCounter); channelCounter.stop(); super.stop(); }
channelCounter.addToEventPutSuccessCount(puts); channelCounter.addToEventTakeSuccessCount(takes); channelCounter.setChannelSize(queue.size());
@Override public void start() { channelCounter.start(); channelCounter.setChannelSize(queue.size()); channelCounter.setChannelSize( Long.valueOf(queue.size() + queue.remainingCapacity())); super.start(); }
private void commitPutsToPrimary() { synchronized (queueLock) { for (Event e : putList) { if (!memQueue.offer(e)) { throw new ChannelException("Unable to insert event into memory " + "queue in spite of spare capacity, this is very unexpected"); } } drainOrder.putPrimary(putList.size()); maxMemQueueSize = (memQueue.size() > maxMemQueueSize) ? memQueue.size() : maxMemQueueSize; channelCounter.setChannelSize(memQueue.size() + drainOrder.overflowCounter); } // update counters and semaphores totalStored.release(putList.size()); channelCounter.addToEventPutSuccessCount(putList.size()); }