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 org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.fs.FileSystem;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.HRegionInfo;
30  import org.apache.hadoop.hbase.HTableDescriptor;
31  import org.apache.hadoop.hbase.KeyValue;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.junit.After;
34  import org.junit.Before;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  import static org.junit.Assert.*;
38  
39  /**
40   * Test that the actions are called while playing with an HLog
41   */
42  public class TestLogActionsListener {
43  
44    protected static final Log LOG =
45        LogFactory.getLog(TestLogActionsListener.class);
46  
47    private final static HBaseTestingUtility TEST_UTIL =
48        new HBaseTestingUtility();
49  
50    private final static byte[] SOME_BYTES =  Bytes.toBytes("t");
51    private static FileSystem fs;
52    private static Path oldLogDir;
53    private static Path logDir;
54    private static Configuration conf;
55  
56    @BeforeClass
57    public static void setUpBeforeClass() throws Exception {
58      conf = TEST_UTIL.getConfiguration();
59      conf.setInt("hbase.regionserver.maxlogs", 5);
60      fs = FileSystem.get(conf);
61      oldLogDir = new Path(HBaseTestingUtility.getTestDir(),
62          HConstants.HREGION_OLDLOGDIR_NAME);
63      logDir = new Path(HBaseTestingUtility.getTestDir(),
64          HConstants.HREGION_LOGDIR_NAME);
65    }
66  
67    @Before
68    public void setUp() throws Exception {
69      fs.delete(logDir, true);
70      fs.delete(oldLogDir, true);
71    }
72  
73    @After
74    public void tearDown() throws Exception {
75      setUp();
76    }
77  
78    /**
79     * Add a bunch of dummy data and roll the logs every two insert. We
80     * should end up with 10 rolled files (plus the roll called in
81     * the constructor). Also test adding a listener while it's running.
82     */
83    @Test
84    public void testActionListener() throws Exception {
85      DummyLogActionsListener list = new DummyLogActionsListener();
86      DummyLogActionsListener laterList = new DummyLogActionsListener();
87      HLog hlog = new HLog(fs, logDir, oldLogDir, conf, null, list, null);
88      HRegionInfo hri = new HRegionInfo(new HTableDescriptor(SOME_BYTES),
89          SOME_BYTES, SOME_BYTES, false);
90  
91      for (int i = 0; i < 20; i++) {
92        byte[] b = Bytes.toBytes(i+"");
93        KeyValue kv = new KeyValue(b,b,b);
94        WALEdit edit = new WALEdit();
95        edit.add(kv);
96        HLogKey key = new HLogKey(b,b, 0, 0);
97        hlog.append(hri, key, edit);
98        if (i == 10) {
99          hlog.addLogActionsListerner(laterList);
100       }
101       if (i % 2 == 0) {
102         hlog.rollWriter();
103       }
104     }
105     assertEquals(11, list.logRollCounter);
106     assertEquals(5, laterList.logRollCounter);
107   }
108 
109   /**
110    * Just counts when methods are called
111    */
112   static class DummyLogActionsListener implements LogActionsListener {
113 
114     public int logRollCounter = 0;
115 
116     @Override
117     public void logRolled(Path newFile) {
118       logRollCounter++;
119     }
120   }
121 }