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.coprocessor.example;
019
020import java.io.IOException;
021import java.util.List;
022import java.util.Optional;
023import java.util.concurrent.ThreadLocalRandom;
024import org.apache.hadoop.hbase.Cell;
025import org.apache.hadoop.hbase.CoprocessorEnvironment;
026import org.apache.hadoop.hbase.client.Get;
027import org.apache.hadoop.hbase.coprocessor.ObserverContext;
028import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
029import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
030import org.apache.hadoop.hbase.coprocessor.RegionObserver;
031import org.apache.hadoop.hbase.metrics.Counter;
032import org.apache.hadoop.hbase.metrics.MetricRegistry;
033import org.apache.hadoop.hbase.metrics.Timer;
034import org.apache.hadoop.hbase.regionserver.FlushLifeCycleTracker;
035import org.apache.hadoop.hbase.regionserver.Store;
036import org.apache.hadoop.hbase.regionserver.StoreFile;
037import org.apache.hadoop.hbase.regionserver.compactions.CompactionLifeCycleTracker;
038import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequest;
039import org.apache.yetus.audience.InterfaceAudience;
040
041/**
042 * An example coprocessor that collects some metrics to demonstrate the usage of exporting custom
043 * metrics from the coprocessor.
044 * <p>
045 * These metrics will be available through the regular Hadoop metrics2 sinks (ganglia, opentsdb,
046 * etc) as well as JMX output. You can view a snapshot of the metrics by going to the http web UI of
047 * the regionserver page, something like http://myregionserverhost:16030/jmx
048 * </p>
049 * @see ExampleMasterObserverWithMetrics
050 */
051@InterfaceAudience.Private
052public class ExampleRegionObserverWithMetrics implements RegionCoprocessor {
053
054  private Counter preGetCounter;
055  private Counter flushCounter;
056  private Counter filesCompactedCounter;
057  private Timer costlyOperationTimer;
058  private ExampleRegionObserver observer;
059
060  class ExampleRegionObserver implements RegionCoprocessor, RegionObserver {
061    @Override
062    public Optional<RegionObserver> getRegionObserver() {
063      return Optional.of(this);
064    }
065
066    @Override
067    public void preGetOp(ObserverContext<RegionCoprocessorEnvironment> e, Get get,
068      List<Cell> results) throws IOException {
069      // Increment the Counter whenever the coprocessor is called
070      preGetCounter.increment();
071    }
072
073    @Override
074    public void postGetOp(ObserverContext<RegionCoprocessorEnvironment> e, Get get,
075      List<Cell> results) throws IOException {
076      // do a costly (high latency) operation which we want to measure how long it takes by
077      // using a Timer (which is a Meter and a Histogram).
078      long start = System.nanoTime();
079      try {
080        performCostlyOperation();
081      } finally {
082        costlyOperationTimer.updateNanos(System.nanoTime() - start);
083      }
084    }
085
086    @Override
087    public void postFlush(ObserverContext<RegionCoprocessorEnvironment> c,
088      FlushLifeCycleTracker tracker) throws IOException {
089      flushCounter.increment();
090    }
091
092    @Override
093    public void postFlush(ObserverContext<RegionCoprocessorEnvironment> c, Store store,
094      StoreFile resultFile, FlushLifeCycleTracker tracker) throws IOException {
095      flushCounter.increment();
096    }
097
098    @Override
099    public void postCompactSelection(ObserverContext<RegionCoprocessorEnvironment> c, Store store,
100      List<? extends StoreFile> selected, CompactionLifeCycleTracker tracker,
101      CompactionRequest request) {
102      if (selected != null) {
103        filesCompactedCounter.increment(selected.size());
104      }
105    }
106
107    private void performCostlyOperation() {
108      try {
109        // simulate the operation by sleeping.
110        Thread.sleep(ThreadLocalRandom.current().nextLong(100));
111      } catch (InterruptedException ignore) {
112      }
113    }
114  }
115
116  @Override
117  public Optional<RegionObserver> getRegionObserver() {
118    return Optional.of(observer);
119  }
120
121  @Override
122  public void start(CoprocessorEnvironment env) throws IOException {
123    // start for the RegionServerObserver will be called only once in the lifetime of the
124    // server. We will construct and register all metrics that we will track across method
125    // invocations.
126
127    if (env instanceof RegionCoprocessorEnvironment) {
128      // Obtain the MetricRegistry for the RegionServer. Metrics from this registry will be reported
129      // at the region server level per-regionserver.
130      MetricRegistry registry =
131        ((RegionCoprocessorEnvironment) env).getMetricRegistryForRegionServer();
132      observer = new ExampleRegionObserver();
133
134      if (preGetCounter == null) {
135        // Create a new Counter, or get the already registered counter.
136        // It is much better to only call this once and save the Counter as a class field instead
137        // of creating the counter every time a coprocessor method is invoked. This will negate
138        // any performance bottleneck coming from map lookups tracking metrics in the registry.
139        // Returned counter instance is shared by all coprocessors of the same class in the same
140        // region server.
141        preGetCounter = registry.counter("preGetRequests");
142      }
143
144      if (costlyOperationTimer == null) {
145        // Create a Timer to track execution times for the costly operation.
146        costlyOperationTimer = registry.timer("costlyOperation");
147      }
148
149      if (flushCounter == null) {
150        // Track the number of flushes that have completed
151        flushCounter = registry.counter("flushesCompleted");
152      }
153
154      if (filesCompactedCounter == null) {
155        // Track the number of files that were compacted (many files may be rewritten in a single
156        // compaction).
157        filesCompactedCounter = registry.counter("filesCompacted");
158      }
159    }
160  }
161
162  @Override
163  public void stop(CoprocessorEnvironment e) throws IOException {
164    // we should NOT remove / deregister the metrics in stop(). The whole registry will be
165    // removed when the last region of the table is closed.
166  }
167}