You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2019/04/12 22:14:14 UTC

[geode] 07/07: wip

This is an automated email from the ASF dual-hosted git repository.

klund pushed a commit to branch GEODE-6626-gatewayReceiver-metrics
in repository https://gitbox.apache.org/repos/asf/geode.git

commit 2e0b5b1404f53fe931e88555ff38f90ec51d22b0
Author: Kirk Lund <kl...@apache.org>
AuthorDate: Fri Apr 12 15:13:01 2019 -0700

    wip
---
 .../internal/cache/wan/GatewayReceiverMetrics.java | 35 ++++++++++
 .../command/GatewayReceiverCommandTest.java        | 81 ++++++++++++++++++++++
 .../cache/wan/GatewayReceiverMetricsTest.java      | 52 ++++++++++++++
 .../cache/wan/GatewayReceiverFactoryImpl.java      |  5 +-
 .../cache/wan/GatewayReceiverFactoryImplTest.java  | 11 +++
 .../cache/wan/GatewayReceiverImplTest.java         | 32 ++++++---
 6 files changed, 206 insertions(+), 10 deletions(-)

diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverMetrics.java b/geode-core/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverMetrics.java
new file mode 100644
index 0000000..d340ebe
--- /dev/null
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverMetrics.java
@@ -0,0 +1,35 @@
+/*
+ * 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.geode.internal.cache.wan;
+
+import io.micrometer.core.instrument.Counter;
+import io.micrometer.core.instrument.MeterRegistry;
+
+public class GatewayReceiverMetrics {
+
+  private final MeterRegistry meterRegistry;
+  private final Counter eventsReceivedCounter;
+
+  public GatewayReceiverMetrics(MeterRegistry meterRegistry) {
+    this.meterRegistry = meterRegistry;
+
+    eventsReceivedCounter = Counter.builder("cache.gatewayreceiver.events.received")
+        .register(meterRegistry);
+  }
+
+  public void close() {
+    meterRegistry.remove(eventsReceivedCounter);
+  }
+}
diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/tier/sockets/command/GatewayReceiverCommandTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/tier/sockets/command/GatewayReceiverCommandTest.java
new file mode 100644
index 0000000..b6096bd
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/internal/cache/tier/sockets/command/GatewayReceiverCommandTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.geode.internal.cache.tier.sockets.command;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import io.micrometer.core.instrument.Counter;
+import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.mockito.quality.Strictness;
+
+import org.apache.geode.internal.cache.InternalCache;
+import org.apache.geode.internal.cache.tier.sockets.Message;
+import org.apache.geode.internal.cache.tier.sockets.Part;
+import org.apache.geode.internal.cache.tier.sockets.ServerConnection;
+import org.apache.geode.internal.cache.wan.GatewayReceiverStats;
+import org.apache.geode.internal.security.SecurityService;
+
+public class GatewayReceiverCommandTest {
+
+  @Rule
+  public MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
+
+  @Mock
+  private Message clientMessage;
+  @Mock
+  private ServerConnection serverConnection;
+  @Mock
+  private SecurityService securityService;
+  @Mock
+  private InternalCache cache;
+
+  private long start;
+
+  private MeterRegistry meterRegistry;
+
+  @Before
+  public void setUp() {
+    start = 1;
+    meterRegistry = new SimpleMeterRegistry();
+
+    when(cache.getMeterRegistry()).thenReturn(meterRegistry);
+    when(clientMessage.getPart(anyInt())).thenReturn(mock(Part.class));
+    when(serverConnection.getCache()).thenReturn(cache);
+    when(serverConnection.getCacheServerStats()).thenReturn(mock(GatewayReceiverStats.class));
+    when(serverConnection.getResponseMessage()).thenReturn(mock(Message.class));
+  }
+
+  @Test
+  public void foo() throws Exception {
+    GatewayReceiverCommand command = new GatewayReceiverCommand();
+    command.cmdExecute(clientMessage, serverConnection, securityService, start);
+
+    Counter counter = meterRegistry.find("cache.gatewayreceiver.events.received")
+        .counter();
+
+    assertThat(counter).isNotNull();
+    assertThat(counter.count()).isEqualTo(1L);
+  }
+}
diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/wan/GatewayReceiverMetricsTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/wan/GatewayReceiverMetricsTest.java
new file mode 100644
index 0000000..a1c5f51
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/internal/cache/wan/GatewayReceiverMetricsTest.java
@@ -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.geode.internal.cache.wan;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import io.micrometer.core.instrument.Counter;
+import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
+import org.junit.Before;
+import org.junit.Test;
+
+public class GatewayReceiverMetricsTest {
+
+  private MeterRegistry meterRegistry;
+
+  @Before
+  public void setUp() {
+    meterRegistry = new SimpleMeterRegistry();
+  }
+
+  @Test
+  public void createsEventsReceivedCounter() throws Exception {
+    GatewayReceiverMetrics metrics = new GatewayReceiverMetrics(meterRegistry);
+    Counter counter = meterRegistry.find("cache.gatewayreceiver.events.received")
+        .counter();
+
+    assertThat(counter).isNotNull();
+  }
+
+  @Test
+  public void closeRemovesEventsReceivedCounter() throws Exception {
+    GatewayReceiverMetrics metrics = new GatewayReceiverMetrics(meterRegistry);
+    metrics.close();
+    Counter counter = meterRegistry.find("cache.gatewayreceiver.events.received")
+        .counter();
+
+    assertThat(counter).isNull();
+  }
+}
diff --git a/geode-wan/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverFactoryImpl.java b/geode-wan/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverFactoryImpl.java
index f3b52bf..fbe5192 100644
--- a/geode-wan/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverFactoryImpl.java
+++ b/geode-wan/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverFactoryImpl.java
@@ -20,6 +20,8 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
+import io.micrometer.core.instrument.MeterRegistry;
+
 import org.apache.geode.cache.wan.GatewayReceiver;
 import org.apache.geode.cache.wan.GatewayReceiverFactory;
 import org.apache.geode.cache.wan.GatewayTransportFilter;
@@ -129,9 +131,10 @@ public class GatewayReceiverFactoryImpl implements GatewayReceiverFactory {
 
     GatewayReceiver recv = null;
     if (this.cache instanceof GemFireCacheImpl) {
+      MeterRegistry meterRegistry = cache.getMeterRegistry();
       recv = new GatewayReceiverImpl(this.cache, this.startPort, this.endPort, this.timeBetPings,
           this.socketBuffSize, this.bindAdd, this.filters, this.hostnameForSenders,
-          this.manualStart);
+          this.manualStart, meterRegistry);
       this.cache.addGatewayReceiver(recv);
       InternalDistributedSystem system =
           (InternalDistributedSystem) this.cache.getDistributedSystem();
diff --git a/geode-wan/src/test/java/org/apache/geode/internal/cache/wan/GatewayReceiverFactoryImplTest.java b/geode-wan/src/test/java/org/apache/geode/internal/cache/wan/GatewayReceiverFactoryImplTest.java
index d164eb9..fff5c0f 100644
--- a/geode-wan/src/test/java/org/apache/geode/internal/cache/wan/GatewayReceiverFactoryImplTest.java
+++ b/geode-wan/src/test/java/org/apache/geode/internal/cache/wan/GatewayReceiverFactoryImplTest.java
@@ -25,6 +25,8 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 
+import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -50,6 +52,7 @@ public class GatewayReceiverFactoryImplTest {
   public InternalCache cache;
 
   private GatewayReceiverFactoryImpl gatewayReceiverFactory;
+  private MeterRegistry meterRegistry;
 
   @Parameters(name = "{0}")
   public static Collection<InternalCache> cacheTypes() {
@@ -67,6 +70,7 @@ public class GatewayReceiverFactoryImplTest {
   @Before
   public void setUp() {
     when(cache.getGatewayReceivers()).thenReturn(Collections.emptySet());
+    when(cache.getMeterRegistry()).thenReturn(meterRegistry);
 
     gatewayReceiverFactory = new GatewayReceiverFactoryImpl(cache);
   }
@@ -283,6 +287,13 @@ public class GatewayReceiverFactoryImplTest {
   }
 
   @Test
+  public void createUsesMeterRegistryFromCache() {
+    gatewayReceiverFactory.create();
+
+    verify(cache).getMeterRegistry();
+  }
+
+  @Test
   public void createThrowsIllegalStateExceptionIfGatewayReceiverAlreadyExists() {
     when(cache.getGatewayReceivers()).thenReturn(singleton(mock(GatewayReceiver.class)));
 
diff --git a/geode-wan/src/test/java/org/apache/geode/internal/cache/wan/GatewayReceiverImplTest.java b/geode-wan/src/test/java/org/apache/geode/internal/cache/wan/GatewayReceiverImplTest.java
index ca6819e..3a040c9 100644
--- a/geode-wan/src/test/java/org/apache/geode/internal/cache/wan/GatewayReceiverImplTest.java
+++ b/geode-wan/src/test/java/org/apache/geode/internal/cache/wan/GatewayReceiverImplTest.java
@@ -31,6 +31,8 @@ import java.net.SocketException;
 import java.net.UnknownHostException;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import io.micrometer.core.instrument.MeterRegistry;
+import org.junit.Before;
 import org.junit.Test;
 
 import org.apache.geode.distributed.internal.InternalDistributedSystem;
@@ -40,11 +42,18 @@ import org.apache.geode.internal.net.SocketCreator;
 
 public class GatewayReceiverImplTest {
 
+  private MeterRegistry meterRegistry;
+
+  @Before
+  public void setUp() {
+    meterRegistry = mock(MeterRegistry.class);
+  }
+
   @Test
   public void getHostOnUnstartedGatewayShouldReturnLocalhost() throws UnknownHostException {
     InternalCache cache = mock(InternalCache.class);
     GatewayReceiverImpl gateway =
-        new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true);
+        new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true, meterRegistry);
     assertEquals(SocketCreator.getLocalHost().getHostName(), gateway.getHost());
   }
 
@@ -57,7 +66,7 @@ public class GatewayReceiverImplTest {
     when(server.getExternalAddress()).thenReturn("hello");
     when(cache.addCacheServer(eq(true))).thenReturn(server);
     GatewayReceiverImpl gateway =
-        new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true);
+        new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true, meterRegistry);
     gateway.start();
     assertEquals("hello", gateway.getHost());
   }
@@ -72,7 +81,7 @@ public class GatewayReceiverImplTest {
     when(server.isRunning()).thenReturn(true);
     when(cache.addCacheServer(eq(true))).thenReturn(server);
     GatewayReceiverImpl gateway =
-        new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true);
+        new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true, meterRegistry);
     gateway.start();
     try {
       gateway.destroy();
@@ -91,7 +100,7 @@ public class GatewayReceiverImplTest {
     when(server.getExternalAddress()).thenReturn("hello");
     when(cache.addCacheServer(eq(true))).thenReturn(server);
     GatewayReceiverImpl gateway =
-        new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true);
+        new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true, meterRegistry);
     gateway.start();
     // sender is mocked already to say running is false
     gateway.destroy();
@@ -107,7 +116,7 @@ public class GatewayReceiverImplTest {
     when(server.getExternalAddress()).thenReturn("hello");
     when(cache.addCacheServer(eq(true))).thenReturn(server);
     GatewayReceiverImpl gateway =
-        new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true);
+        new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true, meterRegistry);
     gateway.start();
     // sender is mocked already to say running is false
     gateway.destroy();
@@ -121,7 +130,7 @@ public class GatewayReceiverImplTest {
     when(cache.addCacheServer(eq(true))).thenReturn(server);
     doThrow(new SocketException("Address already in use")).when(server).start();
     GatewayReceiverImpl gateway =
-        new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true);
+        new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true, meterRegistry);
     assertThatThrownBy(() -> gateway.start()).isInstanceOf(GatewayReceiverException.class)
         .hasMessageContaining("No available free port found in the given range");
     verify(server, times(2)).start();
@@ -134,7 +143,7 @@ public class GatewayReceiverImplTest {
     when(cache.addCacheServer(eq(true))).thenReturn(server);
     doThrow(new SocketException("Address already in use")).when(server).start();
     GatewayReceiverImpl gateway =
-        new GatewayReceiverImpl(cache, 2000, 2000, 5, 100, null, null, null, true);
+        new GatewayReceiverImpl(cache, 2000, 2000, 5, 100, null, null, null, true, meterRegistry);
     assertThatThrownBy(() -> gateway.start()).isInstanceOf(GatewayReceiverException.class)
         .hasMessageContaining("No available free port found in the given range");
     verify(server, times(1)).start();
@@ -147,7 +156,7 @@ public class GatewayReceiverImplTest {
     when(cache.addCacheServer(eq(true))).thenReturn(server);
     doThrow(new SocketException("Address already in use")).when(server).start();
     GatewayReceiverImpl gateway =
-        new GatewayReceiverImpl(cache, 2000, 2100, 5, 100, null, null, null, true);
+        new GatewayReceiverImpl(cache, 2000, 2100, 5, 100, null, null, null, true, meterRegistry);
     assertThatThrownBy(() -> gateway.start()).isInstanceOf(GatewayReceiverException.class)
         .hasMessageContaining("No available free port found in the given range");
     assertTrue(gateway.getPort() == 0);
@@ -171,10 +180,15 @@ public class GatewayReceiverImplTest {
       return 0;
     }).when(server).start();
     GatewayReceiverImpl gateway =
-        new GatewayReceiverImpl(cache, 2000, 2010, 5, 100, null, null, null, true);
+        new GatewayReceiverImpl(cache, 2000, 2010, 5, 100, null, null, null, true, meterRegistry);
     gateway.start();
     assertTrue(gateway.getPort() >= 2000);
     assertEquals(2, callCount.get());
     verify(server, times(3)).start(); // 2 failed tries, 1 succeeded
   }
+
+  @Test
+  public void createsEventsReceivedCounter() {
+
+  }
 }