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; 027 028import org.apache.hbase.thirdparty.com.google.gson.Gson; 029import org.apache.hbase.thirdparty.com.google.gson.JsonObject; 030import org.apache.hbase.thirdparty.com.google.gson.JsonSerializer; 031 032/** 033 * Slow/Large Log payload for hbase-client, to be used by Admin API get_slow_responses and 034 * get_large_responses 035 */ 036@InterfaceAudience.Private 037final public class OnlineLogRecord { 038 039 // used to convert object to pretty printed format 040 // used by toJsonPrettyPrint() 041 private static final Gson GSON = GsonUtil.createGson() 042 .setPrettyPrinting() 043 .registerTypeAdapter(OnlineLogRecord.class, (JsonSerializer<OnlineLogRecord>) 044 (slowLogPayload, type, jsonSerializationContext) -> { 045 Gson gson = new Gson(); 046 JsonObject jsonObj = (JsonObject) gson.toJsonTree(slowLogPayload); 047 if (slowLogPayload.getMultiGetsCount() == 0) { 048 jsonObj.remove("multiGetsCount"); 049 } 050 if (slowLogPayload.getMultiMutationsCount() == 0) { 051 jsonObj.remove("multiMutationsCount"); 052 } 053 if (slowLogPayload.getMultiServiceCalls() == 0) { 054 jsonObj.remove("multiServiceCalls"); 055 } 056 return jsonObj; 057 }).create(); 058 059 private long startTime; 060 private int processingTime; 061 private int queueTime; 062 private long responseSize; 063 private String clientAddress; 064 private String serverClass; 065 private String methodName; 066 private String callDetails; 067 private String param; 068 // we don't want to serialize region name, it is just for the filter purpose 069 // hence avoiding deserialization 070 private transient String regionName; 071 private String userName; 072 private int multiGetsCount; 073 private int multiMutationsCount; 074 private int multiServiceCalls; 075 076 public long getStartTime() { 077 return startTime; 078 } 079 080 public int getProcessingTime() { 081 return processingTime; 082 } 083 084 public int getQueueTime() { 085 return queueTime; 086 } 087 088 public long getResponseSize() { 089 return responseSize; 090 } 091 092 public String getClientAddress() { 093 return clientAddress; 094 } 095 096 public String getServerClass() { 097 return serverClass; 098 } 099 100 public String getMethodName() { 101 return methodName; 102 } 103 104 public String getCallDetails() { 105 return callDetails; 106 } 107 108 public String getParam() { 109 return param; 110 } 111 112 public String getRegionName() { 113 return regionName; 114 } 115 116 public String getUserName() { 117 return userName; 118 } 119 120 public int getMultiGetsCount() { 121 return multiGetsCount; 122 } 123 124 public int getMultiMutationsCount() { 125 return multiMutationsCount; 126 } 127 128 public int getMultiServiceCalls() { 129 return multiServiceCalls; 130 } 131 132 private OnlineLogRecord(final long startTime, final int processingTime, final int queueTime, 133 final long responseSize, final String clientAddress, final String serverClass, 134 final String methodName, final String callDetails, final String param, 135 final String regionName, final String userName, final int multiGetsCount, 136 final int multiMutationsCount, final int multiServiceCalls) { 137 this.startTime = startTime; 138 this.processingTime = processingTime; 139 this.queueTime = queueTime; 140 this.responseSize = responseSize; 141 this.clientAddress = clientAddress; 142 this.serverClass = serverClass; 143 this.methodName = methodName; 144 this.callDetails = callDetails; 145 this.param = param; 146 this.regionName = regionName; 147 this.userName = userName; 148 this.multiGetsCount = multiGetsCount; 149 this.multiMutationsCount = multiMutationsCount; 150 this.multiServiceCalls = multiServiceCalls; 151 } 152 153 public static class OnlineLogRecordBuilder { 154 private long startTime; 155 private int processingTime; 156 private int queueTime; 157 private long responseSize; 158 private String clientAddress; 159 private String serverClass; 160 private String methodName; 161 private String callDetails; 162 private String param; 163 private String regionName; 164 private String userName; 165 private int multiGetsCount; 166 private int multiMutationsCount; 167 private int multiServiceCalls; 168 169 public OnlineLogRecordBuilder setStartTime(long startTime) { 170 this.startTime = startTime; 171 return this; 172 } 173 174 public OnlineLogRecordBuilder setProcessingTime(int processingTime) { 175 this.processingTime = processingTime; 176 return this; 177 } 178 179 public OnlineLogRecordBuilder setQueueTime(int queueTime) { 180 this.queueTime = queueTime; 181 return this; 182 } 183 184 public OnlineLogRecordBuilder setResponseSize(long responseSize) { 185 this.responseSize = responseSize; 186 return this; 187 } 188 189 public OnlineLogRecordBuilder setClientAddress(String clientAddress) { 190 this.clientAddress = clientAddress; 191 return this; 192 } 193 194 public OnlineLogRecordBuilder setServerClass(String serverClass) { 195 this.serverClass = serverClass; 196 return this; 197 } 198 199 public OnlineLogRecordBuilder setMethodName(String methodName) { 200 this.methodName = methodName; 201 return this; 202 } 203 204 public OnlineLogRecordBuilder setCallDetails(String callDetails) { 205 this.callDetails = callDetails; 206 return this; 207 } 208 209 public OnlineLogRecordBuilder setParam(String param) { 210 this.param = param; 211 return this; 212 } 213 214 public OnlineLogRecordBuilder setRegionName(String regionName) { 215 this.regionName = regionName; 216 return this; 217 } 218 219 public OnlineLogRecordBuilder setUserName(String userName) { 220 this.userName = userName; 221 return this; 222 } 223 224 public OnlineLogRecordBuilder setMultiGetsCount(int multiGetsCount) { 225 this.multiGetsCount = multiGetsCount; 226 return this; 227 } 228 229 public OnlineLogRecordBuilder setMultiMutationsCount(int multiMutationsCount) { 230 this.multiMutationsCount = multiMutationsCount; 231 return this; 232 } 233 234 public OnlineLogRecordBuilder setMultiServiceCalls(int multiServiceCalls) { 235 this.multiServiceCalls = multiServiceCalls; 236 return this; 237 } 238 239 public OnlineLogRecord build() { 240 return new OnlineLogRecord(startTime, processingTime, queueTime, responseSize, 241 clientAddress, serverClass, methodName, callDetails, param, regionName, 242 userName, multiGetsCount, multiMutationsCount, multiServiceCalls); 243 } 244 } 245 246 @Override 247 public boolean equals(Object o) { 248 if (this == o) { 249 return true; 250 } 251 252 if (o == null || getClass() != o.getClass()) { 253 return false; 254 } 255 256 OnlineLogRecord that = (OnlineLogRecord) o; 257 258 return new EqualsBuilder() 259 .append(startTime, that.startTime) 260 .append(processingTime, that.processingTime) 261 .append(queueTime, that.queueTime) 262 .append(responseSize, that.responseSize) 263 .append(multiGetsCount, that.multiGetsCount) 264 .append(multiMutationsCount, that.multiMutationsCount) 265 .append(multiServiceCalls, that.multiServiceCalls) 266 .append(clientAddress, that.clientAddress) 267 .append(serverClass, that.serverClass) 268 .append(methodName, that.methodName) 269 .append(callDetails, that.callDetails) 270 .append(param, that.param) 271 .append(regionName, that.regionName) 272 .append(userName, that.userName) 273 .isEquals(); 274 } 275 276 @Override 277 public int hashCode() { 278 return new HashCodeBuilder(17, 37) 279 .append(startTime) 280 .append(processingTime) 281 .append(queueTime) 282 .append(responseSize) 283 .append(clientAddress) 284 .append(serverClass) 285 .append(methodName) 286 .append(callDetails) 287 .append(param) 288 .append(regionName) 289 .append(userName) 290 .append(multiGetsCount) 291 .append(multiMutationsCount) 292 .append(multiServiceCalls) 293 .toHashCode(); 294 } 295 296 public String toJsonPrettyPrint() { 297 return GSON.toJson(this); 298 } 299 300 @Override 301 public String toString() { 302 return new ToStringBuilder(this) 303 .append("startTime", startTime) 304 .append("processingTime", processingTime) 305 .append("queueTime", queueTime) 306 .append("responseSize", responseSize) 307 .append("clientAddress", clientAddress) 308 .append("serverClass", serverClass) 309 .append("methodName", methodName) 310 .append("callDetails", callDetails) 311 .append("param", param) 312 .append("regionName", regionName) 313 .append("userName", userName) 314 .append("multiGetsCount", multiGetsCount) 315 .append("multiMutationsCount", multiMutationsCount) 316 .append("multiServiceCalls", multiServiceCalls) 317 .toString(); 318 } 319 320}