1   /**
2    * Copyright 2011 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 java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.conf.Configuration;
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.util.Bytes;
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.BeforeClass;
35  import org.junit.Test;
36  import static org.junit.Assert.*;
37  
38  /**
39   * Test that the actions are called while playing with an HLog
40   */
41  public class TestWALActionsListener {
42    protected static final Log LOG = LogFactory.getLog(TestWALActionsListener.class);
43  
44    private final static HBaseTestingUtility TEST_UTIL =
45        new HBaseTestingUtility();
46  
47    private final static byte[] SOME_BYTES =  Bytes.toBytes("t");
48    private static FileSystem fs;
49    private static Path oldLogDir;
50    private static Path logDir;
51    private static Configuration conf;
52  
53    @BeforeClass
54    public static void setUpBeforeClass() throws Exception {
55      conf = TEST_UTIL.getConfiguration();
56      conf.setInt("hbase.regionserver.maxlogs", 5);
57      fs = FileSystem.get(conf);
58      oldLogDir = new Path(TEST_UTIL.getDataTestDir(),
59          HConstants.HREGION_OLDLOGDIR_NAME);
60      logDir = new Path(TEST_UTIL.getDataTestDir(),
61          HConstants.HREGION_LOGDIR_NAME);
62    }
63  
64    @Before
65    public void setUp() throws Exception {
66      fs.delete(logDir, true);
67      fs.delete(oldLogDir, true);
68    }
69  
70    @After
71    public void tearDown() throws Exception {
72      setUp();
73    }
74  
75    /**
76     * Add a bunch of dummy data and roll the logs every two insert. We
77     * should end up with 10 rolled files (plus the roll called in
78     * the constructor). Also test adding a listener while it's running.
79     */
80    @Test
81    public void testActionListener() throws Exception {
82      DummyWALActionsListener observer = new DummyWALActionsListener();
83      List<WALActionsListener> list = new ArrayList<WALActionsListener>();
84      list.add(observer);
85      DummyWALActionsListener laterobserver = new DummyWALActionsListener();
86      HLog hlog = new HLog(fs, logDir, oldLogDir, conf, list, null);
87      HRegionInfo hri = new HRegionInfo(SOME_BYTES,
88               SOME_BYTES, SOME_BYTES, false);
89  
90      for (int i = 0; i < 20; i++) {
91        byte[] b = Bytes.toBytes(i+"");
92        KeyValue kv = new KeyValue(b,b,b);
93        WALEdit edit = new WALEdit();
94        edit.add(kv);
95        HTableDescriptor htd = new HTableDescriptor();
96        htd.addFamily(new HColumnDescriptor(b));
97  
98        HLogKey key = new HLogKey(b,b, 0, 0, HConstants.DEFAULT_CLUSTER_ID);
99        hlog.append(hri, key, edit, htd);
100       if (i == 10) {
101         hlog.registerWALActionsListener(laterobserver);
102       }
103       if (i % 2 == 0) {
104         hlog.rollWriter();
105       }
106     }
107 
108     hlog.close();
109     hlog.closeAndDelete();
110 
111     assertEquals(11, observer.logRollCounter);
112     assertEquals(5, laterobserver.logRollCounter);
113     assertEquals(2, observer.closedCount);
114   }
115 
116 
117   /**
118    * Just counts when methods are called
119    */
120   static class DummyWALActionsListener implements WALActionsListener {
121     public int logRollCounter = 0;
122     public int closedCount = 0;
123 
124     @Override
125     public void logRolled(Path newFile) {
126       logRollCounter++;
127     }
128 
129     @Override
130     public void logRollRequested() {
131       // Not interested
132     }
133 
134     @Override
135     public void visitLogEntryBeforeWrite(HRegionInfo info, HLogKey logKey,
136         WALEdit logEdit) {
137       // Not interested
138 
139     }
140 
141     @Override
142     public void logCloseRequested() {
143       closedCount++;
144     }
145 
146     public void visitLogEntryBeforeWrite(HTableDescriptor htd, HLogKey logKey, WALEdit logEdit) {
147       //To change body of implemented methods use File | Settings | File Templates.
148     }
149 
150   }
151 }