From c8db00c87a09e743285134624108dd96d7a7aeab Mon Sep 17 00:00:00 2001 From: Michael Stack Date: Thu, 15 Aug 2013 05:34:41 +0000 Subject: [PATCH] HBASE-9177 Cluster UUID is not properly parsable after rewriting to PB. git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1514161 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/hadoop/hbase/ClusterId.java | 2 +- .../org/apache/hadoop/hbase/util/FSUtils.java | 16 ++++++++-- .../hbase/regionserver/TestClusterId.java | 32 +++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/ClusterId.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/ClusterId.java index d2342bd1612..1ff8970d212 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/ClusterId.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/ClusterId.java @@ -43,7 +43,7 @@ public class ClusterId { this(UUID.randomUUID().toString()); } - ClusterId(final String uuid) { + public ClusterId(final String uuid) { this.id = uuid; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/FSUtils.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/FSUtils.java index e74fdb7eadf..343a83c44ae 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/FSUtils.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/FSUtils.java @@ -711,7 +711,19 @@ public abstract class FSUtils { throw new IOException("content=" + Bytes.toString(content), e); } // If not pb'd, make it so. - if (!ProtobufUtil.isPBMagicPrefix(content)) rewriteAsPb(fs, rootdir, idPath, clusterId); + if (!ProtobufUtil.isPBMagicPrefix(content)) { + String cid = new String(); + in = fs.open(idPath); + try { + cid = in.readUTF(); + clusterId = new ClusterId(cid); + } catch (EOFException eof) { + LOG.warn("Cluster ID file " + idPath.toString() + " was empty"); + } finally { + in.close(); + } + rewriteAsPb(fs, rootdir, idPath, clusterId); + } return clusterId; } else { LOG.warn("Cluster ID file does not exist at " + idPath.toString()); @@ -1324,7 +1336,7 @@ public abstract class FSUtils { /** * Checks if the given path is the one with 'recovered.edits' dir. * @param path - * @return + * @return True if we recovered edits */ public static boolean isRecoveredEdits(Path path) { return path.toString().contains(HConstants.RECOVERED_EDITS_DIR); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestClusterId.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestClusterId.java index d9ab9eb06b9..b972a3f0295 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestClusterId.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestClusterId.java @@ -21,11 +21,19 @@ package org.apache.hadoop.hbase.regionserver; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import java.util.UUID; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.MediumTests; +import org.apache.hadoop.hbase.master.HMaster; +import org.apache.hadoop.hbase.util.FSUtils; import org.apache.hadoop.hbase.util.JVMClusterUtil; import org.apache.hadoop.hbase.zookeeper.ZKClusterId; import org.junit.After; @@ -84,5 +92,29 @@ public class TestClusterId { assertNotNull(clusterId); assertEquals(clusterId, rst.getRegionServer().getClusterId()); } + + @Test + public void testRewritingClusterIdToPB() throws Exception { + TEST_UTIL.startMiniZKCluster(); + TEST_UTIL.startMiniDFSCluster(1); + TEST_UTIL.createRootDir(); + TEST_UTIL.getConfiguration().setBoolean("hbase.replication", true); + Path rootDir = FSUtils.getRootDir(TEST_UTIL.getConfiguration()); + FileSystem fs = rootDir.getFileSystem(TEST_UTIL.getConfiguration()); + Path filePath = new Path(rootDir, HConstants.CLUSTER_ID_FILE_NAME); + FSDataOutputStream s = null; + try { + s = fs.create(filePath); + s.writeUTF(UUID.randomUUID().toString()); + } finally { + if (s != null) { + s.close(); + } + } + TEST_UTIL.startMiniHBaseCluster(1, 1); + HMaster master = TEST_UTIL.getHBaseCluster().getMaster(); + assertEquals(1, master.getServerManager().getOnlineServersList().size()); + } + }