1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver.wal;
19
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.assertFalse;
22
23 import java.util.List;
24 import java.util.ArrayList;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.fs.Path;
29 import org.apache.hadoop.hbase.HBaseTestingUtility;
30 import org.apache.hadoop.hbase.testclassification.MediumTests;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.client.HTable;
33 import org.apache.hadoop.hbase.client.Put;
34 import org.apache.hadoop.hbase.client.Table;
35 import org.apache.hadoop.hbase.regionserver.HRegionServer;
36 import org.apache.hadoop.hbase.wal.WAL;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42
43
44
45
46 @Category(MediumTests.class)
47 public class TestLogRollPeriod {
48 private static final Log LOG = LogFactory.getLog(TestLogRolling.class);
49
50 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
51
52 private final static long LOG_ROLL_PERIOD = 4000;
53
54 @BeforeClass
55 public static void setUpBeforeClass() throws Exception {
56
57 TEST_UTIL.getConfiguration().setInt("hbase.regionsever.info.port", -1);
58
59 TEST_UTIL.getConfiguration().setLong("hbase.regionserver.logroll.period", LOG_ROLL_PERIOD);
60
61 TEST_UTIL.startMiniCluster();
62 }
63
64 @AfterClass
65 public static void tearDownAfterClass() throws Exception {
66 TEST_UTIL.shutdownMiniCluster();
67 }
68
69
70
71
72 @Test
73 public void testNoEdits() throws Exception {
74 TableName tableName = TableName.valueOf("TestLogRollPeriodNoEdits");
75 TEST_UTIL.createTable(tableName, "cf");
76 try {
77 Table table = new HTable(TEST_UTIL.getConfiguration(), tableName);
78 try {
79 HRegionServer server = TEST_UTIL.getRSForFirstRegionInTable(tableName);
80 WAL log = server.getWAL(null);
81 checkMinLogRolls(log, 5);
82 } finally {
83 table.close();
84 }
85 } finally {
86 TEST_UTIL.deleteTable(tableName);
87 }
88 }
89
90
91
92
93 @Test(timeout=60000)
94 public void testWithEdits() throws Exception {
95 final TableName tableName = TableName.valueOf("TestLogRollPeriodWithEdits");
96 final String family = "cf";
97
98 TEST_UTIL.createTable(tableName, family);
99 try {
100 HRegionServer server = TEST_UTIL.getRSForFirstRegionInTable(tableName);
101 WAL log = server.getWAL(null);
102 final Table table = new HTable(TEST_UTIL.getConfiguration(), tableName);
103
104 Thread writerThread = new Thread("writer") {
105 @Override
106 public void run() {
107 try {
108 long row = 0;
109 while (!interrupted()) {
110 Put p = new Put(Bytes.toBytes(String.format("row%d", row)));
111 p.add(Bytes.toBytes(family), Bytes.toBytes("col"), Bytes.toBytes(row));
112 table.put(p);
113 row++;
114
115 Thread.sleep(LOG_ROLL_PERIOD / 16);
116 }
117 } catch (Exception e) {
118 LOG.warn(e);
119 }
120 }
121 };
122
123 try {
124 writerThread.start();
125 checkMinLogRolls(log, 5);
126 } finally {
127 writerThread.interrupt();
128 writerThread.join();
129 table.close();
130 }
131 } finally {
132 TEST_UTIL.deleteTable(tableName);
133 }
134 }
135
136 private void checkMinLogRolls(final WAL log, final int minRolls)
137 throws Exception {
138 final List<Path> paths = new ArrayList<Path>();
139 log.registerWALActionsListener(new WALActionsListener.Base() {
140 @Override
141 public void postLogRoll(Path oldFile, Path newFile) {
142 LOG.debug("postLogRoll: oldFile="+oldFile+" newFile="+newFile);
143 paths.add(newFile);
144 }
145 });
146
147
148 long wtime = System.currentTimeMillis();
149 Thread.sleep((minRolls + 1) * LOG_ROLL_PERIOD);
150
151
152 final int NUM_RETRIES = 1 + 8 * (minRolls - paths.size());
153 for (int retry = 0; paths.size() < minRolls && retry < NUM_RETRIES; ++retry) {
154 Thread.sleep(LOG_ROLL_PERIOD / 4);
155 }
156 wtime = System.currentTimeMillis() - wtime;
157 LOG.info(String.format("got %d rolls after %dms (%dms each) - expected at least %d rolls",
158 paths.size(), wtime, wtime / paths.size(), minRolls));
159 assertFalse(paths.size() < minRolls);
160 }
161 }