You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by hu...@apache.org on 2020/04/01 22:47:17 UTC

[helix] 22/49: Add getShardingKeyInPath to MetadataStoreRoutingData (#817)

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

hulee pushed a commit to branch zooscalability
in repository https://gitbox.apache.org/repos/asf/helix.git

commit 33519e0bc2cf77e4952beb2b2e84b59b86a26188
Author: Neal Sun <ne...@gmail.com>
AuthorDate: Wed Feb 26 14:07:39 2020 -0800

    Add getShardingKeyInPath to MetadataStoreRoutingData (#817)
    
    Add getShardingKeyInPath to MetadataStoreRoutingData
---
 .../datamodel/MetadataStoreRoutingData.java        | 11 ++++++++
 .../helix/msdcommon/datamodel/TrieRoutingData.java | 14 ++++++++++
 .../msdcommon/datamodel/TestTrieRoutingData.java   | 31 ++++++++++++++++++++++
 3 files changed, 56 insertions(+)

diff --git a/metadata-store-directory-common/src/main/java/org/apache/helix/msdcommon/datamodel/MetadataStoreRoutingData.java b/metadata-store-directory-common/src/main/java/org/apache/helix/msdcommon/datamodel/MetadataStoreRoutingData.java
index 2bab8c7..25e5c7e 100644
--- a/metadata-store-directory-common/src/main/java/org/apache/helix/msdcommon/datamodel/MetadataStoreRoutingData.java
+++ b/metadata-store-directory-common/src/main/java/org/apache/helix/msdcommon/datamodel/MetadataStoreRoutingData.java
@@ -22,6 +22,7 @@ package org.apache.helix.msdcommon.datamodel;
 import java.util.Map;
 import java.util.NoSuchElementException;
 
+
 public interface MetadataStoreRoutingData {
   /**
    * Given a path, return all the "metadata store sharding key-metadata store realm address" pairs
@@ -47,6 +48,16 @@ public interface MetadataStoreRoutingData {
   String getMetadataStoreRealm(String path) throws IllegalArgumentException, NoSuchElementException;
 
   /**
+   * Given a path, return the sharding key contained in the path. If the path doesn't contain a
+   * sharding key, throw NoSuchElementException.
+   * @param path - the path that may contain a sharding key
+   * @return the sharding key contained in the path
+   * @throws IllegalArgumentException - when the path is invalid
+   * @throws NoSuchElementException - when the path doesn't contain a sharding key
+   */
+  String getShardingKeyInPath(String path) throws IllegalArgumentException, NoSuchElementException;
+
+  /**
    * Check if the provided sharding key can be inserted to the routing data. The insertion is
    * invalid if: 1. the sharding key is a parent key to an existing sharding key; 2. the sharding
    * key has a parent key that is an existing sharding key; 3. the sharding key already exists. In
diff --git a/metadata-store-directory-common/src/main/java/org/apache/helix/msdcommon/datamodel/TrieRoutingData.java b/metadata-store-directory-common/src/main/java/org/apache/helix/msdcommon/datamodel/TrieRoutingData.java
index 7a05089..0f53c23 100644
--- a/metadata-store-directory-common/src/main/java/org/apache/helix/msdcommon/datamodel/TrieRoutingData.java
+++ b/metadata-store-directory-common/src/main/java/org/apache/helix/msdcommon/datamodel/TrieRoutingData.java
@@ -98,6 +98,20 @@ public class TrieRoutingData implements MetadataStoreRoutingData {
     return node.getRealmAddress();
   }
 
+  public String getShardingKeyInPath(String path)
+      throws IllegalArgumentException, NoSuchElementException {
+    if (!ZkValidationUtil.isPathValid(path)) {
+      throw new IllegalArgumentException("Provided path is not a valid Zookeeper path: " + path);
+    }
+
+    TrieNode node = getLongestPrefixNodeAlongPath(path);
+    if (!node.isShardingKey()) {
+      throw new NoSuchElementException(
+          "No sharding key found within the provided path. Path: " + path);
+    }
+    return node.getPath();
+  }
+
   public boolean isShardingKeyInsertionValid(String shardingKey) {
     if (!ZkValidationUtil.isPathValid(shardingKey)) {
       throw new IllegalArgumentException(
diff --git a/metadata-store-directory-common/src/test/java/org/apache/helix/msdcommon/datamodel/TestTrieRoutingData.java b/metadata-store-directory-common/src/test/java/org/apache/helix/msdcommon/datamodel/TestTrieRoutingData.java
index bad10a4..2dc5dfe 100644
--- a/metadata-store-directory-common/src/test/java/org/apache/helix/msdcommon/datamodel/TestTrieRoutingData.java
+++ b/metadata-store-directory-common/src/test/java/org/apache/helix/msdcommon/datamodel/TestTrieRoutingData.java
@@ -255,6 +255,37 @@ public class TestTrieRoutingData {
   }
 
   @Test(dependsOnMethods = "testConstructionNormal")
+  public void testGetShardingKeyInPath() {
+    try {
+      Assert.assertEquals(_trie.getShardingKeyInPath("/b/c/d/x/y/z"), "/b/c/d");
+    } catch (NoSuchElementException e) {
+      Assert.fail("Not expecting NoSuchElementException");
+    }
+  }
+
+  @Test(dependsOnMethods = "testConstructionNormal")
+  public void testGetShardingKeyInPathWrongPath() {
+    try {
+      _trie.getShardingKeyInPath("/x/y/z");
+      Assert.fail("Expecting NoSuchElementException");
+    } catch (NoSuchElementException e) {
+      Assert.assertTrue(
+          e.getMessage().contains("No sharding key found within the provided path. Path: /x/y/z"));
+    }
+  }
+
+  @Test(dependsOnMethods = "testConstructionNormal")
+  public void testGetShardingKeyInPathNoLeaf() {
+    try {
+      _trie.getShardingKeyInPath("/b/c");
+      Assert.fail("Expecting NoSuchElementException");
+    } catch (NoSuchElementException e) {
+      Assert.assertTrue(
+          e.getMessage().contains("No sharding key found within the provided path. Path: /b/c"));
+    }
+  }
+
+  @Test(dependsOnMethods = "testConstructionNormal")
   public void testIsShardingKeyInsertionValidNoSlash() {
     try {
       _trie.isShardingKeyInsertionValid("x/y/z");