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.client;
019
020import static org.junit.Assert.assertArrayEquals;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertNotNull;
023import static org.junit.Assert.assertTrue;
024
025import java.io.IOException;
026import java.util.Collections;
027import java.util.List;
028import java.util.regex.Pattern;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.testclassification.ClientTests;
032import org.apache.hadoop.hbase.testclassification.LargeTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.After;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.junit.runner.RunWith;
039import org.junit.runners.Parameterized;
040
041@RunWith(Parameterized.class)
042@Category({ LargeTests.class, ClientTests.class })
043public class TestAsyncSnapshotAdminApi extends TestAsyncAdminBase {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestAsyncSnapshotAdminApi.class);
048
049  private static final Pattern MATCH_ALL = Pattern.compile(".*");
050
051  String snapshotName1 = "snapshotName1";
052  String snapshotName2 = "snapshotName2";
053  String snapshotName3 = "snapshotName3";
054
055  @After
056  public void cleanup() throws Exception {
057    admin.deleteSnapshots(MATCH_ALL).get();
058    admin.listTableNames().get().forEach(t -> admin.disableTable(t).join());
059    admin.listTableNames().get().forEach(t -> admin.deleteTable(t).join());
060  }
061
062  @Test
063  public void testTakeSnapshot() throws Exception {
064    Admin syncAdmin = TEST_UTIL.getAdmin();
065
066    Table table = TEST_UTIL.createTable(tableName, Bytes.toBytes("f1"));
067    for (int i = 0; i < 3000; i++) {
068      table.put(new Put(Bytes.toBytes(i)).addColumn(Bytes.toBytes("f1"), Bytes.toBytes("cq"),
069        Bytes.toBytes(i)));
070    }
071
072    admin.snapshot(snapshotName1, tableName).get();
073    admin.snapshot(snapshotName2, tableName).get();
074    List<SnapshotDescription> snapshots = syncAdmin.listSnapshots();
075    Collections.sort(snapshots, (snap1, snap2) -> {
076      assertNotNull(snap1);
077      assertNotNull(snap1.getName());
078      assertNotNull(snap2);
079      assertNotNull(snap2.getName());
080      return snap1.getName().compareTo(snap2.getName());
081    });
082
083    assertEquals(snapshotName1, snapshots.get(0).getName());
084    assertEquals(tableName, snapshots.get(0).getTableName());
085    assertEquals(SnapshotType.FLUSH, snapshots.get(0).getType());
086    assertEquals(snapshotName2, snapshots.get(1).getName());
087    assertEquals(tableName, snapshots.get(1).getTableName());
088    assertEquals(SnapshotType.FLUSH, snapshots.get(1).getType());
089  }
090
091  @Test
092  public void testCloneSnapshot() throws Exception {
093    TableName tableName2 = TableName.valueOf("testCloneSnapshot2");
094    Admin syncAdmin = TEST_UTIL.getAdmin();
095
096    Table table = TEST_UTIL.createTable(tableName, Bytes.toBytes("f1"));
097    for (int i = 0; i < 3000; i++) {
098      table.put(new Put(Bytes.toBytes(i)).addColumn(Bytes.toBytes("f1"), Bytes.toBytes("cq"),
099        Bytes.toBytes(i)));
100    }
101
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());
108
109    // cloneSnapshot into a existed table.
110    boolean failed = false;
111    try {
112      admin.cloneSnapshot(snapshotName1, tableName).get();
113    } catch (Exception e) {
114      failed = true;
115    }
116    assertTrue(failed);
117
118    // cloneSnapshot into a new table.
119    assertTrue(!syncAdmin.tableExists(tableName2));
120    admin.cloneSnapshot(snapshotName1, tableName2).get();
121    syncAdmin.tableExists(tableName2);
122  }
123
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)) {
128        Result result;
129        int rowCount = 0;
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));
134          rowCount += 1;
135        }
136        assertEquals(rowCount, expectedRowCount);
137      }
138    }
139  }
140
141  @Test
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"),
146        Bytes.toBytes(i)));
147    }
148    assertEquals(0, admin.listSnapshots().get().size());
149
150    admin.snapshot(snapshotName1, tableName).get();
151    admin.snapshot(snapshotName2, tableName).get();
152    assertEquals(2, admin.listSnapshots().get().size());
153
154    admin.disableTable(tableName).get();
155    admin.restoreSnapshot(snapshotName1, true).get();
156    admin.enableTable(tableName).get();
157    assertResult(tableName, 3000);
158
159    admin.disableTable(tableName).get();
160    admin.restoreSnapshot(snapshotName2, false).get();
161    admin.enableTable(tableName).get();
162    assertResult(tableName, 3000);
163  }
164
165  @Test
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"),
170        Bytes.toBytes(i)));
171    }
172    assertEquals(0, admin.listSnapshots().get().size());
173
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());
178
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,
184      admin.listTableSnapshots(Pattern.compile("testListSnapshots"), Pattern.compile("s(.*)")).get()
185        .size());
186    assertEquals(0,
187      admin.listTableSnapshots(Pattern.compile("fakeTableName"), Pattern.compile("snap(.*)")).get()
188        .size());
189    assertEquals(2,
190      admin.listTableSnapshots(Pattern.compile("test(.*)"), Pattern.compile("snap(.*)[1|3]")).get()
191        .size());
192  }
193
194  @Test
195  public void testDeleteSnapshots() throws Exception {
196    Table table = TEST_UTIL.createTable(tableName, Bytes.toBytes("f1"));
197    for (int i = 0; i < 3000; i++) {
198      table.put(new Put(Bytes.toBytes(i)).addColumn(Bytes.toBytes("f1"), Bytes.toBytes("cq"),
199        Bytes.toBytes(i)));
200    }
201    assertEquals(0, admin.listSnapshots().get().size());
202
203    admin.snapshot(snapshotName1, tableName).get();
204    admin.snapshot(snapshotName2, tableName).get();
205    admin.snapshot(snapshotName3, tableName).get();
206    assertEquals(3, admin.listSnapshots().get().size());
207
208    admin.deleteSnapshot(snapshotName1).get();
209    assertEquals(2, admin.listSnapshots().get().size());
210
211    admin.deleteSnapshots(Pattern.compile("(.*)abc")).get();
212    assertEquals(2, admin.listSnapshots().get().size());
213
214    admin.deleteSnapshots(Pattern.compile("(.*)1")).get();
215    assertEquals(2, admin.listSnapshots().get().size());
216
217    admin.deleteTableSnapshots(Pattern.compile("(.*)"), Pattern.compile("(.*)1")).get();
218    assertEquals(2, admin.listSnapshots().get().size());
219
220    admin.deleteTableSnapshots(Pattern.compile("(.*)"), Pattern.compile("(.*)2")).get();
221    assertEquals(1, admin.listSnapshots().get().size());
222
223    admin.deleteTableSnapshots(Pattern.compile("(.*)"), Pattern.compile("(.*)3")).get();
224    assertEquals(0, admin.listSnapshots().get().size());
225  }
226}