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.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.HashSet;
024import java.util.List;
025import java.util.Set;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl;
029import org.apache.hadoop.hbase.client.Admin;
030import org.apache.hadoop.hbase.client.Connection;
031import org.apache.hadoop.hbase.client.ConnectionFactory;
032import org.apache.hadoop.hbase.client.Put;
033import org.apache.hadoop.hbase.client.Table;
034import org.apache.hadoop.hbase.testclassification.LargeTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.junit.Assert;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
044
045/**
046 * Create multiple backups for two tables: table1, table2 then perform 1 delete
047 */
048@Category(LargeTests.class)
049public class TestBackupMultipleDeletes extends TestBackupBase {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestBackupMultipleDeletes.class);
054
055  private static final Logger LOG = LoggerFactory.getLogger(TestBackupMultipleDeletes.class);
056
057  @Test
058  public void testBackupMultipleDeletes() throws Exception {
059    // #1 - create full backup for all tables
060    LOG.info("create full backup image for all tables");
061    List<TableName> tables = Lists.newArrayList(table1, table2);
062    Connection conn = ConnectionFactory.createConnection(conf1);
063    Admin admin = conn.getAdmin();
064    BackupAdmin client = new BackupAdminImpl(conn);
065    BackupRequest request = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);
066    String backupIdFull = client.backupTables(request);
067    assertTrue(checkSucceeded(backupIdFull));
068    // #2 - insert some data to table table1
069    Table t1 = conn.getTable(table1);
070    Put p1;
071    for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
072      p1 = new Put(Bytes.toBytes("row-t1" + i));
073      p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
074      t1.put(p1);
075    }
076    Assert.assertEquals(TEST_UTIL.countRows(t1), NB_ROWS_IN_BATCH * 2);
077    t1.close();
078    // #3 - incremental backup for table1
079    tables = Lists.newArrayList(table1);
080    request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
081    String backupIdInc1 = client.backupTables(request);
082    assertTrue(checkSucceeded(backupIdInc1));
083    // #4 - insert some data to table table2
084    Table t2 = conn.getTable(table2);
085    Put p2 = null;
086    for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
087      p2 = new Put(Bytes.toBytes("row-t2" + i));
088      p2.addColumn(famName, qualName, Bytes.toBytes("val" + i));
089      t2.put(p2);
090    }
091    // #5 - incremental backup for table1, table2
092    tables = Lists.newArrayList(table1, table2);
093    request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
094    String backupIdInc2 = client.backupTables(request);
095    assertTrue(checkSucceeded(backupIdInc2));
096    // #6 - insert some data to table table1
097    t1 = conn.getTable(table1);
098    for (int i = NB_ROWS_IN_BATCH; i < 2 * NB_ROWS_IN_BATCH; i++) {
099      p1 = new Put(Bytes.toBytes("row-t1" + i));
100      p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
101      t1.put(p1);
102    }
103    // #7 - incremental backup for table1
104    tables = Lists.newArrayList(table1);
105    request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
106    String backupIdInc3 = client.backupTables(request);
107    assertTrue(checkSucceeded(backupIdInc3));
108    // #8 - insert some data to table table2
109    t2 = conn.getTable(table2);
110    for (int i = NB_ROWS_IN_BATCH; i < 2 * NB_ROWS_IN_BATCH; i++) {
111      p2 = new Put(Bytes.toBytes("row-t1" + i));
112      p2.addColumn(famName, qualName, Bytes.toBytes("val" + i));
113      t2.put(p2);
114    }
115    // #9 - incremental backup for table1, table2
116    tables = Lists.newArrayList(table1, table2);
117    request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
118    String backupIdInc4 = client.backupTables(request);
119    assertTrue(checkSucceeded(backupIdInc4));
120    // #10 full backup for table3
121    tables = Lists.newArrayList(table3);
122    request = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);
123    String backupIdFull2 = client.backupTables(request);
124    assertTrue(checkSucceeded(backupIdFull2));
125    // #11 - incremental backup for table3
126    tables = Lists.newArrayList(table3);
127    request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
128    String backupIdInc5 = client.backupTables(request);
129    assertTrue(checkSucceeded(backupIdInc5));
130    LOG.error("Delete backupIdInc2");
131    client.deleteBackups(new String[] { backupIdInc2 });
132    LOG.error("Delete backupIdInc2 done");
133    List<BackupInfo> list = client.getHistory(100);
134    // First check number of backup images before and after
135    assertEquals(4, list.size());
136    // then verify that no backupIdInc2,3,4
137    Set<String> ids = new HashSet<String>();
138    ids.add(backupIdInc2);
139    ids.add(backupIdInc3);
140    ids.add(backupIdInc4);
141    for (BackupInfo info : list) {
142      String backupId = info.getBackupId();
143      if (ids.contains(backupId)) {
144        assertTrue(false);
145      }
146    }
147    // Verify that backupInc5 contains only table3
148    boolean found = false;
149    for (BackupInfo info : list) {
150      String backupId = info.getBackupId();
151      if (backupId.equals(backupIdInc5)) {
152        assertTrue(info.getTables().size() == 1);
153        assertEquals(table3, info.getTableNames().get(0));
154        found = true;
155      }
156    }
157    assertTrue(found);
158    admin.close();
159    conn.close();
160  }
161
162}