You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by up...@apache.org on 2021/04/29 18:12:37 UTC

[geode] branch feature/redis-memory-overhead created (now 1a0731b)

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

upthewaterspout pushed a change to branch feature/redis-memory-overhead
in repository https://gitbox.apache.org/repos/asf/geode.git.


      at 1a0731b  Adding memory overhead tests for redis

This branch includes the following new commits:

     new 1a0731b  Adding memory overhead tests for redis

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[geode] 01/01: Adding memory overhead tests for redis

Posted by up...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

upthewaterspout pushed a commit to branch feature/redis-memory-overhead
in repository https://gitbox.apache.org/repos/asf/geode.git

commit 1a0731ba749f547cd4e0de894696ebae9721b240
Author: Dan Smith <da...@vmware.com>
AuthorDate: Thu Mar 18 15:22:48 2021 -0700

    Adding memory overhead tests for redis
    
    Adding tests that measure the memory overhead of both native redis and Radish.
    These tests measure the memory used before and after adding entries.
    
    The tests don't actually fail, you need to look at the stdout to see the memory
    overhead.
---
 .../MemoryOverheadNativeRedisAcceptanceTest.java   |  46 +++++++
 .../java/org/apache/geode/NativeRedisTestRule.java |   1 +
 .../AbstractMemoryOverheadIntegrationTest.java     | 148 +++++++++++++++++++++
 .../hash/MemoryOverheadIntegrationTest.java        |  80 +++++++++++
 .../apache/geode/redis/internal/data/RedisKey.java |   2 +-
 5 files changed, 276 insertions(+), 1 deletion(-)

diff --git a/geode-redis/src/acceptanceTest/java/org/apache/geode/redis/internal/executor/hash/MemoryOverheadNativeRedisAcceptanceTest.java b/geode-redis/src/acceptanceTest/java/org/apache/geode/redis/internal/executor/hash/MemoryOverheadNativeRedisAcceptanceTest.java
new file mode 100755
index 0000000..ead78f6
--- /dev/null
+++ b/geode-redis/src/acceptanceTest/java/org/apache/geode/redis/internal/executor/hash/MemoryOverheadNativeRedisAcceptanceTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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.redis.internal.executor.hash;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.junit.ClassRule;
+
+import org.apache.geode.NativeRedisTestRule;
+
+public class MemoryOverheadNativeRedisAcceptanceTest extends AbstractMemoryOverheadIntegrationTest {
+
+  private static Pattern FIND_USED_MEMORY = Pattern.compile("used_memory:(\\d+)");
+
+  @ClassRule
+  public static NativeRedisTestRule redis = new NativeRedisTestRule();
+
+  @Override
+  public int getPort() {
+    return redis.getPort();
+  }
+
+  @Override
+  long getUsedMemory() {
+    String memoryInfo = jedis.info("memory");
+    Matcher matcher = FIND_USED_MEMORY.matcher(memoryInfo);
+    assertTrue(matcher.find());
+    String usedMemory = matcher.group(1);
+    return Long.parseLong(usedMemory);
+  }
+}
diff --git a/geode-redis/src/commonTest/java/org/apache/geode/NativeRedisTestRule.java b/geode-redis/src/commonTest/java/org/apache/geode/NativeRedisTestRule.java
index fabff75..c52a290 100644
--- a/geode-redis/src/commonTest/java/org/apache/geode/NativeRedisTestRule.java
+++ b/geode-redis/src/commonTest/java/org/apache/geode/NativeRedisTestRule.java
@@ -23,6 +23,7 @@ import org.junit.rules.RuleChain;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
 import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.images.ImagePullPolicy;
 
 import org.apache.geode.logging.internal.log4j.api.LogService;
 import org.apache.geode.test.junit.rules.IgnoreOnWindowsRule;
diff --git a/geode-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/hash/AbstractMemoryOverheadIntegrationTest.java b/geode-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/hash/AbstractMemoryOverheadIntegrationTest.java
new file mode 100755
index 0000000..a8e3e0e
--- /dev/null
+++ b/geode-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/hash/AbstractMemoryOverheadIntegrationTest.java
@@ -0,0 +1,148 @@
+/*
+ * 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.redis.internal.executor.hash;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.setAllowComparingPrivateFields;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.IntToLongFunction;
+import java.util.stream.IntStream;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import redis.clients.jedis.Jedis;
+
+import org.apache.geode.test.dunit.rules.RedisPortSupplier;
+
+public abstract class AbstractMemoryOverheadIntegrationTest implements RedisPortSupplier {
+
+  private static final int WARM_UP_ENTRY_COUNT = 1000;
+  private static final int TOTAL_ENTRY_COUNT = 10000;
+  private static final int SAMPLE_INTERVAL = 100;
+  public static final String
+      LARGE_STRING =
+      "value_that_will_force_redis_to_not_use_a_ziplist______________________________________________________________";
+  protected Jedis jedis;
+
+  @Before
+  public void setUp() {
+    jedis = new Jedis("localhost", getPort(), 10000000);
+  }
+
+  @After
+  public void tearDown() {
+    jedis.flushAll();
+    jedis.close();
+  }
+
+  abstract long getUsedMemory();
+
+  @Test
+  @Ignore
+  public void measureSizeOfSmallStrings() {
+
+    doMeasurements(key -> {
+      String keyString = String.format("key-%10d",key);
+      String valueString = String.format("value-%10d",key);
+      String response = jedis.set(keyString, valueString);
+      assertThat(response).isEqualTo("OK");
+
+      //Note - jedis convert strings to bytes with the UTF-8 charset
+      //Since the strings above are all ASCII, the length == the number of bytes
+      return keyString.length() + valueString.length();
+    });
+  }
+
+  @Test
+  public void measureSizeOfSmallHashes() {
+      doMeasurements(key -> {
+        String keyString = String.format("key-%10d", key);
+        String mapKey = "key";
+        Long response = jedis.hset(keyString, mapKey, LARGE_STRING);
+//        assertThat(response).isEqualTo(1);
+        return keyString.length() + mapKey.length() + LARGE_STRING.length();
+      });
+  }
+
+  @Test
+  public void measureSizeOfHashEntries() {
+      doMeasurements(key -> {
+
+        String keyString = String.format("key-%10d",key);
+        String valueString = String.format("%s value-%10d",LARGE_STRING, key);
+        Long response = jedis.hset("TestSet", keyString, valueString);
+//        assertThat(response).isEqualTo(1);
+
+        return keyString.length() + valueString.length();
+      });
+  }
+
+  @Test
+  public void measureSizeOfSmallSets() {
+    doMeasurements(key -> {
+      String keyString = String.format("key-%10d", key);
+      Long
+          response =
+          jedis.sadd(keyString, LARGE_STRING);
+//        assertThat(response).isEqualTo(1);
+      return keyString.length() + LARGE_STRING.length();
+    });
+  }
+
+  @Test
+  public void measureSizeOfSetEntries() {
+    doMeasurements(key -> {
+
+      String valueString = String.format("%s value-%10d",LARGE_STRING, key);
+      Long response = jedis.sadd("TestSet", valueString);
+//        assertThat(response).isEqualTo(1);
+
+      return valueString.length();
+    });
+  }
+
+  //TODO sets
+
+  //TODO sets that redis optimizes (small sets, integers?)
+
+  //TODO memory fragmentation test - try to break redis's allocator
+
+  //TODO
+
+  private void doMeasurements(IntToLongFunction addEntry) {
+    //Warmup
+    for(int i = 0; i < WARM_UP_ENTRY_COUNT; i++) {
+      addEntry.applyAsLong(-i);
+    }
+
+    //Perform measurements
+    long baseline = getUsedMemory();
+    long totalDataSize = 0;
+    System.out.printf("%20s, %20s, %20s", "Used Memory", "Total Mem Per Entry", "Overhead Per Entry\n");
+    for(int i = 0; i < TOTAL_ENTRY_COUNT; i++) {
+      totalDataSize += addEntry.applyAsLong(i);
+      if(i % SAMPLE_INTERVAL == (SAMPLE_INTERVAL-1)) {
+        long currentMemory = getUsedMemory() - baseline;
+        long perEntryMemory = currentMemory / i;
+        long perEntryOverhead = (currentMemory - totalDataSize) / i;
+        System.out.printf("%20d, %20d, %20d\n", currentMemory, perEntryMemory, perEntryOverhead);
+      }
+    }
+  }
+}
diff --git a/geode-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/hash/MemoryOverheadIntegrationTest.java b/geode-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/hash/MemoryOverheadIntegrationTest.java
new file mode 100755
index 0000000..f35a9a7
--- /dev/null
+++ b/geode-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/hash/MemoryOverheadIntegrationTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.redis.internal.executor.hash;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.springframework.shell.converters.AvailableCommandsConverter;
+
+import org.apache.geode.cache.CacheFactory;
+import org.apache.geode.cache.partition.PartitionRegionHelper;
+import org.apache.geode.internal.cache.BucketRegion;
+import org.apache.geode.internal.cache.PartitionedRegion;
+import org.apache.geode.internal.cache.RegionEntry;
+import org.apache.geode.internal.size.ObjectGraphSizer;
+import org.apache.geode.redis.GeodeRedisServerRule;
+import org.apache.geode.redis.internal.RegionProvider;
+
+public class MemoryOverheadIntegrationTest extends AbstractMemoryOverheadIntegrationTest {
+  protected static ObjectGraphSizer.ObjectFilter filter =
+      (parent, object) -> !(object instanceof AvailableCommandsConverter);
+
+  @ClassRule
+  public static GeodeRedisServerRule server = new GeodeRedisServerRule();
+
+  @Before
+  public void assignBuckets() {
+    //TODO - this hack is just because we are using a fake cluster mode that requires buckets
+    //to be created up front
+    PartitionRegionHelper.assignBucketsToPartitions(CacheFactory.getAnyInstance().getRegion(
+        RegionProvider.REDIS_DATA_REGION));
+  }
+
+  @Test
+  public void showStringEntryHistogram() throws IllegalAccessException {
+
+    //Set the empty key
+    String response = jedis.set("", "");
+    assertThat(response).isEqualTo("OK");
+
+    //Extract the region entry from geode and show it's size
+
+    final PartitionedRegion
+        dataRegion =
+        (PartitionedRegion) CacheFactory.getAnyInstance().getRegion(RegionProvider.REDIS_DATA_REGION);
+    final Object redisKey = dataRegion.keys().iterator().next();
+    BucketRegion bucket = dataRegion.getBucketRegion(redisKey);
+    RegionEntry entry = bucket.entries.getEntry(redisKey);
+    System.out.println(ObjectGraphSizer.histogram(entry, false));
+  }
+
+  @Override
+  public int getPort() {
+    return server.getPort();
+  }
+
+  @Override
+  long getUsedMemory() {
+    final Runtime runtime = Runtime.getRuntime();
+    try {
+      return ObjectGraphSizer.size(server.getServer(), filter, true);
+    } catch (IllegalAccessException e) {
+      throw new RuntimeException("Couldn't compute size of cache", e);
+    }
+  }
+}
diff --git a/geode-redis/src/main/java/org/apache/geode/redis/internal/data/RedisKey.java b/geode-redis/src/main/java/org/apache/geode/redis/internal/data/RedisKey.java
index 01eef22..5b0322e 100644
--- a/geode-redis/src/main/java/org/apache/geode/redis/internal/data/RedisKey.java
+++ b/geode-redis/src/main/java/org/apache/geode/redis/internal/data/RedisKey.java
@@ -34,7 +34,7 @@ import org.apache.geode.redis.internal.executor.cluster.RedisPartitionResolver;
 public class RedisKey extends ByteArrayWrapper
     implements DataSerializableFixedID, Serializable {
 
-  private Integer routingId;
+  private int routingId;
 
   public RedisKey() {}