HBASE-1258,1259 ganglia metrics for 'requests' is confusing

git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@753603 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2009-03-14 06:00:31 +00:00
parent ed32441b0c
commit 49a2d5c0e9
1 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,73 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.metrics;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.metrics.MetricsRecord;
import org.apache.hadoop.util.StringUtils;
/**
* Publishes a rate based on a counter - you increment the counter each
* time an event occurs (eg: an RPC call) and this publishes a rate.
*/
public class MetricsRate {
private static final Log LOG = LogFactory.getLog("org.apache.hadoop.hbase.metrics");
private String name;
private int value;
private float prevRate;
private long ts;
public MetricsRate(final String name) {
this.name = name;
this.value = 0;
this.prevRate = 0;
this.ts = System.currentTimeMillis();
}
public synchronized void inc(final int incr) {
value += incr;
}
public synchronized void inc() {
value++;
}
private synchronized void intervalHeartBeat() {
long now = System.currentTimeMillis();
long diff = (now-ts)/1000;
if (diff == 0) diff = 1; // sigh this is crap.
this.prevRate = value / diff;
this.value = 0;
this.ts = now;
}
public synchronized void pushMetric(final MetricsRecord mr) {
intervalHeartBeat();
try {
mr.setMetric(name, getPreviousIntervalValue());
} catch (Exception e) {
LOG.info("pushMetric failed for " + name + "\n" +
StringUtils.stringifyException(e));
}
}
public synchronized float getPreviousIntervalValue() {
return this.prevRate;
}
}