diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestHLogSplit.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestHLogSplit.java index 57a2549..be81143 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestHLogSplit.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestHLogSplit.java @@ -81,8 +81,10 @@ import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; +import org.junit.rules.TestName; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -101,6 +103,7 @@ public class TestHLogSplit { ((Log4JLogger)FSNamesystem.LOG).getLogger().setLevel(Level.ALL); } private final static Log LOG = LogFactory.getLog(TestHLogSplit.class); + @Rule public TestName name = new TestName(); private Configuration conf; private FileSystem fs; @@ -398,49 +401,75 @@ public class TestHLogSplit { @Test (timeout=300000) public void testEmptyLogFiles() throws IOException { + LOG.info("Starting " + name); + HLog.Writer w1 = injectEmptyFile(".empty", true); + HLog.Writer [] writers = generateHLogs(Integer.MAX_VALUE); + HLog.Writer w2 = injectEmptyFile("empty", true); + try { + // make fs act as a different client now + // initialize will create a new DFSClient with a new client ID + fs.initialize(fs.getUri(), conf); - injectEmptyFile(".empty", true); - generateHLogs(Integer.MAX_VALUE); - injectEmptyFile("empty", true); - - // make fs act as a different client now - // initialize will create a new DFSClient with a new client ID - fs.initialize(fs.getUri(), conf); - - int expectedFiles = fs.listStatus(HLOGDIR).length - 2; // less 2 empty files - HLogSplitter.split(HBASEDIR, HLOGDIR, OLDLOGDIR, fs, conf); - for (String region : REGIONS) { - Path[] logfiles = getLogForRegion(HBASEDIR, TABLE_NAME, region); - assertEquals(expectedFiles, logfiles.length); - int count = 0; - for (Path logfile: logfiles) { - count += countHLog(logfile, fs, conf); + int expectedFiles = fs.listStatus(HLOGDIR).length - 2; // less 2 empty files + HLogSplitter.split(HBASEDIR, HLOGDIR, OLDLOGDIR, fs, conf); + for (String region : REGIONS) { + Path[] logfiles = getLogForRegion(HBASEDIR, TABLE_NAME, region); + assertEquals(expectedFiles, logfiles.length); + int count = 0; + for (Path logfile: logfiles) { + count += countHLog(logfile, fs, conf); + } + assertEquals(NUM_WRITERS * ENTRIES, count); } - assertEquals(NUM_WRITERS * ENTRIES, count); + } finally { + closeWriter(w1); + closeWriter(w2); + for (HLog.Writer writer: writers) closeWriter(writer); + LOG.info("Finished " + name); } } @Test (timeout=300000) public void testEmptyOpenLogFiles() throws IOException { - injectEmptyFile(".empty", false); - generateHLogs(Integer.MAX_VALUE); - injectEmptyFile("empty", false); + LOG.info("Starting " + name); + HLog.Writer w1 = injectEmptyFile(".empty", false); + HLog.Writer [] writers = generateHLogs(Integer.MAX_VALUE); + HLog.Writer w2 = injectEmptyFile("empty", false); + try { + // make fs act as a different client now + // initialize will create a new DFSClient with a new client ID + fs.initialize(fs.getUri(), conf); - // make fs act as a different client now - // initialize will create a new DFSClient with a new client ID - fs.initialize(fs.getUri(), conf); + int expectedFiles = fs.listStatus(HLOGDIR).length - 2 ; // less 2 empty files + HLogSplitter.split(HBASEDIR, HLOGDIR, OLDLOGDIR, fs, conf); + for (String region : REGIONS) { + Path[] logfiles = getLogForRegion(HBASEDIR, TABLE_NAME, region); + assertEquals(expectedFiles, logfiles.length); + int count = 0; + for (Path logfile: logfiles) { + count += countHLog(logfile, fs, conf); + } + assertEquals(NUM_WRITERS * ENTRIES, count); + } + } finally { + closeWriter(w1); + closeWriter(w2); + for (HLog.Writer writer: writers) closeWriter(writer); + LOG.info("Finished " + name); + } + } - int expectedFiles = fs.listStatus(HLOGDIR).length - 2 ; // less 2 empty files - HLogSplitter.split(HBASEDIR, HLOGDIR, OLDLOGDIR, fs, conf); - for (String region : REGIONS) { - Path[] logfiles = getLogForRegion(HBASEDIR, TABLE_NAME, region); - assertEquals(expectedFiles, logfiles.length); - int count = 0; - for (Path logfile: logfiles) { - count += countHLog(logfile, fs, conf); + private void closeWriter(final HLog.Writer w) throws IOException { + try { + if (w != null) w.close(); + } catch (RemoteException e) { + IOException ioe = e.unwrapRemoteException(); + if (ioe != null && ioe instanceof LeaseExpiredException) { + // This can happen. Don't fail test if it does. + } else { + throw e; } - assertEquals(NUM_WRITERS * ENTRIES, count); } } @@ -595,12 +624,12 @@ public class TestHLogSplit { Reader.class); InstrumentedSequenceFileLogWriter.activateFailure = false; HLogFactory.resetLogReaderClass(); - + HLog.Writer [] writers = null; try { conf.setClass("hbase.regionserver.hlog.reader.impl", FaultySequenceFileLogReader.class, HLog.Reader.class); conf.set("faultysequencefilelogreader.failuretype", FaultySequenceFileLogReader.FailureType.BEGINNING.name()); - generateHLogs(-1); + writers = generateHLogs(-1); fs.initialize(fs.getUri(), conf); try { HLogSplitter.split(HBASEDIR, HLOGDIR, OLDLOGDIR, fs, conf); @@ -610,6 +639,9 @@ public class TestHLogSplit { NUM_WRITERS, fs.listStatus(HLOGDIR).length); } } finally { + if (writers != null) { + for (HLog.Writer writer: writers) writer.close(); + } conf.setClass("hbase.regionserver.hlog.reader.impl", backupClass, Reader.class); HLogFactory.resetLogReaderClass(); @@ -1219,15 +1251,18 @@ public class TestHLogSplit { @Test (timeout=300000) public void testSplitLogFileEmpty() throws IOException { LOG.info("testSplitLogFileEmpty"); - injectEmptyFile(".empty", true); - - fs.initialize(fs.getUri(), conf); + HLog.Writer w1 = injectEmptyFile(".empty", true); + try { + fs.initialize(fs.getUri(), conf); - HLogSplitter.split(HBASEDIR, HLOGDIR, OLDLOGDIR, fs, conf); - Path tdir = FSUtils.getTableDir(HBASEDIR, TABLE_NAME); - assertFalse(fs.exists(tdir)); + HLogSplitter.split(HBASEDIR, HLOGDIR, OLDLOGDIR, fs, conf); + Path tdir = FSUtils.getTableDir(HBASEDIR, TABLE_NAME); + assertFalse(fs.exists(tdir)); - assertEquals(0, countHLog(fs.listStatus(OLDLOGDIR)[0].getPath(), fs, conf)); + assertEquals(0, countHLog(fs.listStatus(OLDLOGDIR)[0].getPath(), fs, conf)); + } finally { + closeWriter(w1); + } } @Test (timeout=300000) @@ -1499,11 +1534,12 @@ public class TestHLogSplit { } - private void injectEmptyFile(String suffix, boolean closeFile) + private HLog.Writer injectEmptyFile(String suffix, boolean closeFile) throws IOException { HLog.Writer writer = HLogFactory.createWALWriter( fs, new Path(HLOGDIR, HLOG_FILE_PREFIX + suffix), conf); if (closeFile) writer.close(); + return writer; } @SuppressWarnings("unused")