View Javadoc

1   /*
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * 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, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.hadoop.hbase.thrift;
21  
22  import org.apache.hadoop.hbase.CallQueueTooBigException;
23  import org.apache.hadoop.hbase.MultiActionResultTooLarge;
24  import org.apache.hadoop.hbase.NotServingRegionException;
25  import org.apache.hadoop.hbase.RegionTooBusyException;
26  import org.apache.hadoop.hbase.UnknownScannerException;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
30  import org.apache.hadoop.hbase.exceptions.ClientExceptionsUtil;
31  import org.apache.hadoop.hbase.exceptions.FailedSanityCheckException;
32  import org.apache.hadoop.hbase.exceptions.OutOfOrderScannerNextException;
33  import org.apache.hadoop.hbase.exceptions.RegionMovedException;
34  import org.apache.hadoop.hbase.exceptions.ScannerResetException;
35  import org.apache.hadoop.hbase.thrift.generated.IOError;
36  import org.apache.hadoop.hbase.thrift2.generated.TIOError;
37  
38  /**
39   * This class is for maintaining the various statistics of thrift server
40   * and publishing them through the metrics interfaces.
41   */
42  @InterfaceAudience.Private
43  public class ThriftMetrics  {
44  
45  
46    public enum ThriftServerType {
47      ONE,
48      TWO
49    }
50  
51    public MetricsThriftServerSource getSource() {
52      return source;
53    }
54  
55    public void setSource(MetricsThriftServerSource source) {
56      this.source = source;
57    }
58  
59    private MetricsThriftServerSource source;
60    private final long slowResponseTime;
61    public static final String SLOW_RESPONSE_NANO_SEC =
62      "hbase.thrift.slow.response.nano.second";
63    public static final long DEFAULT_SLOW_RESPONSE_NANO_SEC = 10 * 1000 * 1000;
64  
65  
66    public ThriftMetrics(Configuration conf, ThriftServerType t) {
67      slowResponseTime = conf.getLong( SLOW_RESPONSE_NANO_SEC, DEFAULT_SLOW_RESPONSE_NANO_SEC);
68  
69      if (t == ThriftServerType.ONE) {
70        source = CompatibilitySingletonFactory.getInstance(MetricsThriftServerSourceFactory.class).createThriftOneSource();
71      } else if (t == ThriftServerType.TWO) {
72        source = CompatibilitySingletonFactory.getInstance(MetricsThriftServerSourceFactory.class).createThriftTwoSource();
73      }
74  
75    }
76  
77    public void incTimeInQueue(long time) {
78      source.incTimeInQueue(time);
79    }
80  
81    public void setCallQueueLen(int len) {
82      source.setCallQueueLen(len);
83    }
84  
85    public void incNumRowKeysInBatchGet(int diff) {
86      source.incNumRowKeysInBatchGet(diff);
87    }
88  
89    public void incNumRowKeysInBatchMutate(int diff) {
90      source.incNumRowKeysInBatchMutate(diff);
91    }
92  
93    public void incMethodTime(String name, long time) {
94      source.incMethodTime(name, time);
95      // inc general processTime
96      source.incCall(time);
97      if (time > slowResponseTime) {
98        source.incSlowCall(time);
99      }
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 }