Skip to content

Commit

Permalink
HADOOP-11552. Allow handoff on the server side for RPC requests. Cont…
Browse files Browse the repository at this point in the history
…ributed by Siddharth Seth
  • Loading branch information
jian-he committed Nov 24, 2016
1 parent 0de0c32 commit 3d94da1
Show file tree
Hide file tree
Showing 10 changed files with 696 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.hadoop.io.retry.RetryPolicy;
import org.apache.hadoop.ipc.Client.ConnectionId;
import org.apache.hadoop.ipc.RPC.RpcInvoker;
import org.apache.hadoop.ipc.RpcWritable;
import org.apache.hadoop.ipc.protobuf.ProtobufRpcEngineProtos.RequestHeaderProto;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.token.SecretManager;
Expand Down Expand Up @@ -345,6 +344,60 @@ public RPC.Server getServer(Class<?> protocol, Object protocolImpl,
}

public static class Server extends RPC.Server {

static final ThreadLocal<ProtobufRpcEngineCallback> currentCallback =
new ThreadLocal<>();

static final ThreadLocal<CallInfo> currentCallInfo = new ThreadLocal<>();

static class CallInfo {
private final RPC.Server server;
private final String methodName;

public CallInfo(RPC.Server server, String methodName) {
this.server = server;
this.methodName = methodName;
}
}

static class ProtobufRpcEngineCallbackImpl
implements ProtobufRpcEngineCallback {

private final RPC.Server server;
private final Call call;
private final String methodName;
private final long setupTime;

public ProtobufRpcEngineCallbackImpl() {
this.server = currentCallInfo.get().server;
this.call = Server.getCurCall().get();
this.methodName = currentCallInfo.get().methodName;
this.setupTime = Time.now();
}

@Override
public void setResponse(Message message) {
long processingTime = Time.now() - setupTime;
call.setDeferredResponse(RpcWritable.wrap(message));
server.updateDeferredMetrics(methodName, processingTime);
}

@Override
public void error(Throwable t) {
long processingTime = Time.now() - setupTime;
String detailedMetricsName = t.getClass().getSimpleName();
server.updateDeferredMetrics(detailedMetricsName, processingTime);
call.setDeferredError(t);
}
}

@InterfaceStability.Unstable
public static ProtobufRpcEngineCallback registerForDeferredResponse() {
ProtobufRpcEngineCallback callback = new ProtobufRpcEngineCallbackImpl();
currentCallback.set(callback);
return callback;
}

/**
* Construct an RPC server.
*
Expand Down Expand Up @@ -462,20 +515,33 @@ public Writable call(RPC.Server server, String connectionProtocolName,
long startTime = Time.now();
int qTime = (int) (startTime - receiveTime);
Exception exception = null;
boolean isDeferred = false;
try {
server.rpcDetailedMetrics.init(protocolImpl.protocolClass);
currentCallInfo.set(new CallInfo(server, methodName));
result = service.callBlockingMethod(methodDescriptor, null, param);
// Check if this needs to be a deferred response,
// by checking the ThreadLocal callback being set
if (currentCallback.get() != null) {
Server.getCurCall().get().deferResponse();
isDeferred = true;
currentCallback.set(null);
return null;
}
} catch (ServiceException e) {
exception = (Exception) e.getCause();
throw (Exception) e.getCause();
} catch (Exception e) {
exception = e;
throw e;
} finally {
currentCallInfo.set(null);
int processingTime = (int) (Time.now() - startTime);
if (LOG.isDebugEnabled()) {
String msg = "Served: " + methodName + " queueTime= " + qTime +
" procesingTime= " + processingTime;
String msg =
"Served: " + methodName + (isDeferred ? ", deferred" : "") +
", queueTime= " + qTime +
" procesingTime= " + processingTime;
if (exception != null) {
msg += " exception= " + exception.getClass().getSimpleName();
}
Expand All @@ -484,7 +550,8 @@ public Writable call(RPC.Server server, String connectionProtocolName,
String detailedMetricsName = (exception == null) ?
methodName :
exception.getClass().getSimpleName();
server.updateMetrics(detailedMetricsName, qTime, processingTime);
server.updateMetrics(detailedMetricsName, qTime, processingTime,
isDeferred);
}
return RpcWritable.wrap(result);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.ipc;

import com.google.protobuf.Message;

public interface ProtobufRpcEngineCallback {

public void setResponse(Message message);

public void error(Throwable t);

}
Loading

0 comments on commit 3d94da1

Please sign in to comment.