View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import java.lang.reflect.Type;
23  
24  import org.apache.commons.lang.builder.EqualsBuilder;
25  import org.apache.commons.lang.builder.HashCodeBuilder;
26  import org.apache.commons.lang.builder.ToStringBuilder;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.classification.InterfaceStability;
29  import org.apache.hadoop.hbase.util.GsonUtil;
30  
31  import org.apache.hbase.thirdparty.com.google.gson.Gson;
32  import org.apache.hbase.thirdparty.com.google.gson.JsonElement;
33  import org.apache.hbase.thirdparty.com.google.gson.JsonObject;
34  import org.apache.hbase.thirdparty.com.google.gson.JsonSerializationContext;
35  import org.apache.hbase.thirdparty.com.google.gson.JsonSerializer;
36  
37  /**
38   * Slow/Large Log payload for hbase-client, to be used by Admin API get_slow_responses and
39   * get_large_responses
40   */
41  @InterfaceAudience.Public
42  @InterfaceStability.Evolving
43  final public class OnlineLogRecord extends LogEntry {
44    // used to convert object to pretty printed format
45    // used by toJsonPrettyPrint()
46    private static final Gson GSON = GsonUtil.createGson().setPrettyPrinting()
47      .registerTypeAdapter(OnlineLogRecord.class, new JsonSerializer<OnlineLogRecord>() {
48        @Override
49        public JsonElement serialize(OnlineLogRecord slowLogPayload, Type type,
50          JsonSerializationContext jsonSerializationContext) {
51          Gson gson = new Gson();
52          JsonObject jsonObj = (JsonObject) gson.toJsonTree(slowLogPayload);
53          if (slowLogPayload.getMultiGetsCount() == 0) {
54            jsonObj.remove("multiGetsCount");
55          }
56          if (slowLogPayload.getMultiMutationsCount() == 0) {
57            jsonObj.remove("multiMutationsCount");
58          }
59          if (slowLogPayload.getMultiServiceCalls() == 0) {
60            jsonObj.remove("multiServiceCalls");
61          }
62          return jsonObj;
63        }
64      }).create();
65  
66    private final long startTime;
67    private final int processingTime;
68    private final int queueTime;
69    private final long responseSize;
70    private final String clientAddress;
71    private final String serverClass;
72    private final String methodName;
73    private final String callDetails;
74    private final String param;
75    // we don't want to serialize region name, it is just for the filter purpose
76    // hence avoiding deserialization
77    private final transient String regionName;
78    private final String userName;
79    private final int multiGetsCount;
80    private final int multiMutationsCount;
81    private final int multiServiceCalls;
82  
83    public long getStartTime() {
84      return startTime;
85    }
86  
87    public int getProcessingTime() {
88      return processingTime;
89    }
90  
91    public int getQueueTime() {
92      return queueTime;
93    }
94  
95    public long getResponseSize() {
96      return responseSize;
97    }
98  
99    public String getClientAddress() {
100     return clientAddress;
101   }
102 
103   public String getServerClass() {
104     return serverClass;
105   }
106 
107   public String getMethodName() {
108     return methodName;
109   }
110 
111   public String getCallDetails() {
112     return callDetails;
113   }
114 
115   public String getParam() {
116     return param;
117   }
118 
119   public String getRegionName() {
120     return regionName;
121   }
122 
123   public String getUserName() {
124     return userName;
125   }
126 
127   public int getMultiGetsCount() {
128     return multiGetsCount;
129   }
130 
131   public int getMultiMutationsCount() {
132     return multiMutationsCount;
133   }
134 
135   public int getMultiServiceCalls() {
136     return multiServiceCalls;
137   }
138 
139   private OnlineLogRecord(final long startTime, final int processingTime, final int queueTime,
140     final long responseSize, final String clientAddress, final String serverClass,
141     final String methodName, final String callDetails, final String param,
142     final String regionName, final String userName, final int multiGetsCount,
143     final int multiMutationsCount, final int multiServiceCalls) {
144     this.startTime = startTime;
145     this.processingTime = processingTime;
146     this.queueTime = queueTime;
147     this.responseSize = responseSize;
148     this.clientAddress = clientAddress;
149     this.serverClass = serverClass;
150     this.methodName = methodName;
151     this.callDetails = callDetails;
152     this.param = param;
153     this.regionName = regionName;
154     this.userName = userName;
155     this.multiGetsCount = multiGetsCount;
156     this.multiMutationsCount = multiMutationsCount;
157     this.multiServiceCalls = multiServiceCalls;
158   }
159 
160   @InterfaceAudience.Public
161   @InterfaceStability.Evolving
162   public static class OnlineLogRecordBuilder {
163     private long startTime;
164     private int processingTime;
165     private int queueTime;
166     private long responseSize;
167     private String clientAddress;
168     private String serverClass;
169     private String methodName;
170     private String callDetails;
171     private String param;
172     private String regionName;
173     private String userName;
174     private int multiGetsCount;
175     private int multiMutationsCount;
176     private int multiServiceCalls;
177 
178     public OnlineLogRecordBuilder setStartTime(long startTime) {
179       this.startTime = startTime;
180       return this;
181     }
182 
183     public OnlineLogRecordBuilder setProcessingTime(int processingTime) {
184       this.processingTime = processingTime;
185       return this;
186     }
187 
188     public OnlineLogRecordBuilder setQueueTime(int queueTime) {
189       this.queueTime = queueTime;
190       return this;
191     }
192 
193     public OnlineLogRecordBuilder setResponseSize(long responseSize) {
194       this.responseSize = responseSize;
195       return this;
196     }
197 
198     public OnlineLogRecordBuilder setClientAddress(String clientAddress) {
199       this.clientAddress = clientAddress;
200       return this;
201     }
202 
203     public OnlineLogRecordBuilder setServerClass(String serverClass) {
204       this.serverClass = serverClass;
205       return this;
206     }
207 
208     public OnlineLogRecordBuilder setMethodName(String methodName) {
209       this.methodName = methodName;
210       return this;
211     }
212 
213     public OnlineLogRecordBuilder setCallDetails(String callDetails) {
214       this.callDetails = callDetails;
215       return this;
216     }
217 
218     public OnlineLogRecordBuilder setParam(String param) {
219       this.param = param;
220       return this;
221     }
222 
223     public OnlineLogRecordBuilder setRegionName(String regionName) {
224       this.regionName = regionName;
225       return this;
226     }
227 
228     public OnlineLogRecordBuilder setUserName(String userName) {
229       this.userName = userName;
230       return this;
231     }
232 
233     public OnlineLogRecordBuilder setMultiGetsCount(int multiGetsCount) {
234       this.multiGetsCount = multiGetsCount;
235       return this;
236     }
237 
238     public OnlineLogRecordBuilder setMultiMutationsCount(int multiMutationsCount) {
239       this.multiMutationsCount = multiMutationsCount;
240       return this;
241     }
242 
243     public OnlineLogRecordBuilder setMultiServiceCalls(int multiServiceCalls) {
244       this.multiServiceCalls = multiServiceCalls;
245       return this;
246     }
247 
248     public OnlineLogRecord build() {
249       return new OnlineLogRecord(startTime, processingTime, queueTime, responseSize,
250         clientAddress, serverClass, methodName, callDetails, param, regionName,
251         userName, multiGetsCount, multiMutationsCount, multiServiceCalls);
252     }
253   }
254 
255   @Override
256   public boolean equals(Object o) {
257     if (this == o) {
258       return true;
259     }
260 
261     if (o == null || getClass() != o.getClass()) {
262       return false;
263     }
264 
265     OnlineLogRecord that = (OnlineLogRecord) o;
266 
267     return new EqualsBuilder()
268       .append(startTime, that.startTime)
269       .append(processingTime, that.processingTime)
270       .append(queueTime, that.queueTime)
271       .append(responseSize, that.responseSize)
272       .append(multiGetsCount, that.multiGetsCount)
273       .append(multiMutationsCount, that.multiMutationsCount)
274       .append(multiServiceCalls, that.multiServiceCalls)
275       .append(clientAddress, that.clientAddress)
276       .append(serverClass, that.serverClass)
277       .append(methodName, that.methodName)
278       .append(callDetails, that.callDetails)
279       .append(param, that.param)
280       .append(regionName, that.regionName)
281       .append(userName, that.userName)
282       .isEquals();
283   }
284 
285   @Override
286   public int hashCode() {
287     return new HashCodeBuilder(17, 37)
288       .append(startTime)
289       .append(processingTime)
290       .append(queueTime)
291       .append(responseSize)
292       .append(clientAddress)
293       .append(serverClass)
294       .append(methodName)
295       .append(callDetails)
296       .append(param)
297       .append(regionName)
298       .append(userName)
299       .append(multiGetsCount)
300       .append(multiMutationsCount)
301       .append(multiServiceCalls)
302       .toHashCode();
303   }
304 
305   public String toJsonPrettyPrint() {
306     return GSON.toJson(this);
307   }
308 
309   @Override
310   public String toString() {
311     return new ToStringBuilder(this)
312       .append("startTime", startTime)
313       .append("processingTime", processingTime)
314       .append("queueTime", queueTime)
315       .append("responseSize", responseSize)
316       .append("clientAddress", clientAddress)
317       .append("serverClass", serverClass)
318       .append("methodName", methodName)
319       .append("callDetails", callDetails)
320       .append("param", param)
321       .append("regionName", regionName)
322       .append("userName", userName)
323       .append("multiGetsCount", multiGetsCount)
324       .append("multiMutationsCount", multiMutationsCount)
325       .append("multiServiceCalls", multiServiceCalls)
326       .toString();
327   }
328 
329 }