Skip to content

Commit e998ee5

Browse files
committed
fix some RPC problem
1 parent e72417a commit e998ee5

5 files changed

Lines changed: 68 additions & 16 deletions

File tree

framework/ipc/src/org/apache/cloudstack/framework/messaging/AsyncCallbackDispatcher.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
@SuppressWarnings("rawtypes")
2828
public class AsyncCallbackDispatcher implements AsyncCompletionCallback {
29-
private static Map<Class<?>, Method> s_handlerCache = new HashMap<Class<?>, Method>();
29+
private static Map<Class<?>, Map<String, Method>> s_handlerCache = new HashMap<Class<?>, Map<String, Method>>();
3030

3131
private Map<String, Object> _contextMap = new HashMap<String, Object>();
3232
private String _operationName;
@@ -100,17 +100,20 @@ public static boolean dispatch(Object target, AsyncCallbackDispatcher callback)
100100
return true;
101101
}
102102

103-
public static Method resolveHandler(Class<?> handlerClz, String operationName) {
103+
public static Method resolveHandler(Class<?> handlerClz, String command) {
104104
synchronized(s_handlerCache) {
105-
Method handler = s_handlerCache.get(handlerClz);
105+
Map<String, Method> handlerMap = getAndSetHandlerMap(handlerClz);
106+
107+
Method handler = handlerMap.get(command);
106108
if(handler != null)
107109
return handler;
108110

109-
for(Method method : handlerClz.getMethods()) {
111+
for(Method method : handlerClz.getDeclaredMethods()) {
110112
AsyncCallbackHandler annotation = method.getAnnotation(AsyncCallbackHandler.class);
111113
if(annotation != null) {
112-
if(annotation.operationName().equals(operationName)) {
113-
s_handlerCache.put(handlerClz, method);
114+
if(annotation.operationName().equals(command)) {
115+
handlerMap.put(command, method);
116+
method.setAccessible(true);
114117
return method;
115118
}
116119
}
@@ -119,4 +122,18 @@ public static Method resolveHandler(Class<?> handlerClz, String operationName) {
119122

120123
return null;
121124
}
125+
126+
private static Map<String, Method> getAndSetHandlerMap(Class<?> handlerClz) {
127+
Map<String, Method> handlerMap;
128+
synchronized(s_handlerCache) {
129+
handlerMap = s_handlerCache.get(handlerClz);
130+
131+
if(handlerMap == null) {
132+
handlerMap = new HashMap<String, Method>();
133+
s_handlerCache.put(handlerClz, handlerMap);
134+
}
135+
}
136+
137+
return handlerMap;
138+
}
122139
}

framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcCallbackDispatcher.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
public class RpcCallbackDispatcher {
2727

28-
private static Map<Class<?>, Method> s_handlerCache = new HashMap<Class<?>, Method>();
28+
private static Map<Class<?>, Map<String, Method>> s_handlerCache = new HashMap<Class<?>, Map<String, Method>>();
2929

3030
public static boolean dispatch(Object target, RpcClientCall clientCall) {
3131
assert(clientCall != null);
@@ -50,15 +50,18 @@ public static boolean dispatch(Object target, RpcClientCall clientCall) {
5050

5151
public static Method resolveHandler(Class<?> handlerClz, String command) {
5252
synchronized(s_handlerCache) {
53-
Method handler = s_handlerCache.get(handlerClz);
53+
Map<String, Method> handlerMap = getAndSetHandlerMap(handlerClz);
54+
55+
Method handler = handlerMap.get(command);
5456
if(handler != null)
5557
return handler;
5658

57-
for(Method method : handlerClz.getMethods()) {
59+
for(Method method : handlerClz.getDeclaredMethods()) {
5860
RpcCallbackHandler annotation = method.getAnnotation(RpcCallbackHandler.class);
5961
if(annotation != null) {
6062
if(annotation.command().equals(command)) {
61-
s_handlerCache.put(handlerClz, method);
63+
method.setAccessible(true);
64+
handlerMap.put(command, method);
6265
return method;
6366
}
6467
}
@@ -67,4 +70,18 @@ public static Method resolveHandler(Class<?> handlerClz, String command) {
6770

6871
return null;
6972
}
73+
74+
private static Map<String, Method> getAndSetHandlerMap(Class<?> handlerClz) {
75+
Map<String, Method> handlerMap;
76+
synchronized(s_handlerCache) {
77+
handlerMap = s_handlerCache.get(handlerClz);
78+
79+
if(handlerMap == null) {
80+
handlerMap = new HashMap<String, Method>();
81+
s_handlerCache.put(handlerClz, handlerMap);
82+
}
83+
}
84+
85+
return handlerMap;
86+
}
7087
}

framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcProviderImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private void handleCallRequestPdu(String sourceAddress, String targetAddress, Rp
167167
}
168168

169169
for(RpcServiceEndpoint endpoint : endpoints) {
170-
if(RpcServiceDispatcher.dispatch(endpoint, call))
170+
if(endpoint.onCallReceive(call))
171171
return;
172172
}
173173

framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcServiceDispatcher.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
public class RpcServiceDispatcher implements RpcServiceEndpoint {
2727

28-
private static Map<Class<?>, Method> s_handlerCache = new HashMap<Class<?>, Method>();
28+
private static Map<Class<?>, Map<String, Method>> s_handlerCache = new HashMap<Class<?>, Map<String, Method>>();
2929

3030
private static Map<Object, RpcServiceDispatcher> s_targetMap = new HashMap<Object, RpcServiceDispatcher>();
3131
private Object _targetObject;
@@ -75,15 +75,18 @@ public static boolean dispatch(Object target, RpcServerCall serviceCall) {
7575

7676
public static Method resolveHandler(Class<?> handlerClz, String command) {
7777
synchronized(s_handlerCache) {
78-
Method handler = s_handlerCache.get(handlerClz);
78+
Map<String, Method> handlerMap = getAndSetHandlerMap(handlerClz);
79+
80+
Method handler = handlerMap.get(command);
7981
if(handler != null)
8082
return handler;
8183

82-
for(Method method : handlerClz.getMethods()) {
84+
for(Method method : handlerClz.getDeclaredMethods()) {
8385
RpcServiceHandler annotation = method.getAnnotation(RpcServiceHandler.class);
8486
if(annotation != null) {
8587
if(annotation.command().equals(command)) {
86-
s_handlerCache.put(handlerClz, method);
88+
method.setAccessible(true);
89+
handlerMap.put(command, method);
8790
return method;
8891
}
8992
}
@@ -92,7 +95,21 @@ public static Method resolveHandler(Class<?> handlerClz, String command) {
9295

9396
return null;
9497
}
95-
98+
99+
private static Map<String, Method> getAndSetHandlerMap(Class<?> handlerClz) {
100+
Map<String, Method> handlerMap;
101+
synchronized(s_handlerCache) {
102+
handlerMap = s_handlerCache.get(handlerClz);
103+
104+
if(handlerMap == null) {
105+
handlerMap = new HashMap<String, Method>();
106+
s_handlerCache.put(handlerClz, handlerMap);
107+
}
108+
}
109+
110+
return handlerMap;
111+
}
112+
96113
@Override
97114
public boolean onCallReceive(RpcServerCall call) {
98115
return dispatch(_targetObject, call);

framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagerComponent2.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ void onStartCommand(RpcServerCall call) {
6161

6262
s_logger.info("StoragePrepare command arg. pool: " + cmd.getStoragePool() + ", vol: " + cmd.getVolumeId());
6363
SampleStoragePrepareAnswer answer = new SampleStoragePrepareAnswer();
64+
answer.setResult("Successfully executed StoragePrepare command");
6465

6566
call.completeCall(answer);
6667
}

0 commit comments

Comments
 (0)