From 3dc5123a6ce2c1cb9a82f191cbff1b84f42f5b07 Mon Sep 17 00:00:00 2001
From: Bri Augenreich <bbcrabb@vt.edu>
Date: Mon, 25 Mar 2024 15:07:31 -0400
Subject: [PATCH] HBASE-28449 Fix backupSystemTable prefix scans (#5768)

Signed-off-by: Bryan Beaudreault <bbeaudreault@apache.org>
---
 .../hadoop/hbase/backup/impl/BackupSystemTable.java  | 12 ++----------
 .../hadoop/hbase/backup/TestBackupSystemTable.java   |  9 +++++++++
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/hbase-backup/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupSystemTable.java b/hbase-backup/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupSystemTable.java
index 42168d465510..b64587b6fca6 100644
--- a/hbase-backup/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupSystemTable.java
+++ b/hbase-backup/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupSystemTable.java
@@ -1438,11 +1438,7 @@ private Put createPutForWriteRegionServerLogTimestamp(TableName table, byte[] sm
    */
   private Scan createScanForReadLogTimestampMap(String backupRoot) {
     Scan scan = new Scan();
-    byte[] startRow = rowkey(TABLE_RS_LOG_MAP_PREFIX, backupRoot);
-    byte[] stopRow = Arrays.copyOf(startRow, startRow.length);
-    stopRow[stopRow.length - 1] = (byte) (stopRow[stopRow.length - 1] + 1);
-    scan.withStartRow(startRow);
-    scan.withStopRow(stopRow);
+    scan.setStartStopRowForPrefixScan(rowkey(TABLE_RS_LOG_MAP_PREFIX, backupRoot, NULL));
     scan.addFamily(BackupSystemTable.META_FAMILY);
 
     return scan;
@@ -1479,11 +1475,7 @@ private Put createPutForRegionServerLastLogRollResult(String server, Long timest
    */
   private Scan createScanForReadRegionServerLastLogRollResult(String backupRoot) {
     Scan scan = new Scan();
-    byte[] startRow = rowkey(RS_LOG_TS_PREFIX, backupRoot);
-    byte[] stopRow = Arrays.copyOf(startRow, startRow.length);
-    stopRow[stopRow.length - 1] = (byte) (stopRow[stopRow.length - 1] + 1);
-    scan.withStartRow(startRow);
-    scan.withStopRow(stopRow);
+    scan.setStartStopRowForPrefixScan(rowkey(RS_LOG_TS_PREFIX, backupRoot, NULL));
     scan.addFamily(BackupSystemTable.META_FAMILY);
     scan.readVersions(1);
 
diff --git a/hbase-backup/src/test/java/org/apache/hadoop/hbase/backup/TestBackupSystemTable.java b/hbase-backup/src/test/java/org/apache/hadoop/hbase/backup/TestBackupSystemTable.java
index 301ae2a5985e..9f7f81cb3f38 100644
--- a/hbase-backup/src/test/java/org/apache/hadoop/hbase/backup/TestBackupSystemTable.java
+++ b/hbase-backup/src/test/java/org/apache/hadoop/hbase/backup/TestBackupSystemTable.java
@@ -190,8 +190,11 @@ public void testRegionServerLastLogRollResults() throws IOException {
     String[] servers = new String[] { "server1", "server2", "server3" };
     Long[] timestamps = new Long[] { 100L, 102L, 107L };
 
+    // validate the prefix scan in readRegionServerlastLogRollResult will get the right timestamps
+    // when a backup root with the same prefix is present
     for (int i = 0; i < servers.length; i++) {
       table.writeRegionServerLastLogRollResult(servers[i], timestamps[i], "root");
+      table.writeRegionServerLastLogRollResult(servers[i], timestamps[i], "root/backup");
     }
 
     HashMap<String, Long> result = table.readRegionServerLastLogRollResult("root");
@@ -265,7 +268,10 @@ public void testRegionServerLogTimestampMap() throws IOException {
     rsTimestampMap.put("rs2:100", 101L);
     rsTimestampMap.put("rs3:100", 103L);
 
+    // validate the prefix scan in readLogTimestampMap will get the right timestamps
+    // when a backup root with the same prefix is present
     table.writeRegionServerLogTimestamp(tables, rsTimestampMap, "root");
+    table.writeRegionServerLogTimestamp(tables, rsTimestampMap, "root/backup");
 
     Map<TableName, Map<String, Long>> result = table.readLogTimestampMap("root");
 
@@ -291,7 +297,10 @@ public void testRegionServerLogTimestampMap() throws IOException {
     rsTimestampMap1.put("rs2:100", 201L);
     rsTimestampMap1.put("rs3:100", 203L);
 
+    // validate the prefix scan in readLogTimestampMap will get the right timestamps
+    // when a backup root with the same prefix is present
     table.writeRegionServerLogTimestamp(tables1, rsTimestampMap1, "root");
+    table.writeRegionServerLogTimestamp(tables1, rsTimestampMap, "root/backup");
 
     result = table.readLogTimestampMap("root");