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