use of org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode in project bookkeeper by apache.
the class GetBookieInfoProcessorV3 method getGetBookieInfoResponse.
private GetBookieInfoResponse getGetBookieInfoResponse() {
long startTimeNanos = MathUtils.nowInNano();
GetBookieInfoRequest getBookieInfoRequest = request.getGetBookieInfoRequest();
long requested = getBookieInfoRequest.getRequested();
GetBookieInfoResponse.Builder getBookieInfoResponse = GetBookieInfoResponse.newBuilder();
if (!isVersionCompatible()) {
getBookieInfoResponse.setStatus(StatusCode.EBADVERSION);
requestProcessor.getBookieInfoStats.registerFailedEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
return getBookieInfoResponse.build();
}
if (LOG.isDebugEnabled()) {
LOG.debug("Received new getBookieInfo request: {}", request);
}
StatusCode status = StatusCode.EOK;
long freeDiskSpace = 0L, totalDiskSpace = 0L;
try {
if ((requested & GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE) != 0) {
freeDiskSpace = requestProcessor.bookie.getTotalFreeSpace();
getBookieInfoResponse.setFreeDiskSpace(freeDiskSpace);
}
if ((requested & GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE) != 0) {
totalDiskSpace = requestProcessor.bookie.getTotalDiskSpace();
getBookieInfoResponse.setTotalDiskCapacity(totalDiskSpace);
}
LOG.debug("FreeDiskSpace info is " + freeDiskSpace + " totalDiskSpace is: " + totalDiskSpace);
} catch (IOException e) {
status = StatusCode.EIO;
LOG.error("IOException while getting freespace/totalspace", e);
}
getBookieInfoResponse.setStatus(status);
requestProcessor.getBookieInfoStats.registerSuccessfulEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
return getBookieInfoResponse.build();
}
use of org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode in project bookkeeper by apache.
the class PerChannelBookieClient method readV2Response.
private void readV2Response(final BookieProtocol.Response response) {
OperationType operationType = getOperationType(response.getOpCode());
StatusCode status = getStatusCodeFromErrorCode(response.errorCode);
CompletionKey key = acquireV2Key(response.ledgerId, response.entryId, operationType);
CompletionValue completionValue = completionObjects.remove(key);
key.release();
if (completionValue == null) {
// If there's no completion object here, try in the multimap
synchronized (this) {
if (completionObjectsV2Conflicts.containsKey(key)) {
completionValue = completionObjectsV2Conflicts.get(key).get(0);
completionObjectsV2Conflicts.remove(key, completionValue);
}
}
}
if (null == completionValue) {
// Unexpected response, so log it. The txnId should have been present.
if (LOG.isDebugEnabled()) {
LOG.debug("Unexpected response received from bookie : " + addr + " for type : " + operationType + " and ledger:entry : " + response.ledgerId + ":" + response.entryId);
}
response.release();
} else {
long orderingKey = completionValue.ledgerId;
executor.executeOrdered(orderingKey, ReadV2ResponseCallback.create(completionValue, response.ledgerId, response.entryId, status, response));
}
}
use of org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode in project bookkeeper by apache.
the class ReadEntryProcessorV3 method getFenceResponse.
private void getFenceResponse(ReadResponse.Builder readResponse, ByteBuf entryBody, boolean fenceResult) {
StatusCode status;
if (!fenceResult) {
status = StatusCode.EIO;
registerFailedEvent(requestProcessor.fenceReadWaitStats, lastPhaseStartTime);
} else {
status = StatusCode.EOK;
readResponse.setBody(ByteString.copyFrom(entryBody.nioBuffer()));
registerSuccessfulEvent(requestProcessor.fenceReadWaitStats, lastPhaseStartTime);
}
if (null != entryBody) {
ReferenceCountUtil.release(entryBody);
}
readResponse.setStatus(status);
}
use of org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode in project bookkeeper by apache.
the class ReadLacProcessorV3 method getReadLacResponse.
// Returns null if there is no exception thrown
private ReadLacResponse getReadLacResponse() {
final long startTimeNanos = MathUtils.nowInNano();
ReadLacRequest readLacRequest = request.getReadLacRequest();
long ledgerId = readLacRequest.getLedgerId();
final ReadLacResponse.Builder readLacResponse = ReadLacResponse.newBuilder().setLedgerId(ledgerId);
if (!isVersionCompatible()) {
readLacResponse.setStatus(StatusCode.EBADVERSION);
return readLacResponse.build();
}
logger.debug("Received ReadLac request: {}", request);
StatusCode status = StatusCode.EOK;
ByteBuf lastEntry = null;
ByteBuf lac = null;
try {
lac = requestProcessor.bookie.getExplicitLac(ledgerId);
if (lac != null) {
readLacResponse.setLacBody(ByteString.copyFrom(lac.nioBuffer()));
}
} catch (Bookie.NoLedgerException e) {
status = StatusCode.ENOLEDGER;
logger.error("No ledger found while performing readLac from ledger: {}", ledgerId, e);
} catch (IOException e) {
status = StatusCode.EIO;
logger.error("IOException while performing readLac from ledger: {}", ledgerId);
} finally {
ReferenceCountUtil.release(lac);
}
try {
lastEntry = requestProcessor.bookie.readEntry(ledgerId, BookieProtocol.LAST_ADD_CONFIRMED);
if (lastEntry != null) {
readLacResponse.setLastEntryBody(ByteString.copyFrom(lastEntry.nioBuffer()));
}
} catch (Bookie.NoLedgerException e) {
status = StatusCode.ENOLEDGER;
logger.error("No ledger found while trying to read last entry: {}", ledgerId, e);
} catch (IOException e) {
status = StatusCode.EIO;
logger.error("IOException while trying to read last entry: {}", ledgerId, e);
} finally {
ReferenceCountUtil.release(lastEntry);
}
if ((lac == null) && (lastEntry == null)) {
status = StatusCode.ENOENTRY;
}
if (status == StatusCode.EOK) {
requestProcessor.readLacStats.registerSuccessfulEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
} else {
requestProcessor.readLacStats.registerFailedEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
}
// Finally set the status and return
readLacResponse.setStatus(status);
return readLacResponse.build();
}
use of org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode 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