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.FSUtils;
042import org.apache.hadoop.hbase.wal.WALSplitter.PipelineController;
043import org.apache.hadoop.hbase.wal.WALSplitter.RegionEntryBuffer;
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 =
060      TableName.valueOf("test_table");
061
062  private final HBaseTestingUtility util = new HBaseTestingUtility();
063
064  @Test
065  public void testServerNameFromWAL() throws Exception {
066    Path walPath = new Path("/hbase/WALs/regionserver-2.example.com,22101,1487767381290",
067        "regionserver-2.example.com%2C22101%2C1487767381290.null0.1487785392316");
068    ServerName name = AbstractFSWALProvider.getServerNameFromWALDirectoryName(walPath);
069    assertEquals(ServerName.valueOf("regionserver-2.example.com", 22101, 1487767381290L), name);
070  }
071
072  @Test
073  public void testServerNameFromTestWAL() throws Exception {
074    Path walPath = new Path(
075        "/user/example/test-data/12ff1404-68c6-4715-a4b9-775e763842bc/WALs/TestWALRecordReader",
076        "TestWALRecordReader.default.1487787939118");
077    ServerName name = AbstractFSWALProvider.getServerNameFromWALDirectoryName(walPath);
078    assertNull(name);
079  }
080
081  /**
082   * Assert that getSplitEditFilesSorted returns files in expected order and
083   * that it skips moved-aside files.
084   * @throws IOException
085   */
086  @Test public void testGetSplitEditFilesSorted() throws IOException {
087    FileSystem fs = FileSystem.get(util.getConfiguration());
088    Path regiondir = util.getDataTestDir("regiondir");
089    fs.delete(regiondir, true);
090    fs.mkdirs(regiondir);
091    Path recoverededits = WALSplitUtil.getRegionDirRecoveredEditsDir(regiondir);
092    String first = WALSplitUtil.formatRecoveredEditsFileName(-1);
093    createFile(fs, recoverededits, first);
094    createFile(fs, recoverededits, WALSplitUtil.formatRecoveredEditsFileName(0));
095    createFile(fs, recoverededits, WALSplitUtil.formatRecoveredEditsFileName(1));
096    createFile(fs, recoverededits, WALSplitUtil
097        .formatRecoveredEditsFileName(11));
098    createFile(fs, recoverededits, WALSplitUtil.formatRecoveredEditsFileName(2));
099    createFile(fs, recoverededits, WALSplitUtil
100        .formatRecoveredEditsFileName(50));
101    String last = WALSplitUtil.formatRecoveredEditsFileName(Long.MAX_VALUE);
102    createFile(fs, recoverededits, last);
103    createFile(fs, recoverededits,
104      Long.toString(Long.MAX_VALUE) + "." + System.currentTimeMillis());
105
106    final Configuration walConf = new Configuration(util.getConfiguration());
107    FSUtils.setRootDir(walConf, regiondir);
108    (new WALFactory(walConf, "dummyLogName")).getWAL(null);
109
110    NavigableSet<Path> files = WALSplitUtil.getSplitEditFilesSorted(fs, regiondir);
111    assertEquals(7, files.size());
112    assertEquals(files.pollFirst().getName(), first);
113    assertEquals(files.pollLast().getName(), last);
114    assertEquals(files.pollFirst().getName(),
115        WALSplitUtil
116        .formatRecoveredEditsFileName(0));
117    assertEquals(files.pollFirst().getName(),
118        WALSplitUtil
119        .formatRecoveredEditsFileName(1));
120    assertEquals(files.pollFirst().getName(),
121        WALSplitUtil
122        .formatRecoveredEditsFileName(2));
123    assertEquals(files.pollFirst().getName(),
124        WALSplitUtil
125        .formatRecoveredEditsFileName(11));
126  }
127
128  private void createFile(final FileSystem fs, final Path testdir,
129      final String name)
130  throws IOException {
131    FSDataOutputStream fdos = fs.create(new Path(testdir, name), true);
132    fdos.close();
133  }
134
135  @Test
136  public void testRegionEntryBuffer() throws Exception {
137    WALSplitter.RegionEntryBuffer reb = new WALSplitter.RegionEntryBuffer(
138        TEST_TABLE, TEST_REGION);
139    assertEquals(0, reb.heapSize());
140
141    reb.appendEntry(createTestLogEntry(1));
142    assertTrue(reb.heapSize() > 0);
143  }
144
145  @Test
146  public void testEntrySink() throws Exception {
147    EntryBuffers sink = new EntryBuffers(new PipelineController(), 1*1024*1024);
148    for (int i = 0; i < 1000; i++) {
149      WAL.Entry entry = createTestLogEntry(i);
150      sink.appendEntry(entry);
151    }
152
153    assertTrue(sink.totalBuffered > 0);
154    long amountInChunk = sink.totalBuffered;
155    // Get a chunk
156    RegionEntryBuffer chunk = sink.getChunkToWrite();
157    assertEquals(chunk.heapSize(), amountInChunk);
158
159    // Make sure it got marked that a thread is "working on this"
160    assertTrue(sink.isRegionCurrentlyWriting(TEST_REGION));
161
162    // Insert some more entries
163    for (int i = 0; i < 500; i++) {
164      WAL.Entry entry = createTestLogEntry(i);
165      sink.appendEntry(entry);
166    }
167    // Asking for another chunk shouldn't work since the first one
168    // is still writing
169    assertNull(sink.getChunkToWrite());
170
171    // If we say we're done writing the first chunk, then we should be able
172    // to get the second
173    sink.doneWriting(chunk);
174
175    RegionEntryBuffer chunk2 = sink.getChunkToWrite();
176    assertNotNull(chunk2);
177    assertNotSame(chunk, chunk2);
178    long amountInChunk2 = sink.totalBuffered;
179    // The second chunk had fewer rows than the first
180    assertTrue(amountInChunk2 < amountInChunk);
181
182    sink.doneWriting(chunk2);
183    assertEquals(0, sink.totalBuffered);
184  }
185
186  private WAL.Entry createTestLogEntry(int i) {
187    long seq = i;
188    long now = i * 1000;
189
190    WALEdit edit = new WALEdit();
191    edit.add(KeyValueTestUtil.create("row", "fam", "qual", 1234, "val"));
192    WALKeyImpl key = new WALKeyImpl(TEST_REGION, TEST_TABLE, seq, now,
193        HConstants.DEFAULT_CLUSTER_ID);
194    WAL.Entry entry = new WAL.Entry(key, edit);
195    return entry;
196  }
197
198}
199