fix some RPC problem

This commit is contained in:
Kelven Yang 2012-12-13 18:10:02 -08:00
parent e72417a1e7
commit e998ee59f6
5 changed files with 68 additions and 16 deletions

View File

@ -26,7 +26,7 @@ import java.util.Map;
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public class AsyncCallbackDispatcher implements AsyncCompletionCallback { public class AsyncCallbackDispatcher implements AsyncCompletionCallback {
private static Map<Class<?>, Method> s_handlerCache = new HashMap<Class<?>, Method>(); private static Map<Class<?>, Map<String, Method>> s_handlerCache = new HashMap<Class<?>, Map<String, Method>>();
private Map<String, Object> _contextMap = new HashMap<String, Object>(); private Map<String, Object> _contextMap = new HashMap<String, Object>();
private String _operationName; private String _operationName;
@ -100,17 +100,20 @@ public class AsyncCallbackDispatcher implements AsyncCompletionCallback {
return true; return true;
} }
public static Method resolveHandler(Class<?> handlerClz, String operationName) { public static Method resolveHandler(Class<?> handlerClz, String command) {
synchronized(s_handlerCache) { synchronized(s_handlerCache) {
Method handler = s_handlerCache.get(handlerClz); Map<String, Method> handlerMap = getAndSetHandlerMap(handlerClz);
Method handler = handlerMap.get(command);
if(handler != null) if(handler != null)
return handler; return handler;
for(Method method : handlerClz.getMethods()) { for(Method method : handlerClz.getDeclaredMethods()) {
AsyncCallbackHandler annotation = method.getAnnotation(AsyncCallbackHandler.class); AsyncCallbackHandler annotation = method.getAnnotation(AsyncCallbackHandler.class);
if(annotation != null) { if(annotation != null) {
if(annotation.operationName().equals(operationName)) { if(annotation.operationName().equals(command)) {
s_handlerCache.put(handlerClz, method); handlerMap.put(command, method);
method.setAccessible(true);
return method; return method;
} }
} }
@ -119,4 +122,18 @@ public class AsyncCallbackDispatcher implements AsyncCompletionCallback {
return null; return null;
} }
private static Map<String, Method> getAndSetHandlerMap(Class<?> handlerClz) {
Map<String, Method> handlerMap;
synchronized(s_handlerCache) {
handlerMap = s_handlerCache.get(handlerClz);
if(handlerMap == null) {
handlerMap = new HashMap<String, Method>();
s_handlerCache.put(handlerClz, handlerMap);
}
}
return handlerMap;
}
} }

View File

@ -25,7 +25,7 @@ import java.util.Map;
public class RpcCallbackDispatcher { public class RpcCallbackDispatcher {
private static Map<Class<?>, Method> s_handlerCache = new HashMap<Class<?>, Method>(); private static Map<Class<?>, Map<String, Method>> s_handlerCache = new HashMap<Class<?>, Map<String, Method>>();
public static boolean dispatch(Object target, RpcClientCall clientCall) { public static boolean dispatch(Object target, RpcClientCall clientCall) {
assert(clientCall != null); assert(clientCall != null);
@ -50,15 +50,18 @@ public class RpcCallbackDispatcher {
public static Method resolveHandler(Class<?> handlerClz, String command) { public static Method resolveHandler(Class<?> handlerClz, String command) {
synchronized(s_handlerCache) { synchronized(s_handlerCache) {
Method handler = s_handlerCache.get(handlerClz); Map<String, Method> handlerMap = getAndSetHandlerMap(handlerClz);
Method handler = handlerMap.get(command);
if(handler != null) if(handler != null)
return handler; return handler;
for(Method method : handlerClz.getMethods()) { for(Method method : handlerClz.getDeclaredMethods()) {
RpcCallbackHandler annotation = method.getAnnotation(RpcCallbackHandler.class); RpcCallbackHandler annotation = method.getAnnotation(RpcCallbackHandler.class);
if(annotation != null) { if(annotation != null) {
if(annotation.command().equals(command)) { if(annotation.command().equals(command)) {
s_handlerCache.put(handlerClz, method); method.setAccessible(true);
handlerMap.put(command, method);
return method; return method;
} }
} }
@ -67,4 +70,18 @@ public class RpcCallbackDispatcher {
return null; return null;
} }
private static Map<String, Method> getAndSetHandlerMap(Class<?> handlerClz) {
Map<String, Method> handlerMap;
synchronized(s_handlerCache) {
handlerMap = s_handlerCache.get(handlerClz);
if(handlerMap == null) {
handlerMap = new HashMap<String, Method>();
s_handlerCache.put(handlerClz, handlerMap);
}
}
return handlerMap;
}
} }

View File

@ -167,7 +167,7 @@ public class RpcProviderImpl implements RpcProvider {
} }
for(RpcServiceEndpoint endpoint : endpoints) { for(RpcServiceEndpoint endpoint : endpoints) {
if(RpcServiceDispatcher.dispatch(endpoint, call)) if(endpoint.onCallReceive(call))
return; return;
} }

View File

@ -25,7 +25,7 @@ import java.util.Map;
public class RpcServiceDispatcher implements RpcServiceEndpoint { public class RpcServiceDispatcher implements RpcServiceEndpoint {
private static Map<Class<?>, Method> s_handlerCache = new HashMap<Class<?>, Method>(); private static Map<Class<?>, Map<String, Method>> s_handlerCache = new HashMap<Class<?>, Map<String, Method>>();
private static Map<Object, RpcServiceDispatcher> s_targetMap = new HashMap<Object, RpcServiceDispatcher>(); private static Map<Object, RpcServiceDispatcher> s_targetMap = new HashMap<Object, RpcServiceDispatcher>();
private Object _targetObject; private Object _targetObject;
@ -75,15 +75,18 @@ public class RpcServiceDispatcher implements RpcServiceEndpoint {
public static Method resolveHandler(Class<?> handlerClz, String command) { public static Method resolveHandler(Class<?> handlerClz, String command) {
synchronized(s_handlerCache) { synchronized(s_handlerCache) {
Method handler = s_handlerCache.get(handlerClz); Map<String, Method> handlerMap = getAndSetHandlerMap(handlerClz);
Method handler = handlerMap.get(command);
if(handler != null) if(handler != null)
return handler; return handler;
for(Method method : handlerClz.getMethods()) { for(Method method : handlerClz.getDeclaredMethods()) {
RpcServiceHandler annotation = method.getAnnotation(RpcServiceHandler.class); RpcServiceHandler annotation = method.getAnnotation(RpcServiceHandler.class);
if(annotation != null) { if(annotation != null) {
if(annotation.command().equals(command)) { if(annotation.command().equals(command)) {
s_handlerCache.put(handlerClz, method); method.setAccessible(true);
handlerMap.put(command, method);
return method; return method;
} }
} }
@ -92,7 +95,21 @@ public class RpcServiceDispatcher implements RpcServiceEndpoint {
return null; return null;
} }
private static Map<String, Method> getAndSetHandlerMap(Class<?> handlerClz) {
Map<String, Method> handlerMap;
synchronized(s_handlerCache) {
handlerMap = s_handlerCache.get(handlerClz);
if(handlerMap == null) {
handlerMap = new HashMap<String, Method>();
s_handlerCache.put(handlerClz, handlerMap);
}
}
return handlerMap;
}
@Override @Override
public boolean onCallReceive(RpcServerCall call) { public boolean onCallReceive(RpcServerCall call) {
return dispatch(_targetObject, call); return dispatch(_targetObject, call);

View File

@ -61,6 +61,7 @@ public class SampleManagerComponent2 {
s_logger.info("StoragePrepare command arg. pool: " + cmd.getStoragePool() + ", vol: " + cmd.getVolumeId()); s_logger.info("StoragePrepare command arg. pool: " + cmd.getStoragePool() + ", vol: " + cmd.getVolumeId());
SampleStoragePrepareAnswer answer = new SampleStoragePrepareAnswer(); SampleStoragePrepareAnswer answer = new SampleStoragePrepareAnswer();
answer.setResult("Successfully executed StoragePrepare command");
call.completeCall(answer); call.completeCall(answer);
} }