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
.assertFalse
;
21 import static org
.junit
.Assert
.assertTrue
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Collection
;
25 import java
.util
.List
;
26 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
27 import org
.apache
.hadoop
.hbase
.HBaseTestingUtility
;
28 import org
.apache
.hadoop
.hbase
.TableName
;
29 import org
.apache
.hadoop
.hbase
.backup
.BackupInfo
.BackupState
;
30 import org
.apache
.hadoop
.hbase
.backup
.impl
.BackupAdminImpl
;
31 import org
.apache
.hadoop
.hbase
.backup
.impl
.BackupSystemTable
;
32 import org
.apache
.hadoop
.hbase
.backup
.impl
.TableBackupClient
;
33 import org
.apache
.hadoop
.hbase
.backup
.impl
.TableBackupClient
.Stage
;
34 import org
.apache
.hadoop
.hbase
.client
.Admin
;
35 import org
.apache
.hadoop
.hbase
.client
.ColumnFamilyDescriptorBuilder
;
36 import org
.apache
.hadoop
.hbase
.client
.Connection
;
37 import org
.apache
.hadoop
.hbase
.client
.ConnectionFactory
;
38 import org
.apache
.hadoop
.hbase
.client
.Put
;
39 import org
.apache
.hadoop
.hbase
.client
.Table
;
40 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
41 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
42 import org
.apache
.hadoop
.util
.ToolRunner
;
43 import org
.junit
.Assert
;
44 import org
.junit
.ClassRule
;
45 import org
.junit
.Test
;
46 import org
.junit
.experimental
.categories
.Category
;
47 import org
.junit
.runner
.RunWith
;
48 import org
.junit
.runners
.Parameterized
;
49 import org
.slf4j
.Logger
;
50 import org
.slf4j
.LoggerFactory
;
52 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.collect
.Lists
;
54 @Category(LargeTests
.class)
55 @RunWith(Parameterized
.class)
56 public class TestIncrementalBackupWithFailures
extends TestBackupBase
{
59 public static final HBaseClassTestRule CLASS_RULE
=
60 HBaseClassTestRule
.forClass(TestIncrementalBackupWithFailures
.class);
62 private static final Logger LOG
=
63 LoggerFactory
.getLogger(TestIncrementalBackupWithFailures
.class);
65 @Parameterized.Parameters
66 public static Collection
<Object
[]> data() {
67 provider
= "multiwal";
68 List
<Object
[]> params
= new ArrayList
<Object
[]>();
69 params
.add(new Object
[] { Boolean
.TRUE
});
73 public TestIncrementalBackupWithFailures(Boolean b
) {
76 // implement all test cases in 1 test since incremental backup/restore has dependencies
78 public void testIncBackupRestore() throws Exception
{
81 // #1 - create full backup for all tables
82 LOG
.info("create full backup image for all tables");
84 List
<TableName
> tables
= Lists
.newArrayList(table1
, table2
);
85 final byte[] fam3Name
= Bytes
.toBytes("f3");
86 table1Desc
.setColumnFamily(
87 new ColumnFamilyDescriptorBuilder
.ModifyableColumnFamilyDescriptor(fam3Name
));
88 HBaseTestingUtility
.modifyTableSync(TEST_UTIL
.getAdmin(), table1Desc
);
90 Connection conn
= ConnectionFactory
.createConnection(conf1
);
92 insertIntoTable(conn
, table1
, fam3Name
, 3, NB_ROWS_FAM3
).close();
94 Admin admin
= conn
.getAdmin();
95 BackupAdminImpl client
= new BackupAdminImpl(conn
);
97 BackupRequest request
= createBackupRequest(BackupType
.FULL
, tables
, BACKUP_ROOT_DIR
);
98 String backupIdFull
= client
.backupTables(request
);
100 assertTrue(checkSucceeded(backupIdFull
));
102 // #2 - insert some data to table
103 Table t1
= insertIntoTable(conn
, table1
, famName
, 1, ADD_ROWS
);
104 LOG
.debug("writing " + ADD_ROWS
+ " rows to " + table1
);
106 Assert
.assertEquals(TEST_UTIL
.countRows(t1
), NB_ROWS_IN_BATCH
+ ADD_ROWS
+ NB_ROWS_FAM3
);
108 LOG
.debug("written " + ADD_ROWS
+ " rows to " + table1
);
110 Table t2
= conn
.getTable(table2
);
112 for (int i
= 0; i
< 5; i
++) {
113 p2
= new Put(Bytes
.toBytes("row-t2" + i
));
114 p2
.addColumn(famName
, qualName
, Bytes
.toBytes("val" + i
));
118 Assert
.assertEquals(TEST_UTIL
.countRows(t2
), NB_ROWS_IN_BATCH
+ 5);
120 LOG
.debug("written " + 5 + " rows to " + table2
);
122 // #3 - incremental backup for multiple tables
123 incrementalBackupWithFailures();
131 private void incrementalBackupWithFailures() throws Exception
{
132 conf1
.set(TableBackupClient
.BACKUP_CLIENT_IMPL_CLASS
,
133 IncrementalTableBackupClientForTest
.class.getName());
134 int maxStage
= Stage
.values().length
-1;
135 // Fail stages between 0 and 4 inclusive
136 for (int stage
= 0; stage
<= maxStage
; stage
++) {
137 LOG
.info("Running stage " + stage
);
138 runBackupAndFailAtStage(stage
);
142 private void runBackupAndFailAtStage(int stage
) throws Exception
{
144 conf1
.setInt(FullTableBackupClientForTest
.BACKUP_TEST_MODE_STAGE
, stage
);
145 try (BackupSystemTable table
= new BackupSystemTable(TEST_UTIL
.getConnection())) {
146 int before
= table
.getBackupHistory().size();
148 new String
[] { "create", "incremental", BACKUP_ROOT_DIR
, "-t",
149 table1
.getNameAsString() + "," + table2
.getNameAsString() };
151 int ret
= ToolRunner
.run(conf1
, new BackupDriver(), args
);
152 assertFalse(ret
== 0);
153 List
<BackupInfo
> backups
= table
.getBackupHistory();
154 int after
= table
.getBackupHistory().size();
156 assertTrue(after
== before
+1);
157 for (BackupInfo data
: backups
) {
158 if(data
.getType() == BackupType
.FULL
) {
159 assertTrue(data
.getState() == BackupState
.COMPLETE
);
161 assertTrue(data
.getState() == BackupState
.FAILED
);