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