1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.monitoring;
21
22 import org.apache.hadoop.hbase.client.Operation;
23 import org.apache.hadoop.hbase.io.WritableWithSize;
24 import org.apache.hadoop.hbase.util.Bytes;
25 import org.apache.hadoop.io.Writable;
26
27 import org.codehaus.jackson.map.ObjectMapper;
28
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Map;
33
34
35
36
37
38
39 public class MonitoredRPCHandlerImpl extends MonitoredTaskImpl
40 implements MonitoredRPCHandler {
41 private String clientAddress;
42 private int remotePort;
43 private long rpcQueueTime;
44 private long rpcStartTime;
45 private String methodName = "";
46 private Object [] params = {};
47 private Writable packet;
48
49 public MonitoredRPCHandlerImpl() {
50 super();
51
52
53 setState(State.WAITING);
54 }
55
56 @Override
57 public synchronized MonitoredRPCHandlerImpl clone() {
58 return (MonitoredRPCHandlerImpl) super.clone();
59 }
60
61
62
63
64
65
66 @Override
67 public String getStatus() {
68 if (getState() != State.RUNNING) {
69 return super.getStatus();
70 }
71 return super.getStatus() + " from " + getClient() + ": " + getRPC();
72 }
73
74
75
76
77
78
79 public long getRPCQueueTime() {
80 if (getState() != State.RUNNING) {
81 return -1;
82 }
83 return rpcQueueTime;
84 }
85
86
87
88
89
90
91 public long getRPCStartTime() {
92 if (getState() != State.RUNNING) {
93 return -1;
94 }
95 return rpcStartTime;
96 }
97
98
99
100
101
102
103 public String getRPC() {
104 return getRPC(false);
105 }
106
107
108
109
110
111
112
113 public synchronized String getRPC(boolean withParams) {
114 if (getState() != State.RUNNING) {
115
116 return "";
117 }
118 StringBuilder buffer = new StringBuilder(256);
119 buffer.append(methodName);
120 if (withParams) {
121 buffer.append("(");
122 for (int i = 0; i < params.length; i++) {
123 if (i != 0)
124 buffer.append(", ");
125 buffer.append(params[i]);
126 }
127 buffer.append(")");
128 }
129 return buffer.toString();
130 }
131
132
133
134
135
136
137 public long getRPCPacketLength() {
138 if (getState() != State.RUNNING || packet == null) {
139
140 return -1L;
141 }
142 if (!(packet instanceof WritableWithSize)) {
143
144 return -1L;
145 }
146 return ((WritableWithSize) packet).getWritableSize();
147 }
148
149
150
151
152
153
154
155 public String getClient() {
156 return clientAddress + ":" + remotePort;
157 }
158
159
160
161
162
163
164 public boolean isRPCRunning() {
165 return getState() == State.RUNNING;
166 }
167
168
169
170
171
172
173
174
175 public boolean isOperationRunning() {
176 if(!isRPCRunning()) {
177 return false;
178 }
179 for(Object param : params) {
180 if (param instanceof Operation) {
181 return true;
182 }
183 }
184 return false;
185 }
186
187
188
189
190
191
192 public synchronized void setRPC(String methodName, Object [] params,
193 long queueTime) {
194 this.methodName = methodName;
195 this.params = params;
196 this.rpcStartTime = System.currentTimeMillis();
197 this.rpcQueueTime = queueTime;
198 this.state = State.RUNNING;
199 }
200
201
202
203
204
205
206 public void setRPCPacket(Writable param) {
207 this.packet = param;
208 }
209
210
211
212
213
214
215 public void setConnection(String clientAddress, int remotePort) {
216 this.clientAddress = clientAddress;
217 this.remotePort = remotePort;
218 }
219
220 @Override
221 public void markComplete(String status) {
222 super.markComplete(status);
223 this.params = null;
224 this.packet = null;
225 }
226
227 public synchronized Map<String, Object> toMap() {
228
229 Map<String, Object> map = super.toMap();
230 if (getState() != State.RUNNING) {
231 return map;
232 }
233 Map<String, Object> rpcJSON = new HashMap<String, Object>();
234 ArrayList paramList = new ArrayList();
235 map.put("rpcCall", rpcJSON);
236 rpcJSON.put("queuetimems", getRPCQueueTime());
237 rpcJSON.put("starttimems", getRPCStartTime());
238 rpcJSON.put("clientaddress", clientAddress);
239 rpcJSON.put("remoteport", remotePort);
240 rpcJSON.put("packetlength", getRPCPacketLength());
241 rpcJSON.put("method", methodName);
242 rpcJSON.put("params", paramList);
243 for(Object param : params) {
244 if(param instanceof byte []) {
245 paramList.add(Bytes.toStringBinary((byte []) param));
246 } else if (param instanceof Operation) {
247 paramList.add(((Operation) param).toMap());
248 } else {
249 paramList.add(param.toString());
250 }
251 }
252 return map;
253 }
254
255 @Override
256 public String toString() {
257 if (getState() != State.RUNNING) {
258 return super.toString();
259 }
260 return super.toString() + ", rpcMethod=" + getRPC();
261 }
262
263 }