From f4dc3e39f38636bef2ce4db2f9615a52050f85e6 Mon Sep 17 00:00:00 2001 From: Elliott Clark Date: Wed, 5 Nov 2014 15:46:07 -0800 Subject: [PATCH] HBASE-12429 Add port to ClusterManager's actions. --- .../org/apache/hadoop/hbase/ClusterManager.java | 8 +- .../hadoop/hbase/DistributedHBaseCluster.java | 99 +++++++++++++--------- .../apache/hadoop/hbase/chaos/actions/Action.java | 2 +- .../java/org/apache/hadoop/hbase/HBaseCluster.java | 4 +- 4 files changed, 67 insertions(+), 46 deletions(-) diff --git a/hbase-it/src/test/java/org/apache/hadoop/hbase/ClusterManager.java b/hbase-it/src/test/java/org/apache/hadoop/hbase/ClusterManager.java index 836fbe8..2d418d8 100644 --- a/hbase-it/src/test/java/org/apache/hadoop/hbase/ClusterManager.java +++ b/hbase-it/src/test/java/org/apache/hadoop/hbase/ClusterManager.java @@ -72,17 +72,17 @@ public abstract class ClusterManager extends Configured { /** * Start the service on the given host */ - public abstract void start(ServiceType service, String hostname) throws IOException; + public abstract void start(ServiceType service, String hostname, int port) throws IOException; /** * Stop the service on the given host */ - public abstract void stop(ServiceType service, String hostname) throws IOException; + public abstract void stop(ServiceType service, String hostname, int port) throws IOException; /** * Restart the service on the given host */ - public abstract void restart(ServiceType service, String hostname) throws IOException; + public abstract void restart(ServiceType service, String hostname, int port) throws IOException; /** * Send the given posix signal to the service @@ -115,7 +115,7 @@ public abstract class ClusterManager extends Configured { * Returns whether the service is running on the remote host. This only checks whether the * service still has a pid. */ - public abstract boolean isRunning(ServiceType service, String hostname) throws IOException; + public abstract boolean isRunning(ServiceType service, String hostname, int port) throws IOException; /* TODO: further API ideas: * diff --git a/hbase-it/src/test/java/org/apache/hadoop/hbase/DistributedHBaseCluster.java b/hbase-it/src/test/java/org/apache/hadoop/hbase/DistributedHBaseCluster.java index d97862d..62161c7 100644 --- a/hbase-it/src/test/java/org/apache/hadoop/hbase/DistributedHBaseCluster.java +++ b/hbase-it/src/test/java/org/apache/hadoop/hbase/DistributedHBaseCluster.java @@ -19,8 +19,12 @@ package org.apache.hadoop.hbase; import java.io.IOException; import java.util.ArrayList; +import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.TreeSet; import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.conf.Configuration; @@ -99,9 +103,9 @@ public class DistributedHBaseCluster extends HBaseCluster { } @Override - public void startRegionServer(String hostname) throws IOException { + public void startRegionServer(String hostname, int port) throws IOException { LOG.info("Starting RS on: " + hostname); - clusterManager.start(ServiceType.HBASE_REGIONSERVER, hostname); + clusterManager.start(ServiceType.HBASE_REGIONSERVER, hostname, port); } @Override @@ -113,7 +117,7 @@ public class DistributedHBaseCluster extends HBaseCluster { @Override public void stopRegionServer(ServerName serverName) throws IOException { LOG.info("Stopping RS: " + serverName.getServerName()); - clusterManager.stop(ServiceType.HBASE_REGIONSERVER, serverName.getHostname()); + clusterManager.stop(ServiceType.HBASE_REGIONSERVER, serverName.getHostname(), serverName.getPort()); } @Override @@ -127,7 +131,7 @@ public class DistributedHBaseCluster extends HBaseCluster { long start = System.currentTimeMillis(); while ((System.currentTimeMillis() - start) < timeout) { - if (!clusterManager.isRunning(service, serverName.getHostname())) { + if (!clusterManager.isRunning(service, serverName.getHostname(), serverName.getPort())) { return; } Threads.sleep(1000); @@ -143,9 +147,9 @@ public class DistributedHBaseCluster extends HBaseCluster { } @Override - public void startMaster(String hostname) throws IOException { + public void startMaster(String hostname, int port) throws IOException { LOG.info("Starting Master on: " + hostname); - clusterManager.start(ServiceType.HBASE_MASTER, hostname); + clusterManager.start(ServiceType.HBASE_MASTER, hostname, port); } @Override @@ -157,7 +161,7 @@ public class DistributedHBaseCluster extends HBaseCluster { @Override public void stopMaster(ServerName serverName) throws IOException { LOG.info("Stopping Master: " + serverName.getServerName()); - clusterManager.stop(ServiceType.HBASE_MASTER, serverName.getHostname()); + clusterManager.stop(ServiceType.HBASE_MASTER, serverName.getHostname(), serverName.getPort()); } @Override @@ -241,9 +245,9 @@ public class DistributedHBaseCluster extends HBaseCluster { // If initial master is stopped, start it, before restoring the state. // It will come up as a backup master, if there is already an active master. try { - if (!clusterManager.isRunning(ServiceType.HBASE_MASTER, initial.getMaster().getHostname())) { + if (!clusterManager.isRunning(ServiceType.HBASE_MASTER, initial.getMaster().getHostname(), initial.getMaster().getPort())) { LOG.info("Restoring cluster - starting initial active master at:" + initial.getMaster().getHostname()); - startMaster(initial.getMaster().getHostname()); + startMaster(initial.getMaster().getHostname(), initial.getMaster().getPort()); } //master has changed, we would like to undo this. @@ -269,9 +273,9 @@ public class DistributedHBaseCluster extends HBaseCluster { for (ServerName backup : initial.getBackupMasters()) { try { //these are not started in backup mode, but we should already have an active master - if(!clusterManager.isRunning(ServiceType.HBASE_MASTER, backup.getHostname())) { + if(!clusterManager.isRunning(ServiceType.HBASE_MASTER, backup.getHostname(), backup.getPort())) { LOG.info("Restoring cluster - starting initial backup master: " + backup.getHostname()); - startMaster(backup.getHostname()); + startMaster(backup.getHostname(), backup.getPort()); } } catch (IOException ex) { deferred.add(ex); @@ -279,32 +283,34 @@ public class DistributedHBaseCluster extends HBaseCluster { } } else { //current master has not changed, match up backup masters - HashMap initialBackups = new HashMap(); - HashMap currentBackups = new HashMap(); + Set toStart = new TreeSet(new ServerNameIgnoreStartCodeComparator()); + Set toKill = new TreeSet(new ServerNameIgnoreStartCodeComparator()); + toStart.addAll(initial.getBackupMasters()); + toKill.addAll(current.getBackupMasters()); - for (ServerName server : initial.getBackupMasters()) { - initialBackups.put(server.getHostname(), server); - } for (ServerName server : current.getBackupMasters()) { - currentBackups.put(server.getHostname(), server); + toStart.remove(server); + } + for (ServerName server: initial.getBackupMasters()) { + toKill.remove(server); } - for (String hostname : Sets.difference(initialBackups.keySet(), currentBackups.keySet())) { + for (ServerName sn:toStart) { try { - if(!clusterManager.isRunning(ServiceType.HBASE_MASTER, hostname)) { - LOG.info("Restoring cluster - starting initial backup master: " + hostname); - startMaster(hostname); + if(!clusterManager.isRunning(ServiceType.HBASE_MASTER, sn.getHostname(), sn.getPort())) { + LOG.info("Restoring cluster - starting initial backup master: " + sn.getHostAndPort()); + startMaster(sn.getHostname(), sn.getPort()); } } catch (IOException ex) { deferred.add(ex); } } - for (String hostname : Sets.difference(currentBackups.keySet(), initialBackups.keySet())) { + for (ServerName sn:toKill) { try { - if(clusterManager.isRunning(ServiceType.HBASE_MASTER, hostname)) { - LOG.info("Restoring cluster - stopping backup master: " + hostname); - stopMaster(currentBackups.get(hostname)); + if(clusterManager.isRunning(ServiceType.HBASE_MASTER, sn.getHostname(), sn.getPort())) { + LOG.info("Restoring cluster - stopping backup master: " + sn.getHostAndPort()); + stopMaster(sn); } } catch (IOException ex) { deferred.add(ex); @@ -321,34 +327,49 @@ public class DistributedHBaseCluster extends HBaseCluster { return deferred.isEmpty(); } + + private static class ServerNameIgnoreStartCodeComparator implements Comparator { + @Override + public int compare(ServerName o1, ServerName o2) { + int compare = o1.getHostname().compareToIgnoreCase(o2.getHostname()); + if (compare != 0) return compare; + compare = o1.getPort() - o2.getPort(); + if (compare != 0) return compare; + return 0; + } + } + protected boolean restoreRegionServers(ClusterStatus initial, ClusterStatus current) { - HashMap initialServers = new HashMap(); - HashMap currentServers = new HashMap(); + Set toStart = new TreeSet(new ServerNameIgnoreStartCodeComparator()); + Set toKill = new TreeSet(new ServerNameIgnoreStartCodeComparator()); + toStart.addAll(initial.getBackupMasters()); + toKill.addAll(current.getBackupMasters()); - for (ServerName server : initial.getServers()) { - initialServers.put(server.getHostname(), server); - } for (ServerName server : current.getServers()) { - currentServers.put(server.getHostname(), server); + toStart.remove(server); + } + for (ServerName server: initial.getServers()) { + toKill.remove(server); } List deferred = new ArrayList(); - for (String hostname : Sets.difference(initialServers.keySet(), currentServers.keySet())) { + + for(ServerName sn:toStart) { try { - if(!clusterManager.isRunning(ServiceType.HBASE_REGIONSERVER, hostname)) { - LOG.info("Restoring cluster - starting initial region server: " + hostname); - startRegionServer(hostname); + if(!clusterManager.isRunning(ServiceType.HBASE_REGIONSERVER, sn.getHostname(), sn.getPort())) { + LOG.info("Restoring cluster - starting initial region server: " + sn.getHostAndPort()); + startRegionServer(sn.getHostname(), sn.getPort()); } } catch (IOException ex) { deferred.add(ex); } } - for (String hostname : Sets.difference(currentServers.keySet(), initialServers.keySet())) { + for(ServerName sn:toKill) { try { - if(clusterManager.isRunning(ServiceType.HBASE_REGIONSERVER, hostname)) { - LOG.info("Restoring cluster - stopping initial region server: " + hostname); - stopRegionServer(currentServers.get(hostname)); + if(clusterManager.isRunning(ServiceType.HBASE_REGIONSERVER, sn.getHostname(), sn.getPort())) { + LOG.info("Restoring cluster - stopping initial region server: " + sn.getHostAndPort()); + stopRegionServer(sn); } } catch (IOException ex) { deferred.add(ex); diff --git a/hbase-it/src/test/java/org/apache/hadoop/hbase/chaos/actions/Action.java b/hbase-it/src/test/java/org/apache/hadoop/hbase/chaos/actions/Action.java index c01ce0f..372594d 100644 --- a/hbase-it/src/test/java/org/apache/hadoop/hbase/chaos/actions/Action.java +++ b/hbase-it/src/test/java/org/apache/hadoop/hbase/chaos/actions/Action.java @@ -129,7 +129,7 @@ public class Action { protected void startRs(ServerName server) throws IOException { LOG.info("Starting region server:" + server.getHostname()); - cluster.startRegionServer(server.getHostname()); + cluster.startRegionServer(server.getHostname(), server.getPort()); cluster.waitForRegionServerToStart(server.getHostname(), startRsTimeout); LOG.info("Started region server:" + server + ". Reported num of rs:" + cluster.getClusterStatus().getServersSize()); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseCluster.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseCluster.java index 76a9566..9900643 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseCluster.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseCluster.java @@ -118,7 +118,7 @@ public abstract class HBaseCluster implements Closeable, Configurable { * @param hostname the hostname to start the regionserver on * @throws IOException if something goes wrong */ - public abstract void startRegionServer(String hostname) throws IOException; + public abstract void startRegionServer(String hostname, int port) throws IOException; /** * Kills the region server process if this is a distributed cluster, otherwise @@ -169,7 +169,7 @@ public abstract class HBaseCluster implements Closeable, Configurable { * @return whether the operation finished with success * @throws IOException if something goes wrong */ - public abstract void startMaster(String hostname) throws IOException; + public abstract void startMaster(String hostname, int port) throws IOException; /** * Kills the master process if this is a distributed cluster, otherwise, -- 2.1.1