001/* 002 * Copyright The Apache Software Foundation 003 * 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with this 006 * work for additional information regarding copyright ownership. The ASF 007 * licenses this file to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance with the License. 009 * 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, WITHOUT 015 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 016 * License for the specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.hadoop.hbase.thrift; 021 022import org.apache.hadoop.conf.Configuration; 023import org.apache.hadoop.hbase.CallQueueTooBigException; 024import org.apache.hadoop.hbase.CompatibilitySingletonFactory; 025import org.apache.hadoop.hbase.MultiActionResultTooLarge; 026import org.apache.hadoop.hbase.NotServingRegionException; 027import org.apache.hadoop.hbase.RegionTooBusyException; 028import org.apache.hadoop.hbase.UnknownScannerException; 029import org.apache.hadoop.hbase.exceptions.ClientExceptionsUtil; 030import org.apache.hadoop.hbase.exceptions.FailedSanityCheckException; 031import org.apache.hadoop.hbase.exceptions.OutOfOrderScannerNextException; 032import org.apache.hadoop.hbase.exceptions.RegionMovedException; 033import org.apache.hadoop.hbase.exceptions.ScannerResetException; 034import org.apache.hadoop.hbase.thrift.generated.IOError; 035import org.apache.hadoop.hbase.thrift2.generated.TIOError; 036import org.apache.yetus.audience.InterfaceAudience; 037 038/** 039 * This class is for maintaining the various statistics of thrift server 040 * and publishing them through the metrics interfaces. 041 */ 042@InterfaceAudience.Private 043public class ThriftMetrics { 044 045 046 public enum ThriftServerType { 047 ONE, 048 TWO 049 } 050 051 public MetricsThriftServerSource getSource() { 052 return source; 053 } 054 055 public void setSource(MetricsThriftServerSource source) { 056 this.source = source; 057 } 058 059 private MetricsThriftServerSource source; 060 private final long slowResponseTime; 061 public static final String SLOW_RESPONSE_NANO_SEC = 062 "hbase.thrift.slow.response.nano.second"; 063 public static final long DEFAULT_SLOW_RESPONSE_NANO_SEC = 10 * 1000 * 1000; 064 065 066 public ThriftMetrics(Configuration conf, ThriftServerType t) { 067 slowResponseTime = conf.getLong( SLOW_RESPONSE_NANO_SEC, DEFAULT_SLOW_RESPONSE_NANO_SEC); 068 069 if (t == ThriftServerType.ONE) { 070 source = CompatibilitySingletonFactory.getInstance(MetricsThriftServerSourceFactory.class).createThriftOneSource(); 071 } else if (t == ThriftServerType.TWO) { 072 source = CompatibilitySingletonFactory.getInstance(MetricsThriftServerSourceFactory.class).createThriftTwoSource(); 073 } 074 075 } 076 077 public void incTimeInQueue(long time) { 078 source.incTimeInQueue(time); 079 } 080 081 public void setCallQueueLen(int len) { 082 source.setCallQueueLen(len); 083 } 084 085 public void incNumRowKeysInBatchGet(int diff) { 086 source.incNumRowKeysInBatchGet(diff); 087 } 088 089 public void incNumRowKeysInBatchMutate(int diff) { 090 source.incNumRowKeysInBatchMutate(diff); 091 } 092 093 public void incMethodTime(String name, long time) { 094 source.incMethodTime(name, time); 095 // inc general processTime 096 source.incCall(time); 097 if (time > slowResponseTime) { 098 source.incSlowCall(time); 099 } 100 } 101 102 public void incActiveWorkerCount() { 103 source.incActiveWorkerCount(); 104 } 105 106 public void decActiveWorkerCount() { 107 source.decActiveWorkerCount(); 108 } 109 110 /** 111 * Increment the count for a specific exception type. This is called for each exception type 112 * that is returned to the thrift handler. 113 * @param rawThrowable type of exception 114 */ 115 public void exception(Throwable rawThrowable) { 116 source.exception(); 117 118 Throwable throwable = unwrap(rawThrowable); 119 /** 120 * Keep some metrics for commonly seen exceptions 121 * 122 * Try and put the most common types first. 123 * Place child types before the parent type that they extend. 124 * 125 * If this gets much larger we might have to go to a hashmap 126 */ 127 if (throwable != null) { 128 if (throwable instanceof OutOfOrderScannerNextException) { 129 source.outOfOrderException(); 130 } else if (throwable instanceof RegionTooBusyException) { 131 source.tooBusyException(); 132 } else if (throwable instanceof UnknownScannerException) { 133 source.unknownScannerException(); 134 } else if (throwable instanceof ScannerResetException) { 135 source.scannerResetException(); 136 } else if (throwable instanceof RegionMovedException) { 137 source.movedRegionException(); 138 } else if (throwable instanceof NotServingRegionException) { 139 source.notServingRegionException(); 140 } else if (throwable instanceof FailedSanityCheckException) { 141 source.failedSanityException(); 142 } else if (throwable instanceof MultiActionResultTooLarge) { 143 source.multiActionTooLargeException(); 144 } else if (throwable instanceof CallQueueTooBigException) { 145 source.callQueueTooBigException(); 146 } 147 } 148 } 149 150 private static Throwable unwrap(Throwable t) { 151 if (t == null) { 152 return t; 153 } 154 if (t instanceof TIOError || t instanceof IOError) { 155 t = t.getCause(); 156 } 157 return ClientExceptionsUtil.findException(t); 158 } 159}