View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver.wal;
20  
21  import static org.junit.Assert.*;
22  
23  import java.io.IOException;
24  import java.util.NavigableSet;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.FSDataOutputStream;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.*;
31  import org.apache.hadoop.hbase.regionserver.wal.HLogSplitter.EntryBuffers;
32  import org.apache.hadoop.hbase.regionserver.wal.HLogSplitter.RegionEntryBuffer;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  import static org.mockito.Mockito.mock;
38  
39  /**
40   * Simple testing of a few HLog methods.
41   */
42  @Category(SmallTests.class)
43  public class TestHLogMethods {
44    private static final byte[] TEST_REGION = Bytes.toBytes("test_region");;
45    private static final TableName TEST_TABLE =
46        TableName.valueOf("test_table");
47    
48    private final HBaseTestingUtility util = new HBaseTestingUtility();
49  
50    /**
51     * Assert that getSplitEditFilesSorted returns files in expected order and
52     * that it skips moved-aside files.
53     * @throws IOException
54     */
55    @Test public void testGetSplitEditFilesSorted() throws IOException {
56      FileSystem fs = FileSystem.get(util.getConfiguration());
57      Path regiondir = util.getDataTestDir("regiondir");
58      fs.delete(regiondir, true);
59      fs.mkdirs(regiondir);
60      Path recoverededits = HLogUtil.getRegionDirRecoveredEditsDir(regiondir);
61      String first = HLogSplitter.formatRecoveredEditsFileName(-1);
62      createFile(fs, recoverededits, first);
63      createFile(fs, recoverededits, HLogSplitter.formatRecoveredEditsFileName(0));
64      createFile(fs, recoverededits, HLogSplitter.formatRecoveredEditsFileName(1));
65      createFile(fs, recoverededits, HLogSplitter
66          .formatRecoveredEditsFileName(11));
67      createFile(fs, recoverededits, HLogSplitter.formatRecoveredEditsFileName(2));
68      createFile(fs, recoverededits, HLogSplitter
69          .formatRecoveredEditsFileName(50));
70      String last = HLogSplitter.formatRecoveredEditsFileName(Long.MAX_VALUE);
71      createFile(fs, recoverededits, last);
72      createFile(fs, recoverededits,
73        Long.toString(Long.MAX_VALUE) + "." + System.currentTimeMillis());
74  
75      HLogFactory.createHLog(fs, regiondir, "dummyLogName", util.getConfiguration());
76      NavigableSet<Path> files = HLogUtil.getSplitEditFilesSorted(fs, regiondir);
77      assertEquals(7, files.size());
78      assertEquals(files.pollFirst().getName(), first);
79      assertEquals(files.pollLast().getName(), last);
80      assertEquals(files.pollFirst().getName(),
81        HLogSplitter
82          .formatRecoveredEditsFileName(0));
83      assertEquals(files.pollFirst().getName(),
84        HLogSplitter
85          .formatRecoveredEditsFileName(1));
86      assertEquals(files.pollFirst().getName(),
87        HLogSplitter
88          .formatRecoveredEditsFileName(2));
89      assertEquals(files.pollFirst().getName(),
90        HLogSplitter
91          .formatRecoveredEditsFileName(11));
92    }
93  
94    private void createFile(final FileSystem fs, final Path testdir,
95        final String name)
96    throws IOException {
97      FSDataOutputStream fdos = fs.create(new Path(testdir, name), true);
98      fdos.close();
99    }
100 
101   @Test
102   public void testRegionEntryBuffer() throws Exception {
103     HLogSplitter.RegionEntryBuffer reb = new HLogSplitter.RegionEntryBuffer(
104         TEST_TABLE, TEST_REGION);
105     assertEquals(0, reb.heapSize());
106 
107     reb.appendEntry(createTestLogEntry(1));
108     assertTrue(reb.heapSize() > 0);
109   }
110   
111   @Test
112   public void testEntrySink() throws Exception {
113     Configuration conf = new Configuration();
114     HLogSplitter splitter = new HLogSplitter(
115       conf, mock(Path.class), mock(FileSystem.class), null, null);
116 
117     EntryBuffers sink = splitter.new EntryBuffers(1*1024*1024);
118     for (int i = 0; i < 1000; i++) {
119       HLog.Entry entry = createTestLogEntry(i);
120       sink.appendEntry(entry);
121     }
122     
123     assertTrue(sink.totalBuffered > 0);
124     long amountInChunk = sink.totalBuffered;
125     // Get a chunk
126     RegionEntryBuffer chunk = sink.getChunkToWrite();
127     assertEquals(chunk.heapSize(), amountInChunk);
128     
129     // Make sure it got marked that a thread is "working on this"
130     assertTrue(sink.isRegionCurrentlyWriting(TEST_REGION));
131 
132     // Insert some more entries
133     for (int i = 0; i < 500; i++) {
134       HLog.Entry entry = createTestLogEntry(i);
135       sink.appendEntry(entry);
136     }    
137     // Asking for another chunk shouldn't work since the first one
138     // is still writing
139     assertNull(sink.getChunkToWrite());
140     
141     // If we say we're done writing the first chunk, then we should be able
142     // to get the second
143     sink.doneWriting(chunk);
144     
145     RegionEntryBuffer chunk2 = sink.getChunkToWrite();
146     assertNotNull(chunk2);
147     assertNotSame(chunk, chunk2);
148     long amountInChunk2 = sink.totalBuffered;
149     // The second chunk had fewer rows than the first
150     assertTrue(amountInChunk2 < amountInChunk);
151     
152     sink.doneWriting(chunk2);
153     assertEquals(0, sink.totalBuffered);
154   }
155   
156   private HLog.Entry createTestLogEntry(int i) {
157     long seq = i;
158     long now = i * 1000;
159 
160     WALEdit edit = new WALEdit();
161     edit.add(KeyValueTestUtil.create("row", "fam", "qual", 1234, "val"));
162     HLogKey key = new HLogKey(TEST_REGION, TEST_TABLE, seq, now,
163         HConstants.DEFAULT_CLUSTER_ID);
164     HLog.Entry entry = new HLog.Entry(key, edit);
165     return entry;
166   }
167 
168 }
169