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.KeyValueTestUtil;
33  import org.apache.hadoop.hbase.MultithreadedTestUtil;
34  import org.apache.hadoop.hbase.MultithreadedTestUtil.TestContext;
35  import org.apache.hadoop.hbase.MultithreadedTestUtil.TestThread;
36  import org.apache.hadoop.hbase.regionserver.wal.HLogSplitter.EntryBuffers;
37  import org.apache.hadoop.hbase.regionserver.wal.HLogSplitter.RegionEntryBuffer;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.junit.Test;
40  import static org.mockito.Mockito.mock;
41  
42  /**
43   * Simple testing of a few HLog methods.
44   */
45  public class TestHLogMethods {
46    private static final byte[] TEST_REGION = Bytes.toBytes("test_region");;
47    private static final byte[] TEST_TABLE = Bytes.toBytes("test_table");
48    
49    private final HBaseTestingUtility util = new HBaseTestingUtility();
50  
51    /**
52     * Assert that getSplitEditFilesSorted returns files in expected order and
53     * that it skips moved-aside files.
54     * @throws IOException
55     */
56    @Test public void testGetSplitEditFilesSorted() throws IOException {
57      FileSystem fs = FileSystem.get(util.getConfiguration());
58      Path regiondir = HBaseTestingUtility.getTestDir("regiondir");
59      fs.delete(regiondir, true);
60      fs.mkdirs(regiondir);
61      Path recoverededits = HLog.getRegionDirRecoveredEditsDir(regiondir);
62      String first = HLogSplitter.formatRecoveredEditsFileName(-1);
63      createFile(fs, recoverededits, first);
64      createFile(fs, recoverededits, HLogSplitter.formatRecoveredEditsFileName(0));
65      createFile(fs, recoverededits, HLogSplitter.formatRecoveredEditsFileName(1));
66      createFile(fs, recoverededits, HLogSplitter
67          .formatRecoveredEditsFileName(11));
68      createFile(fs, recoverededits, HLogSplitter.formatRecoveredEditsFileName(2));
69      createFile(fs, recoverededits, HLogSplitter
70          .formatRecoveredEditsFileName(50));
71      String last = HLogSplitter.formatRecoveredEditsFileName(Long.MAX_VALUE);
72      createFile(fs, recoverededits, last);
73      createFile(fs, recoverededits,
74        Long.toString(Long.MAX_VALUE) + "." + System.currentTimeMillis());
75      NavigableSet<Path> files = HLog.getSplitEditFilesSorted(fs, regiondir);
76      assertEquals(7, files.size());
77      assertEquals(files.pollFirst().getName(), first);
78      assertEquals(files.pollLast().getName(), last);
79      assertEquals(files.pollFirst().getName(),
80        HLogSplitter
81          .formatRecoveredEditsFileName(0));
82      assertEquals(files.pollFirst().getName(),
83        HLogSplitter
84          .formatRecoveredEditsFileName(1));
85      assertEquals(files.pollFirst().getName(),
86        HLogSplitter
87          .formatRecoveredEditsFileName(2));
88      assertEquals(files.pollFirst().getName(),
89        HLogSplitter
90          .formatRecoveredEditsFileName(11));
91    }
92  
93    private void createFile(final FileSystem fs, final Path testdir,
94        final String name)
95    throws IOException {
96      FSDataOutputStream fdos = fs.create(new Path(testdir, name), true);
97      fdos.close();
98    }
99  
100   @Test
101   public void testRegionEntryBuffer() throws Exception {
102     HLogSplitter.RegionEntryBuffer reb = new HLogSplitter.RegionEntryBuffer(
103         TEST_TABLE, TEST_REGION);
104     assertEquals(0, reb.heapSize());
105 
106     reb.appendEntry(createTestLogEntry(1));
107     assertTrue(reb.heapSize() > 0);
108   }
109   
110   @Test
111   public void testEntrySink() throws Exception {
112     Configuration conf = new Configuration();
113     HLogSplitter splitter = HLogSplitter.createLogSplitter(
114         conf, mock(Path.class), mock(Path.class), mock(Path.class),
115         mock(FileSystem.class));
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     HLog.Entry entry = new HLog.Entry(key, edit);
164     return entry;
165   }
166 }