You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@geode.apache.org by GitBox <gi...@apache.org> on 2018/12/14 23:51:58 UTC

[GitHub] balesh2 closed pull request #25: GEODE-6148: improve performance of Prepopulate

balesh2 closed pull request #25: GEODE-6148: improve performance of Prepopulate
URL: https://github.com/apache/geode-benchmarks/pull/25
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/geode-benchmarks/build.gradle b/geode-benchmarks/build.gradle
index 56b57e7..1944e6b 100644
--- a/geode-benchmarks/build.gradle
+++ b/geode-benchmarks/build.gradle
@@ -42,6 +42,9 @@ dependencies {
     compile(group: 'org.apache.geode', name: 'geode-core', version: geodeVersion)
     compile(group: 'org.slf4j', name: 'slf4j-simple', version: project.'slf4j-simple.version')
     compile(project(':harness'))
+
+    testCompile(group: 'org.mockito', name: 'mockito-all', version: project.'mockito-all.version')
+    testCompile(group: 'org.assertj', name: 'assertj-core', version: project.'assertj-core.version')
 }
 
 test{
diff --git a/geode-benchmarks/src/main/java/org/apache/geode/benchmark/parameters/JVMParameters.java b/geode-benchmarks/src/main/java/org/apache/geode/benchmark/parameters/JVMParameters.java
index a61c458..97d90a2 100644
--- a/geode-benchmarks/src/main/java/org/apache/geode/benchmark/parameters/JVMParameters.java
+++ b/geode-benchmarks/src/main/java/org/apache/geode/benchmark/parameters/JVMParameters.java
@@ -38,7 +38,9 @@
       "-Djava.awt.headless=true",
       "-Dsun.rmi.dgc.server.gcInterval=9223372036854775806",
       "-Dgemfire.OSProcess.ENABLE_OUTPUT_REDIRECTION=true",
-      "-Dgemfire.launcher.registerSignalHandlers=true"
+      "-Dgemfire.launcher.registerSignalHandlers=true",
+      "-Xmx3g",
+      "-Xms3g"
 
   };
 
diff --git a/geode-benchmarks/src/main/java/org/apache/geode/benchmark/tasks/CreatePartitionedRegion.java b/geode-benchmarks/src/main/java/org/apache/geode/benchmark/tasks/CreatePartitionedRegion.java
index 1604dc5..5f06860 100644
--- a/geode-benchmarks/src/main/java/org/apache/geode/benchmark/tasks/CreatePartitionedRegion.java
+++ b/geode-benchmarks/src/main/java/org/apache/geode/benchmark/tasks/CreatePartitionedRegion.java
@@ -28,6 +28,6 @@
   @Override
   public void run(TestContext context) throws Exception {
     Cache cache = (Cache) context.getAttribute("SERVER_CACHE");
-    cache.createRegionFactory(RegionShortcut.PARTITION).create("region");
+    cache.createRegionFactory(RegionShortcut.PARTITION_REDUNDANT).create("region");
   }
 }
diff --git a/geode-benchmarks/src/main/java/org/apache/geode/benchmark/tasks/PrePopulateRegion.java b/geode-benchmarks/src/main/java/org/apache/geode/benchmark/tasks/PrePopulateRegion.java
index 4ec028c..bae2237 100644
--- a/geode-benchmarks/src/main/java/org/apache/geode/benchmark/tasks/PrePopulateRegion.java
+++ b/geode-benchmarks/src/main/java/org/apache/geode/benchmark/tasks/PrePopulateRegion.java
@@ -16,10 +16,19 @@
  */
 package org.apache.geode.benchmark.tasks;
 
+import static org.apache.geode.benchmark.topology.ClientServerTopology.Roles.LOCATOR;
+import static org.apache.geode.benchmark.topology.ClientServerTopology.Roles.SERVER;
+
 import java.time.Duration;
 import java.time.Instant;
-import java.util.concurrent.ThreadLocalRandom;
-import java.util.stream.LongStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.benchmark.geode.data.Portfolio;
 import org.slf4j.Logger;
@@ -33,6 +42,7 @@
 
 public class PrePopulateRegion implements Task {
   long keyRangeToPrepopulate = 10000;
+  private int batchSize = 1000;
   private static final Logger logger = LoggerFactory.getLogger(RemoteJVMFactory.class);
 
   public PrePopulateRegion() {}
@@ -47,21 +57,94 @@ public PrePopulateRegion(long keyRangeToPrepopulate) {
    *
    */
   @Override
-  public void run(TestContext context) {
+  public void run(TestContext context) throws InterruptedException {
     Cache serverCache = (Cache) context.getAttribute("SERVER_CACHE");
-    Region region = serverCache.getRegion("region");
+    Region<Long, Portfolio> region = serverCache.getRegion("region");
+    int numLocators = context.getHostsForRole(LOCATOR).size();
+    int numServers = context.getHostsForRole(SERVER).size();
+    int jvmID = context.getJvmID();
+
+    run(region, numLocators, numServers, jvmID);
+
+  }
+
+  void run(Map<Long, Portfolio> region, int numLocators, int numServers, int jvmID)
+      throws InterruptedException {
+    int serverIndex = jvmID - numLocators;
+    long numPutsPerServer = this.keyRangeToPrepopulate / numServers;
+    int numThreads =
+        numPutsPerServer < getBatchSize() ? 1 : Runtime.getRuntime().availableProcessors();
+
+    // calculate non-overlapping key ranges for each server
+    long lowBound = numPutsPerServer * serverIndex;
+    long highBound = numPutsPerServer * (serverIndex + 1);
+    if (serverIndex == (numServers - 1)) {
+      highBound += this.keyRangeToPrepopulate % (serverIndex + 1);
+    }
+
     logger.info("*******************************************");
     logger.info("      Prepopulating the region ");
     logger.info("*******************************************");
     Instant start = Instant.now();
-    LongStream.range(0, keyRangeToPrepopulate).forEach(i -> {
-      long value = ThreadLocalRandom.current().nextLong(0, keyRangeToPrepopulate);
-      region.put(i, new Portfolio(value));
-    });
+
+    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
+    List<CompletableFuture<Void>> futures = new ArrayList<>();
+
+    long range = highBound - lowBound;
+    long putsPerThread = range / numThreads;
+
+    for (int i = 0; i < numThreads; i++) {
+      int threadNum = i;
+
+      Runnable putThread = () -> {
+        long low = lowBound + (putsPerThread * threadNum);
+        long high = low + putsPerThread;
+
+        if (threadNum == (numThreads - 1)) {
+          high += range % numThreads;
+        }
+
+        doPuts(region, low, high);
+      };
+
+      futures.add(CompletableFuture.runAsync(putThread, threadPool));
+    }
+
+    futures.forEach(CompletableFuture::join);
+
     Instant finish = Instant.now();
     logger.info("*******************************************");
     logger.info("    Prepopulating the region completed");
     logger.info("    Duration = " + Duration.between(start, finish).toMillis() + "ms.");
     logger.info("*******************************************");
+
+    threadPool.shutdownNow();
+    threadPool.awaitTermination(5, TimeUnit.MINUTES);
+  }
+
+  private void doPuts(Map<Long, Portfolio> region, long lowBound, long highBound) {
+    Map<Long, Portfolio> valueMap = new HashMap<>();
+    for (long putIndex = lowBound; putIndex < highBound; putIndex++) {
+      // build a map of to put to the server
+
+      valueMap.put(putIndex, new Portfolio(putIndex));
+
+      if (putIndex % getBatchSize() == 0) {
+        region.putAll(valueMap);
+        valueMap.clear();
+      }
+    }
+
+    if (!valueMap.isEmpty()) {
+      region.putAll(valueMap);
+    }
+  }
+
+  public int getBatchSize() {
+    return batchSize;
+  }
+
+  public void setBatchSize(int batchSize) {
+    this.batchSize = batchSize;
   }
 }
diff --git a/geode-benchmarks/src/test/java/org/apache/geode/benchmark/tasks/PrePopulateRegionTest.java b/geode-benchmarks/src/test/java/org/apache/geode/benchmark/tasks/PrePopulateRegionTest.java
new file mode 100644
index 0000000..9ad091a
--- /dev/null
+++ b/geode-benchmarks/src/test/java/org/apache/geode/benchmark/tasks/PrePopulateRegionTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.benchmark.tasks;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+import java.util.stream.LongStream;
+
+import org.apache.benchmark.geode.data.Portfolio;
+import org.junit.jupiter.api.Test;
+
+class PrePopulateRegionTest {
+
+  @Test
+  public void putsEntriesForServer() throws InterruptedException {
+    PrePopulateRegion prePopulateRegion = new PrePopulateRegion(100);
+
+    Map<Long, Portfolio> region = new ConcurrentHashMap<>();
+
+    prePopulateRegion.run(region, 1, 2, 2);
+
+    // verify that we put the last 50 keys
+    verifyKeys(region, 50, 100);
+  }
+
+  @Test
+  public void putsEntriesForServerWithSmallBatches() throws InterruptedException {
+    PrePopulateRegion prePopulateRegion = new PrePopulateRegion(100);
+    prePopulateRegion.setBatchSize(2);
+
+    Map<Long, Portfolio> region = new ConcurrentHashMap<>();
+
+    prePopulateRegion.run(region, 1, 2, 2);
+
+    // verify that we put the last 50 keys
+    verifyKeys(region, 50, 100);
+  }
+
+  private void verifyKeys(Map<Long, Portfolio> region, int startInclusive, int endExclusive) {
+    List<Long> expectedKeys = LongStream.range(startInclusive, endExclusive)
+        .mapToObj(Long::new)
+        .collect(Collectors.toList());
+
+    assertThat(region.keySet()).containsExactlyInAnyOrderElementsOf(expectedKeys);
+  }
+
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services