From 79a15bf390b81e98d62e0da7d22f9996d1eee5e4 Mon Sep 17 00:00:00 2001 From: Guanghao Zhang Date: Wed, 16 May 2018 11:42:34 +0800 Subject: [PATCH] HBASE-20589 Don't need to assign meta to a new RS when standby master become active --- .../java/org/apache/hadoop/hbase/ServerName.java | 6 +- .../org/apache/hadoop/hbase/TestServerName.java | 19 +++-- .../master/TestMetaAssignmentWithStopMaster.java | 86 ++++++++++++++++++++++ 3 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMetaAssignmentWithStopMaster.java diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/ServerName.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/ServerName.java index 7a32b44..64f5305 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/ServerName.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/ServerName.java @@ -218,9 +218,11 @@ public class ServerName implements Comparable, Serializable { * @return {@link #getServerName()} as bytes with a short-sized prefix with * the ServerName#VERSION of this class. */ - public synchronized byte [] getVersionedBytes() { + public synchronized byte[] getVersionedBytes() { if (this.bytes == null) { - this.bytes = Bytes.add(VERSION_BYTES, Bytes.toBytes(getServerName())); + this.bytes = Bytes.add(VERSION_BYTES, Bytes.toBytes( + getHostname() + SERVERNAME_SEPARATOR + getPort() + SERVERNAME_SEPARATOR + + getStartcode())); } return this.bytes; } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestServerName.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestServerName.java index 2fdf542..af3f75e 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestServerName.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestServerName.java @@ -87,16 +87,21 @@ public class TestServerName { } @Test public void testParseOfBytes() { - final String snStr = "www.example.org,1234,5678"; + final String snStr = "www.Example.org,1234,5678"; ServerName sn = ServerName.valueOf(snStr); byte [] versionedBytes = sn.getVersionedBytes(); - assertEquals(sn.toString(), ServerName.parseVersionedServerName(versionedBytes).toString()); - final String hostnamePortStr = sn.getHostAndPort(); + ServerName parsedSn = ServerName.parseVersionedServerName(versionedBytes); + assertEquals(sn.toString(), parsedSn.toString()); + assertEquals(sn.getHostname(), parsedSn.getHostname()); + assertEquals(sn.getPort(), parsedSn.getPort()); + assertEquals(sn.getStartcode(), parsedSn.getStartcode()); + + final String hostnamePortStr = sn.getAddress().toString(); byte [] bytes = Bytes.toBytes(hostnamePortStr); - String expecting = - hostnamePortStr.replace(":", ServerName.SERVERNAME_SEPARATOR) + - ServerName.SERVERNAME_SEPARATOR + ServerName.NON_STARTCODE; - assertEquals(expecting, ServerName.parseVersionedServerName(bytes).toString()); + parsedSn = ServerName.parseVersionedServerName(bytes); + assertEquals(sn.getHostname(), parsedSn.getHostname()); + assertEquals(sn.getPort(), parsedSn.getPort()); + assertEquals(ServerName.NON_STARTCODE, parsedSn.getStartcode()); } @Test diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMetaAssignmentWithStopMaster.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMetaAssignmentWithStopMaster.java new file mode 100644 index 0000000..9d3ec58 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMetaAssignmentWithStopMaster.java @@ -0,0 +1,86 @@ +/** + * 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.hadoop.hbase.master; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.ServerName; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.ClusterConnection; +import org.apache.hadoop.hbase.client.ConnectionFactory; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +@Category({ LargeTests.class }) +public class TestMetaAssignmentWithStopMaster { + + private static final Logger LOG = + LoggerFactory.getLogger(TestMetaAssignmentWithStopMaster.class); + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestMetaAssignmentWithStopMaster.class); + + private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); + + private static final long WAIT_TIMEOUT = 120000; + + @BeforeClass + public static void setUp() throws Exception { + UTIL.startMiniCluster(2,3); + } + + @Test + public void testStopActiveMaster() throws Exception { + ClusterConnection conn = + (ClusterConnection) ConnectionFactory.createConnection(UTIL.getConfiguration()); + ServerName oldMetaServer = conn.locateRegions(TableName.META_TABLE_NAME).get(0).getServerName(); + ServerName oldMaster = UTIL.getMiniHBaseCluster().getMaster().getServerName(); + + UTIL.getMiniHBaseCluster().getMaster().stop("Stop master for test"); + long startTime = System.currentTimeMillis(); + while (UTIL.getMiniHBaseCluster().getMaster() == null || UTIL.getMiniHBaseCluster().getMaster() + .getServerName().equals(oldMaster)) { + LOG.info("Wait the standby master become active"); + Thread.sleep(3000); + if (System.currentTimeMillis() - startTime > WAIT_TIMEOUT) { + fail("Wait too long for standby master become active"); + } + } + startTime = System.currentTimeMillis(); + while (!UTIL.getMiniHBaseCluster().getMaster().isInitialized()) { + LOG.info("Wait the new active master to be initialized"); + Thread.sleep(3000); + if (System.currentTimeMillis() - startTime > WAIT_TIMEOUT) { + fail("Wait too long for the new active master to be initialized"); + } + } + + ServerName newMetaServer = conn.locateRegions(TableName.META_TABLE_NAME).get(0).getServerName(); + assertTrue("The new meta server " + newMetaServer + " should be same with" + + " the old meta server " + oldMetaServer, newMetaServer.equals(oldMetaServer)); + } +} -- 2.7.4