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