[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | package org.apache.hadoop.metrics.util; |
---|
| 19 | |
---|
| 20 | import org.apache.hadoop.metrics.MetricsRecord; |
---|
| 21 | import org.apache.hadoop.util.StringUtils; |
---|
| 22 | |
---|
| 23 | import org.apache.commons.logging.Log; |
---|
| 24 | import org.apache.commons.logging.LogFactory; |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * The MetricsIntValue class is for a metric that is not time varied |
---|
| 28 | * but changes only when it is set. |
---|
| 29 | * Each time its value is set, it is published only *once* at the next update |
---|
| 30 | * call. |
---|
| 31 | * |
---|
| 32 | */ |
---|
| 33 | public class MetricsIntValue extends MetricsBase { |
---|
| 34 | |
---|
| 35 | private static final Log LOG = |
---|
| 36 | LogFactory.getLog("org.apache.hadoop.metrics.util"); |
---|
| 37 | |
---|
| 38 | private int value; |
---|
| 39 | private boolean changed; |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * Constructor - create a new metric |
---|
| 44 | * @param nam the name of the metrics to be used to publish the metric |
---|
| 45 | * @param registry - where the metrics object will be registered |
---|
| 46 | */ |
---|
| 47 | public MetricsIntValue(final String nam, final MetricsRegistry registry, final String description) { |
---|
| 48 | super(nam, description); |
---|
| 49 | value = 0; |
---|
| 50 | changed = false; |
---|
| 51 | registry.add(nam, this); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * Constructor - create a new metric |
---|
| 56 | * @param nam the name of the metrics to be used to publish the metric |
---|
| 57 | * @param registry - where the metrics object will be registered |
---|
| 58 | * A description of {@link #NO_DESCRIPTION} is used |
---|
| 59 | */ |
---|
| 60 | public MetricsIntValue(final String nam, MetricsRegistry registry) { |
---|
| 61 | this(nam, registry, NO_DESCRIPTION); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * Set the value |
---|
| 68 | * @param newValue |
---|
| 69 | */ |
---|
| 70 | public synchronized void set(final int newValue) { |
---|
| 71 | value = newValue; |
---|
| 72 | changed = true; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Get value |
---|
| 77 | * @return the value last set |
---|
| 78 | */ |
---|
| 79 | public synchronized int get() { |
---|
| 80 | return value; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * Push the metric to the mr. |
---|
| 86 | * The metric is pushed only if it was updated since last push |
---|
| 87 | * |
---|
| 88 | * Note this does NOT push to JMX |
---|
| 89 | * (JMX gets the info via {@link #get()} |
---|
| 90 | * |
---|
| 91 | * @param mr |
---|
| 92 | */ |
---|
| 93 | public synchronized void pushMetric(final MetricsRecord mr) { |
---|
| 94 | if (changed) { |
---|
| 95 | try { |
---|
| 96 | mr.setMetric(getName(), value); |
---|
| 97 | } catch (Exception e) { |
---|
| 98 | LOG.info("pushMetric failed for " + getName() + "\n" + |
---|
| 99 | StringUtils.stringifyException(e)); |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | changed = false; |
---|
| 103 | } |
---|
| 104 | } |
---|