|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.backup; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertNotNull; |
| 22 | +import static org.junit.Assert.assertTrue; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 27 | +import org.apache.hadoop.hbase.HBaseTestingUtil; |
| 28 | +import org.apache.hadoop.hbase.SingleProcessHBaseCluster; |
| 29 | +import org.apache.hadoop.hbase.TableName; |
| 30 | +import org.apache.hadoop.hbase.backup.impl.BackupSystemTable; |
| 31 | +import org.apache.hadoop.hbase.client.Connection; |
| 32 | +import org.apache.hadoop.hbase.client.ConnectionFactory; |
| 33 | +import org.apache.hadoop.hbase.regionserver.HRegionServer; |
| 34 | +import org.apache.hadoop.hbase.testclassification.LargeTests; |
| 35 | +import org.junit.BeforeClass; |
| 36 | +import org.junit.ClassRule; |
| 37 | +import org.junit.Test; |
| 38 | +import org.junit.experimental.categories.Category; |
| 39 | +import org.slf4j.Logger; |
| 40 | +import org.slf4j.LoggerFactory; |
| 41 | + |
| 42 | +import org.apache.hbase.thirdparty.com.google.common.collect.Lists; |
| 43 | + |
| 44 | +/** |
| 45 | + * Tests that WAL files from offline/inactive RegionServers are handled correctly during backup. |
| 46 | + * Specifically verifies that WALs from an offline RS are: |
| 47 | + * <ol> |
| 48 | + * <li>Backed up once in the first backup after the RS goes offline</li> |
| 49 | + * <li>NOT re-backed up in subsequent backups</li> |
| 50 | + * </ol> |
| 51 | + */ |
| 52 | +@Category(LargeTests.class) |
| 53 | +public class TestBackupOfflineRS extends TestBackupBase { |
| 54 | + |
| 55 | + @ClassRule |
| 56 | + public static final HBaseClassTestRule CLASS_RULE = |
| 57 | + HBaseClassTestRule.forClass(TestBackupOfflineRS.class); |
| 58 | + |
| 59 | + private static final Logger LOG = LoggerFactory.getLogger(TestBackupOfflineRS.class); |
| 60 | + |
| 61 | + @BeforeClass |
| 62 | + public static void setUp() throws Exception { |
| 63 | + TEST_UTIL = new HBaseTestingUtil(); |
| 64 | + conf1 = TEST_UTIL.getConfiguration(); |
| 65 | + conf1.setInt("hbase.regionserver.info.port", -1); |
| 66 | + autoRestoreOnFailure = true; |
| 67 | + useSecondCluster = false; |
| 68 | + setUpHelper(); |
| 69 | + // Start an additional RS so we have at least 2 |
| 70 | + TEST_UTIL.getMiniHBaseCluster().startRegionServer(); |
| 71 | + TEST_UTIL.waitTableAvailable(table1); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Tests that when a full backup is taken while an RS is offline (with WALs in oldlogs), the |
| 76 | + * offline host's timestamps are recorded so subsequent incremental backups don't re-include those |
| 77 | + * WALs. |
| 78 | + */ |
| 79 | + @Test |
| 80 | + public void testBackupWithOfflineRS() throws Exception { |
| 81 | + LOG.info("Starting testFullBackupWithOfflineRS"); |
| 82 | + |
| 83 | + SingleProcessHBaseCluster cluster = TEST_UTIL.getMiniHBaseCluster(); |
| 84 | + List<TableName> tables = Lists.newArrayList(table1); |
| 85 | + |
| 86 | + if (cluster.getNumLiveRegionServers() < 2) { |
| 87 | + cluster.startRegionServer(); |
| 88 | + Thread.sleep(2000); |
| 89 | + } |
| 90 | + |
| 91 | + LOG.info("Inserting data to generate WAL entries"); |
| 92 | + try (Connection conn = ConnectionFactory.createConnection(conf1)) { |
| 93 | + insertIntoTable(conn, table1, famName, 2, 100); |
| 94 | + } |
| 95 | + |
| 96 | + int rsToStop = 0; |
| 97 | + HRegionServer rsBeforeStop = cluster.getRegionServer(rsToStop); |
| 98 | + String offlineHost = |
| 99 | + rsBeforeStop.getServerName().getHostname() + ":" + rsBeforeStop.getServerName().getPort(); |
| 100 | + LOG.info("Stopping RS: {}", offlineHost); |
| 101 | + |
| 102 | + cluster.stopRegionServer(rsToStop); |
| 103 | + // Wait for WALs to be moved to oldlogs |
| 104 | + Thread.sleep(5000); |
| 105 | + |
| 106 | + LOG.info("Taking full backup (with offline RS WALs in oldlogs)"); |
| 107 | + String fullBackupId = fullTableBackup(tables); |
| 108 | + assertTrue("Full backup should succeed", checkSucceeded(fullBackupId)); |
| 109 | + |
| 110 | + try (BackupSystemTable sysTable = new BackupSystemTable(TEST_UTIL.getConnection())) { |
| 111 | + Map<TableName, Map<String, Long>> timestamps = sysTable.readLogTimestampMap(BACKUP_ROOT_DIR); |
| 112 | + Map<String, Long> rsTimestamps = timestamps.get(table1); |
| 113 | + LOG.info("RS timestamps after full backup: {}", rsTimestamps); |
| 114 | + |
| 115 | + Long tsAfterFullBackup = rsTimestamps.get(offlineHost); |
| 116 | + assertNotNull("Offline host should have timestamp recorded in trslm after full backup", |
| 117 | + tsAfterFullBackup); |
| 118 | + |
| 119 | + LOG.info("Taking incremental backup (should NOT include offline RS WALs)"); |
| 120 | + String incrBackupId = incrementalTableBackup(tables); |
| 121 | + assertTrue("Incremental backup should succeed", checkSucceeded(incrBackupId)); |
| 122 | + |
| 123 | + timestamps = sysTable.readLogTimestampMap(BACKUP_ROOT_DIR); |
| 124 | + rsTimestamps = timestamps.get(table1); |
| 125 | + assertFalse("Offline host should not have a boundary ", |
| 126 | + rsTimestamps.containsKey(offlineHost)); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments