1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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(TEST_UTIL.getTestDir(),
62 HConstants.HREGION_OLDLOGDIR_NAME);
63 logDir = new Path(TEST_UTIL.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
80
81
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
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 }