001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.wal; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNotNull; 022import static org.junit.Assert.assertNotSame; 023import static org.junit.Assert.assertNull; 024import static org.junit.Assert.assertTrue; 025 026import java.io.IOException; 027import java.util.NavigableSet; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.fs.FSDataOutputStream; 030import org.apache.hadoop.fs.FileSystem; 031import org.apache.hadoop.fs.Path; 032import org.apache.hadoop.hbase.HBaseClassTestRule; 033import org.apache.hadoop.hbase.HBaseTestingUtility; 034import org.apache.hadoop.hbase.HConstants; 035import org.apache.hadoop.hbase.KeyValueTestUtil; 036import org.apache.hadoop.hbase.ServerName; 037import org.apache.hadoop.hbase.TableName; 038import org.apache.hadoop.hbase.testclassification.RegionServerTests; 039import org.apache.hadoop.hbase.testclassification.SmallTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.apache.hadoop.hbase.util.CommonFSUtils; 042import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 043import org.apache.hadoop.hbase.wal.WALSplitter.PipelineController; 044import org.junit.ClassRule; 045import org.junit.Test; 046import org.junit.experimental.categories.Category; 047 048/** 049 * Simple testing of a few WAL methods. 050 */ 051@Category({ RegionServerTests.class, SmallTests.class }) 052public class TestWALMethods { 053 054 @ClassRule 055 public static final HBaseClassTestRule CLASS_RULE = 056 HBaseClassTestRule.forClass(TestWALMethods.class); 057 058 private static final byte[] TEST_REGION = Bytes.toBytes("test_region"); 059 private static final TableName TEST_TABLE = TableName.valueOf("test_table"); 060 061 private final HBaseTestingUtility util = new HBaseTestingUtility(); 062 063 @Test 064 public void testServerNameFromWAL() throws Exception { 065 Path walPath = new Path("/hbase/WALs/regionserver-2.example.com,22101,1487767381290", 066 "regionserver-2.example.com%2C22101%2C1487767381290.null0.1487785392316"); 067 ServerName name = AbstractFSWALProvider.getServerNameFromWALDirectoryName(walPath); 068 assertEquals(ServerName.valueOf("regionserver-2.example.com", 22101, 1487767381290L), name); 069 } 070 071 @Test 072 public void testServerNameFromTestWAL() throws Exception { 073 Path walPath = new Path( 074 "/user/example/test-data/12ff1404-68c6-4715-a4b9-775e763842bc/WALs/TestWALRecordReader", 075 "TestWALRecordReader.default.1487787939118"); 076 ServerName name = AbstractFSWALProvider.getServerNameFromWALDirectoryName(walPath); 077 assertNull(name); 078 } 079 080 /** 081 * Assert that getSplitEditFilesSorted returns files in expected order and that it skips 082 * moved-aside files. 083 */ 084 @Test 085 public void testGetSplitEditFilesSorted() throws IOException { 086 FileSystem fs = FileSystem.get(util.getConfiguration()); 087 Path regiondir = util.getDataTestDir("regiondir"); 088 fs.delete(regiondir, true); 089 fs.mkdirs(regiondir); 090 Path recoverededits = WALSplitUtil.getRegionDirRecoveredEditsDir(regiondir); 091 String first = WALSplitUtil.formatRecoveredEditsFileName(-1); 092 createFile(fs, recoverededits, first); 093 createFile(fs, recoverededits, WALSplitUtil.formatRecoveredEditsFileName(0)); 094 createFile(fs, recoverededits, WALSplitUtil.formatRecoveredEditsFileName(1)); 095 createFile(fs, recoverededits, WALSplitUtil.formatRecoveredEditsFileName(11)); 096 createFile(fs, recoverededits, WALSplitUtil.formatRecoveredEditsFileName(2)); 097 createFile(fs, recoverededits, WALSplitUtil.formatRecoveredEditsFileName(50)); 098 String last = WALSplitUtil.formatRecoveredEditsFileName(Long.MAX_VALUE); 099 createFile(fs, recoverededits, last); 100 createFile(fs, recoverededits, 101 Long.toString(Long.MAX_VALUE) + "." + EnvironmentEdgeManager.currentTime()); 102 103 final Configuration walConf = new Configuration(util.getConfiguration()); 104 CommonFSUtils.setRootDir(walConf, regiondir); 105 (new WALFactory(walConf, "dummyLogName")).getWAL(null); 106 107 NavigableSet<Path> files = WALSplitUtil.getSplitEditFilesSorted(fs, regiondir); 108 assertEquals(7, files.size()); 109 assertEquals(files.pollFirst().getName(), first); 110 assertEquals(files.pollLast().getName(), last); 111 assertEquals(files.pollFirst().getName(), WALSplitUtil.formatRecoveredEditsFileName(0)); 112 assertEquals(files.pollFirst().getName(), WALSplitUtil.formatRecoveredEditsFileName(1)); 113 assertEquals(files.pollFirst().getName(), WALSplitUtil.formatRecoveredEditsFileName(2)); 114 assertEquals(files.pollFirst().getName(), WALSplitUtil.formatRecoveredEditsFileName(11)); 115 } 116 117 private void createFile(final FileSystem fs, final Path testdir, final String name) 118 throws IOException { 119 FSDataOutputStream fdos = fs.create(new Path(testdir, name), true); 120 fdos.close(); 121 } 122 123 @Test 124 public void testRegionEntryBuffer() throws Exception { 125 EntryBuffers.RegionEntryBuffer reb = 126 new EntryBuffers.RegionEntryBuffer(TEST_TABLE, TEST_REGION); 127 assertEquals(0, reb.heapSize()); 128 129 reb.appendEntry(createTestLogEntry(1)); 130 assertTrue(reb.heapSize() > 0); 131 } 132 133 @Test 134 public void testEntrySink() throws Exception { 135 EntryBuffers sink = new EntryBuffers(new PipelineController(), 1 * 1024 * 1024); 136 for (int i = 0; i < 1000; i++) { 137 WAL.Entry entry = createTestLogEntry(i); 138 sink.appendEntry(entry); 139 } 140 141 assertTrue(sink.totalBuffered > 0); 142 long amountInChunk = sink.totalBuffered; 143 // Get a chunk 144 EntryBuffers.RegionEntryBuffer chunk = sink.getChunkToWrite(); 145 assertEquals(chunk.heapSize(), amountInChunk); 146 147 // Make sure it got marked that a thread is "working on this" 148 assertTrue(sink.isRegionCurrentlyWriting(TEST_REGION)); 149 150 // Insert some more entries 151 for (int i = 0; i < 500; i++) { 152 WAL.Entry entry = createTestLogEntry(i); 153 sink.appendEntry(entry); 154 } 155 // Asking for another chunk shouldn't work since the first one 156 // is still writing 157 assertNull(sink.getChunkToWrite()); 158 159 // If we say we're done writing the first chunk, then we should be able 160 // to get the second 161 sink.doneWriting(chunk); 162 163 EntryBuffers.RegionEntryBuffer chunk2 = sink.getChunkToWrite(); 164 assertNotNull(chunk2); 165 assertNotSame(chunk, chunk2); 166 long amountInChunk2 = sink.totalBuffered; 167 // The second chunk had fewer rows than the first 168 assertTrue(amountInChunk2 < amountInChunk); 169 170 sink.doneWriting(chunk2); 171 assertEquals(0, sink.totalBuffered); 172 } 173 174 private WAL.Entry createTestLogEntry(int i) { 175 long seq = i; 176 long now = i * 1000; 177 178 WALEdit edit = new WALEdit(); 179 edit.add(KeyValueTestUtil.create("row", "fam", "qual", 1234, "val")); 180 WALKeyImpl key = 181 new WALKeyImpl(TEST_REGION, TEST_TABLE, seq, now, HConstants.DEFAULT_CLUSTER_ID); 182 WAL.Entry entry = new WAL.Entry(key, edit); 183 return entry; 184 } 185 186}