001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.backup;
019
020import static org.apache.hadoop.hbase.backup.BackupRestoreConstants.CONF_CONTINUOUS_BACKUP_WAL_DIR;
021import static org.apache.hadoop.hbase.backup.BackupRestoreConstants.OPTION_ENABLE_CONTINUOUS_BACKUP;
022import static org.junit.jupiter.api.Assertions.assertEquals;
023import static org.junit.jupiter.api.Assertions.assertFalse;
024import static org.junit.jupiter.api.Assertions.assertTrue;
025
026import java.io.IOException;
027import java.util.Arrays;
028import java.util.HashSet;
029import java.util.List;
030import java.util.Map;
031import java.util.stream.Collectors;
032import org.apache.hadoop.fs.FileSystem;
033import org.apache.hadoop.fs.Path;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.backup.impl.BackupManifest;
036import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
037import org.apache.hadoop.hbase.testclassification.LargeTests;
038import org.apache.hadoop.util.ToolRunner;
039import org.junit.jupiter.api.AfterEach;
040import org.junit.jupiter.api.BeforeEach;
041import org.junit.jupiter.api.Tag;
042import org.junit.jupiter.api.Test;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045
046import org.apache.hbase.thirdparty.com.google.common.collect.Sets;
047
048@Tag(LargeTests.TAG)
049public class TestContinuousBackup extends TestBackupBase {
050
051  private static final Logger LOG = LoggerFactory.getLogger(TestContinuousBackup.class);
052
053  String backupWalDirName = "TestContinuousBackupWalDir";
054
055  @BeforeEach
056  public void beforeTest() throws IOException {
057    Path root = TEST_UTIL.getDataTestDirOnTestFS();
058    Path backupWalDir = new Path(root, backupWalDirName);
059    FileSystem fs = FileSystem.get(conf1);
060    fs.mkdirs(backupWalDir);
061    conf1.set(CONF_CONTINUOUS_BACKUP_WAL_DIR, backupWalDir.toString());
062  }
063
064  @AfterEach
065  public void afterTest() throws IOException {
066    Path root = TEST_UTIL.getDataTestDirOnTestFS();
067    Path backupWalDir = new Path(root, backupWalDirName);
068    FileSystem fs = FileSystem.get(conf1);
069
070    if (fs.exists(backupWalDir)) {
071      fs.delete(backupWalDir, true);
072    }
073
074    conf1.unset(CONF_CONTINUOUS_BACKUP_WAL_DIR);
075    deleteContinuousBackupReplicationPeerIfExists(TEST_UTIL.getAdmin());
076  }
077
078  @Test
079  public void testContinuousBackupWithFullBackup() throws Exception {
080    LOG.info("Testing successful continuous backup with full backup");
081    String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
082    TableName tableName = TableName.valueOf("table_" + methodName);
083    TEST_UTIL.createTable(tableName, "cf");
084
085    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
086      int before = table.getBackupHistory().size();
087
088      // Run backup
089      String[] args = buildBackupArgs("full", new TableName[] { tableName }, true);
090      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
091      assertEquals(0, ret, "Backup should succeed");
092
093      // Verify backup history increased and all the backups are succeeded
094      List<BackupInfo> backups = table.getBackupHistory();
095      assertEquals(before + 1, backups.size(), "Backup history should increase");
096      for (BackupInfo data : List.of(backups.get(0))) {
097        String backupId = data.getBackupId();
098        assertTrue(checkSucceeded(backupId));
099
100        TestBackupUtils.assertBackupCompleteFileExists(conf1, BACKUP_ROOT_DIR, backupId);
101      }
102
103      // Verify backup manifest contains the correct tables
104      BackupManifest manifest = getLatestBackupManifest(backups);
105      assertEquals(Sets.newHashSet(tableName), new HashSet<>(manifest.getTableList()),
106        "Backup should contain the expected tables");
107    }
108
109    // Verify replication peer subscription
110    BackupTestUtil.verifyReplicationPeerSubscription(TEST_UTIL, tableName);
111
112    // Verify table is registered in Backup System Table
113    verifyTableInBackupSystemTable(tableName);
114  }
115
116  @Test
117  public void testContinuousBackupForMultipleTables() throws Exception {
118    LOG.info("Test continuous backup for multiple tables");
119    String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
120    TableName tableName1 = TableName.valueOf("table_" + methodName);
121    TEST_UTIL.createTable(tableName1, "cf");
122    TableName tableName2 = TableName.valueOf("table_" + methodName + "2");
123    TEST_UTIL.createTable(tableName2, "cf");
124
125    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
126      int before = table.getBackupHistory().size();
127
128      // Create full backup for table1
129      String[] args = buildBackupArgs("full", new TableName[] { tableName1 }, true);
130      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
131      assertEquals(0, ret, "Backup should succeed");
132
133      // Create full backup for table2
134      args = buildBackupArgs("full", new TableName[] { tableName2 }, true);
135      ret = ToolRunner.run(conf1, new BackupDriver(), args);
136      assertEquals(0, ret, "Backup should succeed");
137
138      // Verify backup history increased and all the backups are succeeded
139      List<BackupInfo> backups = table.getBackupHistory();
140      assertEquals(before + 2, backups.size(), "Backup history should increase");
141      for (BackupInfo data : List.of(backups.get(0), backups.get(1))) {
142        String backupId = data.getBackupId();
143        assertTrue(checkSucceeded(backupId));
144      }
145
146      // Verify backup manifest contains the correct tables
147      BackupManifest manifest = getLatestBackupManifest(backups);
148      assertEquals(Sets.newHashSet(tableName2), new HashSet<>(manifest.getTableList()),
149        "Backup should contain the expected tables");
150    }
151
152    // Verify replication peer subscription for each table
153    BackupTestUtil.verifyReplicationPeerSubscription(TEST_UTIL, tableName1);
154    BackupTestUtil.verifyReplicationPeerSubscription(TEST_UTIL, tableName2);
155
156    // Verify tables are registered in Backup System Table
157    verifyTableInBackupSystemTable(tableName1);
158    verifyTableInBackupSystemTable(tableName2);
159  }
160
161  @Test
162  public void testInvalidBackupScenarioWithContinuousEnabled() throws Exception {
163    LOG.info("Testing invalid backup scenario with continuous backup enabled");
164    String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
165    TableName tableName1 = TableName.valueOf("table_" + methodName);
166    TEST_UTIL.createTable(tableName1, "cf");
167    TableName tableName2 = TableName.valueOf("table_" + methodName + "2");
168    TEST_UTIL.createTable(tableName2, "cf");
169
170    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
171      int before = table.getBackupHistory().size();
172
173      // Create full backup for table1 with continuous backup enabled
174      String[] args = buildBackupArgs("full", new TableName[] { tableName1 }, true);
175      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
176      assertEquals(0, ret, "Backup should succeed");
177
178      // Create full backup for table2 without continuous backup enabled
179      args = buildBackupArgs("full", new TableName[] { tableName2 }, false);
180      ret = ToolRunner.run(conf1, new BackupDriver(), args);
181      assertEquals(0, ret, "Backup should succeed");
182
183      // Attempt full backup for both tables without continuous backup enabled (should fail)
184      args = buildBackupArgs("full", new TableName[] { tableName1, tableName2 }, false);
185      ret = ToolRunner.run(conf1, new BackupDriver(), args);
186      assertTrue(ret != 0, "Backup should fail due to mismatch in continuous backup settings");
187
188      // Verify backup history size is unchanged after the failed backup
189      int after = table.getBackupHistory().size();
190      assertEquals(before + 2, after, "Backup history should remain unchanged on failure");
191    }
192  }
193
194  @Test
195  public void testContinuousBackupWithWALDirNotSpecified() throws Exception {
196    LOG.info("Testing that continuous backup fails when WAL directory is not specified");
197    String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
198    TableName tableName = TableName.valueOf("table_" + methodName);
199    TEST_UTIL.createTable(tableName, "cf");
200
201    conf1.unset(CONF_CONTINUOUS_BACKUP_WAL_DIR);
202    LOG.info("CONF_CONTINUOUS_BACKUP_WAL_DIR: {}", conf1.get(CONF_CONTINUOUS_BACKUP_WAL_DIR));
203
204    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
205      int before = table.getBackupHistory().size();
206
207      // Run full backup without specifying WAL directory (invalid scenario)
208      String[] args = buildBackupArgs("full", new TableName[] { tableName }, true);
209      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
210
211      assertTrue(ret != 0, "Backup should fail when WAL directory is not specified");
212
213      List<BackupInfo> backups = table.getBackupHistory();
214      int after = backups.size();
215      assertEquals(before + 1, after, "Backup history should increase");
216
217      // last backup should be a failure
218      assertFalse(checkSucceeded(backups.get(0).getBackupId()));
219    }
220  }
221
222  @Test
223  public void testContinuousBackupWithIncrementalBackup() throws Exception {
224    LOG.info("Testing that continuous backup cannot be enabled with incremental backup");
225    String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
226    TableName tableName = TableName.valueOf("table_" + methodName);
227    TEST_UTIL.createTable(tableName, "cf");
228
229    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
230      int before = table.getBackupHistory().size();
231
232      // Run incremental backup with continuous backup flag (invalid scenario)
233      String[] args = buildBackupArgs("incremental", new TableName[] { tableName }, true);
234      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
235
236      assertTrue(ret != 0, "Backup should fail when using continuous backup with incremental mode");
237
238      // Backup history should remain unchanged
239      int after = table.getBackupHistory().size();
240      assertEquals(before, after, "Backup history should remain unchanged on failure");
241    }
242  }
243
244  String[] buildBackupArgs(String backupType, TableName[] tables, boolean continuousEnabled) {
245    String tableNames =
246      Arrays.stream(tables).map(TableName::getNameAsString).collect(Collectors.joining(","));
247
248    if (continuousEnabled) {
249      return new String[] { "create", backupType, BACKUP_ROOT_DIR, "-t", tableNames,
250        "-" + OPTION_ENABLE_CONTINUOUS_BACKUP };
251    } else {
252      return new String[] { "create", backupType, BACKUP_ROOT_DIR, "-t", tableNames };
253    }
254  }
255
256  private void verifyTableInBackupSystemTable(TableName table) throws IOException {
257    try (BackupSystemTable backupTable = new BackupSystemTable(TEST_UTIL.getConnection())) {
258      Map<TableName, Long> tableBackupMap = backupTable.getContinuousBackupTableSet();
259
260      assertTrue(tableBackupMap.containsKey(table),
261        "Table is missing in the continuous backup table set");
262
263      assertTrue(tableBackupMap.get(table) > 0, "Timestamp for table should be greater than 0");
264    }
265  }
266
267}