1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.snapshot;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26 import java.net.URI;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.HashSet;
31 import java.util.Set;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 import org.apache.hadoop.conf.Configuration;
37 import org.apache.hadoop.fs.FileSystem;
38 import org.apache.hadoop.fs.FileStatus;
39 import org.apache.hadoop.fs.FileUtil;
40 import org.apache.hadoop.fs.FSDataOutputStream;
41 import org.apache.hadoop.fs.Path;
42 import org.apache.hadoop.hbase.HBaseTestingUtility;
43 import org.apache.hadoop.hbase.HColumnDescriptor;
44 import org.apache.hadoop.hbase.HConstants;
45 import org.apache.hadoop.hbase.HRegionInfo;
46 import org.apache.hadoop.hbase.HTableDescriptor;
47 import org.apache.hadoop.hbase.KeyValue;
48 import org.apache.hadoop.hbase.MediumTests;
49 import org.apache.hadoop.hbase.MiniHBaseCluster;
50 import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
51 import org.apache.hadoop.hbase.client.HBaseAdmin;
52 import org.apache.hadoop.hbase.client.HTable;
53 import org.apache.hadoop.hbase.util.Bytes;
54 import org.apache.hadoop.hbase.util.FSUtils;
55 import org.apache.hadoop.hbase.util.Pair;
56 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
57 import org.apache.hadoop.hbase.regionserver.HRegion;
58 import org.apache.hadoop.hbase.snapshot.ExportSnapshot;
59 import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
60 import org.apache.hadoop.mapreduce.Job;
61 import org.junit.After;
62 import org.junit.AfterClass;
63 import org.junit.Before;
64 import org.junit.BeforeClass;
65 import org.junit.Test;
66 import org.junit.experimental.categories.Category;
67
68
69
70
71 @Category(MediumTests.class)
72 public class TestExportSnapshot {
73 private final Log LOG = LogFactory.getLog(getClass());
74
75 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
76
77 private final static byte[] FAMILY = Bytes.toBytes("cf");
78
79 private byte[] emptySnapshotName;
80 private byte[] snapshotName;
81 private byte[] tableName;
82 private HBaseAdmin admin;
83
84 @BeforeClass
85 public static void setUpBeforeClass() throws Exception {
86 TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
87 TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
88 TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
89 TEST_UTIL.getConfiguration().setInt("hbase.client.retries.number", 6);
90 TEST_UTIL.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true);
91 TEST_UTIL.startMiniCluster(3);
92 }
93
94 @AfterClass
95 public static void tearDownAfterClass() throws Exception {
96 TEST_UTIL.shutdownMiniCluster();
97 }
98
99
100
101
102 @Before
103 public void setUp() throws Exception {
104 this.admin = TEST_UTIL.getHBaseAdmin();
105
106 long tid = System.currentTimeMillis();
107 tableName = Bytes.toBytes("testtb-" + tid);
108 snapshotName = Bytes.toBytes("snaptb0-" + tid);
109 emptySnapshotName = Bytes.toBytes("emptySnaptb0-" + tid);
110
111
112 HTableDescriptor htd = new HTableDescriptor(tableName);
113 htd.addFamily(new HColumnDescriptor(FAMILY));
114 admin.createTable(htd, null);
115
116
117 admin.snapshot(emptySnapshotName, tableName);
118
119
120 HTable table = new HTable(TEST_UTIL.getConfiguration(), tableName);
121 TEST_UTIL.loadTable(table, FAMILY);
122
123
124 admin.snapshot(snapshotName, tableName);
125 }
126
127 @After
128 public void tearDown() throws Exception {
129 TEST_UTIL.deleteTable(tableName);
130 SnapshotTestingUtils.deleteAllSnapshots(TEST_UTIL.getHBaseAdmin());
131 SnapshotTestingUtils.deleteArchiveDirectory(TEST_UTIL);
132 admin.close();
133 }
134
135
136
137
138
139
140
141
142
143
144 @Test
145 public void testBalanceSplit() throws Exception {
146
147 List<Pair<Path, Long>> files = new ArrayList<Pair<Path, Long>>();
148 for (long i = 0; i <= 20; i++) {
149 files.add(new Pair<Path, Long>(new Path("file-" + i), i));
150 }
151
152
153
154
155
156
157
158 List<List<Path>> splits = ExportSnapshot.getBalancedSplits(files, 5);
159 assertEquals(5, splits.size());
160 assertEquals(Arrays.asList(new Path("file-20"), new Path("file-11"),
161 new Path("file-10"), new Path("file-1"), new Path("file-0")), splits.get(0));
162 assertEquals(Arrays.asList(new Path("file-19"), new Path("file-12"),
163 new Path("file-9"), new Path("file-2")), splits.get(1));
164 assertEquals(Arrays.asList(new Path("file-18"), new Path("file-13"),
165 new Path("file-8"), new Path("file-3")), splits.get(2));
166 assertEquals(Arrays.asList(new Path("file-17"), new Path("file-14"),
167 new Path("file-7"), new Path("file-4")), splits.get(3));
168 assertEquals(Arrays.asList(new Path("file-16"), new Path("file-15"),
169 new Path("file-6"), new Path("file-5")), splits.get(4));
170 }
171
172
173
174
175 @Test
176 public void testExportFileSystemState() throws Exception {
177 testExportFileSystemState(tableName, snapshotName, 2);
178 }
179
180 @Test
181 public void testEmptyExportFileSystemState() throws Exception {
182 testExportFileSystemState(tableName, emptySnapshotName, 1);
183 }
184
185
186
187
188
189 @Test
190 public void testSnapshotWithRefsExportFileSystemState() throws Exception {
191 Configuration conf = TEST_UTIL.getConfiguration();
192
193 final byte[] tableWithRefsName = Bytes.toBytes("tableWithRefs");
194 final String snapshotName = "tableWithRefs";
195 final String TEST_FAMILY = Bytes.toString(FAMILY);
196 final String TEST_HFILE = "abc";
197
198 final SnapshotDescription sd = SnapshotDescription.newBuilder()
199 .setName(snapshotName).setTable(Bytes.toString(tableWithRefsName)).build();
200
201 FileSystem fs = TEST_UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
202 Path rootDir = TEST_UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
203 Path archiveDir = new Path(rootDir, HConstants.HFILE_ARCHIVE_DIRECTORY);
204
205 HTableDescriptor htd = new HTableDescriptor(tableWithRefsName);
206 htd.addFamily(new HColumnDescriptor(TEST_FAMILY));
207
208
209 HRegion r0 = HRegion.createHRegion(new HRegionInfo(htd.getName()), archiveDir,
210 conf, htd, null, true, true);
211 Path storeFile = new Path(new Path(r0.getRegionDir(), TEST_FAMILY), TEST_HFILE);
212 FSDataOutputStream out = fs.create(storeFile);
213 out.write(Bytes.toBytes("Test Data"));
214 out.close();
215 r0.close();
216
217
218
219 HRegion r1 = HRegion.createHRegion(new HRegionInfo(htd.getName()), archiveDir,
220 conf, htd, null, true, true);
221 out = fs.create(new Path(new Path(r1.getRegionDir(), TEST_FAMILY),
222 storeFile.getName() + '.' + r0.getRegionInfo().getEncodedName()));
223 out.write(Bytes.toBytes("Test Data"));
224 out.close();
225 r1.close();
226
227 Path tableDir = HTableDescriptor.getTableDir(archiveDir, tableWithRefsName);
228 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
229 FileUtil.copy(fs, tableDir, fs, snapshotDir, false, conf);
230 SnapshotDescriptionUtils.writeSnapshotInfo(sd, snapshotDir, fs);
231
232 testExportFileSystemState(tableWithRefsName, Bytes.toBytes(snapshotName), 2);
233 }
234
235
236
237
238 private void testExportFileSystemState(final byte[] tableName, final byte[] snapshotName,
239 int filesExpected) throws Exception {
240 Path copyDir = TEST_UTIL.getDataTestDir("export-" + System.currentTimeMillis());
241 URI hdfsUri = FileSystem.get(TEST_UTIL.getConfiguration()).getUri();
242 FileSystem fs = FileSystem.get(copyDir.toUri(), new Configuration());
243 copyDir = copyDir.makeQualified(fs);
244
245
246 int res = ExportSnapshot.innerMain(TEST_UTIL.getConfiguration(), new String[] {
247 "-snapshot", Bytes.toString(snapshotName),
248 "-copy-to", copyDir.toString()
249 });
250 assertEquals(0, res);
251
252
253 FileStatus[] rootFiles = fs.listStatus(copyDir);
254 assertEquals(filesExpected, rootFiles.length);
255 for (FileStatus fileStatus: rootFiles) {
256 String name = fileStatus.getPath().getName();
257 assertTrue(fileStatus.isDir());
258 assertTrue(name.equals(HConstants.SNAPSHOT_DIR_NAME) || name.equals(".archive"));
259 }
260
261
262 final FileSystem hdfs = FileSystem.get(hdfsUri, TEST_UTIL.getConfiguration());
263 final Path snapshotDir = new Path(HConstants.SNAPSHOT_DIR_NAME, Bytes.toString(snapshotName));
264 verifySnapshot(hdfs, new Path(TEST_UTIL.getDefaultRootDirPath(), snapshotDir),
265 fs, new Path(copyDir, snapshotDir));
266 verifyArchive(fs, copyDir, tableName, Bytes.toString(snapshotName));
267 FSUtils.logFileSystemState(hdfs, snapshotDir, LOG);
268
269
270 fs.delete(copyDir, true);
271 }
272
273
274
275
276 private void verifySnapshot(final FileSystem fs1, final Path root1,
277 final FileSystem fs2, final Path root2) throws IOException {
278 Set<String> s = new HashSet<String>();
279 assertEquals(listFiles(fs1, root1, root1), listFiles(fs2, root2, root2));
280 }
281
282
283
284
285 private void verifyArchive(final FileSystem fs, final Path rootDir,
286 final byte[] tableName, final String snapshotName) throws IOException {
287 final Path exportedSnapshot = new Path(rootDir,
288 new Path(HConstants.SNAPSHOT_DIR_NAME, snapshotName));
289 final Path exportedArchive = new Path(rootDir, HConstants.HFILE_ARCHIVE_DIRECTORY);
290 LOG.debug(listFiles(fs, exportedArchive, exportedArchive));
291 SnapshotReferenceUtil.visitReferencedFiles(fs, exportedSnapshot,
292 new SnapshotReferenceUtil.FileVisitor() {
293 public void storeFile (final String region, final String family, final String hfile)
294 throws IOException {
295 verifyNonEmptyFile(new Path(exportedArchive,
296 new Path(Bytes.toString(tableName), new Path(region, new Path(family, hfile)))));
297 }
298
299 public void recoveredEdits (final String region, final String logfile)
300 throws IOException {
301 verifyNonEmptyFile(new Path(exportedSnapshot,
302 new Path(Bytes.toString(tableName), new Path(region, logfile))));
303 }
304
305 public void logFile (final String server, final String logfile)
306 throws IOException {
307 verifyNonEmptyFile(new Path(exportedSnapshot, new Path(server, logfile)));
308 }
309
310 private void verifyNonEmptyFile(final Path path) throws IOException {
311 assertTrue(path + " should exist", fs.exists(path));
312 assertTrue(path + " should not be empty", fs.getFileStatus(path).getLen() > 0);
313 }
314 });
315 }
316
317 private Set<String> listFiles(final FileSystem fs, final Path root, final Path dir)
318 throws IOException {
319 Set<String> files = new HashSet<String>();
320 int rootPrefix = root.toString().length();
321 FileStatus[] list = FSUtils.listStatus(fs, dir);
322 if (list != null) {
323 for (FileStatus fstat: list) {
324 LOG.debug(fstat.getPath());
325 if (fstat.isDir()) {
326 files.addAll(listFiles(fs, root, fstat.getPath()));
327 } else {
328 files.add(fstat.getPath().toString().substring(rootPrefix));
329 }
330 }
331 }
332 return files;
333 }
334 }