You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sirona.apache.org by rm...@apache.org on 2015/11/01 07:19:20 UTC

svn commit: r1711716 [2/2] - in /incubator/sirona/trunk: ./ agent/performance/web/src/main/java/org/apache/sirona/web/lifecycle/ agent/performance/web/src/test/java/org/apache/sirona/test/web/ agent/store/ agent/store/cube/src/main/java/org/apache/siro...

Added: incubator/sirona/trunk/server/websocket-server/src/main/java/org/apache/sirona/websocket/server/GaugeEndpoint.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/server/websocket-server/src/main/java/org/apache/sirona/websocket/server/GaugeEndpoint.java?rev=1711716&view=auto
==============================================================================
--- incubator/sirona/trunk/server/websocket-server/src/main/java/org/apache/sirona/websocket/server/GaugeEndpoint.java (added)
+++ incubator/sirona/trunk/server/websocket-server/src/main/java/org/apache/sirona/websocket/server/GaugeEndpoint.java Sun Nov  1 06:19:18 2015
@@ -0,0 +1,52 @@
+/*
+ * 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.sirona.websocket.server;
+
+import org.apache.johnzon.websocket.mapper.JohnzonTextDecoder;
+import org.apache.sirona.Role;
+import org.apache.sirona.configuration.ioc.IoCs;
+import org.apache.sirona.counters.Unit;
+import org.apache.sirona.repositories.Repository;
+import org.apache.sirona.store.gauge.CollectorGaugeDataStore;
+import org.apache.sirona.websocket.client.domain.WSGauge;
+
+import javax.websocket.OnMessage;
+import javax.websocket.server.ServerEndpoint;
+
+@ServerEndpoint(value = "/wsirona/gauge", decoders = JohnzonTextDecoder.class)
+public class GaugeEndpoint {
+    @OnMessage
+    public void onMessage(final WSGauge gauge) throws Exception {
+        LazyDataStore.COLLECTOR_GAUGE_DATA_STORE.addToGauge(
+            new Role(gauge.getRoleName(), Unit.get(gauge.getRoleUnit())),
+            gauge.getTime(),
+            gauge.getValue(),
+            gauge.getMarker()
+        );
+    }
+
+    private static class LazyDataStore {
+        private static final CollectorGaugeDataStore COLLECTOR_GAUGE_DATA_STORE;
+        static {
+            Repository.INSTANCE.hashCode();
+            COLLECTOR_GAUGE_DATA_STORE = IoCs.findOrCreateInstance(CollectorGaugeDataStore.class);
+            if (COLLECTOR_GAUGE_DATA_STORE == null) {
+                throw new IllegalStateException("Collector only works with " + CollectorGaugeDataStore.class.getName());
+            }
+        }
+    }
+}

Added: incubator/sirona/trunk/server/websocket-server/src/main/java/org/apache/sirona/websocket/server/ValidationEndpoint.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/server/websocket-server/src/main/java/org/apache/sirona/websocket/server/ValidationEndpoint.java?rev=1711716&view=auto
==============================================================================
--- incubator/sirona/trunk/server/websocket-server/src/main/java/org/apache/sirona/websocket/server/ValidationEndpoint.java (added)
+++ incubator/sirona/trunk/server/websocket-server/src/main/java/org/apache/sirona/websocket/server/ValidationEndpoint.java Sun Nov  1 06:19:18 2015
@@ -0,0 +1,75 @@
+/*
+ * 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.sirona.websocket.server;
+
+import org.apache.johnzon.websocket.mapper.JohnzonTextDecoder;
+import org.apache.sirona.configuration.ioc.IoCs;
+import org.apache.sirona.repositories.Repository;
+import org.apache.sirona.status.NodeStatus;
+import org.apache.sirona.status.ValidationResult;
+import org.apache.sirona.store.status.CollectorNodeStatusDataStore;
+import org.apache.sirona.websocket.client.domain.WSValidation;
+
+import javax.websocket.OnMessage;
+import javax.websocket.server.ServerEndpoint;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.Map;
+
+import static java.util.Arrays.asList;
+
+// TODO: think to make it in batch
+@ServerEndpoint(value = "/wsirona/validation", decoders = JohnzonTextDecoder.class)
+public class ValidationEndpoint {
+    @OnMessage
+    public void onMessage(final WSValidation validation) throws Exception {
+        final Map<String, NodeStatus> current = LazyDataStore.COLLECTOR_NODE_STATUS_DATA_STORE.statuses();
+        final NodeStatus ns = current.get(validation.getMarker());
+        final ValidationResult newVr = new ValidationResult(validation.getName(), validation.getStatus(), validation.getMessage());
+        final ValidationResult[] newResults;
+        if (ns != null) {
+            final Collection<ValidationResult> results = new ArrayList<ValidationResult>(asList(ns.getResults()));
+            for (final ValidationResult vr : ns.getResults()) {
+                if (vr.getName().equals(validation.getName())) {
+                    // replace it
+                    results.remove(vr);
+                    break;
+                }
+            }
+
+            // not found so add it
+            results.add(newVr);
+            newResults = results.toArray(new ValidationResult[results.size()]);
+        } else {
+            newResults = new ValidationResult[] { newVr };
+        }
+        LazyDataStore.COLLECTOR_NODE_STATUS_DATA_STORE.store(
+            validation.getMarker(), new NodeStatus(newResults, validation.getDate() == null ? new Date() : validation.getDate()));
+    }
+
+    private static class LazyDataStore {
+        private static final CollectorNodeStatusDataStore COLLECTOR_NODE_STATUS_DATA_STORE;
+        static {
+            IoCs.findOrCreateInstance(Repository.class);
+            COLLECTOR_NODE_STATUS_DATA_STORE = IoCs.findOrCreateInstance(CollectorNodeStatusDataStore.class);
+            if (COLLECTOR_NODE_STATUS_DATA_STORE == null) {
+                throw new IllegalStateException("Collector only works with " + CollectorNodeStatusDataStore.class.getName());
+            }
+        }
+    }
+}

Added: incubator/sirona/trunk/server/websocket-server/src/test/java/org/apache/sirona/websocket/WebSocketTest.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/server/websocket-server/src/test/java/org/apache/sirona/websocket/WebSocketTest.java?rev=1711716&view=auto
==============================================================================
--- incubator/sirona/trunk/server/websocket-server/src/test/java/org/apache/sirona/websocket/WebSocketTest.java (added)
+++ incubator/sirona/trunk/server/websocket-server/src/test/java/org/apache/sirona/websocket/WebSocketTest.java Sun Nov  1 06:19:18 2015
@@ -0,0 +1,187 @@
+/*
+ * 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.sirona.websocket;
+
+import org.apache.catalina.startup.Constants;
+import org.apache.johnzon.websocket.mapper.JohnzonTextDecoder;
+import org.apache.sirona.Role;
+import org.apache.sirona.configuration.ioc.IoCs;
+import org.apache.sirona.counters.Counter;
+import org.apache.sirona.counters.Unit;
+import org.apache.sirona.gauges.Gauge;
+import org.apache.sirona.repositories.DefaultRepository;
+import org.apache.sirona.repositories.Repository;
+import org.apache.sirona.status.NodeStatus;
+import org.apache.sirona.status.NodeStatusReporter;
+import org.apache.sirona.status.Status;
+import org.apache.sirona.status.ValidationResult;
+import org.apache.sirona.store.memory.tracking.InMemoryPathTrackingDataStore;
+import org.apache.sirona.websocket.client.WebSocketCounterDataStore;
+import org.apache.sirona.websocket.client.WebSocketGaugeDataStore;
+import org.apache.sirona.websocket.client.WebSocketNodeStatusDataStore;
+import org.apache.sirona.websocket.server.CounterEndpoint;
+import org.apache.sirona.websocket.server.GaugeEndpoint;
+import org.apache.sirona.websocket.server.ValidationEndpoint;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.net.URL;
+import java.util.Collection;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.SortedMap;
+
+import static java.lang.Thread.sleep;
+import static org.apache.ziplock.JarLocation.jarLocation;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(Arquillian.class)
+public class WebSocketTest {
+    static {
+        System.setProperty(Constants.DEFAULT_JARS_TO_SKIP, "a*,c*,d*,e*,g*,h*,i*,j*,l*,m*,n*,p*,r*,sa*,se*,sh*,su*,v*,w*,x*,z*");
+        System.setProperty(Constants.TLD_JARS_TO_SKIP, "*");
+    }
+
+    @Deployment(testable = false)
+    public static Archive<?> app() {
+        return ShrinkWrap.create(WebArchive.class, "test-websocket.war")
+            .addAsLibraries(
+                ShrinkWrap.create(JavaArchive.class, "sirona-websocket.jar")
+                    .addClasses(CounterEndpoint.class, GaugeEndpoint.class, ValidationEndpoint.class))
+            .addAsLibraries(jarLocation(JohnzonTextDecoder.class));
+    }
+
+    @ArquillianResource
+    protected URL base;
+
+    @Before
+    @After
+    public void reset() {
+        IoCs.findOrCreateInstance(Repository.class).reset();
+    }
+
+    @Test
+    public void runFewClientActions() {
+        // create another repo to keep the main one for the server but use the client stores
+        // to ensure we send to the server with out client
+        final Date date = new Date();
+        final Repository clientRepo = new DefaultRepository(
+            IoCs.processInstance(new WebSocketCounterDataStore() {
+                @Override
+                protected int getPeriod(final String name) {
+                    return 1000;
+                }
+            }),
+            IoCs.processInstance(new WebSocketGaugeDataStore() {
+                @Override
+                protected int getPeriod(final String name) {
+                    return 1000;
+                }
+            }),
+            IoCs.processInstance(new WebSocketNodeStatusDataStore() {
+                @Override
+                protected int getPeriod(final String name) {
+                    return 1000;
+                }
+
+                @Override
+                protected NodeStatusReporter newNodeStatusReporter() {
+                    return new NodeStatusReporter() {
+                        @Override
+                        public synchronized NodeStatus computeStatus() {
+                            return new NodeStatus(new ValidationResult[] {
+                                new ValidationResult("v1", Status.OK, "all is fine"),
+                                new ValidationResult("v2", Status.KO, "oops something went wrong")
+                            }, date);
+                        }
+                    };
+                }
+            }),
+            new InMemoryPathTrackingDataStore()
+        ) {};
+        clientRepo.addGauge(new Gauge() {
+            @Override
+            public Role role() {
+                return new Role("testgauge", Unit.HECTO);
+            }
+
+            @Override
+            public double value() {
+                return 10.;
+            }
+        });
+        clientRepo.getCounter(new Counter.Key(new Role("c", Unit.Time.MINUTE), "testcounter")).add(20);
+
+        try { // period = 1s in sirona.properties so wait 2 periods to ensure to have data
+            sleep(2500);
+        } catch (final InterruptedException e) {
+            Thread.interrupted();
+        }
+
+        { // check gauge
+            final Collection<Role> gauges = Repository.INSTANCE.gauges();
+            assertEquals(1, gauges.size());
+
+            final Iterator<Role> iterator = gauges.iterator();
+            final Role next = iterator.next();
+            assertEquals("testgauge", next.getName());
+            assertEquals("*100", next.getUnit().getName());
+
+            final long now = System.currentTimeMillis();
+            final SortedMap<Long, Double> gaugeValues = Repository.INSTANCE.getGaugeValues(now - 3000, now, next);
+            assertTrue(gauges.size() > 0);
+            assertEquals(10., gaugeValues.get(gaugeValues.firstKey()), 0.);
+        }
+        { // check counter
+            final Collection<Counter> counters = Repository.INSTANCE.counters();
+            assertEquals(1, counters.size());
+            final Counter counter = counters.iterator().next();
+            assertEquals("testcounter", counter.getKey().getName());
+            assertEquals("c", counter.getKey().getRole().getName());
+            assertEquals("min", counter.getKey().getRole().getUnit().getName());
+            assertEquals(20., counter.getSum(), 0.);
+            assertEquals(1, counter.getHits());
+        }
+        { // check validation
+            final Map<String, NodeStatus> statuses = Repository.INSTANCE.statuses();
+            assertEquals(1, statuses.size());
+            assertTrue(statuses.containsKey("test"));
+
+            final NodeStatus status = statuses.get("test");
+            assertTrue(Math.abs(date.getTime() - status.getDate().getTime()) < 1250);
+            assertEquals(Status.KO, status.getStatus());
+            assertEquals(2, status.getResults().length);
+            assertEquals("v1", status.getResults()[0].getName());
+            assertEquals(Status.OK, status.getResults()[0].getStatus());
+            assertEquals("all is fine", status.getResults()[0].getMessage());
+            assertEquals("v2", status.getResults()[1].getName());
+            assertEquals(Status.KO, status.getResults()[1].getStatus());
+            assertEquals("oops something went wrong", status.getResults()[1].getMessage());
+        }
+    }
+}

Copied: incubator/sirona/trunk/server/websocket-server/src/test/resources/arquillian.xml (from r1711709, incubator/sirona/trunk/server/reporting/reporting-webapp-full/src/test/resources/arquillian.xml)
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/server/websocket-server/src/test/resources/arquillian.xml?p2=incubator/sirona/trunk/server/websocket-server/src/test/resources/arquillian.xml&p1=incubator/sirona/trunk/server/reporting/reporting-webapp-full/src/test/resources/arquillian.xml&r1=1711709&r2=1711716&rev=1711716&view=diff
==============================================================================
--- incubator/sirona/trunk/server/reporting/reporting-webapp-full/src/test/resources/arquillian.xml (original)
+++ incubator/sirona/trunk/server/websocket-server/src/test/resources/arquillian.xml Sun Nov  1 06:19:18 2015
@@ -20,12 +20,12 @@
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://jboss.org/schema/arquillian
                                 http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
-  <container qualifier="tomcat7">
+  <container qualifier="tomcat7" default="true">
     <configuration>
       <property name="tomcatHome">target/tomcat7</property>
-      <property name="workDir">target/arquillian-work</property>
+      <property name="workDir">target/arquillian</property>
       <property name="unpackArchive">true</property>
-      <property name="bindHttpPort">${tomcat.http:9090}</property>
+      <property name="bindHttpPort">1235</property>
     </configuration>
   </container>
 </arquillian>

Copied: incubator/sirona/trunk/server/websocket-server/src/test/resources/sirona.properties (from r1711709, incubator/sirona/trunk/server/reporting/reporting-webapp-full/src/test/resources/sirona.properties)
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/server/websocket-server/src/test/resources/sirona.properties?p2=incubator/sirona/trunk/server/websocket-server/src/test/resources/sirona.properties&p1=incubator/sirona/trunk/server/reporting/reporting-webapp-full/src/test/resources/sirona.properties&r1=1711709&r2=1711716&rev=1711716&view=diff
==============================================================================
--- incubator/sirona/trunk/server/reporting/reporting-webapp-full/src/test/resources/sirona.properties (original)
+++ incubator/sirona/trunk/server/websocket-server/src/test/resources/sirona.properties Sun Nov  1 06:19:18 2015
@@ -14,5 +14,21 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-org.apache.sirona.store.status.NodeStatusDataStore = org.apache.sirona.store.status.EmptyStatuses
+
+#
+# Collector
+#
+# all in memory for test
+#
+org.apache.sirona.store.status.NodeStatusDataStore = org.apache.sirona.store.status.InMemoryCollectorNodeStatusDataStore
+org.apache.sirona.store.counter.CollectorCounterStore = org.apache.sirona.store.memory.counter.InMemoryCollectorCounterStore
+org.apache.sirona.store.gauge.CollectorGaugeDataStore = org.apache.sirona.store.gauge.DelegatedCollectorGaugeDataStore
+# not yet compatible
 org.apache.sirona.store.tracking.PathTrackingDataStore = org.apache.sirona.store.memory.tracking.InMemoryPathTrackingDataStore
+
+#
+# client config
+#
+org.apache.sirona.core.gauge.activated = false
+org.apache.sirona.websocket.client.WebSocketClientBuilder.marker = test
+org.apache.sirona.websocket.client.WebSocketClientBuilder.uri = ws://localhost:1235/test-websocket/

Modified: incubator/sirona/trunk/store/memory/src/main/java/org/apache/sirona/store/memory/counter/BatchCounterDataStore.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/store/memory/src/main/java/org/apache/sirona/store/memory/counter/BatchCounterDataStore.java?rev=1711716&r1=1711715&r2=1711716&view=diff
==============================================================================
--- incubator/sirona/trunk/store/memory/src/main/java/org/apache/sirona/store/memory/counter/BatchCounterDataStore.java (original)
+++ incubator/sirona/trunk/store/memory/src/main/java/org/apache/sirona/store/memory/counter/BatchCounterDataStore.java Sun Nov  1 06:19:18 2015
@@ -19,7 +19,6 @@ package org.apache.sirona.store.memory.c
 import org.apache.sirona.configuration.Configuration;
 import org.apache.sirona.configuration.ioc.Destroying;
 import org.apache.sirona.counters.Counter;
-import org.apache.sirona.repositories.Repository;
 import org.apache.sirona.store.BatchFuture;
 import org.apache.sirona.util.DaemonThreadFactory;
 
@@ -62,9 +61,9 @@ public abstract class BatchCounterDataSt
         scheduledTask.done();
     }
 
-    protected void clearCountersIfNeeded(final Repository instance) {
+    protected void clearCountersIfNeeded() {
         if (clearAfterCollect) {
-            instance.clearCounters();
+            clearCounters();
         }
     }
 
@@ -74,9 +73,8 @@ public abstract class BatchCounterDataSt
         @Override
         public void run() {
             try {
-                final Repository instance = Repository.INSTANCE;
-                pushCountersByBatch(instance.counters());
-                clearCountersIfNeeded(instance);
+                pushCountersByBatch(counters.values());
+                clearCountersIfNeeded();
             } catch (final Exception e) {
                 LOGGER.log(Level.SEVERE, e.getMessage(), e);
             }