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 java.io.IOException;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.hbase.HBaseTestingUtility;
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.NamespaceDescriptor;
27  import org.apache.hadoop.hbase.NamespaceNotFoundException;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
30  import org.apache.hadoop.hbase.mob.MobConstants;
31  import org.apache.hadoop.hbase.snapshot.MobSnapshotTestingUtils;
32  import org.apache.hadoop.hbase.snapshot.SnapshotDoesNotExistException;
33  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
34  import org.apache.hadoop.hbase.testclassification.LargeTests;
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 TestMobCloneSnapshotFromClient {
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 Admin 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.getConfiguration().setInt(MobConstants.MOB_FILE_CACHE_SIZE_KEY, 0);
74      TEST_UTIL.getConfiguration().setInt("hfile.format.version", 3);
75      TEST_UTIL.startMiniCluster(3);
76    }
77  
78    @AfterClass
79    public static void tearDownAfterClass() throws Exception {
80      TEST_UTIL.shutdownMiniCluster();
81    }
82  
83    /**
84     * Initialize the tests with a table filled with some data
85     * and two snapshots (snapshotName0, snapshotName1) of different states.
86     * The tableName, snapshotNames and the number of rows in the snapshot are initialized.
87     */
88    @Before
89    public void setup() throws Exception {
90      this.admin = TEST_UTIL.getHBaseAdmin();
91  
92      long tid = System.currentTimeMillis();
93      tableName = TableName.valueOf("testtb-" + tid);
94      emptySnapshot = Bytes.toBytes("emptySnaptb-" + tid);
95      snapshotName0 = Bytes.toBytes("snaptb0-" + tid);
96      snapshotName1 = Bytes.toBytes("snaptb1-" + tid);
97      snapshotName2 = Bytes.toBytes("snaptb2-" + tid);
98  
99      // create Table and disable it
100     MobSnapshotTestingUtils.createMobTable(TEST_UTIL, tableName, getNumReplicas(), FAMILY);
101     admin.disableTable(tableName);
102 
103     // take an empty snapshot
104     admin.snapshot(emptySnapshot, tableName);
105 
106     HTable table = new HTable(TEST_UTIL.getConfiguration(), tableName);
107     try {
108       // enable table and insert data
109       admin.enableTable(tableName);
110       SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 500, FAMILY);
111       snapshot0Rows = MobSnapshotTestingUtils.countMobRows(table);
112       admin.disableTable(tableName);
113 
114       // take a snapshot
115       admin.snapshot(snapshotName0, tableName);
116 
117       // enable table and insert more data
118       admin.enableTable(tableName);
119       SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 500, FAMILY);
120       snapshot1Rows = MobSnapshotTestingUtils.countMobRows(table);
121       admin.disableTable(tableName);
122 
123       // take a snapshot of the updated table
124       admin.snapshot(snapshotName1, tableName);
125 
126       // re-enable table
127       admin.enableTable(tableName);
128     } finally {
129       table.close();
130     }
131   }
132 
133   protected int getNumReplicas() {
134     return 1;
135   }
136 
137   @After
138   public void tearDown() throws Exception {
139     if (admin.tableExists(tableName)) {
140       TEST_UTIL.deleteTable(tableName);
141     }
142     SnapshotTestingUtils.deleteAllSnapshots(admin);
143     SnapshotTestingUtils.deleteArchiveDirectory(TEST_UTIL);
144   }
145 
146   @Test(expected=SnapshotDoesNotExistException.class)
147   public void testCloneNonExistentSnapshot() throws IOException, InterruptedException {
148     String snapshotName = "random-snapshot-" + System.currentTimeMillis();
149     TableName tableName = TableName.valueOf("random-table-" + System.currentTimeMillis());
150     admin.cloneSnapshot(snapshotName, tableName);
151   }
152 
153   @Test(expected = NamespaceNotFoundException.class)
154   public void testCloneOnMissingNamespace() throws IOException, InterruptedException {
155     TableName clonedTableName = TableName.valueOf("unknownNS:clonetb");
156     admin.cloneSnapshot(snapshotName1, clonedTableName);
157   }
158 
159   @Test
160   public void testCloneSnapshot() throws IOException, InterruptedException {
161     TableName clonedTableName = TableName.valueOf("clonedtb-" + System.currentTimeMillis());
162     testCloneSnapshot(clonedTableName, snapshotName0, snapshot0Rows);
163     testCloneSnapshot(clonedTableName, snapshotName1, snapshot1Rows);
164     testCloneSnapshot(clonedTableName, emptySnapshot, 0);
165   }
166 
167   private void testCloneSnapshot(final TableName tableName, final byte[] snapshotName,
168       int snapshotRows) throws IOException, InterruptedException {
169     // create a new table from snapshot
170     admin.cloneSnapshot(snapshotName, tableName);
171     MobSnapshotTestingUtils.verifyMobRowCount(TEST_UTIL, tableName, snapshotRows);
172 
173     verifyReplicasCameOnline(tableName);
174     TEST_UTIL.deleteTable(tableName);
175   }
176 
177   protected void verifyReplicasCameOnline(TableName tableName) throws IOException {
178     SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());
179   }
180 
181   @Test
182   public void testCloneSnapshotCrossNamespace() throws IOException, InterruptedException {
183     String nsName = "testCloneSnapshotCrossNamespace";
184     admin.createNamespace(NamespaceDescriptor.create(nsName).build());
185     TableName clonedTableName =
186         TableName.valueOf(nsName, "clonedtb-" + System.currentTimeMillis());
187     testCloneSnapshot(clonedTableName, snapshotName0, snapshot0Rows);
188     testCloneSnapshot(clonedTableName, snapshotName1, snapshot1Rows);
189     testCloneSnapshot(clonedTableName, emptySnapshot, 0);
190   }
191 
192   /**
193    * Verify that tables created from the snapshot are still alive after source table deletion.
194    */
195   @Test
196   public void testCloneLinksAfterDelete() throws IOException, InterruptedException {
197     // Clone a table from the first snapshot
198     TableName clonedTableName = TableName.valueOf("clonedtb1-" + System.currentTimeMillis());
199     admin.cloneSnapshot(snapshotName0, clonedTableName);
200     MobSnapshotTestingUtils.verifyMobRowCount(TEST_UTIL, clonedTableName, snapshot0Rows);
201 
202     // Take a snapshot of this cloned table.
203     admin.disableTable(clonedTableName);
204     admin.snapshot(snapshotName2, clonedTableName);
205 
206     // Clone the snapshot of the cloned table
207     TableName clonedTableName2 = TableName.valueOf("clonedtb2-" + System.currentTimeMillis());
208     admin.cloneSnapshot(snapshotName2, clonedTableName2);
209     MobSnapshotTestingUtils.verifyMobRowCount(TEST_UTIL, clonedTableName2, snapshot0Rows);
210     admin.disableTable(clonedTableName2);
211 
212     // Remove the original table
213     TEST_UTIL.deleteTable(tableName);
214     waitCleanerRun();
215 
216     // Verify the first cloned table
217     admin.enableTable(clonedTableName);
218     MobSnapshotTestingUtils.verifyMobRowCount(TEST_UTIL, clonedTableName, snapshot0Rows);
219 
220     // Verify the second cloned table
221     admin.enableTable(clonedTableName2);
222     MobSnapshotTestingUtils.verifyMobRowCount(TEST_UTIL, clonedTableName2, snapshot0Rows);
223     admin.disableTable(clonedTableName2);
224 
225     // Delete the first cloned table
226     TEST_UTIL.deleteTable(clonedTableName);
227     waitCleanerRun();
228 
229     // Verify the second cloned table
230     admin.enableTable(clonedTableName2);
231     MobSnapshotTestingUtils.verifyMobRowCount(TEST_UTIL, clonedTableName2, snapshot0Rows);
232 
233     // Clone a new table from cloned
234     TableName clonedTableName3 = TableName.valueOf("clonedtb3-" + System.currentTimeMillis());
235     admin.cloneSnapshot(snapshotName2, clonedTableName3);
236     MobSnapshotTestingUtils.verifyMobRowCount(TEST_UTIL, clonedTableName3, snapshot0Rows);
237 
238     // Delete the cloned tables
239     TEST_UTIL.deleteTable(clonedTableName2);
240     TEST_UTIL.deleteTable(clonedTableName3);
241     admin.deleteSnapshot(snapshotName2);
242   }
243 
244   // ==========================================================================
245   //  Helpers
246   // ==========================================================================
247 
248   private void waitCleanerRun() throws InterruptedException {
249     TEST_UTIL.getMiniHBaseCluster().getMaster().getHFileCleaner().choreForTesting();
250   }
251 }