/** * Non blocking call. tries to flush as many as possible. Returns true if at least one from non-empty currentBatch was flushed or if * currentBatch is empty. Returns false otherwise */ @Override public boolean tryFlush() { if (currentBatch.isEmpty()) { return true; } int publishCount = q.tryPublishInternal(currentBatch); if (publishCount == 0) { q.metrics.notifyInsertFailure(); return false; } else { currentBatch.subList(0, publishCount).clear(); return true; } } } // class BatchInserter
/** * Blocking call - Does not return until at least 1 element is drained or Thread.interrupt() is received. Uses backpressure wait * strategy. */ @Override public void flush() throws InterruptedException { if (currentBatch.isEmpty()) { return; } int publishCount = q.tryPublishInternal(currentBatch); int retryCount = 0; while (publishCount == 0) { // retry till at least 1 element is drained q.metrics.notifyInsertFailure(); if (retryCount == 0) { // check avoids multiple log msgs when in a idle loop LOG.debug("Experiencing Back Pressure when flushing batch to Q: {}. Entering BackPressure Wait.", q.getName()); } retryCount = q.backPressureWaitStrategy.idle(retryCount); if (Thread.interrupted()) { throw new InterruptedException(); } publishCount = q.tryPublishInternal(currentBatch); } currentBatch.subList(0, publishCount).clear(); }
/** * Blocking call, that can be interrupted via Thread.interrupt */ @Override public void publish(Object obj) throws InterruptedException { boolean inserted = q.tryPublishInternal(obj); int idleCount = 0; while (!inserted) { q.metrics.notifyInsertFailure(); if (idleCount == 0) { // check avoids multiple log msgs when in a idle loop LOG.debug("Experiencing Back Pressure on recvQueue: '{}'. Entering BackPressure Wait", q.getName()); } idleCount = q.backPressureWaitStrategy.idle(idleCount); if (Thread.interrupted()) { throw new InterruptedException(); } inserted = q.tryPublishInternal(obj); } }
/** * Non-Blocking call. return value indicates success/failure */ @Override public boolean tryPublish(Object obj) { boolean inserted = q.tryPublishInternal(obj); if (!inserted) { q.metrics.notifyInsertFailure(); return false; } return true; }