View Javadoc

1   /**
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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import java.io.IOException;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.testclassification.LargeTests;
29  import org.apache.hadoop.hbase.NamespaceDescriptor;
30  import org.apache.hadoop.hbase.NamespaceNotFoundException;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
33  import org.apache.hadoop.hbase.snapshot.SnapshotDoesNotExistException;
34  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.junit.After;
37  import org.junit.AfterClass;
38  import org.junit.Before;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  /**
44   * Test clone snapshots from the client
45   */
46  @Category(LargeTests.class)
47  public class TestCloneSnapshotFromClient {
48    final Log LOG = LogFactory.getLog(getClass());
49  
50    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
51  
52    private final byte[] FAMILY = Bytes.toBytes("cf");
53  
54    private byte[] emptySnapshot;
55    private byte[] snapshotName0;
56    private byte[] snapshotName1;
57    private byte[] snapshotName2;
58    private int snapshot0Rows;
59    private int snapshot1Rows;
60    private TableName tableName;
61    private HBaseAdmin admin;
62  
63    @BeforeClass
64    public static void setUpBeforeClass() throws Exception {
65      TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
66      TEST_UTIL.getConfiguration().setBoolean("hbase.online.schema.update.enable", true);
67      TEST_UTIL.getConfiguration().setInt("hbase.hstore.compactionThreshold", 10);
68      TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
69      TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
70      TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
71      TEST_UTIL.getConfiguration().setBoolean(
72          "hbase.master.enabletable.roundrobin", true);
73      TEST_UTIL.startMiniCluster(3);
74    }
75  
76    @AfterClass
77    public static void tearDownAfterClass() throws Exception {
78      TEST_UTIL.shutdownMiniCluster();
79    }
80  
81    /**
82     * Initialize the tests with a table filled with some data
83     * and two snapshots (snapshotName0, snapshotName1) of different states.
84     * The tableName, snapshotNames and the number of rows in the snapshot are initialized.
85     */
86    @Before
87    public void setup() throws Exception {
88      this.admin = TEST_UTIL.getHBaseAdmin();
89  
90      long tid = System.currentTimeMillis();
91      tableName = TableName.valueOf("testtb-" + tid);
92      emptySnapshot = Bytes.toBytes("emptySnaptb-" + tid);
93      snapshotName0 = Bytes.toBytes("snaptb0-" + tid);
94      snapshotName1 = Bytes.toBytes("snaptb1-" + tid);
95      snapshotName2 = Bytes.toBytes("snaptb2-" + tid);
96  
97      // create Table and disable it
98      SnapshotTestingUtils.createTable(TEST_UTIL, tableName, FAMILY);
99      admin.disableTable(tableName);
100 
101     // take an empty snapshot
102     admin.snapshot(emptySnapshot, tableName);
103 
104     HTable table = new HTable(TEST_UTIL.getConfiguration(), tableName);
105     try {
106       // enable table and insert data
107       admin.enableTable(tableName);
108       SnapshotTestingUtils.loadData(TEST_UTIL, table, 500, FAMILY);
109       snapshot0Rows = TEST_UTIL.countRows(table);
110       admin.disableTable(tableName);
111 
112       // take a snapshot
113       admin.snapshot(snapshotName0, tableName);
114 
115       // enable table and insert more data
116       admin.enableTable(tableName);
117       SnapshotTestingUtils.loadData(TEST_UTIL, table, 500, FAMILY);
118       snapshot1Rows = TEST_UTIL.countRows(table);
119       admin.disableTable(tableName);
120 
121       // take a snapshot of the updated table
122       admin.snapshot(snapshotName1, tableName);
123 
124       // re-enable table
125       admin.enableTable(tableName);
126     } finally {
127       table.close();
128     }
129   }
130 
131   @After
132   public void tearDown() throws Exception {
133     if (admin.tableExists(tableName)) {
134       TEST_UTIL.deleteTable(tableName);
135     }
136     SnapshotTestingUtils.deleteAllSnapshots(admin);
137     SnapshotTestingUtils.deleteArchiveDirectory(TEST_UTIL);
138   }
139 
140   @Test(expected=SnapshotDoesNotExistException.class)
141   public void testCloneNonExistentSnapshot() throws IOException, InterruptedException {
142     String snapshotName = "random-snapshot-" + System.currentTimeMillis();
143     String tableName = "random-table-" + System.currentTimeMillis();
144     admin.cloneSnapshot(snapshotName, tableName);
145   }
146 
147   @Test(expected = NamespaceNotFoundException.class)
148   public void testCloneOnMissingNamespace() throws IOException, InterruptedException {
149     TableName clonedTableName = TableName.valueOf("unknownNS:clonetb");
150     admin.cloneSnapshot(snapshotName1, clonedTableName);
151   }
152 
153   @Test
154   public void testCloneSnapshot() throws IOException, InterruptedException {
155     TableName clonedTableName = TableName.valueOf("clonedtb-" + System.currentTimeMillis());
156     testCloneSnapshot(clonedTableName, snapshotName0, snapshot0Rows);
157     testCloneSnapshot(clonedTableName, snapshotName1, snapshot1Rows);
158     testCloneSnapshot(clonedTableName, emptySnapshot, 0);
159   }
160 
161   private void testCloneSnapshot(final TableName tableName, final byte[] snapshotName,
162       int snapshotRows) throws IOException, InterruptedException {
163     // create a new table from snapshot
164     admin.cloneSnapshot(snapshotName, tableName);
165     SnapshotTestingUtils.verifyRowCount(TEST_UTIL, tableName, snapshotRows);
166 
167     TEST_UTIL.deleteTable(tableName);
168   }
169 
170   @Test
171   public void testCloneSnapshotCrossNamespace() throws IOException, InterruptedException {
172     String nsName = "testCloneSnapshotCrossNamespace";
173     admin.createNamespace(NamespaceDescriptor.create(nsName).build());
174     TableName clonedTableName =
175         TableName.valueOf(nsName, "clonedtb-" + System.currentTimeMillis());
176     testCloneSnapshot(clonedTableName, snapshotName0, snapshot0Rows);
177     testCloneSnapshot(clonedTableName, snapshotName1, snapshot1Rows);
178     testCloneSnapshot(clonedTableName, emptySnapshot, 0);
179   }
180 
181   /**
182    * Verify that tables created from the snapshot are still alive after source table deletion.
183    */
184   @Test
185   public void testCloneLinksAfterDelete() throws IOException, InterruptedException {
186     // Clone a table from the first snapshot
187     TableName clonedTableName = TableName.valueOf("clonedtb1-" + System.currentTimeMillis());
188     admin.cloneSnapshot(snapshotName0, clonedTableName);
189     SnapshotTestingUtils.verifyRowCount(TEST_UTIL, clonedTableName, snapshot0Rows);
190 
191     // Take a snapshot of this cloned table.
192     admin.disableTable(clonedTableName);
193     admin.snapshot(snapshotName2, clonedTableName);
194 
195     // Clone the snapshot of the cloned table
196     TableName clonedTableName2 = TableName.valueOf("clonedtb2-" + System.currentTimeMillis());
197     admin.cloneSnapshot(snapshotName2, clonedTableName2);
198     SnapshotTestingUtils.verifyRowCount(TEST_UTIL, clonedTableName2, snapshot0Rows);
199     admin.disableTable(clonedTableName2);
200 
201     // Remove the original table
202     TEST_UTIL.deleteTable(tableName);
203     waitCleanerRun();
204 
205     // Verify the first cloned table
206     admin.enableTable(clonedTableName);
207     SnapshotTestingUtils.verifyRowCount(TEST_UTIL, clonedTableName, snapshot0Rows);
208 
209     // Verify the second cloned table
210     admin.enableTable(clonedTableName2);
211     SnapshotTestingUtils.verifyRowCount(TEST_UTIL, clonedTableName2, snapshot0Rows);
212     admin.disableTable(clonedTableName2);
213 
214     // Delete the first cloned table
215     TEST_UTIL.deleteTable(clonedTableName);
216     waitCleanerRun();
217 
218     // Verify the second cloned table
219     admin.enableTable(clonedTableName2);
220     SnapshotTestingUtils.verifyRowCount(TEST_UTIL, clonedTableName2, snapshot0Rows);
221 
222     // Clone a new table from cloned
223     TableName clonedTableName3 = TableName.valueOf("clonedtb3-" + System.currentTimeMillis());
224     admin.cloneSnapshot(snapshotName2, clonedTableName3);
225     SnapshotTestingUtils.verifyRowCount(TEST_UTIL, clonedTableName3, snapshot0Rows);
226 
227     // Delete the cloned tables
228     TEST_UTIL.deleteTable(clonedTableName2);
229     TEST_UTIL.deleteTable(clonedTableName3);
230     admin.deleteSnapshot(snapshotName2);
231   }
232 
233   // ==========================================================================
234   //  Helpers
235   // ==========================================================================
236 
237   private void waitCleanerRun() throws InterruptedException {
238     TEST_UTIL.getMiniHBaseCluster().getMaster().getHFileCleaner().choreForTesting();
239   }
240 }