HBASE-19917 Improve RSGroupBasedLoadBalancer#filterServers() to be more efficient
Signed-off-by: tedyu <yuzhih...@gmail.com> Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/7f7f2b2d Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/7f7f2b2d Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/7f7f2b2d Branch: refs/heads/HBASE-19064 Commit: 7f7f2b2de53d11dc8ddde6954a4af3599a9e0fa5 Parents: 88d6e06 Author: Xiang Li <lixi...@freewheel.tv> Authored: Sat Feb 3 04:46:55 2018 +0000 Committer: tedyu <yuzhih...@gmail.com> Committed: Sun Feb 4 17:22:41 2018 -0800 ---------------------------------------------------------------------- .../hbase/rsgroup/RSGroupBasedLoadBalancer.java | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/7f7f2b2d/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupBasedLoadBalancer.java ---------------------------------------------------------------------- diff --git a/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupBasedLoadBalancer.java b/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupBasedLoadBalancer.java index 550f734..c47972d 100644 --- a/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupBasedLoadBalancer.java +++ b/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupBasedLoadBalancer.java @@ -20,7 +20,6 @@ package org.apache.hadoop.hbase.rsgroup; import java.io.IOException; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -29,6 +28,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; + import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.ClusterMetrics; import org.apache.hadoop.hbase.HBaseIOException; @@ -288,16 +288,20 @@ public class RSGroupBasedLoadBalancer implements RSGroupableBalancer { * List of servers which are online. * @return the list */ - private List<ServerName> filterServers(Collection<Address> servers, - Collection<ServerName> onlineServers) { - ArrayList<ServerName> finalList = new ArrayList<ServerName>(); - for (Address server : servers) { - for(ServerName curr: onlineServers) { - if(curr.getAddress().equals(server)) { - finalList.add(curr); - } + private List<ServerName> filterServers(Set<Address> servers, + List<ServerName> onlineServers) { + /** + * servers is actually a TreeSet (see {@link org.apache.hadoop.hbase.rsgroup.RSGroupInfo}), + * having its contains()'s time complexity as O(logn), which is good enough. + * TODO: consider using HashSet to pursue O(1) for contains() throughout the calling chain + * if needed. */ + ArrayList<ServerName> finalList = new ArrayList<>(); + for (ServerName onlineServer : onlineServers) { + if (servers.contains(onlineServer.getAddress())) { + finalList.add(onlineServer); } } + return finalList; }