Search in sources :

Example 1 with BulkWriteResponse

use of com.twitter.distributedlog.thrift.service.BulkWriteResponse in project distributedlog by twitter.

the class TestDistributedLogServer method testBulkWriteTotalFailureLostLock.

@Test(timeout = 60000)
public void testBulkWriteTotalFailureLostLock() throws Exception {
    String name = String.format("dlserver-bulk-write-%s", "lost-lock");
    dlClient.routingService.addHost(name, dlServer.getAddress());
    final int writeCount = 8;
    List<ByteBuffer> writes = new ArrayList<ByteBuffer>(writeCount + 1);
    ByteBuffer buf = ByteBuffer.allocate(8);
    writes.add(buf);
    for (long i = 1; i <= writeCount; i++) {
        writes.add(ByteBuffer.wrap(("" + i).getBytes()));
    }
    // Warm it up with a write.
    Await.result(dlClient.dlClient.write(name, ByteBuffer.allocate(8)));
    // Failpoint a lost lock, make sure the failure gets promoted to an operation failure.
    DistributedLogServiceImpl svcImpl = (DistributedLogServiceImpl) dlServer.dlServer.getLeft();
    try {
        FailpointUtils.setFailpoint(FailpointUtils.FailPointName.FP_WriteInternalLostLock, FailpointUtils.FailPointActions.FailPointAction_Default);
        Future<BulkWriteResponse> futures = svcImpl.writeBulkWithContext(name, writes, new WriteContext());
        assertEquals(StatusCode.LOCKING_EXCEPTION, Await.result(futures).header.code);
    } finally {
        FailpointUtils.removeFailpoint(FailpointUtils.FailPointName.FP_WriteInternalLostLock);
    }
}
Also used : ArrayList(java.util.ArrayList) BulkWriteResponse(com.twitter.distributedlog.thrift.service.BulkWriteResponse) ByteBuffer(java.nio.ByteBuffer) WriteContext(com.twitter.distributedlog.thrift.service.WriteContext) Test(org.junit.Test)

Example 2 with BulkWriteResponse

use of com.twitter.distributedlog.thrift.service.BulkWriteResponse 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;
}
Also used : DLSN(com.twitter.distributedlog.DLSN) BulkWriteResponse(com.twitter.distributedlog.thrift.service.BulkWriteResponse) WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) ConstFuture(com.twitter.util.ConstFuture) BulkWriteResponse(com.twitter.distributedlog.thrift.service.BulkWriteResponse) AlreadyClosedException(com.twitter.distributedlog.exceptions.AlreadyClosedException) DLException(com.twitter.distributedlog.exceptions.DLException) LockingException(com.twitter.distributedlog.exceptions.LockingException) RequestDeniedException(com.twitter.distributedlog.exceptions.RequestDeniedException) OwnershipAcquireFailedException(com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException) LogRecord(com.twitter.distributedlog.LogRecord) Iterator(java.util.Iterator) Future(com.twitter.util.Future) ConstFuture(com.twitter.util.ConstFuture) ArrayList(java.util.ArrayList) List(java.util.List) Try(com.twitter.util.Try)

Aggregations

BulkWriteResponse (com.twitter.distributedlog.thrift.service.BulkWriteResponse)2 ArrayList (java.util.ArrayList)2 DLSN (com.twitter.distributedlog.DLSN)1 LogRecord (com.twitter.distributedlog.LogRecord)1 AlreadyClosedException (com.twitter.distributedlog.exceptions.AlreadyClosedException)1 DLException (com.twitter.distributedlog.exceptions.DLException)1 LockingException (com.twitter.distributedlog.exceptions.LockingException)1 OwnershipAcquireFailedException (com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException)1 RequestDeniedException (com.twitter.distributedlog.exceptions.RequestDeniedException)1 WriteContext (com.twitter.distributedlog.thrift.service.WriteContext)1 WriteResponse (com.twitter.distributedlog.thrift.service.WriteResponse)1 ConstFuture (com.twitter.util.ConstFuture)1 Future (com.twitter.util.Future)1 Try (com.twitter.util.Try)1 ByteBuffer (java.nio.ByteBuffer)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Test (org.junit.Test)1