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