use of com.tencent.angel.ps.server.data.handler.Handler 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;
}
}
Aggregations