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.metrics2;
020
021import org.apache.hadoop.classification.InterfaceAudience;
022import org.apache.hadoop.classification.InterfaceStability;
023
024/**
025 * The metrics system interface.
026 * 
027 * The following components are used for metrics.
028 * <ul>
029 * <li>{@link MetricsSource} generate and update metrics information.</li>
030 * <li>{@link MetricsSink} consume the metrics information</li>
031 * </ul>
032 * 
033 * {@link MetricsSource} and {@link MetricsSink} register with the metrics
034 * system. Implementations of {@link MetricsSystem} polls the
035 * {@link MetricsSource}s periodically and pass the {@link MetricsRecord}s to
036 * {@link MetricsSink}.
037 */
038@InterfaceAudience.Public
039@InterfaceStability.Evolving
040public abstract class MetricsSystem implements MetricsSystemMXBean {
041
042  @InterfaceAudience.Private
043  public abstract MetricsSystem init(String prefix);
044
045  /**
046   * Register a metrics source
047   * @param <T>   the actual type of the source object
048   * @param source object to register
049   * @param name  of the source. Must be unique or null (then extracted from
050   *              the annotations of the source object.)
051   * @param desc  the description of the source (or null. See above.)
052   * @return the source object
053   * @exception MetricsException
054   */
055  public abstract <T> T register(String name, String desc, T source);
056
057  /**
058   * Unregister a metrics source
059   * @param name of the source. This is the name you use to call register()
060   */
061  public abstract void unregisterSource(String name);
062
063  /**
064   * Register a metrics source (deriving name and description from the object)
065   * @param <T>   the actual type of the source object
066   * @param source  object to register
067   * @return  the source object
068   * @exception MetricsException
069   */
070  public <T> T register(T source) {
071    return register(null, null, source);
072  }
073
074  /**
075   * @param name  of the metrics source
076   * @return the metrics source (potentially wrapped) object
077   */
078  @InterfaceAudience.Private
079  public abstract MetricsSource getSource(String name);
080
081  /**
082   * Register a metrics sink
083   * @param <T>   the type of the sink
084   * @param sink  to register
085   * @param name  of the sink. Must be unique.
086   * @param desc  the description of the sink
087   * @return the sink
088   * @exception MetricsException
089   */
090  public abstract <T extends MetricsSink>
091  T register(String name, String desc, T sink);
092
093  /**
094   * Register a callback interface for JMX events
095   * @param callback  the callback object implementing the MBean interface.
096   */
097  public abstract void register(Callback callback);
098
099  /**
100   * Requests an immediate publish of all metrics from sources to sinks.
101   * 
102   * This is a "soft" request: the expectation is that a best effort will be
103   * done to synchronously snapshot the metrics from all the sources and put
104   * them in all the sinks (including flushing the sinks) before returning to
105   * the caller. If this can't be accomplished in reasonable time it's OK to
106   * return to the caller before everything is done. 
107   */
108  public abstract void publishMetricsNow();
109
110  /**
111   * Shutdown the metrics system completely (usually during server shutdown.)
112   * The MetricsSystemMXBean will be unregistered.
113   * @return true if shutdown completed
114   */
115  public abstract boolean shutdown();
116
117  /**
118   * The metrics system callback interface (needed for proxies.)
119   */
120  public interface Callback {
121    /**
122     * Called before start()
123     */
124    void preStart();
125
126    /**
127     * Called after start()
128     */
129    void postStart();
130
131    /**
132     * Called before stop()
133     */
134    void preStop();
135
136    /**
137     * Called after stop()
138     */
139    void postStop();
140  }
141
142  /**
143   * Convenient abstract class for implementing callback interface
144   */
145  public static abstract class AbstractCallback implements Callback {
146    @Override public void preStart() {}
147    @Override public void postStart() {}
148    @Override public void preStop() {}
149    @Override public void postStop() {}
150  }
151}