Search in sources :

Example 1 with ResponseHeader

use of com.tencent.angel.ps.server.data.response.ResponseHeader 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)

Example 2 with ResponseHeader

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

the class StreamIndexGetRowsHandler method handle.

@Override
public ResponseData handle(RequestHeader requestHeader, RequestData data) throws Exception {
    IndexPartGetRowsRequest request = (IndexPartGetRowsRequest) data;
    IndexPartGetRowsResponse response = new IndexPartGetRowsResponse();
    int[] rowIds = request.getRowIds();
    int rowNum = rowIds.length;
    ServerBasicTypeRow row0 = (ServerBasicTypeRow) context.getMatrixStorageManager().getRow(requestHeader.matrixId, request.getRowIds()[0], requestHeader.partId);
    ValueType valueType = MatrixUtils.getValueType(row0.getRowType());
    ResponseHeader responseHeader = new ResponseHeader(requestHeader.seqId, requestHeader.methodId, context.getRunningContext().getState(), ResponseType.SUCCESS);
    ByteBuf in = request.getInputBuffer();
    // Read router type
    RouterType routerType = RouterType.valueOf(in.readInt());
    // Key type
    KeyType keyType = KeyType.valueOf(in.readInt());
    // Row id
    int rowId = in.readInt();
    // Key number
    int size = in.readInt();
    // Calculate final output buffer len
    // Response header
    int buffLen = responseHeader.bufferLen();
    // Data flag
    buffLen += ByteBufSerdeUtils.BOOLEN_LENGTH;
    // Value type
    buffLen += ByteBufSerdeUtils.INT_LENGTH;
    // Row id
    buffLen += ByteBufSerdeUtils.INT_LENGTH;
    // Size
    buffLen += ByteBufSerdeUtils.INT_LENGTH;
    // Data
    buffLen += size * ByteBufSerdeUtils.serializedValueLen(valueType);
    // Allocate result buffer
    ByteBuf resultBuf = ByteBufUtils.allocResultBuf(buffLen, context.isUseDirectBuffer());
    // Header
    responseHeader.serialize(resultBuf);
    ByteBufSerdeUtils.serializeBoolean(resultBuf, true);
    int colNum = 0;
    int markPos = in.readerIndex();
    for (int i = 0; i < rowNum; i++) {
        in.readerIndex(markPos);
        // Serialize Value part head
        ByteBufSerdeUtils.serializeInt(resultBuf, valueType.getTypeId());
        ByteBufSerdeUtils.serializeInt(resultBuf, rowIds[i]);
        ByteBufSerdeUtils.serializeInt(resultBuf, colNum);
        ServerBasicTypeRow row = (ServerBasicTypeRow) context.getMatrixStorageManager().getRow(requestHeader.matrixId, rowIds[i], requestHeader.partId);
        resultBuf.writeInt(rowIds[i]);
        if (request.getFunc() == null) {
            row.startRead();
            try {
                row.indexGet(keyType, colNum, in, resultBuf, null);
            } finally {
                row.endRead();
            }
        } else {
            row.startWrite();
            try {
                row.indexGet(keyType, colNum, in, resultBuf, request.getFunc());
            } finally {
                row.endWrite();
            }
        }
    }
    // response.setOutputBuffer(resultBuf);
    return response;
}
Also used : ResponseHeader(com.tencent.angel.ps.server.data.response.ResponseHeader) IndexPartGetRowsRequest(com.tencent.angel.ps.server.data.request.IndexPartGetRowsRequest) KeyType(com.tencent.angel.ps.server.data.request.KeyType) ValueType(com.tencent.angel.ps.server.data.request.ValueType) RouterType(com.tencent.angel.psagent.matrix.transport.router.RouterType) IndexPartGetRowsResponse(com.tencent.angel.ps.server.data.response.IndexPartGetRowsResponse) ServerBasicTypeRow(com.tencent.angel.ps.storage.vector.ServerBasicTypeRow) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with ResponseHeader

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

the class StreamIndexGetRowHandler method handle.

@Override
public ResponseData handle(RequestHeader requestHeader, RequestData data) throws Exception {
    IndexPartGetRowRequest request = (IndexPartGetRowRequest) data;
    StreamIndexPartGetRowResponse response = new StreamIndexPartGetRowResponse();
    ByteBuf in = request.getInputBuffer();
    ServerBasicTypeRow row = (ServerBasicTypeRow) context.getMatrixStorageManager().getRow(requestHeader.matrixId, request.getRowId(), requestHeader.partId);
    ValueType valueType = MatrixUtils.getValueType(row.getRowType());
    ResponseHeader responseHeader = new ResponseHeader(requestHeader.seqId, requestHeader.methodId, context.getRunningContext().getState(), ResponseType.SUCCESS);
    // Read router type
    RouterType routerType = RouterType.valueOf(in.readInt());
    // Key type
    KeyType keyType = KeyType.valueOf(in.readInt());
    // Row id
    int rowId = in.readInt();
    // Key number
    int size = in.readInt();
    // Calculate final output buffer len
    // Response header
    int buffLen = responseHeader.bufferLen();
    // Data flag
    buffLen += ByteBufSerdeUtils.BOOLEN_LENGTH;
    // Value type
    buffLen += ByteBufSerdeUtils.INT_LENGTH;
    // Row id
    buffLen += ByteBufSerdeUtils.INT_LENGTH;
    // Size
    buffLen += ByteBufSerdeUtils.INT_LENGTH;
    // Data
    buffLen += size * ByteBufSerdeUtils.serializedValueLen(valueType);
    // Allocate final result byte buffer
    ByteBuf resultBuf = ByteBufUtils.allocResultBuf(buffLen, context.isUseDirectBuffer());
    // Write response header
    responseHeader.serialize(resultBuf);
    // Data
    // Value part flag
    ByteBufSerdeUtils.serializeBoolean(resultBuf, true);
    // Value type
    ByteBufSerdeUtils.serializeInt(resultBuf, valueType.getTypeId());
    // Row id
    ByteBufSerdeUtils.serializeInt(resultBuf, rowId);
    // Values number
    ByteBufSerdeUtils.serializeInt(resultBuf, size);
    // Result data
    if (request.getFunc() == null) {
        row.startRead();
        try {
            row.indexGet(keyType, size, in, resultBuf, null);
        } finally {
            row.endRead();
        }
    } else {
        row.startWrite();
        try {
            row.indexGet(keyType, size, in, resultBuf, request.getFunc());
        } finally {
            row.endWrite();
        }
    }
    response.setOutputBuffer(resultBuf);
    return response;
}
Also used : ResponseHeader(com.tencent.angel.ps.server.data.response.ResponseHeader) KeyType(com.tencent.angel.ps.server.data.request.KeyType) ValueType(com.tencent.angel.ps.server.data.request.ValueType) RouterType(com.tencent.angel.psagent.matrix.transport.router.RouterType) IndexPartGetRowRequest(com.tencent.angel.ps.server.data.request.IndexPartGetRowRequest) StreamIndexPartGetRowResponse(com.tencent.angel.ps.server.data.response.StreamIndexPartGetRowResponse) ByteBuf(io.netty.buffer.ByteBuf) ServerBasicTypeRow(com.tencent.angel.ps.storage.vector.ServerBasicTypeRow)

Aggregations

ResponseHeader (com.tencent.angel.ps.server.data.response.ResponseHeader)3 ByteBuf (io.netty.buffer.ByteBuf)3 KeyType (com.tencent.angel.ps.server.data.request.KeyType)2 ValueType (com.tencent.angel.ps.server.data.request.ValueType)2 ServerBasicTypeRow (com.tencent.angel.ps.storage.vector.ServerBasicTypeRow)2 RouterType (com.tencent.angel.psagent.matrix.transport.router.RouterType)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 IndexPartGetRowRequest (com.tencent.angel.ps.server.data.request.IndexPartGetRowRequest)1 IndexPartGetRowsRequest (com.tencent.angel.ps.server.data.request.IndexPartGetRowsRequest)1 RequestData (com.tencent.angel.ps.server.data.request.RequestData)1