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  @Override
087  public void getMetrics(MetricsCollector metricsCollector, boolean all) {
088    super.getMetrics(metricsCollector, all);
089    clearZKExceptionMetrics();
090  }
091
092  private void clearZKExceptionMetrics() {
093    // Reset the exception metrics.
094    clearMetricIfNotNull(authFailedFailedOpCount);
095    clearMetricIfNotNull(connectionLossFailedOpCount);
096    clearMetricIfNotNull(dataInconsistencyFailedOpCount);
097    clearMetricIfNotNull(invalidACLFailedOpCount);
098    clearMetricIfNotNull(noAuthFailedOpCount);
099    clearMetricIfNotNull(operationTimeOutFailedOpCount);
100    clearMetricIfNotNull(runtimeInconsistencyFailedOpCount);
101    clearMetricIfNotNull(sessionExpiredFailedOpCount);
102    clearMetricIfNotNull(systemErrorFailedOpCount);
103    clearMetricIfNotNull(totalFailedZKCalls);
104  }
105
106  private static void clearMetricIfNotNull(MutableGaugeLong metric) {
107    if (metric != null) {
108      metric.set(0L);
109    }
110  }
111
112  @Override
113  public void incrementAuthFailedCount() {
114    authFailedFailedOpCount.incr();
115  }
116
117  @Override
118  public void incrementConnectionLossCount() {
119    connectionLossFailedOpCount.incr();
120  }
121
122  @Override
123  public void incrementDataInconsistencyCount() {
124    dataInconsistencyFailedOpCount.incr();
125  }
126
127  @Override
128  public void incrementInvalidACLCount() {
129    invalidACLFailedOpCount.incr();
130  }
131
132  @Override
133  public void incrementNoAuthCount() {
134    noAuthFailedOpCount.incr();
135  }
136
137  @Override
138  public void incrementOperationTimeoutCount() {
139    operationTimeOutFailedOpCount.incr();
140  }
141
142  @Override
143  public void incrementRuntimeInconsistencyCount() {
144    runtimeInconsistencyFailedOpCount.incr();
145  }
146
147  @Override
148  public void incrementSessionExpiredCount() {
149    sessionExpiredFailedOpCount.incr();
150  }
151
152  @Override
153  public void incrementSystemErrorCount() {
154    systemErrorFailedOpCount.incr();
155  }
156
157  @Override
158  public void incrementTotalFailedZKCalls() {
159    totalFailedZKCalls.incr();
160  }
161
162  @Override
163  public void recordReadOperationLatency(long latency) {
164    readOpLatency.add(latency);
165  }
166
167  @Override
168  public void recordWriteOperationLatency(long latency) {
169    writeOpLatency.add(latency);
170  }
171
172  @Override
173  public void recordSyncOperationLatency(long latency) {
174    syncOpLatency.add(latency);
175  }
176}