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

[geode] 03/18: Acceptance test fails now

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

dhemery pushed a commit to branch GEODE-7001-region-entry-count
in repository https://gitbox.apache.org/repos/asf/geode.git

commit c8dcc76bb4c30a48b55b4c4f9a4642b441041c7c
Author: Michael Oleske <mo...@pivotal.io>
AuthorDate: Wed Jul 10 10:24:32 2019 -0700

    Acceptance test fails now
    
    Authored-by: Michael Oleske <mo...@pivotal.io>
---
 .../apache/geode/metrics/RegionMetricsTest.java    | 151 ++++++++++++++++++---
 .../statistics/meters/LegacyStatGauge.java         |  15 ++
 2 files changed, 145 insertions(+), 21 deletions(-)

diff --git a/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/RegionMetricsTest.java b/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/RegionMetricsTest.java
index c26bb73..70d5b24 100644
--- a/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/RegionMetricsTest.java
+++ b/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/RegionMetricsTest.java
@@ -1,11 +1,34 @@
+/*
+ * 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.metrics;
 
 import static java.util.Arrays.asList;
+import static org.apache.geode.test.awaitility.GeodeAwaitility.await;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
 
+import io.micrometer.core.instrument.Gauge;
+import org.apache.commons.io.IOUtils;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
@@ -18,9 +41,11 @@ import org.apache.geode.cache.RegionShortcut;
 import org.apache.geode.cache.client.ClientCache;
 import org.apache.geode.cache.client.ClientCacheFactory;
 import org.apache.geode.cache.client.ClientRegionShortcut;
+import org.apache.geode.cache.execute.Function;
+import org.apache.geode.cache.execute.FunctionContext;
 import org.apache.geode.internal.AvailablePortHelper;
+import org.apache.geode.test.compiler.ClassBuilder;
 import org.apache.geode.test.junit.categories.MetricsTest;
-import org.apache.geode.test.junit.rules.gfsh.GfshExecution;
 import org.apache.geode.test.junit.rules.gfsh.GfshRule;
 
 @Category(MetricsTest.class)
@@ -43,6 +68,7 @@ public class RegionMetricsTest {
   private Region<String, String> region;
   private int locatorPort;
   private int serverPort;
+
   @Before
   public void startMembers() throws Exception {
     int[] availablePorts = AvailablePortHelper.getRandomAvailableTCPPorts(2);
@@ -51,40 +77,50 @@ public class RegionMetricsTest {
     locatorFolder = temporaryFolder.newFolder(locatorName);
     serverFolder = temporaryFolder.newFolder(serverName);
 
-     String startLocatorCommand = String.join(SPACE,
-     "start locator",
-     "--name=" + locatorName,
-     "--dir=" + locatorFolder.getAbsolutePath(),
-     "--port=" + locatorPort);
+    String startLocatorCommand = String.join(SPACE,
+        "start locator",
+        "--name=" + locatorName,
+        "--dir=" + locatorFolder.getAbsolutePath(),
+        "--port=" + locatorPort);
 
-     String startServerCommand = String.join(SPACE,
-     "start server",
-     "--name=" + serverName,
-     "--dir=" + serverFolder.getAbsolutePath(),
-     "--server-port=" + serverPort,
-     "--locators=localhost[" + locatorPort + "]");
+    String metricsPublishingServiceJarPath =
+        newJarForMetricsPublishingServiceClass(SimpleMetricsPublishingService.class,
+            "metrics-publishing-service.jar");
 
-     String connectToLocatorCommand =
-     "connect --locator=localhost[" + locatorPort + "]";
+    String startServerCommand = String.join(SPACE,
+        "start server",
+        "--name=" + serverName,
+        "--dir=" + serverFolder.getAbsolutePath(),
+        "--server-port=" + serverPort,
+        "--locators=localhost[" + locatorPort + "]",
+        "--classpath=" + metricsPublishingServiceJarPath);
+
+    String connectToLocatorCommand =
+        "connect --locator=localhost[" + locatorPort + "]";
 
     String createRegionCommand = String.join(SPACE,
         "create region",
         "--name=" + regionName,
         "--type=" + RegionShortcut.PARTITION.name());
 
-    final GfshExecution execute = gfshRule.execute(startLocatorCommand, startServerCommand, connectToLocatorCommand, createRegionCommand);
-    System.out.println("what is going on please???????????????" + execute.getOutputText());
+    gfshRule.execute(startLocatorCommand, startServerCommand, connectToLocatorCommand,
+        createRegionCommand);
+
+    String functionJarPath =
+        newJarForFunctionClass(GetMemberRegionEntriesGaugeFunction.class, "function.jar");
+    String deployCommand = "deploy --jar=" + functionJarPath;
+    String listFunctionsCommand = "list functions";
+
+    gfshRule.execute(connectToLocatorCommand, deployCommand, listFunctionsCommand);
 
     clientCache = new ClientCacheFactory().addPoolLocator("localhost", locatorPort).create();
 
-    region =
-        clientCache.<String, String>createClientRegionFactory(ClientRegionShortcut.PROXY)
-            .create(regionName);
+    region = clientCache.<String, String>createClientRegionFactory(ClientRegionShortcut.PROXY)
+        .create(regionName);
   }
 
   @After
-  public void stopMembers() throws Exception {
-
+  public void stopMembers() {
     String connectToLocatorCommand =
         "connect --locator=localhost[" + locatorPort + "]";
 
@@ -104,5 +140,78 @@ public class RegionMetricsTest {
 
     assertThat(region.sizeOnServer()).isEqualTo(keys.size());
 
+    String connectToLocatorCommand =
+        "connect --locator=localhost[" + locatorPort + "]";
+    String executeFunctionCommand =
+        "execute function --id=" + GetMemberRegionEntriesGaugeFunction.ID;
+
+    await().untilAsserted(() -> {
+      String output =
+          gfshRule.execute(connectToLocatorCommand, executeFunctionCommand).getOutputText();
+
+      assertThat(output.trim())
+          .as("Returned gauge of region entries.")
+          .endsWith("[" + keys.size() + ".0]");
+    });
+  }
+
+  private String newJarForMetricsPublishingServiceClass(Class clazz, String jarName)
+      throws IOException {
+    File jar = temporaryFolder.newFile(jarName);
+
+    String className = clazz.getName();
+    String classAsPath = className.replace('.', '/') + ".class";
+    InputStream stream = clazz.getClassLoader().getResourceAsStream(classAsPath);
+    byte[] bytes = IOUtils.toByteArray(stream);
+    try (FileOutputStream out = new FileOutputStream(jar)) {
+      JarOutputStream jarOutputStream = new JarOutputStream(out);
+
+      // Add the class file to the JAR file
+      JarEntry classEntry = new JarEntry(classAsPath);
+      classEntry.setTime(System.currentTimeMillis());
+      jarOutputStream.putNextEntry(classEntry);
+      jarOutputStream.write(bytes);
+      jarOutputStream.closeEntry();
+
+      String metaInfPath = "META-INF/services/org.apache.geode.metrics.MetricsPublishingService";
+
+      JarEntry metaInfEntry = new JarEntry(metaInfPath);
+      metaInfEntry.setTime(System.currentTimeMillis());
+      jarOutputStream.putNextEntry(metaInfEntry);
+      jarOutputStream.write(className.getBytes());
+      jarOutputStream.closeEntry();
+
+      jarOutputStream.close();
+    }
+
+    return jar.getAbsolutePath();
+  }
+
+  private String newJarForFunctionClass(Class clazz, String jarName) throws IOException {
+    File jar = temporaryFolder.newFile(jarName);
+    new ClassBuilder().writeJarFromClass(clazz, jar);
+    return jar.getAbsolutePath();
+  }
+
+  public static class GetMemberRegionEntriesGaugeFunction implements Function<Void> {
+    static final String ID = "GetEventsReceivedCountFunction";
+
+    @Override
+    public void execute(FunctionContext<Void> context) {
+      Gauge memberRegionEntriesGauge = SimpleMetricsPublishingService.getRegistry()
+          .find("member.region.entries")
+          .gauge();
+
+      Object result = memberRegionEntriesGauge == null
+          ? "Meter not found."
+          : memberRegionEntriesGauge.value();
+
+      context.getResultSender().lastResult(result);
+    }
+
+    @Override
+    public String getId() {
+      return ID;
+    }
   }
 }
diff --git a/geode-core/src/main/java/org/apache/geode/internal/statistics/meters/LegacyStatGauge.java b/geode-core/src/main/java/org/apache/geode/internal/statistics/meters/LegacyStatGauge.java
index 0acfcc8..9c5b084 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/statistics/meters/LegacyStatGauge.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/statistics/meters/LegacyStatGauge.java
@@ -1,3 +1,18 @@
+/*
+ * 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.statistics.meters;
 
 import io.micrometer.core.instrument.Gauge;