Channel channel = getChannel();
Transaction txn = channel.getTransaction();
try {
txn.begin();
String indexName = getIndexName();
BulkRequestBuilder bulkRequest = client.prepareBulk();
for (int i = 0; i < batchSize; i++) {
Event event = channel.take();
if (event == null) {
break;
}
XContentBuilder builder = serializer.getContentBuilder(event);
IndexRequestBuilder request = client.prepareIndex(indexName, indexType)
.setSource(builder);
if (ttlMs > 0) {
request.setTTL(ttlMs);
}
bulkRequest.add(request);
}
int size = bulkRequest.numberOfActions();
if (size <= 0) {
sinkCounter.incrementBatchEmptyCount();
counterGroup.incrementAndGet("channel.underflow");
status = Status.BACKOFF;
} else {
if (size < batchSize) {
sinkCounter.incrementBatchUnderflowCount();
status = Status.BACKOFF;
} else {
sinkCounter.incrementBatchCompleteCount();
}
sinkCounter.addToEventDrainAttemptCount(size);
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
throw new EventDeliveryException(bulkResponse.buildFailureMessage());
}
}
txn.commit();