View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver.wal;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.concurrent.atomic.AtomicLong;
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 org.junit.experimental.categories.Category;
37  
38  import static org.junit.Assert.*;
39  
40  /**
41   * Test that the actions are called while playing with an HLog
42   */
43  @Category(SmallTests.class)
44  public class TestWALActionsListener {
45    protected static final Log LOG = LogFactory.getLog(TestWALActionsListener.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 String logName;
55    private static Configuration conf;
56  
57    @BeforeClass
58    public static void setUpBeforeClass() throws Exception {
59      conf = TEST_UTIL.getConfiguration();
60      conf.setInt("hbase.regionserver.maxlogs", 5);
61      fs = FileSystem.get(conf);
62      oldLogDir = new Path(TEST_UTIL.getDataTestDir(),
63          HConstants.HREGION_OLDLOGDIR_NAME);
64      logName = HConstants.HREGION_LOGDIR_NAME;
65      logDir = new Path(TEST_UTIL.getDataTestDir(),
66          logName);
67    }
68  
69    @Before
70    public void setUp() throws Exception {
71      fs.delete(logDir, true);
72      fs.delete(oldLogDir, true);
73    }
74  
75    @After
76    public void tearDown() throws Exception {
77      setUp();
78    }
79  
80    /**
81     * Add a bunch of dummy data and roll the logs every two insert. We
82     * should end up with 10 rolled files (plus the roll called in
83     * the constructor). Also test adding a listener while it's running.
84     */
85    @Test
86    public void testActionListener() throws Exception {
87      DummyWALActionsListener observer = new DummyWALActionsListener();
88      List<WALActionsListener> list = new ArrayList<WALActionsListener>();
89      list.add(observer);
90      DummyWALActionsListener laterobserver = new DummyWALActionsListener();
91      HLog hlog = HLogFactory.createHLog(fs, TEST_UTIL.getDataTestDir(), logName,
92                                         conf, list, null);
93      final AtomicLong sequenceId = new AtomicLong(1);
94      HRegionInfo hri = new HRegionInfo(TableName.valueOf(SOME_BYTES),
95               SOME_BYTES, SOME_BYTES, false);
96  
97      for (int i = 0; i < 20; i++) {
98        byte[] b = Bytes.toBytes(i+"");
99        KeyValue kv = new KeyValue(b,b,b);
100       WALEdit edit = new WALEdit();
101       edit.add(kv);
102       HTableDescriptor htd = new HTableDescriptor();
103       htd.addFamily(new HColumnDescriptor(b));
104 
105       hlog.append(hri, TableName.valueOf(b), edit, 0, htd, sequenceId);
106       if (i == 10) {
107         hlog.registerWALActionsListener(laterobserver);
108       }
109       if (i % 2 == 0) {
110         hlog.rollWriter();
111       }
112     }
113 
114     hlog.close();
115     hlog.closeAndDelete();
116 
117     assertEquals(11, observer.preLogRollCounter);
118     assertEquals(11, observer.postLogRollCounter);
119     assertEquals(5, laterobserver.preLogRollCounter);
120     assertEquals(5, laterobserver.postLogRollCounter);
121     assertEquals(1, observer.closedCount);
122   }
123 
124 
125   /**
126    * Just counts when methods are called
127    */
128   static class DummyWALActionsListener implements WALActionsListener {
129     public int preLogRollCounter = 0;
130     public int postLogRollCounter = 0;
131     public int closedCount = 0;
132 
133     @Override
134     public void preLogRoll(Path oldFile, Path newFile) {
135       preLogRollCounter++;
136     }
137 
138     @Override
139     public void postLogRoll(Path oldFile, Path newFile) {
140       postLogRollCounter++;
141     }
142 
143     @Override
144     public void preLogArchive(Path oldFile, Path newFile) {
145       // Not interested
146     }
147 
148     @Override
149     public void postLogArchive(Path oldFile, Path newFile) {
150       // Not interested
151     }
152 
153     @Override
154     public void logRollRequested() {
155       // Not interested
156     }
157 
158     @Override
159     public void visitLogEntryBeforeWrite(HRegionInfo info, HLogKey logKey,
160         WALEdit logEdit) {
161       // Not interested
162 
163     }
164 
165     @Override
166     public void logCloseRequested() {
167       closedCount++;
168     }
169 
170     public void visitLogEntryBeforeWrite(HTableDescriptor htd, HLogKey logKey, WALEdit logEdit) {
171       //To change body of implemented methods use File | Settings | File Templates.
172     }
173 
174   }
175 
176 }
177