2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.backup
;
20 import static org
.junit
.Assert
.assertTrue
;
22 import java
.util
.List
;
23 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
24 import org
.apache
.hadoop
.hbase
.TableName
;
25 import org
.apache
.hadoop
.hbase
.backup
.impl
.BackupAdminImpl
;
26 import org
.apache
.hadoop
.hbase
.backup
.util
.BackupUtils
;
27 import org
.apache
.hadoop
.hbase
.client
.Admin
;
28 import org
.apache
.hadoop
.hbase
.client
.Connection
;
29 import org
.apache
.hadoop
.hbase
.client
.ConnectionFactory
;
30 import org
.apache
.hadoop
.hbase
.client
.Table
;
31 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
32 import org
.junit
.Assert
;
33 import org
.junit
.ClassRule
;
34 import org
.junit
.Test
;
35 import org
.junit
.experimental
.categories
.Category
;
36 import org
.slf4j
.Logger
;
37 import org
.slf4j
.LoggerFactory
;
39 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.collect
.Lists
;
41 @Category(LargeTests
.class)
42 public class TestBackupMerge
extends TestBackupBase
{
45 public static final HBaseClassTestRule CLASS_RULE
=
46 HBaseClassTestRule
.forClass(TestBackupMerge
.class);
48 private static final Logger LOG
=
49 LoggerFactory
.getLogger(TestBackupMerge
.class);
54 public void TestIncBackupMergeRestore() throws Exception
{
56 // #1 - create full backup for all tables
57 LOG
.info("create full backup image for all tables");
59 List
<TableName
> tables
= Lists
.newArrayList(table1
, table2
);
60 // Set custom Merge Job implementation
63 Connection conn
= ConnectionFactory
.createConnection(conf1
);
65 Admin admin
= conn
.getAdmin();
66 BackupAdminImpl client
= new BackupAdminImpl(conn
);
68 BackupRequest request
= createBackupRequest(BackupType
.FULL
, tables
, BACKUP_ROOT_DIR
);
69 String backupIdFull
= client
.backupTables(request
);
71 assertTrue(checkSucceeded(backupIdFull
));
73 // #2 - insert some data to table1
74 Table t1
= insertIntoTable(conn
, table1
, famName
, 1, ADD_ROWS
);
75 LOG
.debug("writing " + ADD_ROWS
+ " rows to " + table1
);
77 Assert
.assertEquals(TEST_UTIL
.countRows(t1
), NB_ROWS_IN_BATCH
+ ADD_ROWS
);
79 LOG
.debug("written " + ADD_ROWS
+ " rows to " + table1
);
81 Table t2
= insertIntoTable(conn
, table2
, famName
, 1, ADD_ROWS
);
83 Assert
.assertEquals(TEST_UTIL
.countRows(t2
), NB_ROWS_IN_BATCH
+ ADD_ROWS
);
85 LOG
.debug("written " + ADD_ROWS
+ " rows to " + table2
);
87 // #3 - incremental backup for multiple tables
88 tables
= Lists
.newArrayList(table1
, table2
);
89 request
= createBackupRequest(BackupType
.INCREMENTAL
, tables
, BACKUP_ROOT_DIR
);
90 String backupIdIncMultiple
= client
.backupTables(request
);
92 assertTrue(checkSucceeded(backupIdIncMultiple
));
94 t1
= insertIntoTable(conn
, table1
, famName
, 2, ADD_ROWS
);
97 t2
= insertIntoTable(conn
, table2
, famName
, 2, ADD_ROWS
);
100 // #3 - incremental backup for multiple tables
101 request
= createBackupRequest(BackupType
.INCREMENTAL
, tables
, BACKUP_ROOT_DIR
);
102 String backupIdIncMultiple2
= client
.backupTables(request
);
103 assertTrue(checkSucceeded(backupIdIncMultiple2
));
105 try (BackupAdmin bAdmin
= new BackupAdminImpl(conn
)) {
106 String
[] backups
= new String
[] { backupIdIncMultiple
, backupIdIncMultiple2
};
107 bAdmin
.mergeBackups(backups
);
110 // #6 - restore incremental backup for multiple tables, with overwrite
111 TableName
[] tablesRestoreIncMultiple
= new TableName
[] { table1
, table2
};
112 TableName
[] tablesMapIncMultiple
= new TableName
[] { table1_restore
, table2_restore
};
113 client
.restore(BackupUtils
.createRestoreRequest(BACKUP_ROOT_DIR
, backupIdIncMultiple2
, false,
114 tablesRestoreIncMultiple
, tablesMapIncMultiple
, true));
116 Table hTable
= conn
.getTable(table1_restore
);
117 LOG
.debug("After incremental restore: " + hTable
.getDescriptor());
118 int countRows
= TEST_UTIL
.countRows(hTable
, famName
);
119 LOG
.debug("f1 has " + countRows
+ " rows");
120 Assert
.assertEquals(NB_ROWS_IN_BATCH
+ 2 * ADD_ROWS
, countRows
);
124 hTable
= conn
.getTable(table2_restore
);
125 Assert
.assertEquals(TEST_UTIL
.countRows(hTable
), NB_ROWS_IN_BATCH
+ 2 * ADD_ROWS
);