1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.snapshot;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.IOException;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.HConstants;
32 import org.apache.hadoop.hbase.HTableDescriptor;
33 import org.apache.hadoop.hbase.testclassification.SmallTests;
34 import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
35 import org.apache.hadoop.hbase.io.HFileLink;
36 import org.apache.hadoop.hbase.monitoring.MonitoredTask;
37 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
38 import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
39 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils.SnapshotMock;
40 import org.apache.hadoop.hbase.util.FSTableDescriptors;
41 import org.apache.hadoop.hbase.util.FSUtils;
42 import org.junit.After;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46 import org.mockito.Mockito;
47
48
49
50
51 @Category(SmallTests.class)
52 public class TestRestoreSnapshotHelper {
53 final Log LOG = LogFactory.getLog(getClass());
54
55 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
56 private final static String TEST_HFILE = "abc";
57
58 private Configuration conf;
59 private Path archiveDir;
60 private FileSystem fs;
61 private Path rootDir;
62
63 @Before
64 public void setup() throws Exception {
65 rootDir = TEST_UTIL.getDataTestDir("testRestore");
66 archiveDir = new Path(rootDir, HConstants.HFILE_ARCHIVE_DIRECTORY);
67 fs = TEST_UTIL.getTestFileSystem();
68 conf = TEST_UTIL.getConfiguration();
69 FSUtils.setRootDir(conf, rootDir);
70 }
71
72 @After
73 public void tearDown() throws Exception {
74 fs.delete(TEST_UTIL.getDataTestDir(), true);
75 }
76
77 @Test
78 public void testRestore() throws IOException {
79
80
81 SnapshotMock snapshotMock = new SnapshotMock(TEST_UTIL.getConfiguration(), fs, rootDir);
82 SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2("snapshot");
83 builder.addRegionV1();
84 builder.addRegionV2();
85 builder.addRegionV2();
86 builder.addRegionV1();
87 Path snapshotDir = builder.commit();
88 HTableDescriptor htd = builder.getTableDescriptor();
89 SnapshotDescription desc = builder.getSnapshotDescription();
90
91
92 HTableDescriptor htdClone = snapshotMock.createHtd("testtb-clone");
93 testRestore(snapshotDir, desc, htdClone);
94 verifyRestore(rootDir, htd, htdClone);
95
96
97 SnapshotDescription cloneDesc = SnapshotDescription.newBuilder()
98 .setName("cloneSnapshot")
99 .setTable("testtb-clone")
100 .build();
101 Path cloneDir = FSUtils.getTableDir(rootDir, htdClone.getTableName());
102 HTableDescriptor htdClone2 = snapshotMock.createHtd("testtb-clone2");
103 testRestore(cloneDir, cloneDesc, htdClone2);
104 verifyRestore(rootDir, htd, htdClone2);
105 }
106
107 private void verifyRestore(final Path rootDir, final HTableDescriptor sourceHtd,
108 final HTableDescriptor htdClone) throws IOException {
109 String[] files = SnapshotTestingUtils.listHFileNames(fs,
110 FSUtils.getTableDir(rootDir, htdClone.getTableName()));
111 assertEquals(12, files.length);
112 for (int i = 0; i < files.length; i += 2) {
113 String linkFile = files[i];
114 String refFile = files[i+1];
115 assertTrue(linkFile + " should be a HFileLink", HFileLink.isHFileLink(linkFile));
116 assertTrue(refFile + " should be a Referene", StoreFileInfo.isReference(refFile));
117 assertEquals(sourceHtd.getTableName(), HFileLink.getReferencedTableName(linkFile));
118 Path refPath = getReferredToFile(refFile);
119 LOG.debug("get reference name for file " + refFile + " = " + refPath);
120 assertTrue(refPath.getName() + " should be a HFileLink", HFileLink.isHFileLink(refPath.getName()));
121 assertEquals(linkFile, refPath.getName());
122 }
123 }
124
125
126
127
128
129
130
131 public void testRestore(final Path snapshotDir, final SnapshotDescription sd,
132 final HTableDescriptor htdClone) throws IOException {
133 LOG.debug("pre-restore table=" + htdClone.getTableName() + " snapshot=" + snapshotDir);
134 FSUtils.logFileSystemState(fs, rootDir, LOG);
135
136 new FSTableDescriptors(conf).createTableDescriptor(htdClone);
137 RestoreSnapshotHelper helper = getRestoreHelper(rootDir, snapshotDir, sd, htdClone);
138 helper.restoreHdfsRegions();
139
140 LOG.debug("post-restore table=" + htdClone.getTableName() + " snapshot=" + snapshotDir);
141 FSUtils.logFileSystemState(fs, rootDir, LOG);
142 }
143
144
145
146
147 private RestoreSnapshotHelper getRestoreHelper(final Path rootDir, final Path snapshotDir,
148 final SnapshotDescription sd, final HTableDescriptor htdClone) throws IOException {
149 ForeignExceptionDispatcher monitor = Mockito.mock(ForeignExceptionDispatcher.class);
150 MonitoredTask status = Mockito.mock(MonitoredTask.class);
151
152 SnapshotManifest manifest = SnapshotManifest.open(conf, fs, snapshotDir, sd);
153 return new RestoreSnapshotHelper(conf, fs, manifest,
154 htdClone, rootDir, monitor, status);
155 }
156
157 private Path getReferredToFile(final String referenceName) {
158 Path fakeBasePath = new Path(new Path("table", "region"), "cf");
159 return StoreFileInfo.getReferredToFile(new Path(fakeBasePath, referenceName));
160 }
161 }