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.*;
32  import org.apache.hadoop.hbase.regionserver.wal.HLogSplitter.EntryBuffers;
33  import org.apache.hadoop.hbase.regionserver.wal.HLogSplitter.RegionEntryBuffer;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  import static org.mockito.Mockito.mock;
39  
40  /**
41   * Simple testing of a few HLog methods.
42   */
43  @Category(SmallTests.class)
44  public class TestHLogMethods {
45    private static final byte[] TEST_REGION = Bytes.toBytes("test_region");;
46    private static final byte[] TEST_TABLE = Bytes.toBytes("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 = HLog.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      NavigableSet<Path> files = HLog.getSplitEditFilesSorted(fs, regiondir);
75      assertEquals(7, files.size());
76      assertEquals(files.pollFirst().getName(), first);
77      assertEquals(files.pollLast().getName(), last);
78      assertEquals(files.pollFirst().getName(),
79        HLogSplitter
80          .formatRecoveredEditsFileName(0));
81      assertEquals(files.pollFirst().getName(),
82        HLogSplitter
83          .formatRecoveredEditsFileName(1));
84      assertEquals(files.pollFirst().getName(),
85        HLogSplitter
86          .formatRecoveredEditsFileName(2));
87      assertEquals(files.pollFirst().getName(),
88        HLogSplitter
89          .formatRecoveredEditsFileName(11));
90    }
91  
92    private void createFile(final FileSystem fs, final Path testdir,
93        final String name)
94    throws IOException {
95      FSDataOutputStream fdos = fs.create(new Path(testdir, name), true);
96      fdos.close();
97    }
98  
99    @Test
100   public void testRegionEntryBuffer() throws Exception {
101     HLogSplitter.RegionEntryBuffer reb = new HLogSplitter.RegionEntryBuffer(
102         TEST_TABLE, TEST_REGION);
103     assertEquals(0, reb.heapSize());
104 
105     reb.appendEntry(createTestLogEntry(1));
106     assertTrue(reb.heapSize() > 0);
107   }
108   
109   @Test
110   public void testEntrySink() throws Exception {
111     Configuration conf = new Configuration();
112     HLogSplitter splitter = HLogSplitter.createLogSplitter(
113         conf, mock(Path.class), mock(Path.class), mock(Path.class),
114         mock(FileSystem.class));
115 
116     EntryBuffers sink = splitter.new EntryBuffers(1*1024*1024);
117     for (int i = 0; i < 1000; i++) {
118       HLog.Entry entry = createTestLogEntry(i);
119       sink.appendEntry(entry);
120     }
121     
122     assertTrue(sink.totalBuffered > 0);
123     long amountInChunk = sink.totalBuffered;
124     // Get a chunk
125     RegionEntryBuffer chunk = sink.getChunkToWrite();
126     assertEquals(chunk.heapSize(), amountInChunk);
127     
128     // Make sure it got marked that a thread is "working on this"
129     assertTrue(sink.isRegionCurrentlyWriting(TEST_REGION));
130 
131     // Insert some more entries
132     for (int i = 0; i < 500; i++) {
133       HLog.Entry entry = createTestLogEntry(i);
134       sink.appendEntry(entry);
135     }    
136     // Asking for another chunk shouldn't work since the first one
137     // is still writing
138     assertNull(sink.getChunkToWrite());
139     
140     // If we say we're done writing the first chunk, then we should be able
141     // to get the second
142     sink.doneWriting(chunk);
143     
144     RegionEntryBuffer chunk2 = sink.getChunkToWrite();
145     assertNotNull(chunk2);
146     assertNotSame(chunk, chunk2);
147     long amountInChunk2 = sink.totalBuffered;
148     // The second chunk had fewer rows than the first
149     assertTrue(amountInChunk2 < amountInChunk);
150     
151     sink.doneWriting(chunk2);
152     assertEquals(0, sink.totalBuffered);
153   }
154   
155   private HLog.Entry createTestLogEntry(int i) {
156     long seq = i;
157     long now = i * 1000;
158 
159     WALEdit edit = new WALEdit();
160     edit.add(KeyValueTestUtil.create("row", "fam", "qual", 1234, "val"));
161     HLogKey key = new HLogKey(TEST_REGION, TEST_TABLE, seq, now,
162         HConstants.DEFAULT_CLUSTER_ID);
163     HLog.Entry entry = new HLog.Entry(key, edit);
164     return entry;
165   }
166 
167   @org.junit.Rule
168   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
169     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
170 }
171