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    
019    package org.apache.hadoop.metrics2;
020    
021    import org.apache.hadoop.classification.InterfaceAudience;
022    import org.apache.hadoop.classification.InterfaceStability;
023    
024    /**
025     * The metrics system interface
026     */
027    @InterfaceAudience.Public
028    @InterfaceStability.Evolving
029    public abstract class MetricsSystem implements MetricsSystemMXBean {
030    
031      @InterfaceAudience.Private
032      public abstract MetricsSystem init(String prefix);
033    
034      /**
035       * Register a metrics source
036       * @param <T>   the actual type of the source object
037       * @param source object to register
038       * @param name  of the source. Must be unique or null (then extracted from
039       *              the annotations of the source object.)
040       * @param desc  the description of the source (or null. See above.)
041       * @return the source object
042       * @exception MetricsException
043       */
044      public abstract <T> T register(String name, String desc, T source);
045    
046      /**
047       * Register a metrics source (deriving name and description from the object)
048       * @param <T>   the actual type of the source object
049       * @param source  object to register
050       * @return  the source object
051       * @exception MetricsException
052       */
053      public <T> T register(T source) {
054        return register(null, null, source);
055      }
056    
057      /**
058       * @param name  of the metrics source
059       * @return the metrics source (potentially wrapped) object
060       */
061      @InterfaceAudience.Private
062      public abstract MetricsSource getSource(String name);
063    
064      /**
065       * Register a metrics sink
066       * @param <T>   the type of the sink
067       * @param sink  to register
068       * @param name  of the sink. Must be unique.
069       * @param desc  the description of the sink
070       * @return the sink
071       * @exception MetricsException
072       */
073      public abstract <T extends MetricsSink>
074      T register(String name, String desc, T sink);
075    
076      /**
077       * Register a callback interface for JMX events
078       * @param callback  the callback object implementing the MBean interface.
079       */
080      public abstract void register(Callback callback);
081    
082      /**
083       * Shutdown the metrics system completely (usually during server shutdown.)
084       * The MetricsSystemMXBean will be unregistered.
085       * @return true if shutdown completed
086       */
087      public abstract boolean shutdown();
088    
089      /**
090       * The metrics system callback interface (needed for proxies.)
091       */
092      public interface Callback {
093        /**
094         * Called before start()
095         */
096        void preStart();
097    
098        /**
099         * Called after start()
100         */
101        void postStart();
102    
103        /**
104         * Called before stop()
105         */
106        void preStop();
107    
108        /**
109         * Called after stop()
110         */
111        void postStop();
112      }
113    
114      /**
115       * Convenient abstract class for implementing callback interface
116       */
117      public static abstract class AbstractCallback implements Callback {
118        @Override public void preStart() {}
119        @Override public void postStart() {}
120        @Override public void preStop() {}
121        @Override public void postStop() {}
122      }
123    }