| 324 | |
| 325 | /* Not thread safe. Have one instance per producer thread or synchronize externally */ |
| 326 | private static class BatchInserter implements Inserter { |
| 327 | private final int batchSz; |
| 328 | private JCQueue queue; |
| 329 | private ArrayList<Object> currentBatch; |
| 330 | |
| 331 | BatchInserter(JCQueue queue, int batchSz) { |
| 332 | this.queue = queue; |
| 333 | this.batchSz = batchSz; |
| 334 | this.currentBatch = new ArrayList<>(batchSz + 1); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Blocking call - retires till element is successfully added. |
| 339 | */ |
| 340 | @Override |
| 341 | public void publish(Object obj) throws InterruptedException { |
| 342 | currentBatch.add(obj); |
| 343 | if (currentBatch.size() >= batchSz) { |
| 344 | flush(); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Non-Blocking call. return value indicates success/failure |
| 350 | */ |
| 351 | @Override |
| 352 | public boolean tryPublish(Object obj) { |
| 353 | if (currentBatch.size() >= batchSz) { |
| 354 | if (!tryFlush()) { |
| 355 | return false; |
| 356 | } |
| 357 | } |
| 358 | currentBatch.add(obj); |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Blocking call - Does not return until at least 1 element is drained or Thread.interrupt() is received. Uses backpressure wait |
| 364 | * strategy. |
| 365 | */ |
| 366 | @Override |
| 367 | public void flush() throws InterruptedException { |
| 368 | if (currentBatch.isEmpty()) { |
| 369 | return; |
| 370 | } |
| 371 | int publishCount = queue.tryPublishInternal(currentBatch); |
| 372 | int retryCount = 0; |
| 373 | while (publishCount == 0) { // retry till at least 1 element is drained |
| 374 | for (JCQueueMetrics jcQueueMetric : queue.jcqMetrics) { |
| 375 | jcQueueMetric.notifyInsertFailure(); |
| 376 | } |
| 377 | if (retryCount == 0) { // check avoids multiple log msgs when in a idle loop |
| 378 | LOG.debug("Experiencing Back Pressure when flushing batch to Q: '{}'. Entering BackPressure Wait.", |
| 379 | queue.getQueueName()); |
| 380 | } |
| 381 | retryCount = queue.backPressureWaitStrategy.idle(retryCount); |
| 382 | if (Thread.interrupted()) { |
| 383 | throw new InterruptedException(); |
nothing calls this directly
no outgoing calls
no test coverage detected