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,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.wake;
020
021import org.apache.reef.wake.metrics.Meter;
022
023import java.util.concurrent.atomic.AtomicBoolean;
024
025/**
026 * An {@link EStage} that implements metering
027 *
028 * @param <T> type
029 */
030public abstract class AbstractEStage<T> implements EStage<T> {
031
032  protected final AtomicBoolean closed;
033  protected final String name;
034  protected final Meter inMeter;
035
036  /**
037   * outputs share a single meter
038   */
039  protected final Meter outMeter;
040
041  /**
042   * Constructs an abstract estage
043   *
044   * @parm stageName the stage name
045   */
046  public AbstractEStage(String stageName) {
047    this.closed = new AtomicBoolean(false);
048    this.name = stageName;
049    this.inMeter = new Meter(stageName + "_in");
050    this.outMeter = new Meter(stageName + "_out");
051  }
052
053  /**
054   * Gets the input meter of this stage
055   *
056   * @return the input meter
057   */
058  public Meter getInMeter() {
059    return inMeter;
060  }
061
062  /**
063   * Gets the output meter of this stage
064   *
065   * @return the output meter
066   */
067  public Meter getOutMeter() {
068    return outMeter;
069  }
070
071  /**
072   * Updates the input meter.
073   * <p/>
074   * Stages that want to meter their
075   * input must call this each time an event is input.
076   */
077  protected void beforeOnNext() {
078    inMeter.mark(1);
079  }
080
081  /**
082   * Updates the output meter.
083   * <p/>
084   * Stages that want to meter their
085   * output must call this each time an event is output.
086   */
087  protected void afterOnNext() {
088    outMeter.mark(1);
089  }
090
091}