You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2018/09/30 18:39:10 UTC

[GitHub] sijie closed pull request #1719: [table service][common] add BytesHashRouter

sijie closed pull request #1719: [table service][common] add BytesHashRouter
URL: https://github.com/apache/bookkeeper/pull/1719
 
 
   

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/stream/clients/java/base/src/main/java/org/apache/bookkeeper/clients/impl/routing/RangeRouter.java b/stream/clients/java/base/src/main/java/org/apache/bookkeeper/clients/impl/routing/RangeRouter.java
index 18e1e56334..b1b5bd5c58 100644
--- a/stream/clients/java/base/src/main/java/org/apache/bookkeeper/clients/impl/routing/RangeRouter.java
+++ b/stream/clients/java/base/src/main/java/org/apache/bookkeeper/clients/impl/routing/RangeRouter.java
@@ -58,7 +58,11 @@ public RangeRouter(HashRouter<K> keyRouter) {
      * @return the range to write.
      * @throws IllegalStateException if ranges is empty.
      */
-    public Long getRange(@Nullable K key) {
+    public long getRange(@Nullable K key) {
+        return getRangeProperties(key).getRangeId();
+    }
+
+    public RangeProperties getRangeProperties(@Nullable K key) {
         long routingKey;
         if (null != key) {
             routingKey = keyRouter.getRoutingKey(key);
@@ -79,7 +83,7 @@ public Long getRange(@Nullable K key) {
         checkState(null != rs, "No range is available");
 
         Map.Entry<Long, RangeProperties> ceilingEntry = rs.getRanges().floorEntry(routingKey);
-        return ceilingEntry.getValue().getRangeId();
+        return ceilingEntry.getValue();
     }
 
     public HashStreamRanges getRanges() {
diff --git a/stream/common/src/main/java/org/apache/bookkeeper/common/hash/Murmur3.java b/stream/common/src/main/java/org/apache/bookkeeper/common/hash/Murmur3.java
index 47852ba764..b61a7c535f 100644
--- a/stream/common/src/main/java/org/apache/bookkeeper/common/hash/Murmur3.java
+++ b/stream/common/src/main/java/org/apache/bookkeeper/common/hash/Murmur3.java
@@ -208,7 +208,7 @@ public static int hash32(ByteBuf data, int offset, int length, int seed) {
      * @param seed   - seed. (default is 0)
      * @return - hashcode (2 longs)
      */
-    public static long[] hash128(byte[] data, int offset, int length, int seed) {
+    public static long[] hash128(byte[] data, int offset, int length, long seed) {
         long h1 = seed;
         long h2 = seed;
         final int nblocks = length >> 4;
diff --git a/stream/common/src/main/java/org/apache/bookkeeper/common/router/BytesHashRouter.java b/stream/common/src/main/java/org/apache/bookkeeper/common/router/BytesHashRouter.java
new file mode 100644
index 0000000000..e8f61d9ad4
--- /dev/null
+++ b/stream/common/src/main/java/org/apache/bookkeeper/common/router/BytesHashRouter.java
@@ -0,0 +1,41 @@
+/*
+ * 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.bookkeeper.common.router;
+
+import org.apache.bookkeeper.common.hash.Murmur3;
+
+/**
+ * Hash router that computes the hash value of a byte array.
+ */
+public class BytesHashRouter implements HashRouter<byte[]> {
+
+    public static BytesHashRouter of() {
+        return ROUTER;
+    }
+
+    private static final BytesHashRouter ROUTER = new BytesHashRouter();
+
+    private BytesHashRouter() {}
+
+    @Override
+    public Long getRoutingKey(byte[] key) {
+        return Murmur3.hash128(key, 0, key.length, AbstractHashRouter.HASH_SEED)[0];
+    }
+}
diff --git a/stream/common/src/test/java/org/apache/bookkeeper/common/router/HashRouterTest.java b/stream/common/src/test/java/org/apache/bookkeeper/common/router/HashRouterTest.java
index 5d4ed79ade..8fad535dad 100644
--- a/stream/common/src/test/java/org/apache/bookkeeper/common/router/HashRouterTest.java
+++ b/stream/common/src/test/java/org/apache/bookkeeper/common/router/HashRouterTest.java
@@ -15,6 +15,7 @@
 package org.apache.bookkeeper.common.router;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 
 import com.google.common.hash.Hashing;
@@ -32,33 +33,41 @@
 
     @Test
     public void testByteBufHashRouter() {
-        ByteBuf key = Unpooled.wrappedBuffer("foo".getBytes(UTF_8));
+        byte[] keyBytes = "foo".getBytes(UTF_8);
+        ByteBuf key = Unpooled.wrappedBuffer(keyBytes);
 
         // murmur3 - 32 bits
         int hash32 = Murmur3.hash32(
             key, key.readerIndex(), key.readableBytes(), (int) AbstractHashRouter.HASH_SEED);
+        int bytesHash32 = Murmur3.hash32(keyBytes, 0, keyBytes.length, (int) AbstractHashRouter.HASH_SEED);
         int guavaHash32 = Hashing.murmur3_32((int) AbstractHashRouter.HASH_SEED)
             .newHasher()
             .putString("foo", UTF_8)
             .hash()
             .asInt();
+        assertEquals(hash32, bytesHash32);
         assertEquals(hash32, guavaHash32);
 
         // murmur3 - 128 bits
         long[] hash128 = Murmur3.hash128(
             key, key.readerIndex(), key.readableBytes(), AbstractHashRouter.HASH_SEED);
-        log.info("hash128: {}", hash128);
+        long[] bytesHash128 = Murmur3.hash128(keyBytes, 0, keyBytes.length, AbstractHashRouter.HASH_SEED);
+        log.info("hash128: {}, bytes hash128: {}", hash128, bytesHash128);
         long guavaHash128 = Hashing.murmur3_128((int) AbstractHashRouter.HASH_SEED)
             .newHasher()
             .putString("foo", UTF_8)
             .hash()
             .asLong();
+        assertArrayEquals(hash128, bytesHash128);
         assertEquals(hash128[0], guavaHash128);
 
         ByteBufHashRouter router = ByteBufHashRouter.of();
         long routingHash = router.getRoutingKey(key).longValue();
         log.info("Routing hash = {}", routingHash);
         assertEquals(hash128[0], routingHash);
+        BytesHashRouter bytesRouter = BytesHashRouter.of();
+        long bytesRoutingHash = bytesRouter.getRoutingKey(keyBytes).longValue();
+        assertEquals(hash128[0], bytesRoutingHash);
     }
 
 }


 

----------------------------------------------------------------
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