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 */
018
019package org.apache.hadoop.hbase.zookeeper;
020
021import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
022import org.apache.hadoop.metrics2.MetricsCollector;
023import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
024import org.apache.hadoop.metrics2.lib.MutableHistogram;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * Class that transitions metrics from MetricsZooKeeper into the metrics subsystem.
029 *
030 * Implements BaseSource through BaseSourceImpl, following the pattern.
031 */
032@InterfaceAudience.Private
033public class MetricsZooKeeperSourceImpl extends BaseSourceImpl implements MetricsZooKeeperSource {
034
035  private final MutableGaugeLong authFailedFailedOpCount;
036  private final MutableGaugeLong connectionLossFailedOpCount;
037  private final MutableGaugeLong dataInconsistencyFailedOpCount;
038  private final MutableGaugeLong invalidACLFailedOpCount;
039  private final MutableGaugeLong noAuthFailedOpCount;
040  private final MutableGaugeLong operationTimeOutFailedOpCount;
041  private final MutableGaugeLong runtimeInconsistencyFailedOpCount;
042  private final MutableGaugeLong sessionExpiredFailedOpCount;
043  private final MutableGaugeLong systemErrorFailedOpCount;
044  private final MutableGaugeLong totalFailedZKCalls;
045
046  private MutableHistogram readOpLatency;
047  private MutableHistogram writeOpLatency;
048  private MutableHistogram syncOpLatency;
049
050  public MetricsZooKeeperSourceImpl() {
051    this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT);
052  }
053
054  public MetricsZooKeeperSourceImpl(String metricsName, String metricsDescription,
055      String metricsContext, String metricsJmxContext) {
056    super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
057
058    //Create and store the metrics that will be used.
059    authFailedFailedOpCount = this.getMetricsRegistry().newGauge(
060            EXCEPTION_AUTHFAILED, EXCEPTION_AUTHFAILED_DESC, 0L);
061    connectionLossFailedOpCount = this.getMetricsRegistry().newGauge(
062            EXCEPTION_CONNECTIONLOSS, EXCEPTION_CONNECTIONLOSS_DESC, 0L);
063    dataInconsistencyFailedOpCount = this.getMetricsRegistry().newGauge(
064            EXCEPTION_DATAINCONSISTENCY, EXCEPTION_DATAINCONSISTENCY_DESC, 0L);
065    invalidACLFailedOpCount = this.getMetricsRegistry().newGauge(
066            EXCEPTION_INVALIDACL, EXCEPTION_INVALIDACL_DESC, 0L);
067    noAuthFailedOpCount = this.getMetricsRegistry().newGauge(
068            EXCEPTION_NOAUTH, EXCEPTION_NOAUTH_DESC, 0L);
069    operationTimeOutFailedOpCount = this.getMetricsRegistry().newGauge(
070            EXCEPTION_OPERATIONTIMEOUT, EXCEPTION_OPERATIONTIMEOUT_DESC, 0L);
071    runtimeInconsistencyFailedOpCount = this.getMetricsRegistry().newGauge(
072            EXCEPTION_RUNTIMEINCONSISTENCY, EXCEPTION_RUNTIMEINCONSISTENCY_DESC, 0L);
073    sessionExpiredFailedOpCount = this.getMetricsRegistry().newGauge(
074            EXCEPTION_SESSIONEXPIRED, EXCEPTION_SESSIONEXPIRED_DESC, 0L);
075    systemErrorFailedOpCount = this.getMetricsRegistry().newGauge(
076            EXCEPTION_SYSTEMERROR, EXCEPTION_SYSTEMERROR_DESC, 0L);
077    totalFailedZKCalls = this.getMetricsRegistry().newGauge(
078            TOTAL_FAILED_ZK_CALLS, TOTAL_FAILED_ZK_CALLS_DESC, 0L);
079
080    readOpLatency = this.getMetricsRegistry().newHistogram(
081            READ_OPERATION_LATENCY_NAME, READ_OPERATION_LATENCY_DESC);
082    writeOpLatency = this.getMetricsRegistry().newHistogram(
083            WRITE_OPERATION_LATENCY_NAME, WRITE_OPERATION_LATENCY_DESC);
084    syncOpLatency = this.getMetricsRegistry().newHistogram(
085            SYNC_OPERATION_LATENCY_NAME, SYNC_OPERATION_LATENCY_DESC);
086  }
087
088  public void getMetrics(MetricsCollector metricsCollector, boolean all) {
089    super.getMetrics(metricsCollector, all);
090    clearZKExceptionMetrics();
091  }
092
093  private void clearZKExceptionMetrics() {
094    //Reset the exception metrics.
095    clearMetricIfNotNull(authFailedFailedOpCount);
096    clearMetricIfNotNull(connectionLossFailedOpCount);
097    clearMetricIfNotNull(dataInconsistencyFailedOpCount);
098    clearMetricIfNotNull(invalidACLFailedOpCount);
099    clearMetricIfNotNull(noAuthFailedOpCount);
100    clearMetricIfNotNull(operationTimeOutFailedOpCount);
101    clearMetricIfNotNull(runtimeInconsistencyFailedOpCount);
102    clearMetricIfNotNull(sessionExpiredFailedOpCount);
103    clearMetricIfNotNull(systemErrorFailedOpCount);
104    clearMetricIfNotNull(totalFailedZKCalls);
105  }
106
107  private static void clearMetricIfNotNull(MutableGaugeLong metric) {
108    if (metric != null) {
109      metric.set(0L);
110    }
111  }
112
113  @Override
114  public void incrementAuthFailedCount() {
115    authFailedFailedOpCount.incr();
116  }
117
118  @Override
119  public void incrementConnectionLossCount() {
120    connectionLossFailedOpCount.incr();
121  }
122
123  @Override
124  public void incrementDataInconsistencyCount() {
125    dataInconsistencyFailedOpCount.incr();
126  }
127
128  @Override
129  public void incrementInvalidACLCount() {
130    invalidACLFailedOpCount.incr();
131  }
132
133  @Override
134  public void incrementNoAuthCount() {
135    noAuthFailedOpCount.incr();
136  }
137
138  @Override
139  public void incrementOperationTimeoutCount() {
140    operationTimeOutFailedOpCount.incr();
141  }
142
143  @Override
144  public void incrementRuntimeInconsistencyCount() {
145    runtimeInconsistencyFailedOpCount.incr();
146  }
147
148  @Override
149  public void incrementSessionExpiredCount() {
150    sessionExpiredFailedOpCount.incr();
151  }
152
153  @Override
154  public void incrementSystemErrorCount() {
155    systemErrorFailedOpCount.incr();
156  }
157
158  @Override
159  public void incrementTotalFailedZKCalls() {
160    totalFailedZKCalls.incr();
161  }
162
163  @Override
164  public void recordReadOperationLatency(long latency) {
165    readOpLatency.add(latency);
166  }
167
168  @Override
169  public void recordWriteOperationLatency(long latency) {
170    writeOpLatency.add(latency);
171  }
172
173  @Override
174  public void recordSyncOperationLatency(long latency) {
175    syncOpLatency.add(latency);
176  }
177}