1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util.hbck;
19
20 import static org.apache.hadoop.hbase.util.hbck.HbckTestingUtil.assertErrors;
21 import static org.apache.hadoop.hbase.util.hbck.HbckTestingUtil.doFsck;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.IOException;
27 import java.util.Arrays;
28 import java.util.List;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.fs.FileStatus;
33 import org.apache.hadoop.fs.FileSystem;
34 import org.apache.hadoop.fs.Path;
35 import org.apache.hadoop.hbase.HConstants;
36 import org.apache.hadoop.hbase.HTableDescriptor;
37 import org.apache.hadoop.hbase.NamespaceDescriptor;
38 import org.apache.hadoop.hbase.TableName;
39 import org.apache.hadoop.hbase.testclassification.MediumTests;
40 import org.apache.hadoop.hbase.client.Admin;
41 import org.apache.hadoop.hbase.client.Connection;
42 import org.apache.hadoop.hbase.client.ConnectionFactory;
43 import org.apache.hadoop.hbase.util.Bytes;
44 import org.apache.hadoop.hbase.util.FSUtils;
45 import org.apache.hadoop.hbase.util.HBaseFsck;
46 import org.apache.hadoop.hbase.util.HBaseFsck.ErrorReporter.ERROR_CODE;
47 import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
48 import org.junit.Test;
49 import org.junit.experimental.categories.Category;
50
51
52
53
54 @Category(MediumTests.class)
55 public class TestOfflineMetaRebuildBase extends OfflineMetaRebuildTestCore {
56 private static final Log LOG = LogFactory.getLog(TestOfflineMetaRebuildBase.class);
57
58 @Test(timeout = 120000)
59 public void testMetaRebuild() throws Exception {
60 wipeOutMeta();
61
62
63 assertEquals(1, scanMeta());
64 assertErrors(doFsck(conf, false),
65 new ERROR_CODE[] {
66 ERROR_CODE.NOT_IN_META_OR_DEPLOYED,
67 ERROR_CODE.NOT_IN_META_OR_DEPLOYED,
68 ERROR_CODE.NOT_IN_META_OR_DEPLOYED,
69 ERROR_CODE.NOT_IN_META_OR_DEPLOYED});
70
71
72
73
74 TEST_UTIL.shutdownMiniHBaseCluster();
75 TEST_UTIL.shutdownMiniZKCluster();
76
77
78 HBaseFsck fsck = new HBaseFsck(conf);
79 assertTrue(fsck.rebuildMeta(false));
80 assertTrue("HBCK meta recovery WAL directory exist.", validateHBCKMetaRecoveryWALDir());
81
82
83 TEST_UTIL.startMiniZKCluster();
84 TEST_UTIL.restartHBaseCluster(3);
85 validateMetaAndUserTableRows(1, 5);
86 }
87
88 @Test(timeout = 300000)
89 public void testHMasterStartupOnMetaRebuild() throws Exception {
90
91 TEST_UTIL.shutdownMiniHBaseCluster();
92
93
94 TEST_UTIL.getConfiguration().set("hbase.balancer.tablesOnMaster", "hbase:meta");
95
96 TEST_UTIL.getConfiguration().set("hbase.master.namespace.init.timeout", "150000");
97 TEST_UTIL.restartHBaseCluster(3);
98 TEST_UTIL.getMiniHBaseCluster().waitForActiveAndReadyMaster();
99
100 try {
101
102 TEST_UTIL.getHBaseAdmin().createNamespace(NamespaceDescriptor.create("ns1").build());
103 TEST_UTIL.getHBaseAdmin().createNamespace(NamespaceDescriptor.create("ns2").build());
104
105 TEST_UTIL.createTable(TableName.valueOf("ns1:testHMasterStartupOnMetaRebuild"),
106 Bytes.toBytes("cf1"));
107 TEST_UTIL.createTable(TableName.valueOf("ns2:testHMasterStartupOnMetaRebuild"),
108 Bytes.toBytes("cf1"));
109
110
111 TEST_UTIL.flush(TableName.META_TABLE_NAME);
112
113
114 TEST_UTIL.getHBaseCluster().getMaster().shutdown();
115
116
117 List<RegionServerThread> regionServerThreads =
118 TEST_UTIL.getHBaseCluster().getRegionServerThreads();
119 for (RegionServerThread regionServerThread : regionServerThreads) {
120 TEST_UTIL.getHBaseCluster()
121 .killRegionServer(regionServerThread.getRegionServer().getServerName());
122 }
123
124
125 HBaseFsck fsck = new HBaseFsck(conf);
126 assertTrue(fsck.rebuildMeta(false));
127
128
129 TEST_UTIL.restartHBaseCluster(3);
130 validateMetaAndUserTableRows(3, 7);
131 } finally {
132
133 TEST_UTIL.deleteTable("ns1:testHMasterStartupOnMetaRebuild");
134 TEST_UTIL.deleteTable("ns2:testHMasterStartupOnMetaRebuild");
135 TEST_UTIL.getHBaseAdmin().deleteNamespace("ns1");
136 TEST_UTIL.getHBaseAdmin().deleteNamespace("ns2");
137 }
138 }
139
140
141
142
143 private void validateMetaAndUserTableRows(int totalTableCount, int totalRegionCount)
144 throws Exception {
145 try (Connection connection = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
146 Admin admin = connection.getAdmin();
147 admin.enableTable(table);
148 LOG.info("Waiting for no more RIT");
149 TEST_UTIL.waitUntilNoRegionsInTransition(60000);
150 LOG.info("No more RIT in ZK, now doing final test verification");
151
152
153 assertEquals(totalRegionCount, scanMeta());
154 HTableDescriptor[] htbls = admin.listTables();
155 LOG.info("Tables present after restart: " + Arrays.toString(htbls));
156 assertEquals(totalTableCount, htbls.length);
157 }
158
159 assertErrors(doFsck(conf, false), new ERROR_CODE[] {});
160 LOG.info("Table " + table + " has " + tableRowCount(conf, table) + " entries.");
161 assertEquals(16, tableRowCount(conf, table));
162 }
163
164
165
166
167
168 private boolean validateHBCKMetaRecoveryWALDir() throws IOException {
169 Path rootdir = FSUtils.getRootDir(TEST_UTIL.getConfiguration());
170 Path walLogDir = new Path(rootdir, HConstants.HREGION_LOGDIR_NAME);
171 FileSystem fs = TEST_UTIL.getTestFileSystem();
172 FileStatus[] walFiles = FSUtils.listStatus(fs, walLogDir, null);
173 assertNotNull(walFiles);
174 for (FileStatus fsStat : walFiles) {
175 if (fsStat.isDirectory() && fsStat.getPath().getName().startsWith("hregion-")) {
176 return false;
177 }
178 }
179 return true;
180 }
181 }