1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.migration;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.assertFalse;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.hadoop.conf.Configuration;
34 import org.apache.hadoop.fs.FileSystem;
35 import org.apache.hadoop.fs.FileUtil;
36 import org.apache.hadoop.fs.FsShell;
37 import org.apache.hadoop.fs.Path;
38 import org.apache.hadoop.hbase.Cell;
39 import org.apache.hadoop.hbase.CellUtil;
40 import org.apache.hadoop.hbase.HBaseTestingUtility;
41 import org.apache.hadoop.hbase.HColumnDescriptor;
42 import org.apache.hadoop.hbase.HConstants;
43 import org.apache.hadoop.hbase.HRegionInfo;
44 import org.apache.hadoop.hbase.HTableDescriptor;
45 import org.apache.hadoop.hbase.MediumTests;
46 import org.apache.hadoop.hbase.NamespaceDescriptor;
47 import org.apache.hadoop.hbase.TableName;
48 import org.apache.hadoop.hbase.Waiter;
49 import org.apache.hadoop.hbase.client.Get;
50 import org.apache.hadoop.hbase.client.HTable;
51 import org.apache.hadoop.hbase.client.Put;
52 import org.apache.hadoop.hbase.client.Result;
53 import org.apache.hadoop.hbase.client.ResultScanner;
54 import org.apache.hadoop.hbase.client.Scan;
55 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;
56 import org.apache.hadoop.hbase.regionserver.HRegion;
57 import org.apache.hadoop.hbase.regionserver.HRegionFileSystem;
58 import org.apache.hadoop.hbase.security.access.AccessControlLists;
59 import org.apache.hadoop.hbase.util.Bytes;
60 import org.apache.hadoop.hbase.util.FSTableDescriptors;
61 import org.apache.hadoop.hbase.util.FSUtils;
62 import org.apache.hadoop.util.ToolRunner;
63 import org.junit.AfterClass;
64 import org.junit.Assert;
65 import org.junit.BeforeClass;
66 import org.junit.Test;
67 import org.junit.experimental.categories.Category;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 @Category(MediumTests.class)
87 public class TestNamespaceUpgrade {
88 static final Log LOG = LogFactory.getLog(TestNamespaceUpgrade.class);
89 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
90 private final static String snapshot1Keys[] =
91 {"1","10","2","3","4","5","6","7","8","9"};
92 private final static String snapshot2Keys[] =
93 {"1","2","3","4","5","6","7","8","9"};
94 private final static String currentKeys[] =
95 {"1","2","3","4","5","6","7","8","9","A"};
96 private final static String tables[] = {"foo", "ns1.foo","ns.two.foo"};
97
98 @BeforeClass
99 public static void setUpBeforeClass() throws Exception {
100
101
102 TEST_UTIL.startMiniZKCluster();
103 TEST_UTIL.startMiniDFSCluster(1);
104 Path testdir = TEST_UTIL.getDataTestDir("TestNamespaceUpgrade");
105
106 File untar = untar(new File(testdir.toString()));
107
108 Configuration conf = TEST_UTIL.getConfiguration();
109 FsShell shell = new FsShell(conf);
110 FileSystem fs = FileSystem.get(conf);
111
112 Path hbaseRootDir = TEST_UTIL.getDefaultRootDirPath();
113 if (!fs.isDirectory(hbaseRootDir.getParent())) {
114
115 fs.mkdirs(hbaseRootDir.getParent());
116 }
117 if(org.apache.hadoop.util.VersionInfo.getVersion().startsWith("2.")) {
118 LOG.info("Hadoop version is 2.x, pre-migrating snapshot dir");
119 FileSystem localFS = FileSystem.getLocal(conf);
120 if(!localFS.rename(new Path(untar.toString(), HConstants.OLD_SNAPSHOT_DIR_NAME),
121 new Path(untar.toString(), HConstants.SNAPSHOT_DIR_NAME))) {
122 throw new IllegalStateException("Failed to move snapshot dir to 2.x expectation");
123 }
124 }
125 doFsCommand(shell,
126 new String [] {"-put", untar.toURI().toString(), hbaseRootDir.toString()});
127 doFsCommand(shell, new String [] {"-lsr", "/"});
128
129 Configuration toolConf = TEST_UTIL.getConfiguration();
130 conf.set(HConstants.HBASE_DIR, TEST_UTIL.getDefaultRootDirPath().toString());
131 ToolRunner.run(toolConf, new NamespaceUpgrade(), new String[]{"--upgrade"});
132 assertTrue(FSUtils.getVersion(fs, hbaseRootDir).equals(HConstants.FILE_SYSTEM_VERSION));
133 doFsCommand(shell, new String [] {"-lsr", "/"});
134 TEST_UTIL.startMiniHBaseCluster(1, 1);
135
136 for(String table: tables) {
137 int count = 0;
138 for(Result res: new HTable(TEST_UTIL.getConfiguration(), table).getScanner(new Scan())) {
139 assertEquals(currentKeys[count++], Bytes.toString(res.getRow()));
140 }
141 Assert.assertEquals(currentKeys.length, count);
142 }
143 assertEquals(2, TEST_UTIL.getHBaseAdmin().listNamespaceDescriptors().length);
144
145
146 HTable secureTable = new HTable(conf, AccessControlLists.ACL_TABLE_NAME);
147 ResultScanner scanner = secureTable.getScanner(new Scan());
148 int count = 0;
149 for(Result r : scanner) {
150 count++;
151 }
152 assertEquals(3, count);
153 assertFalse(TEST_UTIL.getHBaseAdmin().tableExists("_acl_"));
154
155
156 List<HRegion> regions = TEST_UTIL.getMiniHBaseCluster().getRegions(secureTable.getName());
157 for(HRegion region : regions) {
158 assertEquals(1, region.getStores().size());
159 }
160 }
161
162 static File untar(final File testdir) throws IOException {
163
164 final String datafile = "TestNamespaceUpgrade";
165 File srcTarFile = new File(
166 System.getProperty("project.build.testSourceDirectory", "src/test") +
167 File.separator + "data" + File.separator + datafile + ".tgz");
168 File homedir = new File(testdir.toString());
169 File tgtUntarDir = new File(homedir, "hbase");
170 if (tgtUntarDir.exists()) {
171 if (!FileUtil.fullyDelete(tgtUntarDir)) {
172 throw new IOException("Failed delete of " + tgtUntarDir.toString());
173 }
174 }
175 if (!srcTarFile.exists()) {
176 throw new IOException(srcTarFile+" does not exist");
177 }
178 LOG.info("Untarring " + srcTarFile + " into " + homedir.toString());
179 FileUtil.unTar(srcTarFile, homedir);
180 Assert.assertTrue(tgtUntarDir.exists());
181 return tgtUntarDir;
182 }
183
184 private static void doFsCommand(final FsShell shell, final String [] args)
185 throws Exception {
186
187 int errcode = shell.run(args);
188 if (errcode != 0) throw new IOException("Failed put; errcode=" + errcode);
189 }
190
191 @AfterClass
192 public static void tearDownAfterClass() throws Exception {
193 TEST_UTIL.shutdownMiniCluster();
194 }
195
196 @Test (timeout=300000)
197 public void testSnapshots() throws IOException, InterruptedException {
198 String snapshots[][] = {snapshot1Keys, snapshot2Keys};
199 for(int i = 1; i <= snapshots.length; i++) {
200 for(String table: tables) {
201 TEST_UTIL.getHBaseAdmin().cloneSnapshot(table+"_snapshot"+i, table+"_clone"+i);
202 FSUtils.logFileSystemState(FileSystem.get(TEST_UTIL.getConfiguration()),
203 FSUtils.getRootDir(TEST_UTIL.getConfiguration()),
204 LOG);
205 int count = 0;
206 for(Result res: new HTable(TEST_UTIL.getConfiguration(), table+"_clone"+i).getScanner(new
207 Scan())) {
208 assertEquals(snapshots[i-1][count++], Bytes.toString(res.getRow()));
209 }
210 Assert.assertEquals(table+"_snapshot"+i, snapshots[i-1].length, count);
211 }
212 }
213 }
214
215 @Test (timeout=300000)
216 public void testRenameUsingSnapshots() throws Exception {
217 String newNS = "newNS";
218 TEST_UTIL.getHBaseAdmin().createNamespace(NamespaceDescriptor.create(newNS).build());
219 for(String table: tables) {
220 int count = 0;
221 for(Result res: new HTable(TEST_UTIL.getConfiguration(), table).getScanner(new
222 Scan())) {
223 assertEquals(currentKeys[count++], Bytes.toString(res.getRow()));
224 }
225 TEST_UTIL.getHBaseAdmin().snapshot(table + "_snapshot3", table);
226 final String newTableName = newNS + TableName.NAMESPACE_DELIM + table + "_clone3";
227 TEST_UTIL.getHBaseAdmin().cloneSnapshot(table + "_snapshot3", newTableName);
228 Thread.sleep(1000);
229 count = 0;
230 for(Result res: new HTable(TEST_UTIL.getConfiguration(), newTableName).getScanner(new
231 Scan())) {
232 assertEquals(currentKeys[count++], Bytes.toString(res.getRow()));
233 }
234 FSUtils.logFileSystemState(TEST_UTIL.getTestFileSystem(), TEST_UTIL.getDefaultRootDirPath()
235 , LOG);
236 Assert.assertEquals(newTableName, currentKeys.length, count);
237 TEST_UTIL.getHBaseAdmin().flush(newTableName);
238 TEST_UTIL.getHBaseAdmin().majorCompact(newTableName);
239 TEST_UTIL.waitFor(30000, new Waiter.Predicate<IOException>() {
240 @Override
241 public boolean evaluate() throws IOException {
242 try {
243 return TEST_UTIL.getHBaseAdmin().getCompactionState(newTableName) ==
244 AdminProtos.GetRegionInfoResponse.CompactionState.NONE;
245 } catch (InterruptedException e) {
246 throw new IOException(e);
247 }
248 }
249 });
250 }
251
252 String nextNS = "nextNS";
253 TEST_UTIL.getHBaseAdmin().createNamespace(NamespaceDescriptor.create(nextNS).build());
254 for(String table: tables) {
255 String srcTable = newNS + TableName.NAMESPACE_DELIM + table + "_clone3";
256 TEST_UTIL.getHBaseAdmin().snapshot(table + "_snapshot4", srcTable);
257 String newTableName = nextNS + TableName.NAMESPACE_DELIM + table + "_clone4";
258 TEST_UTIL.getHBaseAdmin().cloneSnapshot(table+"_snapshot4", newTableName);
259 FSUtils.logFileSystemState(TEST_UTIL.getTestFileSystem(), TEST_UTIL.getDefaultRootDirPath(),
260 LOG);
261 int count = 0;
262 for(Result res: new HTable(TEST_UTIL.getConfiguration(), newTableName).getScanner(new
263 Scan())) {
264 assertEquals(currentKeys[count++], Bytes.toString(res.getRow()));
265 }
266 Assert.assertEquals(newTableName, currentKeys.length, count);
267 }
268 }
269
270 @Test (timeout=300000)
271 public void testOldDirsAreGonePostMigration() throws IOException {
272 FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration());
273 Path hbaseRootDir = TEST_UTIL.getDefaultRootDirPath();
274 List <String> dirs = new ArrayList<String>(NamespaceUpgrade.NON_USER_TABLE_DIRS);
275
276 dirs.remove(HConstants.HBCK_SIDELINEDIR_NAME);
277 dirs.remove(HConstants.SNAPSHOT_DIR_NAME);
278 dirs.remove(HConstants.HBASE_TEMP_DIRECTORY);
279 for (String dir: dirs) {
280 assertFalse(fs.exists(new Path(hbaseRootDir, dir)));
281 }
282 }
283
284 @Test (timeout=300000)
285 public void testNewDirsArePresentPostMigration() throws IOException {
286 FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration());
287
288 String [] newdirs = new String [] {HConstants.BASE_NAMESPACE_DIR,
289 HConstants.HREGION_LOGDIR_NAME};
290 Path hbaseRootDir = TEST_UTIL.getDefaultRootDirPath();
291 for (String dir: newdirs) {
292 assertTrue(dir, fs.exists(new Path(hbaseRootDir, dir)));
293 }
294 }
295
296 @Test (timeout = 300000)
297 public void testACLTableMigration() throws IOException {
298 Path rootDir = TEST_UTIL.getDataTestDirOnTestFS("testACLTable");
299 FileSystem fs = TEST_UTIL.getTestFileSystem();
300 Configuration conf = TEST_UTIL.getConfiguration();
301 byte[] FAMILY = Bytes.toBytes("l");
302 byte[] QUALIFIER = Bytes.toBytes("testUser");
303 byte[] VALUE = Bytes.toBytes("RWCA");
304
305
306 HTableDescriptor aclTable = new HTableDescriptor(TableName.valueOf("testACLTable"));
307 aclTable.addFamily(new HColumnDescriptor(FAMILY));
308 FSTableDescriptors fstd = new FSTableDescriptors(fs, rootDir);
309 fstd.createTableDescriptor(aclTable);
310 HRegionInfo hriAcl = new HRegionInfo(aclTable.getTableName(), null, null);
311 HRegion region = HRegion.createHRegion(hriAcl, rootDir, conf, aclTable);
312 try {
313
314 Put p = new Put(Bytes.toBytes("-ROOT-"));
315 p.addImmutable(FAMILY, QUALIFIER, VALUE);
316 region.put(p);
317 p = new Put(Bytes.toBytes(".META."));
318 p.addImmutable(FAMILY, QUALIFIER, VALUE);
319 region.put(p);
320 p = new Put(Bytes.toBytes("_acl_"));
321 p.addImmutable(FAMILY, QUALIFIER, VALUE);
322 region.put(p);
323
324 NamespaceUpgrade upgrade = new NamespaceUpgrade();
325 upgrade.updateAcls(region);
326
327
328 Get g = new Get(Bytes.toBytes("-ROOT-"));
329 Result r = region.get(g);
330 assertTrue(r == null || r.size() == 0);
331
332
333 g = new Get(AccessControlLists.ACL_TABLE_NAME.toBytes());
334 r = region.get(g);
335 assertTrue(r != null && r.size() == 1);
336 assertTrue(Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIER)) == 0);
337
338
339 g = new Get(TableName.META_TABLE_NAME.toBytes());
340 r = region.get(g);
341 assertTrue(r != null && r.size() == 1);
342 assertTrue(Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIER)) == 0);
343 } finally {
344 region.close();
345
346 HRegionFileSystem.deleteRegionFromFileSystem(conf, fs,
347 FSUtils.getTableDir(rootDir, hriAcl.getTable()), hriAcl);
348 }
349 }
350 }