commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1533377 - in /commons/sandbox/monitoring/trunk: core/src/main/java/org/apache/commons/monitoring/configuration/ core/src/main/java/org/apache/commons/monitoring/counters/ core/src/main/java/org/apache/commons/monitoring/store/ cube/src/mai...
Date Fri, 18 Oct 2013 10:04:47 GMT
Author: rmannibucau
Date: Fri Oct 18 10:04:46 2013
New Revision: 1533377

URL: http://svn.apache.org/r1533377
Log:
pushin minimum data when using cube to try to reduce computation overhead

Added:
    commons/sandbox/monitoring/trunk/cube/src/main/java/org/apache/commons/monitoring/cube/CubeCounter.java
Modified:
    commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/configuration/Configuration.java
    commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/counters/DefaultCounter.java
    commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/store/BatchCounterDataStore.java
    commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/store/InMemoryCounterBaseStore.java
    commons/sandbox/monitoring/trunk/cube/src/main/java/org/apache/commons/monitoring/cube/CubeDataStore.java
    commons/sandbox/monitoring/trunk/cube/src/test/java/org/apache/commons/monitoring/cube/CubeDataStoreTest.java

Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/configuration/Configuration.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/configuration/Configuration.java?rev=1533377&r1=1533376&r2=1533377&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/configuration/Configuration.java
(original)
+++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/configuration/Configuration.java
Fri Oct 18 10:04:46 2013
@@ -31,6 +31,7 @@ import java.lang.reflect.Array;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Map;
@@ -146,6 +147,10 @@ public final class Configuration {
             Class<?> current = loadedClass;
             while (current != null && !current.isInterface() && !Object.class.equals(current))
{
                 for (final Field field : loadedClass.getDeclaredFields()) {
+                    if (Modifier.isFinal(field.getModifiers())) {
+                        continue;
+                    }
+
                     final String value = PROPERTIES.getProperty(loadedClass.getName() + "."
+ field.getName());
                     if (value != null) {
                         final boolean acc = field.isAccessible();

Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/counters/DefaultCounter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/counters/DefaultCounter.java?rev=1533377&r1=1533376&r2=1533377&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/counters/DefaultCounter.java
(original)
+++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/counters/DefaultCounter.java
Fri Oct 18 10:04:46 2013
@@ -66,6 +66,7 @@ public class DefaultCounter implements C
     @Override
     public void reset() {
         statistics.clear();
+        concurrency.set(0);
     }
 
     @Override

Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/store/BatchCounterDataStore.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/store/BatchCounterDataStore.java?rev=1533377&r1=1533376&r2=1533377&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/store/BatchCounterDataStore.java
(original)
+++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/store/BatchCounterDataStore.java
Fri Oct 18 10:04:46 2013
@@ -18,10 +18,14 @@ package org.apache.commons.monitoring.st
 
 import org.apache.commons.monitoring.Role;
 import org.apache.commons.monitoring.configuration.Configuration;
+import org.apache.commons.monitoring.counters.Counter;
+import org.apache.commons.monitoring.counters.DefaultCounter;
 import org.apache.commons.monitoring.repositories.Repository;
 import org.apache.commons.monitoring.util.DaemonThreadFactory;
 
+import java.util.Collections;
 import java.util.Locale;
+import java.util.Map;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
@@ -45,10 +49,32 @@ public abstract class BatchCounterDataSt
     }
 
     @Override
+    public Counter getOrCreateCounter(final Counter.Key key) {
+        Counter counter = counters.get(key);
+        if (counter == null) {
+            counter = newCounter(key);
+            final Counter previous = counters.putIfAbsent(key, counter);
+            if (previous != null) {
+                counter = previous;
+            }
+        }
+        return counter;
+    }
+
+    protected Counter newCounter(final Counter.Key key) {
+        return new DefaultCounter(key, this);
+    }
+
+    @Override
     public void createOrNoopGauge(final Role role) {
         // no-op
     }
 
+    @Override
+    public Map<Long, Double> getGaugeValues(final GaugeValuesRequest gaugeValuesRequest)
{
+        return Collections.emptyMap(); // often we use another aggregator/visualisator in
this case
+    }
+
     protected abstract void pushCountersByBatch(final Repository instance);
 
     private class BatchPushCountersTask implements Runnable {

Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/store/InMemoryCounterBaseStore.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/store/InMemoryCounterBaseStore.java?rev=1533377&r1=1533376&r2=1533377&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/store/InMemoryCounterBaseStore.java
(original)
+++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/store/InMemoryCounterBaseStore.java
Fri Oct 18 10:04:46 2013
@@ -25,7 +25,7 @@ import java.util.concurrent.ConcurrentMa
 import java.util.concurrent.locks.Lock;
 
 public abstract class InMemoryCounterBaseStore implements DataStore {
-    private final ConcurrentMap<Counter.Key, Counter> counters = new ConcurrentHashMap<Counter.Key,
Counter>(50);
+    protected final ConcurrentMap<Counter.Key, Counter> counters = new ConcurrentHashMap<Counter.Key,
Counter>(50);
 
     @Override
     public Counter getOrCreateCounter(final Counter.Key key) {

Added: commons/sandbox/monitoring/trunk/cube/src/main/java/org/apache/commons/monitoring/cube/CubeCounter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/cube/src/main/java/org/apache/commons/monitoring/cube/CubeCounter.java?rev=1533377&view=auto
==============================================================================
--- commons/sandbox/monitoring/trunk/cube/src/main/java/org/apache/commons/monitoring/cube/CubeCounter.java
(added)
+++ commons/sandbox/monitoring/trunk/cube/src/main/java/org/apache/commons/monitoring/cube/CubeCounter.java
Fri Oct 18 10:04:46 2013
@@ -0,0 +1,137 @@
+/*
+ * 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.commons.monitoring.cube;
+
+import org.apache.commons.monitoring.counters.Counter;
+import org.apache.commons.monitoring.counters.Unit;
+import org.apache.commons.monitoring.store.DataStore;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+public class CubeCounter implements Counter {
+    private final Key key;
+    private volatile long hits = 0;
+    private volatile double sum = 0;
+    private final AtomicInteger concurrency = new AtomicInteger(0);
+    private volatile int maxConcurrency = 0;
+    private Lock lock = new ReentrantLock();
+    private final DataStore dataStore;
+
+    public CubeCounter(final Key key, final DataStore store) {
+        this.key = key;
+        this.dataStore = store;
+    }
+
+    @Override
+    public Key getKey() {
+        return key;
+    }
+
+    @Override
+    public void reset() {
+        sum = 0;
+        hits = 0;
+    }
+
+    @Override
+    public void add(final double delta) {
+        dataStore.addToCounter(this, delta);
+    }
+
+    public void addInternal(final double delta) { // should be called from a thread safe
environment
+        sum += delta;
+        hits++;
+    }
+
+    @Override
+    public void add(final double delta, final Unit unit) {
+        add(key.getRole().getUnit().convert(delta, unit));
+    }
+
+    @Override
+    public AtomicInteger currentConcurrency() {
+        return concurrency;
+    }
+
+    @Override
+    public void updateConcurrency(final int concurrency) {
+        if (concurrency > maxConcurrency) {
+            maxConcurrency = concurrency;
+        }
+    }
+
+    @Override
+    public int getMaxConcurrency() {
+        return maxConcurrency;
+    }
+
+    @Override
+    public long getHits() {
+        return hits;
+    }
+
+    @Override
+    public double getSum() {
+        return sum;
+    }
+
+    public Lock getLock() {
+        return lock;
+    }
+
+    @Override
+    public double getMax() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public double getMin() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public double getStandardDeviation() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public double getVariance() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public double getMean() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public double getGeometricMean() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public double getSumOfLogs() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public double getSumOfSquares() {
+        throw new UnsupportedOperationException();
+    }
+}

Modified: commons/sandbox/monitoring/trunk/cube/src/main/java/org/apache/commons/monitoring/cube/CubeDataStore.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/cube/src/main/java/org/apache/commons/monitoring/cube/CubeDataStore.java?rev=1533377&r1=1533376&r2=1533377&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/cube/src/main/java/org/apache/commons/monitoring/cube/CubeDataStore.java
(original)
+++ commons/sandbox/monitoring/trunk/cube/src/main/java/org/apache/commons/monitoring/cube/CubeDataStore.java
Fri Oct 18 10:04:46 2013
@@ -23,11 +23,11 @@ import org.apache.commons.monitoring.cou
 import org.apache.commons.monitoring.gauges.Gauge;
 import org.apache.commons.monitoring.repositories.Repository;
 import org.apache.commons.monitoring.store.BatchCounterDataStore;
-import org.apache.commons.monitoring.store.GaugeValuesRequest;
+import org.apache.commons.monitoring.store.DefaultDataStore;
 
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.locks.Lock;
 
 public class CubeDataStore extends BatchCounterDataStore {
     private static final String GAUGE_TYPE = "gauge";
@@ -36,22 +36,40 @@ public class CubeDataStore extends Batch
     private final Cube cube = Configuration.findOrCreateInstance(CubeBuilder.class).build();
 
     @Override
+    protected Counter newCounter(final Counter.Key key) {
+        return new CubeCounter(key, this);
+    }
+
+    @Override
+    public void addToCounter(final Counter counter, final double delta) {
+        if (!CubeCounter.class.isInstance(counter)) {
+            throw new IllegalArgumentException(DefaultDataStore.class.getName() + " only
supports " + CubeCounter.class.getName());
+        }
+
+        final CubeCounter cubeCounter = CubeCounter.class.cast(counter);
+        final Lock lock = cubeCounter.getLock();
+        lock.lock();
+        try {
+            cubeCounter.addInternal(delta);
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    @Override
     protected synchronized void pushCountersByBatch(final Repository instance) {
         final long ts = System.currentTimeMillis();
         final StringBuilder events = cube.newEventStream();
         for (final Counter counter : instance) {
-            final MapBuilder data = new MapBuilder()
+            cube.buildEvent(events, COUNTER_TYPE, ts, new MapBuilder()
                 .add("name", counter.getKey().getName())
-                .add("role", counter.getKey().getRole().getName());
-
-            for (final MetricData metric : MetricData.values()) {
-                final double value = metric.value(counter);
-                if (!Double.isNaN(value) && !Double.isInfinite(value)) {
-                    data.add(metric.name(), value);
-                }
-            }
-
-            cube.buildEvent(events, COUNTER_TYPE, ts, data.map());
+                .add("role", counter.getKey().getRole().getName())
+                // other metrics are not handled by CubeCounter and useless since cube re-aggregate
+                // so to reduce overhead we just store it locally
+                .add("concurrency", MetricData.Concurrency.value(counter))
+                .add("sum", MetricData.Sum.value(counter))
+                .add("hits", MetricData.Hits.value(counter))
+                .map());
         }
         cube.post(events);
     }
@@ -69,11 +87,6 @@ public class CubeDataStore extends Batch
                     .map()));
     }
 
-    @Override
-    public Map<Long, Double> getGaugeValues(final GaugeValuesRequest gaugeValuesRequest)
{
-        return Collections.emptyMap(); // TODO: maybe query cube?
-    }
-
     private static class MapBuilder {
         private final Map<String, Object> map = new HashMap<String, Object>();
 

Modified: commons/sandbox/monitoring/trunk/cube/src/test/java/org/apache/commons/monitoring/cube/CubeDataStoreTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/cube/src/test/java/org/apache/commons/monitoring/cube/CubeDataStoreTest.java?rev=1533377&r1=1533376&r2=1533377&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/cube/src/test/java/org/apache/commons/monitoring/cube/CubeDataStoreTest.java
(original)
+++ commons/sandbox/monitoring/trunk/cube/src/test/java/org/apache/commons/monitoring/cube/CubeDataStoreTest.java
Fri Oct 18 10:04:46 2013
@@ -82,14 +82,14 @@ public class CubeDataStoreTest {
         assertTrue(gauges.contains("[{\"type\": \"gauge\",\"time\": \"-\",\"data\": {\"unit\":\"u\",\"marker\":\""
+ host + "\",\"value\":0.0,\"role\":\"mock\"}}]"));
         assertTrue(gauges.contains("[{\"type\": \"gauge\",\"time\": \"-\",\"data\": {\"unit\":\"u\",\"marker\":\""
+ host + "\",\"value\":1.0,\"role\":\"mock\"}}]"));
         assertTrue(gauges.contains("[{\"type\": \"gauge\",\"time\": \"-\",\"data\": {\"unit\":\"u\",\"marker\":\""
+ host + "\",\"value\":2.0,\"role\":\"mock\"}}]"));
-        assertTrue(gauges.contains("[{\"type\": \"gauge\",\"time\": \"-\",\"data\": {\"unit\":\"u\",\"marker\":\""
+ host + "\",\"value\":3.0,\"role\":\"mock\"}}]"));
 
         assertTrue(counters >= 3);
         assertNotNull(aCounterMessage);
-        assertThat(aCounterMessage, containsString("Variance"));
-        assertThat(aCounterMessage, containsString("Value"));
-        assertThat(aCounterMessage, containsString("Hits"));
-        assertThat(aCounterMessage, containsString("Sum"));
+        assertThat(aCounterMessage, containsString("name"));
+        assertThat(aCounterMessage, containsString("role"));
+        assertThat(aCounterMessage, containsString("hits"));
+        assertThat(aCounterMessage, containsString("sum"));
+        assertThat(aCounterMessage, containsString("concurrency"));
         assertThat(aCounterMessage, containsString("marker"));
     }
 }



Mime
View raw message