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.impl; 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.any; 026import static org.mockito.ArgumentMatchers.eq; 027import static org.mockito.Mockito.mock; 028import static org.mockito.Mockito.mockConstruction; 029import static org.mockito.Mockito.mockStatic; 030import static org.mockito.Mockito.never; 031import static org.mockito.Mockito.verify; 032import static org.mockito.Mockito.when; 033 034import java.io.IOException; 035import java.util.ArrayList; 036import java.util.List; 037import org.apache.hadoop.conf.Configuration; 038import org.apache.hadoop.fs.Path; 039import org.apache.hadoop.hbase.TableName; 040import org.apache.hadoop.hbase.backup.BackupInfo; 041import org.apache.hadoop.hbase.backup.BackupInfo.BackupState; 042import org.apache.hadoop.hbase.backup.BackupType; 043import org.apache.hadoop.hbase.backup.HBackupFileSystem; 044import org.apache.hadoop.hbase.backup.PointInTimeRestoreRequest; 045import org.apache.hadoop.hbase.backup.impl.BackupManifest.BackupImage; 046import org.apache.hadoop.hbase.client.Connection; 047import org.apache.hadoop.hbase.testclassification.SmallTests; 048import org.junit.jupiter.api.BeforeEach; 049import org.junit.jupiter.api.Tag; 050import org.junit.jupiter.api.Test; 051import org.junit.jupiter.api.io.TempDir; 052import org.mockito.MockedConstruction; 053import org.mockito.MockedStatic; 054 055@Tag(SmallTests.TAG) 056public class TestCustomBackupLocationPitrRestoreHandler 057 extends TestAbstractPitrRestoreHandlerGetBackupMetadata { 058 protected static final String BACKUP_ROOT_DIR = "/backup/root"; 059 060 @BeforeEach 061 void setUp() { 062 mockConn = mock(Connection.class); 063 when(mockConn.getConfiguration()).thenReturn(new Configuration()); 064 request = new PointInTimeRestoreRequest.Builder().withBackupRootDir(BACKUP_ROOT_DIR).build(); 065 } 066 067 private BackupImage buildImage(String backupId, BackupType type, long startTime, 068 long completeTime) { 069 return BackupImage.newBuilder().withBackupId(backupId).withType(type) 070 .withRootDir(BACKUP_ROOT_DIR).withTableList(TABLE_LIST).withStartTime(startTime) 071 .withCompleteTime(completeTime).build(); 072 } 073 074 private static void stubBackupImages(MockedStatic<HBackupFileSystem> mockedHBFS, 075 List<BackupImage> images) { 076 mockedHBFS.when(() -> HBackupFileSystem.getAllBackupImages(any(Configuration.class), any())) 077 .thenReturn(images); 078 } 079 080 private static void stubMarker(MockedStatic<HBackupFileSystem> mockedHBFS, String backupId, 081 boolean present) { 082 mockedHBFS.when(() -> HBackupFileSystem.isBackupComplete(any(Configuration.class), 083 eq(BACKUP_ROOT_DIR), eq(backupId))).thenReturn(present); 084 } 085 086 /** 087 * Builds one backup image per state. Only backups in the {@link BackupState#COMPLETE} state get a 088 * backup completion marker. 089 */ 090 @Override 091 protected List<PitrBackupMetadata> callGetBackupMetadataWithBackupStates(BackupState... states) 092 throws IOException { 093 List<BackupImage> images = new ArrayList<>(); 094 try (MockedStatic<HBackupFileSystem> mockedHBFS = mockStatic(HBackupFileSystem.class)) { 095 for (int i = 0; i < states.length; i++) { 096 String backupId = "backup_" + i; 097 BackupType backupType = i == 0 ? BackupType.FULL : BackupType.INCREMENTAL; 098 images.add(buildImage(backupId, backupType, (i + 1) * 100L, (i + 1) * 100L + 100)); 099 stubMarker(mockedHBFS, backupId, states[i] == BackupState.COMPLETE); 100 } 101 stubBackupImages(mockedHBFS, images); 102 103 CustomBackupLocationPitrRestoreHandler handler = 104 new CustomBackupLocationPitrRestoreHandler(mockConn, request); 105 return handler.getBackupMetadata(request); 106 } 107 } 108 109 // Verify the user gets a detailed error message when every backup lacks the backup complete 110 // marker file during PITR 111 @Override 112 @Test 113 void testGetBackupMetadataReturnsEmptyWhenNoCompleteBackups() { 114 IOException e = assertThrows(IOException.class, 115 () -> callGetBackupMetadataWithBackupStates(BackupState.RUNNING)); 116 String message = "PITR failed: none of the 1 backup(s) in custom backup " 117 + "location /backup/root contain a .backup.complete marker file"; 118 assertTrue(e.getMessage().contains(message)); 119 } 120 121 @Test 122 public void testGetBackupMetadataReturnsEmptyWhenNoBackupsExist() throws IOException { 123 assertTrue(callGetBackupMetadataWithBackupStates().isEmpty()); 124 } 125 126 @Test 127 public void testGetBackupMetadataFiltersBackupsWithoutMarker() throws IOException { 128 BackupImage imageWithMarker = buildImage("backup_1", BackupType.FULL, 100L, 200L); 129 BackupImage imageWithoutMarker = buildImage("backup_2", BackupType.INCREMENTAL, 300L, 400L); 130 131 try (MockedStatic<HBackupFileSystem> mockedHBFS = mockStatic(HBackupFileSystem.class)) { 132 stubBackupImages(mockedHBFS, List.of(imageWithMarker, imageWithoutMarker)); 133 stubMarker(mockedHBFS, "backup_1", true); 134 stubMarker(mockedHBFS, "backup_2", false); 135 136 CustomBackupLocationPitrRestoreHandler handler = 137 new CustomBackupLocationPitrRestoreHandler(mockConn, request); 138 List<PitrBackupMetadata> result = handler.getBackupMetadata(request); 139 140 assertEquals(1, result.size()); 141 assertEquals("backup_1", result.get(0).getBackupId()); 142 } 143 } 144 145 // A failure to check for the completion marker must not be mistaken for an incomplete backup 146 @Test 147 public void testGetBackupMetadataPropagatesIOException() { 148 BackupImage completeImage = buildImage("backup_1", BackupType.FULL, 100L, 200L); 149 BackupImage errorImage = buildImage("backup_2", BackupType.INCREMENTAL, 300L, 400L); 150 151 try (MockedStatic<HBackupFileSystem> mockedHBFS = mockStatic(HBackupFileSystem.class)) { 152 stubBackupImages(mockedHBFS, List.of(completeImage, errorImage)); 153 stubMarker(mockedHBFS, "backup_1", true); 154 mockedHBFS.when(() -> HBackupFileSystem.isBackupComplete(any(Configuration.class), 155 eq(BACKUP_ROOT_DIR), eq("backup_2"))).thenThrow(new IOException("Connection lost")); 156 157 CustomBackupLocationPitrRestoreHandler handler = 158 new CustomBackupLocationPitrRestoreHandler(mockConn, request); 159 160 IOException e = assertThrows(IOException.class, () -> handler.getBackupMetadata(request)); 161 assertEquals("Connection lost", e.getMessage()); 162 } 163 } 164 165 // Backups in a custom location may have been created by another cluster, so the current 166 // cluster's BackupSystemTable must not be used to determine whether a backup is complete 167 @Test 168 public void testGetBackupMetadataDoesNotUseBackupSystemTable() throws IOException { 169 BackupImage image = buildImage("backup_1", BackupType.FULL, 100L, 200L); 170 171 try (MockedStatic<HBackupFileSystem> mockedHBFS = mockStatic(HBackupFileSystem.class); 172 MockedConstruction<BackupSystemTable> mockedSysTable = 173 mockConstruction(BackupSystemTable.class)) { 174 stubBackupImages(mockedHBFS, List.of(image)); 175 stubMarker(mockedHBFS, "backup_1", true); 176 177 CustomBackupLocationPitrRestoreHandler handler = 178 new CustomBackupLocationPitrRestoreHandler(mockConn, request); 179 List<PitrBackupMetadata> result = handler.getBackupMetadata(request); 180 181 assertEquals(1, result.size()); 182 assertEquals("backup_1", result.get(0).getBackupId()); 183 assertTrue(mockedSysTable.constructed().isEmpty(), 184 "BackupSystemTable should not be used for a custom backup location"); 185 } 186 } 187 188 // End-to-end check against a real file system: only the backup with a completion marker on disk 189 // is returned, with no BackupSystemTable involved 190 @Test 191 public void testGetBackupMetadataUsesMarkerFileOnDisk(@TempDir java.nio.file.Path tempDir) 192 throws IOException { 193 Configuration conf = new Configuration(); 194 String backupRootDir = new Path(tempDir.toUri()).toString(); 195 TableName[] tables = TABLE_LIST.toArray(new TableName[0]); 196 String completeBackupId = "backup_1000"; 197 String incompleteBackupId = "backup_2000"; 198 199 new BackupManifest(new BackupInfo(completeBackupId, BackupType.FULL, tables, backupRootDir)) 200 .store(conf); 201 new BackupManifest( 202 new BackupInfo(incompleteBackupId, BackupType.INCREMENTAL, tables, backupRootDir)) 203 .store(conf); 204 HBackupFileSystem.writeBackupCompleteMarker(conf, backupRootDir, completeBackupId); 205 206 when(mockConn.getConfiguration()).thenReturn(conf); 207 PointInTimeRestoreRequest diskRequest = 208 new PointInTimeRestoreRequest.Builder().withBackupRootDir(backupRootDir).build(); 209 210 try (MockedConstruction<BackupSystemTable> mockedSysTable = 211 mockConstruction(BackupSystemTable.class)) { 212 CustomBackupLocationPitrRestoreHandler handler = 213 new CustomBackupLocationPitrRestoreHandler(mockConn, diskRequest); 214 List<PitrBackupMetadata> result = handler.getBackupMetadata(diskRequest); 215 216 // Verify backup metadata for just COMPLETE backups are returned, and this can happen even 217 // if the cluster's BackupSystemTable is empty 218 assertEquals(1, result.size()); 219 assertEquals(completeBackupId, result.get(0).getBackupId()); 220 assertTrue(mockedSysTable.constructed().isEmpty()); 221 } 222 } 223 224 // Removing a table from a backup deletes its completion marker while the backup stays COMPLETE 225 // in the backup system table. The modified backup no longer matches its manifest, so it must no 226 // longer be used for PITR from a custom backup location. 227 @Test 228 public void testBackupNotUsedAfterTableRemovedFromBackup(@TempDir java.nio.file.Path tempDir) 229 throws IOException { 230 Configuration conf = new Configuration(); 231 String backupRootDir = new Path(tempDir.toUri()).toString(); 232 TableName removedTable = TableName.valueOf("removed_table"); 233 TableName keptTable = TableName.valueOf(TABLE_NAME); 234 String backupId = "backup_1000"; 235 236 BackupInfo info = new BackupInfo(backupId, BackupType.FULL, 237 new TableName[] { removedTable, keptTable }, backupRootDir); 238 info.setState(BackupState.COMPLETE); 239 new BackupManifest(info).store(conf); 240 HBackupFileSystem.writeBackupCompleteMarker(conf, backupRootDir, backupId); 241 assertBackupCompleteFileExists(conf, backupRootDir, backupId); 242 243 when(mockConn.getConfiguration()).thenReturn(conf); 244 PointInTimeRestoreRequest diskRequest = 245 new PointInTimeRestoreRequest.Builder().withBackupRootDir(backupRootDir).build(); 246 CustomBackupLocationPitrRestoreHandler handler = 247 new CustomBackupLocationPitrRestoreHandler(mockConn, diskRequest); 248 249 // Before the table is removed, the backup is usable 250 List<PitrBackupMetadata> before = handler.getBackupMetadata(diskRequest); 251 assertEquals(1, before.size()); 252 assertEquals(backupId, before.get(0).getBackupId()); 253 254 BackupSystemTable sysTable = mock(BackupSystemTable.class); 255 new BackupAdminImpl(mockConn).removeTableFromBackupImage(info, removedTable, sysTable); 256 257 // The backup is kept (and stays COMPLETE), but its completion marker is gone 258 verify(sysTable).updateBackupInfo(info); 259 verify(sysTable, never()).deleteBackupInfo(any()); 260 assertEquals(BackupState.COMPLETE, info.getState()); 261 assertBackupCompleteFileDoesNotExist(conf, backupRootDir, backupId); 262 263 // So the backup can no longer be used for PITR 264 IOException e = assertThrows(IOException.class, () -> handler.getBackupMetadata(diskRequest)); 265 assertTrue(e.getMessage().contains("PITR failed: none of the 1 backup(s) in custom backup " 266 + "location " + backupRootDir + " contain a .backup.complete marker file")); 267 } 268}