Search in sources :

Example 1 with ResponseData

use of com.tencent.angel.ps.server.data.response.ResponseData in project angel by Tencent.

the class ByteBufUtils method serializeResponse.

public static ByteBuf serializeResponse(Response response, boolean useDirectorBuffer) {
    ResponseData data = response.getData();
    // If is stream response, just return bytebuf
    if (data != null && (data instanceof IStreamResponse) && (((IStreamResponse) data).getOutputBuffer() != null)) {
        return ((IStreamResponse) data).getOutputBuffer();
    }
    ByteBuf buf = null;
    try {
        buf = allocResultBuf(response, useDirectorBuffer);
        response.serialize(buf);
    } catch (Throwable x) {
        LOG.error("serialize response failed ", x);
        if (buf != null) {
            buf.release();
        }
        if (response.getResponseType() == ResponseType.SUCCESS) {
            // Reset the response and allocate buffer again
            response.setResponseType(ResponseType.SERVER_IS_BUSY);
            response.setDetail("can not serialize the response");
            // Release response data
            response.setData(null);
            buf = allocResultBuf(response, useDirectorBuffer);
            response.serialize(buf);
        } else {
            throw x;
        }
    }
    return buf;
}
Also used : ResponseData(com.tencent.angel.ps.server.data.response.ResponseData) ByteBuf(io.netty.buffer.ByteBuf) IStreamResponse(com.tencent.angel.ps.server.data.response.IStreamResponse)

Example 2 with ResponseData

use of com.tencent.angel.ps.server.data.response.ResponseData in project angel by Tencent.

the class WorkerPool method handle.

/**
 * Handle rpc
 *
 * @param requestHeader request meta data
 * @param in request data
 * @param ctx netty channel context
 */
private void handle(RequestHeader requestHeader, ByteBuf in, ChannelHandlerContext ctx) {
    ResponseData responseData = null;
    ResponseHeader responseHeader;
    ServerState state = runningContext.getState();
    if (state == ServerState.BUSY) {
        responseHeader = new ResponseHeader(requestHeader.seqId, requestHeader.methodId, ServerState.BUSY, ResponseType.SERVER_IS_BUSY, "server is busy now, retry later");
    } else {
        // Idle, handle the request
        // Parse head success
        Handler handler = methodIdToHandler.get(requestHeader.methodId);
        // Handle request
        if (handler != null) {
            try {
                ByteBufSerdeUtils.deserializeBoolean(in);
                RequestData request = handler.parseRequest(in);
                requestSize.addAndGet(request.requestSize);
                responseData = handler.handle(requestHeader, request);
                requestSize.addAndGet(responseData.bufferLen());
                responseHeader = new ResponseHeader(requestHeader.seqId, requestHeader.methodId, state, ResponseType.SUCCESS);
            } catch (Throwable e) {
                // Handle error, generate response
                responseHeader = new ResponseHeader(requestHeader.seqId, requestHeader.methodId, state, ResponseType.SERVER_HANDLE_FAILED, StringUtils.stringifyException(e));
            }
        } else {
            responseHeader = new ResponseHeader(requestHeader.seqId, requestHeader.methodId, state, ResponseType.UNSUPPORT_REQUEST);
        }
    }
    // Release the input buffer
    ByteBufUtils.releaseBuf(in);
    // Create response
    Response response = new Response(responseHeader, responseData);
    // LOG.info("Response header = " + responseHeader);
    // Serialize the response
    // 2. Serialize the response
    ByteBuf out = ByteBufUtils.serializeResponse(response, context.isUseDirectBuffer());
    // Send the serialized response
    if (out != null) {
        sendResult(requestHeader, ctx, out);
        return;
    } else {
        runningContext.after(requestHeader.clientId, requestHeader.seqId);
        return;
    }
}
Also used : Response(com.tencent.angel.ps.server.data.response.Response) ResponseHeader(com.tencent.angel.ps.server.data.response.ResponseHeader) RequestData(com.tencent.angel.ps.server.data.request.RequestData) ResponseData(com.tencent.angel.ps.server.data.response.ResponseData) GetPartHandler(com.tencent.angel.ps.server.data.handler.GetPartHandler) BasicStreamUpdateHandler(com.tencent.angel.ps.server.data.handler.BasicStreamUpdateHandler) StreamIndexGetRowHandler(com.tencent.angel.ps.server.data.handler.StreamIndexGetRowHandler) IndexGetRowHandler(com.tencent.angel.ps.server.data.handler.IndexGetRowHandler) IndexGetRowsHandler(com.tencent.angel.ps.server.data.handler.IndexGetRowsHandler) GetRowsHandler(com.tencent.angel.ps.server.data.handler.GetRowsHandler) Handler(com.tencent.angel.ps.server.data.handler.Handler) PSFUpdateHandler(com.tencent.angel.ps.server.data.handler.PSFUpdateHandler) PSFGetHandler(com.tencent.angel.ps.server.data.handler.PSFGetHandler) GetRowHandler(com.tencent.angel.ps.server.data.handler.GetRowHandler) StreamIndexGetRowsHandler(com.tencent.angel.ps.server.data.handler.StreamIndexGetRowsHandler) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ResponseData (com.tencent.angel.ps.server.data.response.ResponseData)2 ByteBuf (io.netty.buffer.ByteBuf)2 BasicStreamUpdateHandler (com.tencent.angel.ps.server.data.handler.BasicStreamUpdateHandler)1 GetPartHandler (com.tencent.angel.ps.server.data.handler.GetPartHandler)1 GetRowHandler (com.tencent.angel.ps.server.data.handler.GetRowHandler)1 GetRowsHandler (com.tencent.angel.ps.server.data.handler.GetRowsHandler)1 Handler (com.tencent.angel.ps.server.data.handler.Handler)1 IndexGetRowHandler (com.tencent.angel.ps.server.data.handler.IndexGetRowHandler)1 IndexGetRowsHandler (com.tencent.angel.ps.server.data.handler.IndexGetRowsHandler)1 PSFGetHandler (com.tencent.angel.ps.server.data.handler.PSFGetHandler)1 PSFUpdateHandler (com.tencent.angel.ps.server.data.handler.PSFUpdateHandler)1 StreamIndexGetRowHandler (com.tencent.angel.ps.server.data.handler.StreamIndexGetRowHandler)1 StreamIndexGetRowsHandler (com.tencent.angel.ps.server.data.handler.StreamIndexGetRowsHandler)1 RequestData (com.tencent.angel.ps.server.data.request.RequestData)1 IStreamResponse (com.tencent.angel.ps.server.data.response.IStreamResponse)1 Response (com.tencent.angel.ps.server.data.response.Response)1 ResponseHeader (com.tencent.angel.ps.server.data.response.ResponseHeader)1