use of org.apache.bookkeeper.proto.BookkeeperProtocol.WriteLacRequest in project bookkeeper by apache.
the class PerChannelBookieClient method writeLac.
void writeLac(final long ledgerId, final byte[] masterKey, final long lac, ByteBufList toSend, WriteLacCallback cb, Object ctx) {
final long txnId = getTxnId();
final CompletionKey completionKey = new V3CompletionKey(txnId, OperationType.WRITE_LAC);
// writeLac is mostly like addEntry hence uses addEntryTimeout
completionObjects.put(completionKey, new WriteLacCompletion(completionKey, cb, ctx, lac));
// Build the request
BKPacketHeader.Builder headerBuilder = BKPacketHeader.newBuilder().setVersion(ProtocolVersion.VERSION_THREE).setOperation(OperationType.WRITE_LAC).setTxnId(txnId);
WriteLacRequest.Builder writeLacBuilder = WriteLacRequest.newBuilder().setLedgerId(ledgerId).setLac(lac).setMasterKey(ByteString.copyFrom(masterKey)).setBody(ByteString.copyFrom(toSend.toArray()));
final Request writeLacRequest = Request.newBuilder().setHeader(headerBuilder).setWriteLacRequest(writeLacBuilder).build();
writeAndFlush(channel, completionKey, writeLacRequest);
}
use of org.apache.bookkeeper.proto.BookkeeperProtocol.WriteLacRequest in project bookkeeper by apache.
the class WriteLacProcessorV3 method getWriteLacResponse.
// Returns null if there is no exception thrown
private WriteLacResponse getWriteLacResponse() {
final long startTimeNanos = MathUtils.nowInNano();
WriteLacRequest writeLacRequest = request.getWriteLacRequest();
long lac = writeLacRequest.getLac();
long ledgerId = writeLacRequest.getLedgerId();
final WriteLacResponse.Builder writeLacResponse = WriteLacResponse.newBuilder().setLedgerId(ledgerId);
if (!isVersionCompatible()) {
writeLacResponse.setStatus(StatusCode.EBADVERSION);
return writeLacResponse.build();
}
if (requestProcessor.bookie.isReadOnly()) {
logger.warn("BookieServer is running as readonly mode, so rejecting the request from the client!");
writeLacResponse.setStatus(StatusCode.EREADONLY);
return writeLacResponse.build();
}
StatusCode status = null;
ByteBuffer lacToAdd = writeLacRequest.getBody().asReadOnlyByteBuffer();
byte[] masterKey = writeLacRequest.getMasterKey().toByteArray();
try {
requestProcessor.bookie.setExplicitLac(Unpooled.wrappedBuffer(lacToAdd), channel, masterKey);
status = StatusCode.EOK;
} catch (IOException e) {
logger.error("Error saving lac {} for ledger:{}", lac, ledgerId, e);
status = StatusCode.EIO;
} catch (BookieException e) {
logger.error("Unauthorized access to ledger:{} while adding lac:{}", ledgerId, lac, e);
status = StatusCode.EUA;
} catch (Throwable t) {
logger.error("Unexpected exception while writing lac {} for ledger:{}", lac, ledgerId, t);
// some bad request which cause unexpected exception
status = StatusCode.EBADREQ;
}
// dosn't return a response back to the caller.
if (status.equals(StatusCode.EOK)) {
requestProcessor.writeLacStats.registerSuccessfulEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
} else {
requestProcessor.writeLacStats.registerFailedEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
}
writeLacResponse.setStatus(status);
return writeLacResponse.build();
}
Aggregations