You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@slider.apache.org by st...@apache.org on 2015/01/14 17:28:45 UTC
[5/5] incubator-slider git commit: SLIDER-754 add metering/counter of
REST operations
SLIDER-754 add metering/counter of REST operations
Project: http://git-wip-us.apache.org/repos/asf/incubator-slider/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-slider/commit/61855abf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-slider/tree/61855abf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-slider/diff/61855abf
Branch: refs/heads/develop
Commit: 61855abf1d14624041e66f73d7f9c4120edf1c35
Parents: 52e5594
Author: Steve Loughran <st...@apache.org>
Authored: Wed Jan 14 11:23:33 2015 +0000
Committer: Steve Loughran <st...@apache.org>
Committed: Wed Jan 14 16:28:27 2015 +0000
----------------------------------------------------------------------
.../appmaster/management/MeterAndCounter.java | 96 ++++++++++++++++++++
.../management/MetricsAndMonitoring.java | 32 +++++++
2 files changed, 128 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/61855abf/slider-core/src/main/java/org/apache/slider/server/appmaster/management/MeterAndCounter.java
----------------------------------------------------------------------
diff --git a/slider-core/src/main/java/org/apache/slider/server/appmaster/management/MeterAndCounter.java b/slider-core/src/main/java/org/apache/slider/server/appmaster/management/MeterAndCounter.java
new file mode 100644
index 0000000..ed55f73
--- /dev/null
+++ b/slider-core/src/main/java/org/apache/slider/server/appmaster/management/MeterAndCounter.java
@@ -0,0 +1,96 @@
+/*
+ * 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.slider.server.appmaster.management;
+
+import com.codahale.metrics.Counter;
+import com.codahale.metrics.Meter;
+import com.codahale.metrics.MetricRegistry;
+
+/**
+ * A combined meter and counter that can be used to measure load.
+ * Hash and equality are derived from the name
+ */
+public class MeterAndCounter {
+
+ /**
+ * suffix for counters: {@value}
+ */
+ public static final String COUNTER = ".counter";
+
+ /**
+ * suffix for meters: {@value}
+ */
+ public static final String METER = ".meter";
+
+ final Meter meter;
+ final Counter counter;
+ final String name;
+
+ /**
+ * Construct an instance
+ * @param metrics metrics to bond to
+ * @param name name before suffixes are appended
+ */
+ public MeterAndCounter(MetricRegistry metrics, String name) {
+ this.name = name;
+ counter = metrics.counter(name + COUNTER);
+ meter = metrics.meter(name + METER);
+ }
+
+ /**
+ * Construct an instance
+ * @param metrics metrics to bond to
+ * @param clazz class to use to derive name
+ * @param name name before suffixes are appended
+ */
+
+ public MeterAndCounter(MetricRegistry metrics, Class clazz, String name) {
+ this.name = name;
+ counter = metrics.counter(MetricRegistry.name(clazz, name + COUNTER));
+ meter = metrics.meter(MetricRegistry.name(clazz, name + METER));
+ }
+
+ /**
+ * Increment the counter, mark the meter
+ */
+ public void mark() {
+ counter.inc();
+ meter.mark();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ MeterAndCounter that = (MeterAndCounter) o;
+
+ return name.equals(that.name);
+
+ }
+
+ @Override
+ public int hashCode() {
+ return name.hashCode();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/61855abf/slider-core/src/main/java/org/apache/slider/server/appmaster/management/MetricsAndMonitoring.java
----------------------------------------------------------------------
diff --git a/slider-core/src/main/java/org/apache/slider/server/appmaster/management/MetricsAndMonitoring.java b/slider-core/src/main/java/org/apache/slider/server/appmaster/management/MetricsAndMonitoring.java
index 77204d6..c14639d 100644
--- a/slider-core/src/main/java/org/apache/slider/server/appmaster/management/MetricsAndMonitoring.java
+++ b/slider-core/src/main/java/org/apache/slider/server/appmaster/management/MetricsAndMonitoring.java
@@ -24,6 +24,10 @@ import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.service.AbstractService;
import org.apache.hadoop.service.CompositeService;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
/**
* Class for all metrics and monitoring
*/
@@ -44,6 +48,10 @@ public class MetricsAndMonitoring extends CompositeService {
final HealthCheckRegistry health = new HealthCheckRegistry();
+ private final Map<String, MeterAndCounter> meterAndCounterMap
+ = new ConcurrentHashMap<String, MeterAndCounter>();
+
+
public MetricRegistry getMetrics() {
return metrics;
}
@@ -57,6 +65,30 @@ public class MetricsAndMonitoring extends CompositeService {
addService(new MetricsBindingService("MetricsBindingService",
metrics));
super.serviceInit(conf);
+ }
+ public MeterAndCounter getMeterAndCounter(String name) {
+ return meterAndCounterMap.get(name);
}
+
+ /**
+ * Get or create the meter/counter pair
+ * @param name name of instance
+ * @return an instance
+ */
+ public MeterAndCounter getOrCreateMeterAndCounter(String name) {
+ MeterAndCounter instance = meterAndCounterMap.get(name);
+ if (instance == null) {
+ synchronized (this) {
+ // check in a sync block
+ instance = meterAndCounterMap.get(name);
+ if (instance == null) {
+ instance = new MeterAndCounter(metrics, name);
+ meterAndCounterMap.put(name, instance);
+ }
+ }
+ }
+ return instance;
+ }
+
}