001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.client;
021
022import org.apache.commons.lang3.builder.EqualsBuilder;
023import org.apache.commons.lang3.builder.HashCodeBuilder;
024import org.apache.commons.lang3.builder.ToStringBuilder;
025import org.apache.hadoop.hbase.util.GsonUtil;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.yetus.audience.InterfaceStability;
028
029import org.apache.hbase.thirdparty.com.google.gson.Gson;
030import org.apache.hbase.thirdparty.com.google.gson.JsonObject;
031import org.apache.hbase.thirdparty.com.google.gson.JsonSerializer;
032
033/**
034 * Slow/Large Log payload for hbase-client, to be used by Admin API get_slow_responses and
035 * get_large_responses
036 */
037@InterfaceAudience.Public
038@InterfaceStability.Evolving
039final public class OnlineLogRecord extends LogEntry {
040
041  // used to convert object to pretty printed format
042  // used by toJsonPrettyPrint()
043  private static final Gson GSON = GsonUtil.createGson()
044    .setPrettyPrinting()
045    .registerTypeAdapter(OnlineLogRecord.class, (JsonSerializer<OnlineLogRecord>)
046      (slowLogPayload, type, jsonSerializationContext) -> {
047        Gson gson = new Gson();
048        JsonObject jsonObj = (JsonObject) gson.toJsonTree(slowLogPayload);
049        if (slowLogPayload.getMultiGetsCount() == 0) {
050          jsonObj.remove("multiGetsCount");
051        }
052        if (slowLogPayload.getMultiMutationsCount() == 0) {
053          jsonObj.remove("multiMutationsCount");
054        }
055        if (slowLogPayload.getMultiServiceCalls() == 0) {
056          jsonObj.remove("multiServiceCalls");
057        }
058        return jsonObj;
059      }).create();
060
061  private final long startTime;
062  private final int processingTime;
063  private final int queueTime;
064  private final long responseSize;
065  private final String clientAddress;
066  private final String serverClass;
067  private final String methodName;
068  private final String callDetails;
069  private final String param;
070  // we don't want to serialize region name, it is just for the filter purpose
071  // hence avoiding deserialization
072  private final transient String regionName;
073  private final String userName;
074  private final int multiGetsCount;
075  private final int multiMutationsCount;
076  private final int multiServiceCalls;
077
078  public long getStartTime() {
079    return startTime;
080  }
081
082  public int getProcessingTime() {
083    return processingTime;
084  }
085
086  public int getQueueTime() {
087    return queueTime;
088  }
089
090  public long getResponseSize() {
091    return responseSize;
092  }
093
094  public String getClientAddress() {
095    return clientAddress;
096  }
097
098  public String getServerClass() {
099    return serverClass;
100  }
101
102  public String getMethodName() {
103    return methodName;
104  }
105
106  public String getCallDetails() {
107    return callDetails;
108  }
109
110  public String getParam() {
111    return param;
112  }
113
114  public String getRegionName() {
115    return regionName;
116  }
117
118  public String getUserName() {
119    return userName;
120  }
121
122  public int getMultiGetsCount() {
123    return multiGetsCount;
124  }
125
126  public int getMultiMutationsCount() {
127    return multiMutationsCount;
128  }
129
130  public int getMultiServiceCalls() {
131    return multiServiceCalls;
132  }
133
134  private OnlineLogRecord(final long startTime, final int processingTime, final int queueTime,
135      final long responseSize, final String clientAddress, final String serverClass,
136      final String methodName, final String callDetails, final String param,
137      final String regionName, final String userName, final int multiGetsCount,
138      final int multiMutationsCount, final int multiServiceCalls) {
139    this.startTime = startTime;
140    this.processingTime = processingTime;
141    this.queueTime = queueTime;
142    this.responseSize = responseSize;
143    this.clientAddress = clientAddress;
144    this.serverClass = serverClass;
145    this.methodName = methodName;
146    this.callDetails = callDetails;
147    this.param = param;
148    this.regionName = regionName;
149    this.userName = userName;
150    this.multiGetsCount = multiGetsCount;
151    this.multiMutationsCount = multiMutationsCount;
152    this.multiServiceCalls = multiServiceCalls;
153  }
154
155  public static class OnlineLogRecordBuilder {
156    private long startTime;
157    private int processingTime;
158    private int queueTime;
159    private long responseSize;
160    private String clientAddress;
161    private String serverClass;
162    private String methodName;
163    private String callDetails;
164    private String param;
165    private String regionName;
166    private String userName;
167    private int multiGetsCount;
168    private int multiMutationsCount;
169    private int multiServiceCalls;
170
171    public OnlineLogRecordBuilder setStartTime(long startTime) {
172      this.startTime = startTime;
173      return this;
174    }
175
176    public OnlineLogRecordBuilder setProcessingTime(int processingTime) {
177      this.processingTime = processingTime;
178      return this;
179    }
180
181    public OnlineLogRecordBuilder setQueueTime(int queueTime) {
182      this.queueTime = queueTime;
183      return this;
184    }
185
186    public OnlineLogRecordBuilder setResponseSize(long responseSize) {
187      this.responseSize = responseSize;
188      return this;
189    }
190
191    public OnlineLogRecordBuilder setClientAddress(String clientAddress) {
192      this.clientAddress = clientAddress;
193      return this;
194    }
195
196    public OnlineLogRecordBuilder setServerClass(String serverClass) {
197      this.serverClass = serverClass;
198      return this;
199    }
200
201    public OnlineLogRecordBuilder setMethodName(String methodName) {
202      this.methodName = methodName;
203      return this;
204    }
205
206    public OnlineLogRecordBuilder setCallDetails(String callDetails) {
207      this.callDetails = callDetails;
208      return this;
209    }
210
211    public OnlineLogRecordBuilder setParam(String param) {
212      this.param = param;
213      return this;
214    }
215
216    public OnlineLogRecordBuilder setRegionName(String regionName) {
217      this.regionName = regionName;
218      return this;
219    }
220
221    public OnlineLogRecordBuilder setUserName(String userName) {
222      this.userName = userName;
223      return this;
224    }
225
226    public OnlineLogRecordBuilder setMultiGetsCount(int multiGetsCount) {
227      this.multiGetsCount = multiGetsCount;
228      return this;
229    }
230
231    public OnlineLogRecordBuilder setMultiMutationsCount(int multiMutationsCount) {
232      this.multiMutationsCount = multiMutationsCount;
233      return this;
234    }
235
236    public OnlineLogRecordBuilder setMultiServiceCalls(int multiServiceCalls) {
237      this.multiServiceCalls = multiServiceCalls;
238      return this;
239    }
240
241    public OnlineLogRecord build() {
242      return new OnlineLogRecord(startTime, processingTime, queueTime, responseSize,
243        clientAddress, serverClass, methodName, callDetails, param, regionName,
244        userName, multiGetsCount, multiMutationsCount, multiServiceCalls);
245    }
246  }
247
248  @Override
249  public boolean equals(Object o) {
250    if (this == o) {
251      return true;
252    }
253
254    if (o == null || getClass() != o.getClass()) {
255      return false;
256    }
257
258    OnlineLogRecord that = (OnlineLogRecord) o;
259
260    return new EqualsBuilder()
261      .append(startTime, that.startTime)
262      .append(processingTime, that.processingTime)
263      .append(queueTime, that.queueTime)
264      .append(responseSize, that.responseSize)
265      .append(multiGetsCount, that.multiGetsCount)
266      .append(multiMutationsCount, that.multiMutationsCount)
267      .append(multiServiceCalls, that.multiServiceCalls)
268      .append(clientAddress, that.clientAddress)
269      .append(serverClass, that.serverClass)
270      .append(methodName, that.methodName)
271      .append(callDetails, that.callDetails)
272      .append(param, that.param)
273      .append(regionName, that.regionName)
274      .append(userName, that.userName)
275      .isEquals();
276  }
277
278  @Override
279  public int hashCode() {
280    return new HashCodeBuilder(17, 37)
281      .append(startTime)
282      .append(processingTime)
283      .append(queueTime)
284      .append(responseSize)
285      .append(clientAddress)
286      .append(serverClass)
287      .append(methodName)
288      .append(callDetails)
289      .append(param)
290      .append(regionName)
291      .append(userName)
292      .append(multiGetsCount)
293      .append(multiMutationsCount)
294      .append(multiServiceCalls)
295      .toHashCode();
296  }
297
298  @Override
299  public String toJsonPrettyPrint() {
300    return GSON.toJson(this);
301  }
302
303  @Override
304  public String toString() {
305    return new ToStringBuilder(this)
306      .append("startTime", startTime)
307      .append("processingTime", processingTime)
308      .append("queueTime", queueTime)
309      .append("responseSize", responseSize)
310      .append("clientAddress", clientAddress)
311      .append("serverClass", serverClass)
312      .append("methodName", methodName)
313      .append("callDetails", callDetails)
314      .append("param", param)
315      .append("regionName", regionName)
316      .append("userName", userName)
317      .append("multiGetsCount", multiGetsCount)
318      .append("multiMutationsCount", multiMutationsCount)
319      .append("multiServiceCalls", multiServiceCalls)
320      .toString();
321  }
322
323}