Search in sources :

Example 1 with BoundedNettyByteBufReceive

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;
}
Also used : PartitionRequestInfo(com.github.ambry.protocol.PartitionRequestInfo) GetOption(com.github.ambry.protocol.GetOption) DataInputStream(java.io.DataInputStream) LongAdder(java.util.concurrent.atomic.LongAdder) ByteBufferChannel(com.github.ambry.utils.ByteBufferChannel) BlobProperties(com.github.ambry.messageformat.BlobProperties) ServerErrorCode(com.github.ambry.server.ServerErrorCode) UndeleteResponse(com.github.ambry.protocol.UndeleteResponse) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) GetResponse(com.github.ambry.protocol.GetResponse) ByteBufferSend(com.github.ambry.network.ByteBufferSend) DeleteResponse(com.github.ambry.protocol.DeleteResponse) Map(java.util.Map) RequestOrResponseType(com.github.ambry.protocol.RequestOrResponseType) UndeleteRequest(com.github.ambry.protocol.UndeleteRequest) SystemTime(com.github.ambry.utils.SystemTime) RequestOrResponse(com.github.ambry.protocol.RequestOrResponse) TtlUpdateResponse(com.github.ambry.protocol.TtlUpdateResponse) LinkedList(java.util.LinkedList) PutRequest(com.github.ambry.protocol.PutRequest) GetRequest(com.github.ambry.protocol.GetRequest) Container(com.github.ambry.account.Container) MessageMetadata(com.github.ambry.messageformat.MessageMetadata) PartitionResponseInfo(com.github.ambry.protocol.PartitionResponseInfo) DeleteRequest(com.github.ambry.protocol.DeleteRequest) BoundedNettyByteBufReceive(com.github.ambry.network.BoundedNettyByteBufReceive) Channels(java.nio.channels.Channels) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClusterMap(com.github.ambry.clustermap.ClusterMap) Utils(com.github.ambry.utils.Utils) IOException(java.io.IOException) MessageFormatException(com.github.ambry.messageformat.MessageFormatException) PutResponse(com.github.ambry.protocol.PutResponse) StoreKey(com.github.ambry.store.StoreKey) List(java.util.List) TtlUpdateRequest(com.github.ambry.protocol.TtlUpdateRequest) MessageInfo(com.github.ambry.store.MessageInfo) Crc32(com.github.ambry.utils.Crc32) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) Account(com.github.ambry.account.Account) Send(com.github.ambry.network.Send) MessageFormatRecord(com.github.ambry.messageformat.MessageFormatRecord) BlobType(com.github.ambry.messageformat.BlobType) RequestOrResponseType(com.github.ambry.protocol.RequestOrResponseType) TtlUpdateRequest(com.github.ambry.protocol.TtlUpdateRequest) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) PutRequest(com.github.ambry.protocol.PutRequest) UndeleteRequest(com.github.ambry.protocol.UndeleteRequest) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ServerErrorCode(com.github.ambry.server.ServerErrorCode) RequestOrResponse(com.github.ambry.protocol.RequestOrResponse) LongAdder(java.util.concurrent.atomic.LongAdder) BoundedNettyByteBufReceive(com.github.ambry.network.BoundedNettyByteBufReceive) ByteBufferChannel(com.github.ambry.utils.ByteBufferChannel) GetRequest(com.github.ambry.protocol.GetRequest) DeleteRequest(com.github.ambry.protocol.DeleteRequest)

Example 2 with BoundedNettyByteBufReceive

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");
    }
}
Also used : BoundedNettyByteBufReceive(com.github.ambry.network.BoundedNettyByteBufReceive) ArrayList(java.util.ArrayList) NetworkReceive(com.github.ambry.network.NetworkReceive) NetworkSend(com.github.ambry.network.NetworkSend) IOException(java.io.IOException)

Aggregations

BoundedNettyByteBufReceive (com.github.ambry.network.BoundedNettyByteBufReceive)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Account (com.github.ambry.account.Account)1 Container (com.github.ambry.account.Container)1 ClusterMap (com.github.ambry.clustermap.ClusterMap)1 BlobProperties (com.github.ambry.messageformat.BlobProperties)1 BlobType (com.github.ambry.messageformat.BlobType)1 MessageFormatException (com.github.ambry.messageformat.MessageFormatException)1 MessageFormatRecord (com.github.ambry.messageformat.MessageFormatRecord)1 MessageMetadata (com.github.ambry.messageformat.MessageMetadata)1 ByteBufferSend (com.github.ambry.network.ByteBufferSend)1 NetworkReceive (com.github.ambry.network.NetworkReceive)1 NetworkSend (com.github.ambry.network.NetworkSend)1 Send (com.github.ambry.network.Send)1 DeleteRequest (com.github.ambry.protocol.DeleteRequest)1 DeleteResponse (com.github.ambry.protocol.DeleteResponse)1 GetOption (com.github.ambry.protocol.GetOption)1 GetRequest (com.github.ambry.protocol.GetRequest)1 GetResponse (com.github.ambry.protocol.GetResponse)1