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  
19  package org.apache.hadoop.hbase.client;
20  
21  import java.io.IOException;
22  import java.util.Arrays;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.Cell;
30  import org.apache.hadoop.hbase.CellScanner;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.testclassification.LargeTests;
33  import org.apache.hadoop.hbase.TableName;
34  import org.apache.hadoop.hbase.mapreduce.TestTableSnapshotInputFormat;
35  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
36  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.apache.hadoop.hbase.util.FSUtils;
39  import org.junit.After;
40  import org.junit.Assert;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  @Category(LargeTests.class)
45  public class TestTableSnapshotScanner {
46  
47    private static final Log LOG = LogFactory.getLog(TestTableSnapshotInputFormat.class);
48    private final HBaseTestingUtility UTIL = new HBaseTestingUtility();
49    private static final int NUM_REGION_SERVERS = 2;
50    private static final byte[][] FAMILIES = {Bytes.toBytes("f1"), Bytes.toBytes("f2")};
51    public static byte[] bbb = Bytes.toBytes("bbb");
52    public static byte[] yyy = Bytes.toBytes("yyy");
53  
54    private FileSystem fs;
55    private Path rootDir;
56  
57    public void setupCluster() throws Exception {
58      setupConf(UTIL.getConfiguration());
59      UTIL.startMiniCluster(NUM_REGION_SERVERS, true);
60      rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
61      fs = rootDir.getFileSystem(UTIL.getConfiguration());
62    }
63  
64    public void tearDownCluster() throws Exception {
65      UTIL.shutdownMiniCluster();
66    }
67  
68    private static void setupConf(Configuration conf) {
69      // Enable snapshot
70      conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
71    }
72  
73    @After
74    public void tearDown() throws Exception {
75    }
76  
77    public static void createTableAndSnapshot(HBaseTestingUtility util, TableName tableName,
78        String snapshotName, int numRegions)
79        throws Exception {
80      try {
81        util.deleteTable(tableName);
82      } catch(Exception ex) {
83        // ignore
84      }
85  
86      if (numRegions > 1) {
87        util.createTable(tableName, FAMILIES, 1, bbb, yyy, numRegions);
88      } else {
89        util.createTable(tableName, FAMILIES);
90      }
91      Admin admin = util.getHBaseAdmin();
92  
93      // put some stuff in the table
94      HTable table = new HTable(util.getConfiguration(), tableName);
95      util.loadTable(table, FAMILIES);
96  
97      Path rootDir = FSUtils.getRootDir(util.getConfiguration());
98      FileSystem fs = rootDir.getFileSystem(util.getConfiguration());
99  
100     SnapshotTestingUtils.createSnapshotAndValidate(admin, tableName,
101         Arrays.asList(FAMILIES), null, snapshotName, rootDir, fs, true);
102 
103     // load different values
104     byte[] value = Bytes.toBytes("after_snapshot_value");
105     util.loadTable(table, FAMILIES, value);
106 
107     // cause flush to create new files in the region
108     admin.flush(tableName);
109     table.close();
110   }
111 
112   @Test
113   public void testNoDuplicateResultsWhenSplitting() throws Exception {
114     setupCluster();
115     TableName tableName = TableName.valueOf("testNoDuplicateResultsWhenSplitting");
116     String snapshotName = "testSnapshotBug";
117     try {
118       if (UTIL.getHBaseAdmin().tableExists(tableName)) {
119         UTIL.deleteTable(tableName);
120       }
121 
122       UTIL.createTable(tableName, FAMILIES);
123       Admin admin = UTIL.getHBaseAdmin();
124 
125       // put some stuff in the table
126       Table table = UTIL.getConnection().getTable(tableName);
127       UTIL.loadTable(table, FAMILIES);
128 
129       // split to 2 regions
130       admin.split(tableName, Bytes.toBytes("eee"));
131       TestTableSnapshotInputFormat.blockUntilSplitFinished(UTIL, tableName, 2);
132 
133       Path rootDir = FSUtils.getRootDir(UTIL.getConfiguration());
134       FileSystem fs = rootDir.getFileSystem(UTIL.getConfiguration());
135 
136       SnapshotTestingUtils.createSnapshotAndValidate(admin, tableName,
137         Arrays.asList(FAMILIES), null, snapshotName, rootDir, fs, true);
138 
139       // load different values
140       byte[] value = Bytes.toBytes("after_snapshot_value");
141       UTIL.loadTable(table, FAMILIES, value);
142 
143       // cause flush to create new files in the region
144       admin.flush(tableName);
145       table.close();
146 
147       Path restoreDir = UTIL.getDataTestDirOnTestFS(snapshotName);
148       Scan scan = new Scan().withStartRow(bbb).withStopRow(yyy); // limit the scan
149 
150       TableSnapshotScanner scanner =
151           new TableSnapshotScanner(UTIL.getConfiguration(), restoreDir, snapshotName, scan);
152 
153       verifyScanner(scanner, bbb, yyy);
154       scanner.close();
155     } finally {
156       UTIL.getHBaseAdmin().deleteSnapshot(snapshotName);
157       UTIL.deleteTable(tableName);
158       tearDownCluster();
159     }
160   }
161 
162   @Test
163   public void testWithSingleRegion() throws Exception {
164     testScanner(UTIL, "testWithSingleRegion", 1, false);
165   }
166 
167   @Test
168   public void testWithMultiRegion() throws Exception {
169     testScanner(UTIL, "testWithMultiRegion", 10, false);
170   }
171 
172   @Test
173   public void testWithOfflineHBaseMultiRegion() throws Exception {
174     testScanner(UTIL, "testWithMultiRegion", 20, true);
175   }
176 
177   private void testScanner(HBaseTestingUtility util, String snapshotName, int numRegions,
178       boolean shutdownCluster) throws Exception {
179     setupCluster();
180     TableName tableName = TableName.valueOf("testScanner");
181     try {
182       createTableAndSnapshot(util, tableName, snapshotName, numRegions);
183 
184       if (shutdownCluster) {
185         util.shutdownMiniHBaseCluster();
186       }
187 
188       Path restoreDir = util.getDataTestDirOnTestFS(snapshotName);
189       Scan scan = new Scan(bbb, yyy); // limit the scan
190 
191       TableSnapshotScanner scanner = new TableSnapshotScanner(UTIL.getConfiguration(), restoreDir,
192         snapshotName, scan);
193 
194       verifyScanner(scanner, bbb, yyy);
195       scanner.close();
196     } finally {
197       if (!shutdownCluster) {
198         util.getHBaseAdmin().deleteSnapshot(snapshotName);
199         util.deleteTable(tableName);
200         tearDownCluster();
201       }
202     }
203   }
204 
205   private void verifyScanner(ResultScanner scanner, byte[] startRow, byte[] stopRow)
206       throws IOException, InterruptedException {
207 
208     HBaseTestingUtility.SeenRowTracker rowTracker =
209         new HBaseTestingUtility.SeenRowTracker(startRow, stopRow);
210 
211     while (true) {
212       Result result = scanner.next();
213       if (result == null) {
214         break;
215       }
216       verifyRow(result);
217       rowTracker.addRow(result.getRow());
218     }
219 
220     // validate all rows are seen
221     rowTracker.validate();
222   }
223 
224   private static void verifyRow(Result result) throws IOException {
225     byte[] row = result.getRow();
226     CellScanner scanner = result.cellScanner();
227     while (scanner.advance()) {
228       Cell cell = scanner.current();
229 
230       //assert that all Cells in the Result have the same key
231      Assert.assertEquals(0, Bytes.compareTo(row, 0, row.length,
232          cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
233     }
234 
235     for (int j = 0; j < FAMILIES.length; j++) {
236       byte[] actual = result.getValue(FAMILIES[j], FAMILIES[j]);
237       Assert.assertArrayEquals("Row in snapshot does not match, expected:" + Bytes.toString(row)
238           + " ,actual:" + Bytes.toString(actual), row, actual);
239     }
240   }
241 
242 }