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
.client
;
20 import static org
.junit
.Assert
.assertArrayEquals
;
21 import static org
.junit
.Assert
.assertEquals
;
22 import static org
.junit
.Assert
.assertNotNull
;
23 import static org
.junit
.Assert
.assertTrue
;
25 import java
.io
.IOException
;
26 import java
.util
.Collections
;
27 import java
.util
.List
;
28 import java
.util
.regex
.Pattern
;
29 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
30 import org
.apache
.hadoop
.hbase
.TableName
;
31 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
32 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
33 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
34 import org
.junit
.After
;
35 import org
.junit
.ClassRule
;
36 import org
.junit
.Test
;
37 import org
.junit
.experimental
.categories
.Category
;
38 import org
.junit
.runner
.RunWith
;
39 import org
.junit
.runners
.Parameterized
;
41 @RunWith(Parameterized
.class)
42 @Category({ LargeTests
.class, ClientTests
.class })
43 public class TestAsyncSnapshotAdminApi
extends TestAsyncAdminBase
{
46 public static final HBaseClassTestRule CLASS_RULE
=
47 HBaseClassTestRule
.forClass(TestAsyncSnapshotAdminApi
.class);
49 private static final Pattern MATCH_ALL
= Pattern
.compile(".*");
51 String snapshotName1
= "snapshotName1";
52 String snapshotName2
= "snapshotName2";
53 String snapshotName3
= "snapshotName3";
56 public void cleanup() throws Exception
{
57 admin
.deleteSnapshots(MATCH_ALL
).get();
58 admin
.listTableNames().get().forEach(t
-> admin
.disableTable(t
).join());
59 admin
.listTableNames().get().forEach(t
-> admin
.deleteTable(t
).join());
63 public void testTakeSnapshot() throws Exception
{
64 Admin syncAdmin
= TEST_UTIL
.getAdmin();
66 Table table
= TEST_UTIL
.createTable(tableName
, Bytes
.toBytes("f1"));
67 for (int i
= 0; i
< 3000; i
++) {
68 table
.put(new Put(Bytes
.toBytes(i
)).addColumn(Bytes
.toBytes("f1"), Bytes
.toBytes("cq"),
72 admin
.snapshot(snapshotName1
, tableName
).get();
73 admin
.snapshot(snapshotName2
, tableName
).get();
74 List
<SnapshotDescription
> snapshots
= syncAdmin
.listSnapshots();
75 Collections
.sort(snapshots
, (snap1
, snap2
) -> {
77 assertNotNull(snap1
.getName());
79 assertNotNull(snap2
.getName());
80 return snap1
.getName().compareTo(snap2
.getName());
83 assertEquals(snapshotName1
, snapshots
.get(0).getName());
84 assertEquals(tableName
, snapshots
.get(0).getTableName());
85 assertEquals(SnapshotType
.FLUSH
, snapshots
.get(0).getType());
86 assertEquals(snapshotName2
, snapshots
.get(1).getName());
87 assertEquals(tableName
, snapshots
.get(1).getTableName());
88 assertEquals(SnapshotType
.FLUSH
, snapshots
.get(1).getType());
92 public void testCloneSnapshot() throws Exception
{
93 TableName tableName2
= TableName
.valueOf("testCloneSnapshot2");
94 Admin syncAdmin
= TEST_UTIL
.getAdmin();
96 Table table
= TEST_UTIL
.createTable(tableName
, Bytes
.toBytes("f1"));
97 for (int i
= 0; i
< 3000; i
++) {
98 table
.put(new Put(Bytes
.toBytes(i
)).addColumn(Bytes
.toBytes("f1"), Bytes
.toBytes("cq"),
102 admin
.snapshot(snapshotName1
, tableName
).get();
103 List
<SnapshotDescription
> snapshots
= syncAdmin
.listSnapshots();
104 assertEquals(1, snapshots
.size());
105 assertEquals(snapshotName1
, snapshots
.get(0).getName());
106 assertEquals(tableName
, snapshots
.get(0).getTableName());
107 assertEquals(SnapshotType
.FLUSH
, snapshots
.get(0).getType());
109 // cloneSnapshot into a existed table.
110 boolean failed
= false;
112 admin
.cloneSnapshot(snapshotName1
, tableName
).get();
113 } catch (Exception e
) {
118 // cloneSnapshot into a new table.
119 assertTrue(!syncAdmin
.tableExists(tableName2
));
120 admin
.cloneSnapshot(snapshotName1
, tableName2
).get();
121 syncAdmin
.tableExists(tableName2
);
124 private void assertResult(TableName tableName
, int expectedRowCount
) throws IOException
{
125 try (Table table
= TEST_UTIL
.getConnection().getTable(tableName
)) {
126 Scan scan
= new Scan();
127 try (ResultScanner scanner
= table
.getScanner(scan
)) {
130 while ((result
= scanner
.next()) != null) {
131 assertArrayEquals(result
.getRow(), Bytes
.toBytes(rowCount
));
132 assertArrayEquals(result
.getValue(Bytes
.toBytes("f1"), Bytes
.toBytes("cq")),
133 Bytes
.toBytes(rowCount
));
136 assertEquals(rowCount
, expectedRowCount
);
142 public void testRestoreSnapshot() throws Exception
{
143 Table table
= TEST_UTIL
.createTable(tableName
, Bytes
.toBytes("f1"));
144 for (int i
= 0; i
< 3000; i
++) {
145 table
.put(new Put(Bytes
.toBytes(i
)).addColumn(Bytes
.toBytes("f1"), Bytes
.toBytes("cq"),
148 assertEquals(0, admin
.listSnapshots().get().size());
150 admin
.snapshot(snapshotName1
, tableName
).get();
151 admin
.snapshot(snapshotName2
, tableName
).get();
152 assertEquals(2, admin
.listSnapshots().get().size());
154 admin
.disableTable(tableName
).get();
155 admin
.restoreSnapshot(snapshotName1
, true).get();
156 admin
.enableTable(tableName
).get();
157 assertResult(tableName
, 3000);
159 admin
.disableTable(tableName
).get();
160 admin
.restoreSnapshot(snapshotName2
, false).get();
161 admin
.enableTable(tableName
).get();
162 assertResult(tableName
, 3000);
166 public void testListSnapshots() throws Exception
{
167 Table table
= TEST_UTIL
.createTable(tableName
, Bytes
.toBytes("f1"));
168 for (int i
= 0; i
< 3000; i
++) {
169 table
.put(new Put(Bytes
.toBytes(i
)).addColumn(Bytes
.toBytes("f1"), Bytes
.toBytes("cq"),
172 assertEquals(0, admin
.listSnapshots().get().size());
174 admin
.snapshot(snapshotName1
, tableName
).get();
175 admin
.snapshot(snapshotName2
, tableName
).get();
176 admin
.snapshot(snapshotName3
, tableName
).get();
177 assertEquals(3, admin
.listSnapshots().get().size());
179 assertEquals(3, admin
.listSnapshots(Pattern
.compile("(.*)")).get().size());
180 assertEquals(3, admin
.listSnapshots(Pattern
.compile("snapshotName(\\d+)")).get().size());
181 assertEquals(2, admin
.listSnapshots(Pattern
.compile("snapshotName[1|3]")).get().size());
182 assertEquals(3, admin
.listSnapshots(Pattern
.compile("snapshot(.*)")).get().size());
183 assertEquals(3, admin
.listTableSnapshots(Pattern
.compile("testListSnapshots"),
184 Pattern
.compile("s(.*)")).get().size());
185 assertEquals(0, admin
.listTableSnapshots(Pattern
.compile("fakeTableName"),
186 Pattern
.compile("snap(.*)")).get().size());
187 assertEquals(2, admin
.listTableSnapshots(Pattern
.compile("test(.*)"),
188 Pattern
.compile("snap(.*)[1|3]")).get().size());
192 public void testDeleteSnapshots() throws Exception
{
193 Table table
= TEST_UTIL
.createTable(tableName
, Bytes
.toBytes("f1"));
194 for (int i
= 0; i
< 3000; i
++) {
195 table
.put(new Put(Bytes
.toBytes(i
)).addColumn(Bytes
.toBytes("f1"), Bytes
.toBytes("cq"),
198 assertEquals(0, admin
.listSnapshots().get().size());
200 admin
.snapshot(snapshotName1
, tableName
).get();
201 admin
.snapshot(snapshotName2
, tableName
).get();
202 admin
.snapshot(snapshotName3
, tableName
).get();
203 assertEquals(3, admin
.listSnapshots().get().size());
205 admin
.deleteSnapshot(snapshotName1
).get();
206 assertEquals(2, admin
.listSnapshots().get().size());
208 admin
.deleteSnapshots(Pattern
.compile("(.*)abc")).get();
209 assertEquals(2, admin
.listSnapshots().get().size());
211 admin
.deleteSnapshots(Pattern
.compile("(.*)1")).get();
212 assertEquals(2, admin
.listSnapshots().get().size());
214 admin
.deleteTableSnapshots(Pattern
.compile("(.*)"), Pattern
.compile("(.*)1")).get();
215 assertEquals(2, admin
.listSnapshots().get().size());
217 admin
.deleteTableSnapshots(Pattern
.compile("(.*)"), Pattern
.compile("(.*)2")).get();
218 assertEquals(1, admin
.listSnapshots().get().size());
220 admin
.deleteTableSnapshots(Pattern
.compile("(.*)"), Pattern
.compile("(.*)3")).get();
221 assertEquals(0, admin
.listSnapshots().get().size());