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.master; 019 020import static org.junit.Assert.assertEquals; 021 022import java.util.HashMap; 023import java.util.HashSet; 024import java.util.Map; 025import java.util.Set; 026import org.apache.hadoop.hbase.HBaseTestingUtil; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.backup.impl.BackupSystemTable; 029import org.apache.hadoop.hbase.client.Admin; 030import org.apache.hadoop.hbase.testclassification.MasterTests; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.junit.AfterClass; 033import org.junit.BeforeClass; 034import org.junit.Test; 035import org.junit.experimental.categories.Category; 036 037@Category({ MasterTests.class, MediumTests.class }) 038public class TestRestoreBackupSystemTable { 039 private static final String BACKUP_ROOT = "root"; 040 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 041 042 @BeforeClass 043 public static void setUp() throws Exception { 044 UTIL.startMiniCluster(); 045 } 046 047 @Test 048 public void itRestoresFromSnapshot() throws Exception { 049 BackupSystemTable table = new BackupSystemTable(UTIL.getConnection()); 050 Set<TableName> tables = new HashSet<>(); 051 052 tables.add(TableName.valueOf("test1")); 053 tables.add(TableName.valueOf("test2")); 054 tables.add(TableName.valueOf("test3")); 055 056 Map<String, Long> rsTimestampMap = new HashMap<>(); 057 rsTimestampMap.put("rs1:100", 100L); 058 rsTimestampMap.put("rs2:100", 101L); 059 rsTimestampMap.put("rs3:100", 103L); 060 061 table.writeRegionServerLogTimestamp(tables, rsTimestampMap, BACKUP_ROOT); 062 BackupSystemTable.snapshot(UTIL.getConnection()); 063 064 Admin admin = UTIL.getAdmin(); 065 TableName backupSystemTn = BackupSystemTable.getTableName(UTIL.getConfiguration()); 066 admin.disableTable(backupSystemTn); 067 admin.truncateTable(backupSystemTn, true); 068 069 BackupSystemTable.restoreFromSnapshot(UTIL.getConnection()); 070 Map<TableName, Map<String, Long>> results = table.readLogTimestampMap(BACKUP_ROOT); 071 072 assertEquals(results.size(), tables.size()); 073 074 for (TableName tableName : tables) { 075 Map<String, Long> resultMap = results.get(tableName); 076 assertEquals(resultMap, rsTimestampMap); 077 } 078 } 079 080 @AfterClass 081 public static void tearDown() throws Exception { 082 UTIL.shutdownMiniCluster(); 083 } 084}