You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by lx...@apache.org on 2018/03/23 19:29:24 UTC

[3/6] helix git commit: Allow to get all resources from RoutingTableProvider class.

Allow to get all resources from RoutingTableProvider class.


Project: http://git-wip-us.apache.org/repos/asf/helix/repo
Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/6f6ab65a
Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/6f6ab65a
Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/6f6ab65a

Branch: refs/heads/master
Commit: 6f6ab65a6e6c4526f7c6957f2f0cdfaed78e44d5
Parents: 623330e
Author: Lei Xia <lx...@linkedin.com>
Authored: Thu Feb 8 15:52:01 2018 -0800
Committer: Lei Xia <lx...@linkedin.com>
Committed: Fri Mar 23 12:12:44 2018 -0700

----------------------------------------------------------------------
 .../apache/helix/spectator/RoutingTable.java    | 15 +++++---
 .../helix/spectator/RoutingTableProvider.java   | 11 +++++-
 .../java/org/apache/helix/TestRoutingTable.java | 36 +++++++++++++++++---
 .../Spectator/TestRoutingTableProvider.java     |  4 +++
 4 files changed, 56 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/helix/blob/6f6ab65a/helix-core/src/main/java/org/apache/helix/spectator/RoutingTable.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/spectator/RoutingTable.java b/helix-core/src/main/java/org/apache/helix/spectator/RoutingTable.java
index 564a218..f704ee4 100644
--- a/helix-core/src/main/java/org/apache/helix/spectator/RoutingTable.java
+++ b/helix-core/src/main/java/org/apache/helix/spectator/RoutingTable.java
@@ -211,7 +211,7 @@ class RoutingTable {
     if (instanceList == null) {
       instanceList = Collections.emptyList();
     }
-    return instanceList;
+    return Collections.unmodifiableList(instanceList);
   }
 
   /**
@@ -219,7 +219,7 @@ class RoutingTable {
    * @return
    */
   protected Collection<LiveInstance> getLiveInstances() {
-    return _liveInstances;
+    return Collections.unmodifiableCollection(_liveInstances);
   }
 
   /**
@@ -227,7 +227,14 @@ class RoutingTable {
    * @return
    */
   protected Collection<InstanceConfig> getInstanceConfigs() {
-    return _instanceConfigs;
+    return Collections.unmodifiableCollection(_instanceConfigs);
+  }
+
+  /**
+   * Return names of all resources (shown in ExternalView) in this cluster.
+   */
+  protected Collection<String> getResources() {
+    return Collections.unmodifiableCollection(_resourceInfoMap.keySet());
   }
 
   /**
@@ -261,7 +268,7 @@ class RoutingTable {
       return Collections.emptyList();
     }
 
-    return instanceList;
+    return Collections.unmodifiableList(instanceList);
   }
 
   private void refresh(Collection<ExternalView> externalViewList,

http://git-wip-us.apache.org/repos/asf/helix/blob/6f6ab65a/helix-core/src/main/java/org/apache/helix/spectator/RoutingTableProvider.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/spectator/RoutingTableProvider.java b/helix-core/src/main/java/org/apache/helix/spectator/RoutingTableProvider.java
index cd4e3d2..be85b65 100644
--- a/helix-core/src/main/java/org/apache/helix/spectator/RoutingTableProvider.java
+++ b/helix-core/src/main/java/org/apache/helix/spectator/RoutingTableProvider.java
@@ -20,6 +20,7 @@ package org.apache.helix.spectator;
  */
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
@@ -140,7 +141,8 @@ public class RoutingTableProvider implements ExternalViewChangeListener, Instanc
    */
   public List<InstanceConfig> getInstancesForResourceGroup(String resourceGroupName,
       String partitionName, String state) {
-    return _routingTableRef.get().getInstancesForResourceGroup(resourceGroupName, partitionName, state);
+    return _routingTableRef.get().getInstancesForResourceGroup(resourceGroupName, partitionName,
+        state);
   }
 
   /**
@@ -229,6 +231,13 @@ public class RoutingTableProvider implements ExternalViewChangeListener, Instanc
     return _routingTableRef.get().getInstanceConfigs();
   }
 
+  /**
+   * Return names of all resources (shown in ExternalView) in this cluster.
+   */
+  public Collection<String> getResources() {
+    return _routingTableRef.get().getResources();
+  }
+
   @Override
   @PreFetch(enabled = false)
   public void onExternalViewChange(List<ExternalView> externalViewList,

http://git-wip-us.apache.org/repos/asf/helix/blob/6f6ab65a/helix-core/src/test/java/org/apache/helix/TestRoutingTable.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/TestRoutingTable.java b/helix-core/src/test/java/org/apache/helix/TestRoutingTable.java
index 11f3701..8987147 100644
--- a/helix-core/src/test/java/org/apache/helix/TestRoutingTable.java
+++ b/helix-core/src/test/java/org/apache/helix/TestRoutingTable.java
@@ -20,8 +20,10 @@ package org.apache.helix;
  */
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -36,6 +38,7 @@ import org.apache.helix.model.ExternalView;
 import org.apache.helix.model.HelixConfigScope.ConfigScopeProperty;
 import org.apache.helix.model.InstanceConfig;
 import org.apache.helix.spectator.RoutingTableProvider;
+import org.testng.Assert;
 import org.testng.AssertJUnit;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
@@ -60,7 +63,6 @@ public class TestRoutingTable {
             @SuppressWarnings("unchecked")
             @Override
             public <T extends HelixProperty> List<T> getChildValues(PropertyKey key)
-            // public List<ZNRecord> getChildValues(PropertyType type, String... keys)
             {
               PropertyType type = key.getType();
               String[] keys = key.getParams();
@@ -104,7 +106,7 @@ public class TestRoutingTable {
 
     // one master
     add(record, "TESTDB_0", "localhost_8900", "MASTER");
-    List<ExternalView> externalViewList = new ArrayList<ExternalView>();
+    List<ExternalView> externalViewList = new ArrayList<>();
     externalViewList.add(new ExternalView(record));
     routingTable.onExternalViewChange(externalViewList, changeContext);
 
@@ -119,6 +121,7 @@ public class TestRoutingTable {
     externalViewList = new ArrayList<ExternalView>();
     externalViewList.add(new ExternalView(record));
     routingTable.onExternalViewChange(externalViewList, changeContext);
+
     instances = routingTable.getInstances("TESTDB", "TESTDB_0", "MASTER");
     AssertJUnit.assertNotNull(instances);
     AssertJUnit.assertEquals(instances.size(), 2);
@@ -129,7 +132,7 @@ public class TestRoutingTable {
 
     // updates
     add(record, "TESTDB_0", "localhost_8901", "SLAVE");
-    externalViewList = new ArrayList<ExternalView>();
+    externalViewList = new ArrayList<>();
     externalViewList.add(new ExternalView(record));
     routingTable.onExternalViewChange(externalViewList, changeContext);
     instances = routingTable.getInstances("TESTDB", "TESTDB_0", "SLAVE");
@@ -137,6 +140,29 @@ public class TestRoutingTable {
     AssertJUnit.assertEquals(instances.size(), 1);
   }
 
+
+  @Test()
+  public void testGetResources() {
+    RoutingTableProvider routingTable = new RoutingTableProvider();
+    List<ExternalView> externalViewList = new ArrayList<>();
+    Set<String> databases = new HashSet<>();
+
+    for (int i = 0; i < 5; i++) {
+      String db = "TESTDB" + i;
+      ZNRecord record = new ZNRecord(db);
+      // one master
+      add(record, db+"_0", "localhost_8900", "MASTER");
+      add(record, db+"_1", "localhost_8901", "SLAVE");
+      externalViewList.add(new ExternalView(record));
+      databases.add(db);
+    }
+
+    routingTable.onExternalViewChange(externalViewList, changeContext);
+    Collection<String> resources = routingTable.getResources();
+    Assert.assertEquals(databases.size(), externalViewList.size());
+    Assert.assertEquals(databases, new HashSet<>(resources));
+  }
+
   @Test()
   public void testStateUnitGroupDeletion() throws InterruptedException {
     List<InstanceConfig> instances;
@@ -207,7 +233,7 @@ public class TestRoutingTable {
   @Test()
   public void testMultiThread() throws Exception {
     final RoutingTableProvider routingTable = new RoutingTableProvider();
-    List<ExternalView> externalViewList = new ArrayList<ExternalView>();
+    List<ExternalView> externalViewList = new ArrayList<>();
     ZNRecord record = new ZNRecord("TESTDB");
     for (int i = 0; i < 1000; i++) {
       add(record, "TESTDB_" + i, "localhost_8900", "MASTER");
@@ -258,7 +284,7 @@ public class TestRoutingTable {
   private void add(ZNRecord record, String stateUnitKey, String instanceName, String state) {
     Map<String, String> stateUnitKeyMap = record.getMapField(stateUnitKey);
     if (stateUnitKeyMap == null) {
-      stateUnitKeyMap = new HashMap<String, String>();
+      stateUnitKeyMap = new HashMap<>();
       record.setMapField(stateUnitKey, stateUnitKeyMap);
     }
     stateUnitKeyMap.put(instanceName, state);

http://git-wip-us.apache.org/repos/asf/helix/blob/6f6ab65a/helix-core/src/test/java/org/apache/helix/integration/Spectator/TestRoutingTableProvider.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/Spectator/TestRoutingTableProvider.java b/helix-core/src/test/java/org/apache/helix/integration/Spectator/TestRoutingTableProvider.java
index aa731e5..0f72091 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/Spectator/TestRoutingTableProvider.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/Spectator/TestRoutingTableProvider.java
@@ -2,6 +2,7 @@ package org.apache.helix.integration.Spectator;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
@@ -112,6 +113,9 @@ public class TestRoutingTableProvider extends ZkIntegrationTestBase {
         Sets.newSet(_instances.get(1), _instances.get(2)));
     validateRoutingTable(_routingTableProvider2, Sets.newSet(_instances.get(0)),
         Sets.newSet(_instances.get(1), _instances.get(2)));
+
+    Collection<String> databases = _routingTableProvider.getResources();
+    Assert.assertEquals(databases.size(), 1);
   }
 
   @Test(dependsOnMethods = { "testRoutingTable" })