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.TestBackupUtils.assertBackupCompleteFileDoesNotExist; 021import static org.apache.hadoop.hbase.backup.TestBackupUtils.assertBackupCompleteFileExists; 022import static org.junit.jupiter.api.Assertions.assertEquals; 023import static org.junit.jupiter.api.Assertions.assertThrows; 024import static org.junit.jupiter.api.Assertions.assertTrue; 025import static org.mockito.ArgumentMatchers.argThat; 026import static org.mockito.Mockito.mock; 027import static org.mockito.Mockito.never; 028import static org.mockito.Mockito.verify; 029import static org.mockito.Mockito.verifyNoInteractions; 030import static org.mockito.Mockito.when; 031 032import java.io.IOException; 033import java.util.List; 034import org.apache.hadoop.conf.Configuration; 035import org.apache.hadoop.fs.FSDataOutputStream; 036import org.apache.hadoop.fs.FileSystem; 037import org.apache.hadoop.fs.FilterFileSystem; 038import org.apache.hadoop.fs.LocalFileSystem; 039import org.apache.hadoop.fs.Path; 040import org.apache.hadoop.hbase.TableName; 041import org.apache.hadoop.hbase.backup.BackupInfo.BackupPhase; 042import org.apache.hadoop.hbase.backup.impl.BackupManifest; 043import org.apache.hadoop.hbase.backup.impl.BackupManifest.BackupImage; 044import org.apache.hadoop.hbase.backup.impl.TableBackupClient; 045import org.apache.hadoop.hbase.backup.util.BackupUtils; 046import org.apache.hadoop.hbase.client.Connection; 047import org.apache.hadoop.hbase.testclassification.SmallTests; 048import org.junit.jupiter.api.Tag; 049import org.junit.jupiter.api.Test; 050import org.junit.jupiter.api.io.TempDir; 051 052@Tag(SmallTests.TAG) 053public class TestHBackupFileSystem { 054 055 private static final Path ROOT_DIR = new Path("/backup/root"); 056 private static final String BACKUP_ID = "123"; 057 058 @Test 059 public void testRootDirManifestPathConversion() throws IOException { 060 Path manifestPath = 061 HBackupFileSystem.getManifestPath(new Configuration(), ROOT_DIR, BACKUP_ID, false); 062 Path convertedRootDir = HBackupFileSystem.getRootDirFromBackupPath(manifestPath, BACKUP_ID); 063 assertEquals(ROOT_DIR, convertedRootDir); 064 } 065 066 // This test verifies the .tmp directory is skipped over when each subdirectory in the backup 067 // root dir is searched for its .backup.manifest file. 068 @Test 069 public void testGetAllBackupImagesSkipsTmpDir(@TempDir java.nio.file.Path tempDir) 070 throws IOException { 071 Configuration conf = new Configuration(); 072 Path backupRootPath = new Path(tempDir.toUri()); 073 FileSystem fs = FileSystem.get(backupRootPath.toUri(), conf); 074 075 TableName[] tables = new TableName[] { TableName.valueOf("test_table") }; 076 String backupId1 = "backup_0001"; 077 String backupId2 = "backup_0002"; 078 079 BackupInfo info1 = 080 new BackupInfo(backupId1, BackupType.FULL, tables, backupRootPath.toString()); 081 new BackupManifest(info1).store(conf); 082 083 BackupInfo info2 = 084 new BackupInfo(backupId2, BackupType.INCREMENTAL, tables, backupRootPath.toString()); 085 new BackupManifest(info2).store(conf); 086 087 fs.mkdirs(new Path(backupRootPath, ".tmp")); 088 089 // Use fully-qualified Log4j2 names to avoid banned-import enforcer rule 090 org.apache.logging.log4j.core.Appender mockAppender = 091 mock(org.apache.logging.log4j.core.Appender.class); 092 when(mockAppender.getName()).thenReturn("mockAppender"); 093 when(mockAppender.isStarted()).thenReturn(true); 094 org.apache.logging.log4j.core.Logger log4jLogger = 095 (org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager 096 .getLogger(HBackupFileSystem.class); 097 log4jLogger.addAppender(mockAppender); 098 099 try { 100 List<BackupImage> images = HBackupFileSystem.getAllBackupImages(conf, backupRootPath); 101 102 // The backupRoot/.tmp dir should not be checked for a .backup.manifest file because it will 103 // not have one. Verify an ERROR is not logged with the following message: 104 // Cannot load backup manifest from: /path/to/.tmp 105 verify(mockAppender, never()) 106 .append(argThat((org.apache.logging.log4j.core.LogEvent event) -> event.getLevel() 107 .equals(org.apache.logging.log4j.Level.ERROR) 108 && event.getMessage().getFormattedMessage().contains("Cannot load backup manifest from: ") 109 && event.getMessage().getFormattedMessage().contains(".tmp"))); 110 111 assertEquals(2, images.size()); 112 assertEquals(backupId2, images.get(0).getBackupId()); 113 assertEquals(backupId1, images.get(1).getBackupId()); 114 } finally { 115 log4jLogger.removeAppender(mockAppender); 116 } 117 } 118 119 // Verifies a failure to create a .backup.complete file in a completed backup directory does 120 // not cause the backup to fail 121 @Test 122 public void testWriteBackupCompleteMarkerFailsGracefully(@TempDir java.nio.file.Path tempDir) 123 throws IOException { 124 Configuration conf = new Configuration(); 125 // Tell hadoop to use MarkerCreationFailFileSystem instead of the default LocalFileSystem 126 // whenever a file:// URI is resolved 127 conf.setClass("fs.file.impl", MarkerCreationFailFileSystem.class, FileSystem.class); 128 // Disable cache to force Hadoop to instantiate a fresh filesystem. If LocalFileSystem is 129 // cached, then Hadoop will use that instead of MarkerCreationFailFileSystem 130 conf.setBoolean("fs.file.impl.disable.cache", true); 131 132 String backupRootDir = tempDir.toUri().toString(); 133 String backupId = "backup_fail_marker"; 134 135 // Create the backup directory so the path is valid 136 Path backupDir = HBackupFileSystem.getBackupPath(backupRootDir, backupId); 137 FileSystem realFs = FileSystem.getLocal(new Configuration()); 138 realFs.mkdirs(backupDir); 139 140 // This should NOT throw -- marker creation is best-effort 141 HBackupFileSystem.writeBackupCompleteMarker(conf, backupRootDir, backupId); 142 143 // Marker should not exist since create() was intercepted 144 assertBackupCompleteFileDoesNotExist(new Configuration(), backupRootDir, backupId); 145 } 146 147 @Test 148 public void testDeleteBackupCompleteMarker(@TempDir java.nio.file.Path tempDir) 149 throws IOException { 150 Configuration conf = new Configuration(); 151 String backupRootDir = tempDir.toUri().toString(); 152 String backupId = "backup_delete_marker"; 153 154 HBackupFileSystem.writeBackupCompleteMarker(conf, backupRootDir, backupId); 155 assertBackupCompleteFileExists(conf, backupRootDir, backupId); 156 157 HBackupFileSystem.deleteBackupCompleteMarker(conf, backupRootDir, backupId); 158 assertBackupCompleteFileDoesNotExist(conf, backupRootDir, backupId); 159 160 // Deleting a marker that does not exist is a no-op 161 HBackupFileSystem.deleteBackupCompleteMarker(conf, backupRootDir, backupId); 162 } 163 164 // Verifies a failure to delete the marker is surfaced, so callers do not go on to delete backup 165 // data while leaving a marker behind 166 @Test 167 public void testDeleteBackupCompleteMarkerFailureThrows(@TempDir java.nio.file.Path tempDir) 168 throws IOException { 169 String backupRootDir = tempDir.toUri().toString(); 170 String backupId = "backup_delete_marker_fail"; 171 HBackupFileSystem.writeBackupCompleteMarker(new Configuration(), backupRootDir, backupId); 172 173 Configuration conf = new Configuration(); 174 conf.setClass("fs.file.impl", MarkerDeletionFailFileSystem.class, FileSystem.class); 175 conf.setBoolean("fs.file.impl.disable.cache", true); 176 177 assertThrows(IOException.class, 178 () -> HBackupFileSystem.deleteBackupCompleteMarker(conf, backupRootDir, backupId)); 179 assertBackupCompleteFileExists(new Configuration(), backupRootDir, backupId); 180 } 181 182 // Verifies backup delete cleanup aborts without deleting any data if the marker cannot be 183 // removed, and propagates the failure so the caller does not delete the backup's metadata 184 @Test 185 public void testBackupDataCleanupAbortsWhenMarkerDeleteFails(@TempDir java.nio.file.Path tempDir) 186 throws IOException { 187 String backupRootDir = tempDir.toUri().toString(); 188 BackupInfo info = createBackupWithMarkerAndData(backupRootDir, "backup_cleanup_marker_fail"); 189 190 assertThrows(IOException.class, 191 () -> BackupUtils.cleanupBackupData(info, createMarkerDeletionFailConf())); 192 assertMarkerAndDataRemain(backupRootDir, info); 193 } 194 195 // Verifies failed-backup recovery aborts before any destructive step if the marker cannot be 196 // removed: no snapshots are deleted, the backup system table is not restored (which would remove 197 // the RUNNING backup record), and no backup data is deleted. This leaves the failed backup 198 // discoverable so repair can retry. 199 @Test 200 public void testFailedBackupRecoveryAbortsBeforeDestructiveStepsWhenMarkerDeleteFails( 201 @TempDir java.nio.file.Path tempDir) throws IOException { 202 String backupRootDir = tempDir.toUri().toString(); 203 BackupInfo info = createBackupWithMarkerAndData(backupRootDir, "backup_failed_marker_fail"); 204 info.setPhase(BackupPhase.SNAPSHOTCOPY); 205 Connection conn = mock(Connection.class); 206 207 assertThrows(IOException.class, () -> TableBackupClient.cleanupAndRestoreBackupSystem(conn, 208 info, createMarkerDeletionFailConf())); 209 verifyNoInteractions(conn); 210 assertMarkerAndDataRemain(backupRootDir, info); 211 } 212 213 private static BackupInfo createBackupWithMarkerAndData(String backupRootDir, String backupId) 214 throws IOException { 215 TableName table = TableName.valueOf("test_table"); 216 BackupInfo info = 217 new BackupInfo(backupId, BackupType.FULL, new TableName[] { table }, backupRootDir); 218 Path tableBackupDir = 219 new Path(HBackupFileSystem.getTableBackupDir(backupRootDir, backupId, table)); 220 FileSystem fs = FileSystem.getLocal(new Configuration()); 221 fs.mkdirs(tableBackupDir); 222 fs.create(new Path(tableBackupDir, "data")).close(); 223 HBackupFileSystem.writeBackupCompleteMarker(new Configuration(), backupRootDir, backupId); 224 assertBackupCompleteFileExists(new Configuration(), backupRootDir, backupId); 225 return info; 226 } 227 228 private static void assertMarkerAndDataRemain(String backupRootDir, BackupInfo info) 229 throws IOException { 230 Configuration conf = new Configuration(); 231 assertBackupCompleteFileExists(conf, backupRootDir, info.getBackupId()); 232 for (TableName table : info.getTableNames()) { 233 Path tableBackupDir = 234 new Path(HBackupFileSystem.getTableBackupDir(backupRootDir, info.getBackupId(), table)); 235 assertTrue(FileSystem.getLocal(conf).exists(new Path(tableBackupDir, "data"))); 236 } 237 } 238 239 private static Configuration createMarkerDeletionFailConf() { 240 Configuration conf = new Configuration(); 241 conf.setClass("fs.file.impl", MarkerDeletionFailFileSystem.class, FileSystem.class); 242 conf.setBoolean("fs.file.impl.disable.cache", true); 243 return conf; 244 } 245 246 public static class MarkerDeletionFailFileSystem extends FilterFileSystem { 247 public MarkerDeletionFailFileSystem() { 248 super(new LocalFileSystem()); 249 } 250 251 @Override 252 public boolean delete(Path f, boolean recursive) throws IOException { 253 if (f.getName().equals(HBackupFileSystem.BACKUP_COMPLETE_FILE_NAME)) { 254 return false; 255 } 256 return super.delete(f, recursive); 257 } 258 } 259 260 public static class MarkerCreationFailFileSystem extends FilterFileSystem { 261 public MarkerCreationFailFileSystem() { 262 super(new LocalFileSystem()); 263 } 264 265 @Override 266 public FSDataOutputStream create(Path f, boolean overwrite) throws IOException { 267 if (f.getName().equals(HBackupFileSystem.BACKUP_COMPLETE_FILE_NAME)) { 268 throw new IOException("Injected failure for marker creation test"); 269 } 270 return super.create(f, overwrite); 271 } 272 } 273}