From d0fe2b5b6b11140f3e81d30bdcbcf7f78b7dcfbe Mon Sep 17 00:00:00 2001 From: Jan Van Besien NGDATA <93314377+janvanbesien-ngdata@users.noreply.github.com> Date: Sat, 7 Oct 2023 03:21:40 +0200 Subject: [PATCH] HubSpot Backport: HBASE-28082 oldWALs naming can be incompatible with HBase backup (#5445) Make the hostname parsing in BackupUtils#parseHostFromOldLog more lenient by not making any assumptions about the name of the file other than that it starts with a org.apache.hadoop.hbase.ServerName. Signed-off-by: Duo Zhang --- .../hadoop/hbase/backup/util/BackupUtils.java | 10 +++--- .../hadoop/hbase/backup/TestBackupUtils.java | 33 ++++++++++++------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/hbase-backup/src/main/java/org/apache/hadoop/hbase/backup/util/BackupUtils.java b/hbase-backup/src/main/java/org/apache/hadoop/hbase/backup/util/BackupUtils.java index 5be8eed3952c..d8bcfdbd6cac 100644 --- a/hbase-backup/src/main/java/org/apache/hadoop/hbase/backup/util/BackupUtils.java +++ b/hbase-backup/src/main/java/org/apache/hadoop/hbase/backup/util/BackupUtils.java @@ -65,6 +65,7 @@ import org.slf4j.LoggerFactory; import org.apache.hbase.thirdparty.com.google.common.base.Splitter; +import org.apache.hbase.thirdparty.com.google.common.collect.Iterables; import org.apache.hbase.thirdparty.com.google.common.collect.Iterators; /** @@ -365,10 +366,11 @@ public static String parseHostFromOldLog(Path p) { return null; } try { - String n = p.getName(); - int idx = n.lastIndexOf(LOGNAME_SEPARATOR); - String s = URLDecoder.decode(n.substring(0, idx), "UTF8"); - return ServerName.valueOf(s).getAddress().toString(); + String urlDecodedName = URLDecoder.decode(p.getName(), "UTF8"); + Iterable nameSplitsOnComma = Splitter.on(",").split(urlDecodedName); + String host = Iterables.get(nameSplitsOnComma, 0); + String port = Iterables.get(nameSplitsOnComma, 1); + return host + ":" + port; } catch (Exception e) { LOG.warn("Skip log file (can't parse): {}", p); return null; diff --git a/hbase-backup/src/test/java/org/apache/hadoop/hbase/backup/TestBackupUtils.java b/hbase-backup/src/test/java/org/apache/hadoop/hbase/backup/TestBackupUtils.java index 831ec309cfc6..d4e6900fb28b 100644 --- a/hbase-backup/src/test/java/org/apache/hadoop/hbase/backup/TestBackupUtils.java +++ b/hbase-backup/src/test/java/org/apache/hadoop/hbase/backup/TestBackupUtils.java @@ -87,21 +87,32 @@ public Path run() { @Test public void testFilesystemWalHostNameParsing() throws IOException { - String host = "localhost"; - int port = 60030; - ServerName serverName = ServerName.valueOf(host, port, 1234); + String[] hosts = + new String[] { "10.20.30.40", "127.0.0.1", "localhost", "a-region-server.domain.com" }; + Path walRootDir = CommonFSUtils.getWALRootDir(conf); Path oldLogDir = new Path(walRootDir, HConstants.HREGION_OLDLOGDIR_NAME); - Path testWalPath = new Path(oldLogDir, - serverName.toString() + BackupUtils.LOGNAME_SEPARATOR + EnvironmentEdgeManager.currentTime()); - Path testMasterWalPath = - new Path(oldLogDir, testWalPath.getName() + MasterRegionFactory.ARCHIVED_WAL_SUFFIX); + int port = 60030; + for (String host : hosts) { + ServerName serverName = ServerName.valueOf(host, port, 1234); + + Path testOldWalPath = new Path(oldLogDir, + serverName + BackupUtils.LOGNAME_SEPARATOR + EnvironmentEdgeManager.currentTime()); + Assert.assertEquals(host + Addressing.HOSTNAME_PORT_SEPARATOR + port, + BackupUtils.parseHostFromOldLog(testOldWalPath)); + + Path testMasterWalPath = + new Path(oldLogDir, testOldWalPath.getName() + MasterRegionFactory.ARCHIVED_WAL_SUFFIX); + Assert.assertNull(BackupUtils.parseHostFromOldLog(testMasterWalPath)); - String parsedHost = BackupUtils.parseHostFromOldLog(testMasterWalPath); - Assert.assertNull(parsedHost); + // org.apache.hadoop.hbase.wal.BoundedGroupingStrategy does this + Path testOldWalWithRegionGroupingPath = new Path(oldLogDir, + serverName + BackupUtils.LOGNAME_SEPARATOR + serverName + BackupUtils.LOGNAME_SEPARATOR + + "regiongroup-0" + BackupUtils.LOGNAME_SEPARATOR + EnvironmentEdgeManager.currentTime()); + Assert.assertEquals(host + Addressing.HOSTNAME_PORT_SEPARATOR + port, + BackupUtils.parseHostFromOldLog(testOldWalWithRegionGroupingPath)); + } - parsedHost = BackupUtils.parseHostFromOldLog(testWalPath); - Assert.assertEquals(parsedHost, host + Addressing.HOSTNAME_PORT_SEPARATOR + port); } }