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