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.CONTINUOUS_BACKUP_REPLICATION_PEER; 021import static org.junit.jupiter.api.Assertions.assertEquals; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 024import java.io.IOException; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.fs.FileSystem; 027import org.apache.hadoop.fs.FilterFileSystem; 028import org.apache.hadoop.fs.Path; 029import org.apache.hadoop.hbase.HBaseTestingUtil; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl; 032import org.apache.hadoop.hbase.backup.impl.BackupManager; 033import org.apache.hadoop.hbase.client.Admin; 034import org.apache.hadoop.hbase.client.Connection; 035import org.apache.hadoop.hbase.client.ConnectionFactory; 036import org.apache.hadoop.hbase.replication.ReplicationPeerDescription; 037import org.apache.hadoop.hdfs.DistributedFileSystem; 038import org.apache.yetus.audience.InterfaceAudience; 039 040@InterfaceAudience.Private 041public class BackupTestUtil { 042 private BackupTestUtil() { 043 } 044 045 static BackupInfo verifyBackup(Configuration conf, String backupId, BackupType expectedType, 046 BackupInfo.BackupState expectedState) throws IOException { 047 try (Connection connection = ConnectionFactory.createConnection(conf); 048 BackupAdmin backupAdmin = new BackupAdminImpl(connection)) { 049 BackupInfo backupInfo = backupAdmin.getBackupInfo(backupId); 050 051 // Verify managed backup in HBase 052 assertEquals(backupId, backupInfo.getBackupId()); 053 assertEquals(expectedState, backupInfo.getState()); 054 assertEquals(expectedType, backupInfo.getType()); 055 return backupInfo; 056 } 057 } 058 059 public static void enableBackup(Configuration conf) { 060 conf.setBoolean(BackupRestoreConstants.BACKUP_ENABLE_KEY, true); 061 BackupManager.decorateMasterConfiguration(conf); 062 BackupManager.decorateRegionServerConfiguration(conf); 063 } 064 065 public static void verifyReplicationPeerSubscription(HBaseTestingUtil util, TableName tableName) 066 throws IOException { 067 try (Admin admin = util.getAdmin()) { 068 ReplicationPeerDescription peerDesc = admin.listReplicationPeers().stream() 069 .filter(peer -> peer.getPeerId().equals(CONTINUOUS_BACKUP_REPLICATION_PEER)).findFirst() 070 .orElseThrow(() -> new AssertionError("Replication peer not found")); 071 072 assertTrue(peerDesc.getPeerConfig().getTableCFsMap().containsKey(tableName), 073 "Table should be subscribed to the replication peer"); 074 } 075 } 076 077 /** 078 * Returns a copy of the given configuration whose HDFS file system implementation is 079 * {@link MarkerDeletionFailHDFS}, with file system caching disabled so the override takes effect. 080 */ 081 static Configuration createMarkerDeletionFailConf(Configuration base) { 082 Configuration conf = new Configuration(base); 083 conf.setClass("fs.hdfs.impl", MarkerDeletionFailHDFS.class, FileSystem.class); 084 conf.setBoolean("fs.hdfs.impl.disable.cache", true); 085 return conf; 086 } 087 088 /** 089 * A {@link DistributedFileSystem} wrapper that refuses to delete the backup completion marker 090 * file ({@link HBackupFileSystem#BACKUP_COMPLETE_FILE_NAME}). 091 */ 092 public static class MarkerDeletionFailHDFS extends FilterFileSystem { 093 public MarkerDeletionFailHDFS() { 094 super(new DistributedFileSystem()); 095 } 096 097 @Override 098 public boolean delete(Path f, boolean recursive) throws IOException { 099 if (f.getName().equals(HBackupFileSystem.BACKUP_COMPLETE_FILE_NAME)) { 100 return false; 101 } 102 return super.delete(f, recursive); 103 } 104 } 105}