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