View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.regionserver.wal;
20  
21  import java.util.concurrent.ConcurrentHashMap;
22  import java.util.concurrent.ConcurrentMap;
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.TableName;
25  import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
26  import org.apache.hadoop.metrics2.MetricHistogram;
27  import org.apache.hadoop.metrics2.lib.MutableFastCounter;
28  
29  
30  /**
31   * Class that transitions metrics from MetricsWAL into the metrics subsystem.
32   *
33   * Implements BaseSource through BaseSourceImpl, following the pattern.
34   * @see org.apache.hadoop.hbase.regionserver.wal.MetricsWAL
35   */
36  @InterfaceAudience.Private
37  public class MetricsWALSourceImpl extends BaseSourceImpl implements MetricsWALSource {
38  
39    private final MetricHistogram appendSizeHisto;
40    private final MetricHistogram appendTimeHisto;
41    private final MetricHistogram syncTimeHisto;
42    private final MutableFastCounter appendCount;
43    private final MutableFastCounter slowAppendCount;
44    private final MutableFastCounter logRollRequested;
45    private final MutableFastCounter errorRollRequested;
46    private final MutableFastCounter lowReplicationRollRequested;
47    private final MutableFastCounter slowSyncRollRequested;
48    private final MutableFastCounter sizeRollRequested;
49    private final MutableFastCounter writtenBytes;
50    private final MutableFastCounter successfulLogRolls;
51    // Per table metrics.
52    private final ConcurrentMap<TableName, MutableFastCounter> perTableAppendCount;
53    private final ConcurrentMap<TableName, MutableFastCounter> perTableAppendSize;
54  
55    public MetricsWALSourceImpl() {
56      this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT);
57    }
58  
59    public MetricsWALSourceImpl(String metricsName,
60                                String metricsDescription,
61                                String metricsContext,
62                                String metricsJmxContext) {
63      super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
64  
65      //Create and store the metrics that will be used.
66      appendTimeHisto = this.getMetricsRegistry().newTimeHistogram(APPEND_TIME, APPEND_TIME_DESC);
67      appendSizeHisto = this.getMetricsRegistry().newSizeHistogram(APPEND_SIZE, APPEND_SIZE_DESC);
68      appendCount = this.getMetricsRegistry().newCounter(APPEND_COUNT, APPEND_COUNT_DESC, 0l);
69      slowAppendCount =
70          this.getMetricsRegistry().newCounter(SLOW_APPEND_COUNT, SLOW_APPEND_COUNT_DESC, 0l);
71      syncTimeHisto = this.getMetricsRegistry().newTimeHistogram(SYNC_TIME, SYNC_TIME_DESC);
72      logRollRequested =
73          this.getMetricsRegistry().newCounter(ROLL_REQUESTED, ROLL_REQUESTED_DESC, 0L);
74      errorRollRequested = this.getMetricsRegistry()
75          .newCounter(ERROR_ROLL_REQUESTED, ERROR_ROLL_REQUESTED_DESC, 0L);
76      lowReplicationRollRequested = this.getMetricsRegistry()
77          .newCounter(LOW_REPLICA_ROLL_REQUESTED, LOW_REPLICA_ROLL_REQUESTED_DESC, 0L);
78      slowSyncRollRequested = this.getMetricsRegistry()
79          .newCounter(SLOW_SYNC_ROLL_REQUESTED, SLOW_SYNC_ROLL_REQUESTED_DESC, 0L);
80      sizeRollRequested = this.getMetricsRegistry()
81          .newCounter(SIZE_ROLL_REQUESTED, SIZE_ROLL_REQUESTED_DESC, 0L);
82      writtenBytes = this.getMetricsRegistry().newCounter(WRITTEN_BYTES, WRITTEN_BYTES_DESC, 0L);
83      successfulLogRolls = this.getMetricsRegistry()
84        .newCounter(SUCCESSFUL_LOG_ROLLS, SUCCESSFUL_LOG_ROLLS_DESC, 0L);
85      perTableAppendCount = new ConcurrentHashMap<>();
86      perTableAppendSize = new ConcurrentHashMap<>();
87    }
88  
89    @Override
90    public void incrementAppendSize(TableName tableName, long size) {
91      appendSizeHisto.add(size);
92      MutableFastCounter tableAppendSizeCounter = perTableAppendSize.get(tableName);
93      if (tableAppendSizeCounter == null) {
94        // Ideally putIfAbsent is atomic and we don't need a branch check but we still do it to avoid
95        // expensive string construction for every append.
96        String metricsKey = String.format("%s.%s", tableName, APPEND_SIZE);
97        perTableAppendSize.putIfAbsent(
98            tableName, getMetricsRegistry().newCounter(metricsKey, APPEND_SIZE_DESC, 0L));
99        tableAppendSizeCounter = perTableAppendSize.get(tableName);
100     }
101     tableAppendSizeCounter.incr(size);
102   }
103 
104   @Override
105   public void incrementAppendTime(long time) {
106     appendTimeHisto.add(time);
107   }
108 
109   @Override
110   public void incrementAppendCount(TableName tableName) {
111     appendCount.incr();
112     MutableFastCounter tableAppendCounter = perTableAppendCount.get(tableName);
113     if (tableAppendCounter == null) {
114       String metricsKey = String.format("%s.%s", tableName, APPEND_COUNT);
115       perTableAppendCount.putIfAbsent(
116           tableName, getMetricsRegistry().newCounter(metricsKey, APPEND_COUNT_DESC, 0L));
117       tableAppendCounter = perTableAppendCount.get(tableName);
118     }
119     tableAppendCounter.incr();
120   }
121 
122   @Override
123   public void incrementSlowAppendCount() {
124     slowAppendCount.incr();
125   }
126 
127   @Override
128   public void incrementSyncTime(long time) {
129     syncTimeHisto.add(time);
130   }
131 
132   @Override
133   public void incrementLogRollRequested() {
134     logRollRequested.incr();
135   }
136 
137   @Override
138   public void incrementErrorLogRoll() {
139     errorRollRequested.incr();
140   }
141 
142   @Override
143   public void incrementLowReplicationLogRoll() {
144     lowReplicationRollRequested.incr();
145   }
146 
147   @Override
148   public void incrementSlowSyncLogRoll() {
149     slowSyncRollRequested.incr();
150   }
151 
152   @Override
153   public void incrementSizeLogRoll() {
154     sizeRollRequested.incr();
155   }
156 
157   @Override
158   public void incrementWrittenBytes(long val) {
159     writtenBytes.incr(val);
160   }
161 
162   @Override
163   public void incrementSuccessfulLogRolls() {
164     successfulLogRolls.incr();
165   }
166 
167   @Override
168   public long getSuccessfulLogRolls() {
169     return successfulLogRolls.value();
170   }
171 }