use of org.apache.bookkeeper.proto.BookkeeperProtocol.AddRequest in project bookkeeper by apache.
the class TestBookieRequestProcessor method testFlagsV3.
@Test
public void testFlagsV3() {
ReadRequest read = ReadRequest.newBuilder().setLedgerId(10).setEntryId(1).setFlag(ReadRequest.Flag.FENCE_LEDGER).build();
assertTrue(RequestUtils.hasFlag(read, ReadRequest.Flag.FENCE_LEDGER));
assertFalse(RequestUtils.hasFlag(read, ReadRequest.Flag.ENTRY_PIGGYBACK));
read = ReadRequest.newBuilder().setLedgerId(10).setEntryId(1).setFlag(ReadRequest.Flag.ENTRY_PIGGYBACK).build();
assertFalse(RequestUtils.hasFlag(read, ReadRequest.Flag.FENCE_LEDGER));
assertTrue(RequestUtils.hasFlag(read, ReadRequest.Flag.ENTRY_PIGGYBACK));
read = ReadRequest.newBuilder().setLedgerId(10).setEntryId(1).build();
assertFalse(RequestUtils.hasFlag(read, ReadRequest.Flag.FENCE_LEDGER));
assertFalse(RequestUtils.hasFlag(read, ReadRequest.Flag.ENTRY_PIGGYBACK));
AddRequest add = AddRequest.newBuilder().setLedgerId(10).setEntryId(1).setFlag(AddRequest.Flag.RECOVERY_ADD).setMasterKey(ByteString.EMPTY).setBody(ByteString.EMPTY).build();
assertTrue(RequestUtils.hasFlag(add, AddRequest.Flag.RECOVERY_ADD));
add = AddRequest.newBuilder().setLedgerId(10).setEntryId(1).setMasterKey(ByteString.EMPTY).setBody(ByteString.EMPTY).build();
assertFalse(RequestUtils.hasFlag(add, AddRequest.Flag.RECOVERY_ADD));
add = AddRequest.newBuilder().setLedgerId(10).setEntryId(1).setFlag(AddRequest.Flag.RECOVERY_ADD).setMasterKey(ByteString.EMPTY).setBody(ByteString.EMPTY).build();
assertTrue(RequestUtils.hasFlag(add, AddRequest.Flag.RECOVERY_ADD));
}
use of org.apache.bookkeeper.proto.BookkeeperProtocol.AddRequest in project bookkeeper by apache.
the class WriteEntryProcessorV3 method getAddResponse.
// Returns null if there is no exception thrown
private AddResponse getAddResponse() {
final long startTimeNanos = MathUtils.nowInNano();
AddRequest addRequest = request.getAddRequest();
long ledgerId = addRequest.getLedgerId();
long entryId = addRequest.getEntryId();
final AddResponse.Builder addResponse = AddResponse.newBuilder().setLedgerId(ledgerId).setEntryId(entryId);
if (!isVersionCompatible()) {
addResponse.setStatus(StatusCode.EBADVERSION);
return addResponse.build();
}
if (requestProcessor.bookie.isReadOnly()) {
logger.warn("BookieServer is running as readonly mode, so rejecting the request from the client!");
addResponse.setStatus(StatusCode.EREADONLY);
return addResponse.build();
}
BookkeeperInternalCallbacks.WriteCallback wcb = new BookkeeperInternalCallbacks.WriteCallback() {
@Override
public void writeComplete(int rc, long ledgerId, long entryId, BookieSocketAddress addr, Object ctx) {
if (BookieProtocol.EOK == rc) {
requestProcessor.addEntryStats.registerSuccessfulEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
} else {
requestProcessor.addEntryStats.registerFailedEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
}
StatusCode status;
switch(rc) {
case BookieProtocol.EOK:
status = StatusCode.EOK;
break;
case BookieProtocol.EIO:
status = StatusCode.EIO;
break;
default:
status = StatusCode.EUA;
break;
}
addResponse.setStatus(status);
Response.Builder response = Response.newBuilder().setHeader(getHeader()).setStatus(addResponse.getStatus()).setAddResponse(addResponse);
Response resp = response.build();
sendResponse(status, resp, requestProcessor.addRequestStats);
}
};
final EnumSet<WriteFlag> writeFlags;
if (addRequest.hasWriteFlags()) {
writeFlags = WriteFlag.getWriteFlags(addRequest.getWriteFlags());
} else {
writeFlags = EnumSet.noneOf(WriteFlag.class);
}
final boolean ackBeforeSync = writeFlags.contains(WriteFlag.DEFERRED_SYNC);
StatusCode status = null;
byte[] masterKey = addRequest.getMasterKey().toByteArray();
ByteBuf entryToAdd = Unpooled.wrappedBuffer(addRequest.getBody().asReadOnlyByteBuffer());
try {
if (RequestUtils.hasFlag(addRequest, AddRequest.Flag.RECOVERY_ADD)) {
requestProcessor.bookie.recoveryAddEntry(entryToAdd, wcb, channel, masterKey);
} else {
requestProcessor.bookie.addEntry(entryToAdd, ackBeforeSync, wcb, channel, masterKey);
}
status = StatusCode.EOK;
} catch (OperationRejectedException e) {
// unable to keep up with the write rate.
if (logger.isDebugEnabled()) {
logger.debug("Operation rejected while writing {}", request, e);
}
status = StatusCode.EIO;
} catch (IOException e) {
logger.error("Error writing entry:{} to ledger:{}", entryId, ledgerId, e);
status = StatusCode.EIO;
} catch (BookieException.LedgerFencedException e) {
logger.error("Ledger fenced while writing entry:{} to ledger:{}", entryId, ledgerId, e);
status = StatusCode.EFENCED;
} catch (BookieException e) {
logger.error("Unauthorized access to ledger:{} while writing entry:{}", ledgerId, entryId, e);
status = StatusCode.EUA;
} catch (Throwable t) {
logger.error("Unexpected exception while writing {}@{} : ", entryId, ledgerId, t);
// some bad request which cause unexpected exception
status = StatusCode.EBADREQ;
}
// doesn't return a response back to the caller.
if (!status.equals(StatusCode.EOK)) {
addResponse.setStatus(status);
return addResponse.build();
}
return null;
}
Aggregations