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 protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
56 protected final static String TEST_HFILE = "abc";
57
58 protected Configuration conf;
59 protected Path archiveDir;
60 protected FileSystem fs;
61 protected Path rootDir;
62
63 protected void setupConf(Configuration conf) {
64 }
65
66 @Before
67 public void setup() throws Exception {
68 rootDir = TEST_UTIL.getDataTestDir("testRestore");
69 archiveDir = new Path(rootDir, HConstants.HFILE_ARCHIVE_DIRECTORY);
70 fs = TEST_UTIL.getTestFileSystem();
71 conf = TEST_UTIL.getConfiguration();
72 setupConf(conf);
73 FSUtils.setRootDir(conf, rootDir);
74 }
75
76 @After
77 public void tearDown() throws Exception {
78 fs.delete(TEST_UTIL.getDataTestDir(), true);
79 }
80
81 protected SnapshotMock createSnapshotMock() throws IOException {
82 return new SnapshotMock(TEST_UTIL.getConfiguration(), fs, rootDir);
83 }
84
85 @Test
86 public void testRestore() throws IOException {
87 restoreAndVerify("snapshot", "testRestore");
88 }
89
90 @Test
91 public void testRestoreWithNamespace() throws IOException {
92 restoreAndVerify("snapshot", "namespace1:testRestoreWithNamespace");
93 }
94
95 private void restoreAndVerify(final String snapshotName, final String tableName)
96 throws IOException {
97
98
99 SnapshotMock snapshotMock = createSnapshotMock();
100 SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2("snapshot", tableName);
101 builder.addRegionV1();
102 builder.addRegionV2();
103 builder.addRegionV2();
104 builder.addRegionV1();
105 Path snapshotDir = builder.commit();
106 HTableDescriptor htd = builder.getTableDescriptor();
107 SnapshotDescription desc = builder.getSnapshotDescription();
108
109
110 HTableDescriptor htdClone = snapshotMock.createHtd("testtb-clone");
111 testRestore(snapshotDir, desc, htdClone);
112 verifyRestore(rootDir, htd, htdClone);
113
114
115 SnapshotDescription cloneDesc = SnapshotDescription.newBuilder()
116 .setName("cloneSnapshot")
117 .setTable("testtb-clone")
118 .build();
119 Path cloneDir = FSUtils.getTableDir(rootDir, htdClone.getTableName());
120 HTableDescriptor htdClone2 = snapshotMock.createHtd("testtb-clone2");
121 testRestore(cloneDir, cloneDesc, htdClone2);
122 verifyRestore(rootDir, htd, htdClone2);
123 }
124
125 private void verifyRestore(final Path rootDir, final HTableDescriptor sourceHtd,
126 final HTableDescriptor htdClone) throws IOException {
127 String[] files = SnapshotTestingUtils.listHFileNames(fs,
128 FSUtils.getTableDir(rootDir, htdClone.getTableName()));
129 assertEquals(12, files.length);
130 for (int i = 0; i < files.length; i += 2) {
131 String linkFile = files[i];
132 String refFile = files[i+1];
133 assertTrue(linkFile + " should be a HFileLink", HFileLink.isHFileLink(linkFile));
134 assertTrue(refFile + " should be a Referene", StoreFileInfo.isReference(refFile));
135 assertEquals(sourceHtd.getTableName(), HFileLink.getReferencedTableName(linkFile));
136 Path refPath = getReferredToFile(refFile);
137 LOG.debug("get reference name for file " + refFile + " = " + refPath);
138 assertTrue(refPath.getName() + " should be a HFileLink", HFileLink.isHFileLink(refPath.getName()));
139 assertEquals(linkFile, refPath.getName());
140 }
141 }
142
143
144
145
146
147
148
149 private void testRestore(final Path snapshotDir, final SnapshotDescription sd,
150 final HTableDescriptor htdClone) throws IOException {
151 LOG.debug("pre-restore table=" + htdClone.getTableName() + " snapshot=" + snapshotDir);
152 FSUtils.logFileSystemState(fs, rootDir, LOG);
153
154 new FSTableDescriptors(conf).createTableDescriptor(htdClone);
155 RestoreSnapshotHelper helper = getRestoreHelper(rootDir, snapshotDir, sd, htdClone);
156 helper.restoreHdfsRegions();
157
158 LOG.debug("post-restore table=" + htdClone.getTableName() + " snapshot=" + snapshotDir);
159 FSUtils.logFileSystemState(fs, rootDir, LOG);
160 }
161
162
163
164
165 private RestoreSnapshotHelper getRestoreHelper(final Path rootDir, final Path snapshotDir,
166 final SnapshotDescription sd, final HTableDescriptor htdClone) throws IOException {
167 ForeignExceptionDispatcher monitor = Mockito.mock(ForeignExceptionDispatcher.class);
168 MonitoredTask status = Mockito.mock(MonitoredTask.class);
169
170 SnapshotManifest manifest = SnapshotManifest.open(conf, fs, snapshotDir, sd);
171 return new RestoreSnapshotHelper(conf, fs, manifest,
172 htdClone, rootDir, monitor, status);
173 }
174
175 private Path getReferredToFile(final String referenceName) {
176 Path fakeBasePath = new Path(new Path("table", "region"), "cf");
177 return StoreFileInfo.getReferredToFile(new Path(fakeBasePath, referenceName));
178 }
179 }