use of com.twitter.util.ConstFuture in project distributedlog by twitter.
the class BulkWriteOp method executeOp.
@Override
protected Future<BulkWriteResponse> executeOp(AsyncLogWriter writer, Sequencer sequencer, Object txnLock) {
// Need to convert input buffers to LogRecords.
List<LogRecord> records;
Future<List<Future<DLSN>>> futureList;
synchronized (txnLock) {
records = asRecordList(buffers, sequencer);
futureList = writer.writeBulk(records);
}
// Collect into a list of tries to make it easier to extract exception or DLSN.
Future<List<Try<DLSN>>> writes = asTryList(futureList);
Future<BulkWriteResponse> response = writes.flatMap(new AbstractFunction1<List<Try<DLSN>>, Future<BulkWriteResponse>>() {
@Override
public Future<BulkWriteResponse> apply(List<Try<DLSN>> results) {
// Considered a success at batch level even if no individual writes succeeed.
// The reason is that its impossible to make an appropriate decision re retries without
// individual buffer failure reasons.
List<WriteResponse> writeResponses = new ArrayList<WriteResponse>(results.size());
BulkWriteResponse bulkWriteResponse = ResponseUtils.bulkWriteSuccess().setWriteResponses(writeResponses);
// failed.
if (results.size() > 0) {
Try<DLSN> firstResult = results.get(0);
if (isDefiniteFailure(firstResult)) {
return new ConstFuture(firstResult);
}
}
// Translate all futures to write responses.
Iterator<Try<DLSN>> iterator = results.iterator();
while (iterator.hasNext()) {
Try<DLSN> completedFuture = iterator.next();
try {
DLSN dlsn = completedFuture.get();
WriteResponse writeResponse = ResponseUtils.writeSuccess().setDlsn(dlsn.serialize());
writeResponses.add(writeResponse);
successRecordCounter.inc();
} catch (Exception ioe) {
WriteResponse writeResponse = ResponseUtils.write(ResponseUtils.exceptionToHeader(ioe));
writeResponses.add(writeResponse);
if (StatusCode.FOUND == writeResponse.getHeader().getCode()) {
redirectRecordCounter.inc();
} else {
failureRecordCounter.inc();
}
}
}
return Future.value(bulkWriteResponse);
}
});
return response;
}
Aggregations