use of com.github.ambry.network.BoundedNettyByteBufReceive in project ambry by linkedin.
the class StoredBlob method send.
/**
* Take in a request in the form of {@link Send} and return a response in the form of a
* {@link BoundedNettyByteBufReceive}.
* @param send the request.
* @return the response.
* @throws IOException if there was an error in interpreting the request.
*/
public BoundedNettyByteBufReceive send(Send send) throws IOException {
if (!shouldRespond) {
return null;
}
ServerErrorCode serverError = hardError != null ? hardError : serverErrors.size() > 0 ? serverErrors.poll() : ServerErrorCode.No_Error;
RequestOrResponseType type = ((RequestOrResponse) send).getRequestType();
RequestOrResponse response;
requestCounts.computeIfAbsent(type, k -> new LongAdder()).increment();
switch(type) {
case PutRequest:
response = makePutResponse((PutRequest) send, serverError);
break;
case GetRequest:
response = makeGetResponse((GetRequest) send, serverError);
break;
case DeleteRequest:
response = makeDeleteResponse((DeleteRequest) send, serverError);
break;
case TtlUpdateRequest:
response = makeTtlUpdateResponse((TtlUpdateRequest) send, serverError);
break;
case UndeleteRequest:
response = makeUndeleteResponse((UndeleteRequest) send, serverError);
break;
default:
throw new IOException("Unknown request type received");
}
ByteBufferChannel channel = new ByteBufferChannel(ByteBuffer.allocate((int) response.sizeInBytes()));
response.writeTo(channel);
response.release();
ByteBuffer payload = channel.getBuffer();
payload.flip();
BoundedNettyByteBufReceive receive = new BoundedNettyByteBufReceive(100 * 1024 * 1024);
receive.readFrom(Channels.newChannel(new ByteBufferInputStream(payload)));
return receive;
}
use of com.github.ambry.network.BoundedNettyByteBufReceive in project ambry by linkedin.
the class MockSelector method poll.
/**
* Mocks sending and polling. Calls into the {@link MockServer} associated with the connection id with the request
* and uses the response from the call to construct receives. If the state is not {@link MockSelectorState#Good},
* then the behavior will be as determined by the state.
* @param timeoutMs Ignored.
* @param sends The list of new sends.
*/
@Override
public void poll(long timeoutMs, List<NetworkSend> sends) throws IOException {
this.sends = sends == null ? null : new ArrayList<>(sends);
disconnected.clear();
if (state.get() == MockSelectorState.FailConnectionInitiationOnPoll) {
disconnected.addAll(connected);
connected.clear();
}
if (sends != null) {
for (NetworkSend send : sends) {
if (state.get() == MockSelectorState.ThrowExceptionOnSend) {
throw new IOException("Mock exception on send");
}
if (state.get() == MockSelectorState.ThrowThrowableOnSend) {
throw new Error("Mock throwable on send");
}
if (state.get() == MockSelectorState.DisconnectOnSend) {
disconnected.add(send.getConnectionId());
this.sends.remove(send);
} else {
MockServer server = connIdToServer.get(send.getConnectionId());
BoundedNettyByteBufReceive receive = server.send(send.getPayload());
if (receive != null) {
receives.add(new NetworkReceive(send.getConnectionId(), receive, time));
}
// Just like Selector, MockSelector needs to release the sends' resources after completed.
send.getPayload().release();
}
}
}
if (state.get() == MockSelectorState.ThrowExceptionOnAllPoll) {
throw new IOException("Mock exception on poll");
}
}
Aggregations