|
21 | 21 | import static org.junit.Assert.assertFalse; |
22 | 22 | import static org.junit.Assert.assertTrue; |
23 | 23 |
|
| 24 | +import com.google.common.collect.ImmutableSet; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.ArrayList; |
24 | 27 | import java.util.Collection; |
25 | | -import java.util.Collections; |
26 | 28 | import java.util.LinkedHashSet; |
27 | 29 | import java.util.List; |
28 | 30 | import java.util.Map; |
29 | 31 | import java.util.Set; |
30 | 32 | import org.apache.hadoop.fs.FileStatus; |
31 | 33 | import org.apache.hadoop.fs.Path; |
32 | 34 | import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 35 | +import org.apache.hadoop.hbase.HRegionLocation; |
| 36 | +import org.apache.hadoop.hbase.ServerName; |
33 | 37 | import org.apache.hadoop.hbase.TableName; |
34 | 38 | import org.apache.hadoop.hbase.backup.BackupType; |
35 | 39 | import org.apache.hadoop.hbase.backup.TestBackupBase; |
36 | 40 | import org.apache.hadoop.hbase.backup.impl.BackupSystemTable; |
37 | 41 | import org.apache.hadoop.hbase.client.Connection; |
38 | 42 | import org.apache.hadoop.hbase.client.Put; |
| 43 | +import org.apache.hadoop.hbase.client.RegionInfo; |
39 | 44 | import org.apache.hadoop.hbase.client.Table; |
40 | 45 | import org.apache.hadoop.hbase.master.HMaster; |
| 46 | +import org.apache.hadoop.hbase.regionserver.HRegionServer; |
41 | 47 | import org.apache.hadoop.hbase.testclassification.LargeTests; |
42 | 48 | import org.apache.hadoop.hbase.util.Bytes; |
43 | 49 | import org.junit.ClassRule; |
@@ -66,6 +72,7 @@ public void testBackupLogCleaner() throws Exception { |
66 | 72 | List<TableName> tableSetFull = List.of(table1, table2, table3, table4); |
67 | 73 | List<TableName> tableSet14 = List.of(table1, table4); |
68 | 74 | List<TableName> tableSet23 = List.of(table2, table3); |
| 75 | + TEST_UTIL.getConfiguration().setLong(BackupLogCleaner.TS_BUFFER_KEY, 0); |
69 | 76 |
|
70 | 77 | try (BackupSystemTable systemTable = new BackupSystemTable(TEST_UTIL.getConnection())) { |
71 | 78 | // Verify that we have no backup sessions yet |
@@ -193,35 +200,131 @@ public void testBackupLogCleaner() throws Exception { |
193 | 200 | // Taking the minimum timestamp (= 2), this means all WALs preceding B3 can be deleted. |
194 | 201 | deletable = cleaner.getDeletableFiles(walFilesAfterB5); |
195 | 202 | assertEquals(toSet(walFilesAfterB2), toSet(deletable)); |
| 203 | + } finally { |
| 204 | + TEST_UTIL.truncateTable(BackupSystemTable.getTableName(TEST_UTIL.getConfiguration())).close(); |
196 | 205 | } |
197 | 206 | } |
198 | 207 |
|
199 | | - private Set<FileStatus> mergeAsSet(Collection<FileStatus> toCopy, Collection<FileStatus> toAdd) { |
200 | | - Set<FileStatus> result = new LinkedHashSet<>(toCopy); |
201 | | - result.addAll(toAdd); |
202 | | - return result; |
| 208 | + @Test |
| 209 | + public void testDoesNotDeleteWALsFromNewServers() throws Exception { |
| 210 | + Path backupRoot1 = new Path(BACKUP_ROOT_DIR, "backup1"); |
| 211 | + List<TableName> tableSetFull = List.of(table1, table2, table3, table4); |
| 212 | + |
| 213 | + try (BackupSystemTable systemTable = new BackupSystemTable(TEST_UTIL.getConnection())) { |
| 214 | + LOG.info("Creating initial backup B1"); |
| 215 | + String backupIdB1 = backupTables(BackupType.FULL, tableSetFull, backupRoot1.toString()); |
| 216 | + assertTrue(checkSucceeded(backupIdB1)); |
| 217 | + |
| 218 | + List<FileStatus> walsAfterB1 = getListOfWALFiles(TEST_UTIL.getConfiguration()); |
| 219 | + LOG.info("WALs after B1: {}", walsAfterB1.size()); |
| 220 | + |
| 221 | + String startCodeStr = systemTable.readBackupStartCode(backupRoot1.toString()); |
| 222 | + long b1StartCode = Long.parseLong(startCodeStr); |
| 223 | + LOG.info("B1 startCode: {}", b1StartCode); |
| 224 | + |
| 225 | + // Add a new RegionServer to the cluster |
| 226 | + LOG.info("Adding new RegionServer to cluster"); |
| 227 | + HRegionServer newRS = TEST_UTIL.getMiniHBaseCluster().startRegionServer().getRegionServer(); |
| 228 | + ServerName newServerName = newRS.getServerName(); |
| 229 | + LOG.info("New RegionServer started: {}", newServerName); |
| 230 | + |
| 231 | + // Move a region to the new server to ensure it creates a WAL |
| 232 | + List<RegionInfo> regions = TEST_UTIL.getAdmin().getRegions(table1); |
| 233 | + RegionInfo regionToMove = regions.get(0); |
| 234 | + |
| 235 | + LOG.info("Moving region {} to new server {}", regionToMove.getEncodedName(), newServerName); |
| 236 | + TEST_UTIL.getAdmin().move(regionToMove.getEncodedNameAsBytes(), newServerName); |
| 237 | + |
| 238 | + TEST_UTIL.waitFor(30000, () -> { |
| 239 | + try { |
| 240 | + HRegionLocation location = TEST_UTIL.getConnection().getRegionLocator(table1) |
| 241 | + .getRegionLocation(regionToMove.getStartKey()); |
| 242 | + return location.getServerName().equals(newServerName); |
| 243 | + } catch (IOException e) { |
| 244 | + return false; |
| 245 | + } |
| 246 | + }); |
| 247 | + |
| 248 | + // Write some data to trigger WAL creation on the new server |
| 249 | + try (Table t1 = TEST_UTIL.getConnection().getTable(table1)) { |
| 250 | + for (int i = 0; i < 100; i++) { |
| 251 | + Put p = new Put(Bytes.toBytes("newserver-row-" + i)); |
| 252 | + p.addColumn(famName, qualName, Bytes.toBytes("val" + i)); |
| 253 | + t1.put(p); |
| 254 | + } |
| 255 | + } |
| 256 | + TEST_UTIL.getAdmin().flushRegion(regionToMove.getEncodedNameAsBytes()); |
| 257 | + |
| 258 | + List<FileStatus> walsAfterNewServer = getListOfWALFiles(TEST_UTIL.getConfiguration()); |
| 259 | + LOG.info("WALs after adding new server: {}", walsAfterNewServer.size()); |
| 260 | + assertTrue("Should have more WALs after new server", |
| 261 | + walsAfterNewServer.size() > walsAfterB1.size()); |
| 262 | + |
| 263 | + List<FileStatus> newServerWALs = new ArrayList<>(walsAfterNewServer); |
| 264 | + newServerWALs.removeAll(walsAfterB1); |
| 265 | + assertFalse("Should have WALs from new server", newServerWALs.isEmpty()); |
| 266 | + |
| 267 | + BackupLogCleaner cleaner = new BackupLogCleaner(); |
| 268 | + cleaner.setConf(TEST_UTIL.getConfiguration()); |
| 269 | + cleaner.init(Map.of(HMaster.MASTER, TEST_UTIL.getHBaseCluster().getMaster())); |
| 270 | + |
| 271 | + Set<FileStatus> deletable = |
| 272 | + ImmutableSet.copyOf(cleaner.getDeletableFiles(walsAfterNewServer)); |
| 273 | + for (FileStatus newWAL : newServerWALs) { |
| 274 | + assertFalse("WAL from new server should NOT be deletable: " + newWAL.getPath(), |
| 275 | + deletable.contains(newWAL)); |
| 276 | + } |
| 277 | + } finally { |
| 278 | + TEST_UTIL.truncateTable(BackupSystemTable.getTableName(TEST_UTIL.getConfiguration())).close(); |
| 279 | + } |
203 | 280 | } |
204 | 281 |
|
205 | | - private <T> Set<T> toSet(Iterable<T> iterable) { |
206 | | - Set<T> result = new LinkedHashSet<>(); |
207 | | - iterable.forEach(result::add); |
208 | | - return result; |
| 282 | + @Test |
| 283 | + public void testCanDeleteFileWithNewServerWALs() { |
| 284 | + long backupStartCode = 1000000L; |
| 285 | + |
| 286 | + // Old WAL from before the backup |
| 287 | + Path oldWAL = new Path("/hbase/oldWALs/server1%2C60020%2C12345.500000"); |
| 288 | + assertTrue("WAL older than backup should be deletable", |
| 289 | + BackupLogCleaner.canDeleteFile(backupStartCode, oldWAL)); |
| 290 | + |
| 291 | + // WAL from exactly at the backup boundary |
| 292 | + Path boundaryWAL = new Path("/hbase/oldWALs/server1%2C60020%2C12345.1000000"); |
| 293 | + assertTrue("WAL at boundary should be deletable", |
| 294 | + BackupLogCleaner.canDeleteFile(backupStartCode, boundaryWAL)); |
| 295 | + |
| 296 | + // WAL from a server that joined AFTER the backup |
| 297 | + Path newServerWAL = new Path("/hbase/oldWALs/newserver%2C60020%2C99999.1500000"); |
| 298 | + assertFalse("WAL from new server (after backup) should NOT be deletable", |
| 299 | + BackupLogCleaner.canDeleteFile(backupStartCode, newServerWAL)); |
209 | 300 | } |
210 | 301 |
|
211 | 302 | @Test |
212 | 303 | public void testCleansUpHMasterWal() { |
213 | 304 | Path path = new Path("/hbase/MasterData/WALs/hmaster,60000,1718808578163"); |
214 | | - assertTrue(BackupLogCleaner.canDeleteFile(Collections.emptyMap(), path)); |
| 305 | + assertTrue(BackupLogCleaner.canDeleteFile(Long.MIN_VALUE, path)); |
215 | 306 | } |
216 | 307 |
|
217 | 308 | @Test |
218 | 309 | public void testCleansUpArchivedHMasterWal() { |
219 | 310 | Path normalPath = |
220 | 311 | new Path("/hbase/oldWALs/hmaster%2C60000%2C1716224062663.1716247552189$masterlocalwal$"); |
221 | | - assertTrue(BackupLogCleaner.canDeleteFile(Collections.emptyMap(), normalPath)); |
| 312 | + assertTrue(BackupLogCleaner.canDeleteFile(Long.MIN_VALUE, normalPath)); |
222 | 313 |
|
223 | 314 | Path masterPath = new Path( |
224 | 315 | "/hbase/MasterData/oldWALs/hmaster%2C60000%2C1716224062663.1716247552189$masterlocalwal$"); |
225 | | - assertTrue(BackupLogCleaner.canDeleteFile(Collections.emptyMap(), masterPath)); |
| 316 | + assertTrue(BackupLogCleaner.canDeleteFile(Long.MIN_VALUE, masterPath)); |
| 317 | + } |
| 318 | + |
| 319 | + private Set<FileStatus> mergeAsSet(Collection<FileStatus> toCopy, Collection<FileStatus> toAdd) { |
| 320 | + Set<FileStatus> result = new LinkedHashSet<>(toCopy); |
| 321 | + result.addAll(toAdd); |
| 322 | + return result; |
| 323 | + } |
| 324 | + |
| 325 | + private <T> Set<T> toSet(Iterable<T> iterable) { |
| 326 | + Set<T> result = new LinkedHashSet<>(); |
| 327 | + iterable.forEach(result::add); |
| 328 | + return result; |
226 | 329 | } |
227 | 330 | } |
0 commit comments