2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.regionserver
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertFalse
;
22 import static org
.junit
.Assert
.assertNotEquals
;
23 import static org
.junit
.Assert
.assertTrue
;
25 import org
.apache
.hadoop
.conf
.Configuration
;
26 import org
.apache
.hadoop
.fs
.FileSystem
;
27 import org
.apache
.hadoop
.fs
.Path
;
28 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
29 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
30 import org
.apache
.hadoop
.hbase
.HConstants
;
31 import org
.apache
.hadoop
.hbase
.regionserver
.wal
.FSHLog
;
32 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
33 import org
.apache
.hadoop
.hbase
.testclassification
.RegionServerTests
;
34 import org
.apache
.hadoop
.hbase
.wal
.WAL
;
35 import org
.junit
.After
;
36 import org
.junit
.Before
;
37 import org
.junit
.ClassRule
;
38 import org
.junit
.Test
;
39 import org
.junit
.experimental
.categories
.Category
;
40 import org
.mockito
.Mockito
;
41 import java
.util
.HashMap
;
42 import java
.util
.Iterator
;
45 @Category({RegionServerTests
.class, MediumTests
.class})
46 public class TestLogRoller
{
49 public static final HBaseClassTestRule CLASS_RULE
=
50 HBaseClassTestRule
.forClass(TestLogRoller
.class);
52 private static final HBaseTestingUtil TEST_UTIL
= new HBaseTestingUtil();
54 private static final int LOG_ROLL_PERIOD
= 20 * 1000;
55 private static final String LOG_DIR
= "WALs";
56 private static final String ARCHIVE_DIR
= "archiveWALs";
57 private static final String WAL_PREFIX
= "test-log-roller";
58 private static Configuration CONF
;
59 private static LogRoller ROLLER
;
60 private static Path ROOT_DIR
;
61 private static FileSystem FS
;
64 public void setup() throws Exception
{
65 CONF
= TEST_UTIL
.getConfiguration();
66 CONF
.setInt("hbase.regionserver.logroll.period", LOG_ROLL_PERIOD
);
67 CONF
.setInt(HConstants
.THREAD_WAKE_FREQUENCY
, 300);
68 ROOT_DIR
= TEST_UTIL
.getRandomDir();
69 FS
= FileSystem
.get(CONF
);
70 RegionServerServices services
= Mockito
.mock(RegionServerServices
.class);
71 Mockito
.when(services
.getConfiguration()).thenReturn(CONF
);
72 ROLLER
= new LogRoller(services
);
77 public void tearDown() throws Exception
{
80 TEST_UTIL
.shutdownMiniCluster();
84 public void testRemoveClosedWAL() throws Exception
{
85 assertEquals(0, ROLLER
.getWalNeedsRoll().size());
86 for (int i
= 1; i
<= 3; i
++) {
87 FSHLog wal
= new FSHLog(FS
, ROOT_DIR
, LOG_DIR
, ARCHIVE_DIR
, CONF
, null,
88 true, WAL_PREFIX
, getWALSuffix(i
));
92 assertEquals(3, ROLLER
.getWalNeedsRoll().size());
93 Iterator
<WAL
> it
= ROLLER
.getWalNeedsRoll().keySet().iterator();
95 assertTrue(ROLLER
.getWalNeedsRoll().containsKey(wal
));
98 Thread
.sleep(LOG_ROLL_PERIOD
+ 5000);
100 assertEquals(2, ROLLER
.getWalNeedsRoll().size());
101 assertFalse(ROLLER
.getWalNeedsRoll().containsKey(wal
));
107 Thread
.sleep(LOG_ROLL_PERIOD
+ 5000);
109 assertEquals(0, ROLLER
.getWalNeedsRoll().size());
113 * verify that each wal roll separately
116 public void testRequestRollWithMultiWal() throws Exception
{
118 Map
<FSHLog
, Path
> wals
= new HashMap
<>();
119 for (int i
= 1; i
<= 3; i
++) {
120 FSHLog wal
= new FSHLog(FS
, ROOT_DIR
, LOG_DIR
, ARCHIVE_DIR
, CONF
, null,
121 true, WAL_PREFIX
, getWALSuffix(i
));
123 wals
.put(wal
, wal
.getCurrentFileName());
129 Iterator
<Map
.Entry
<FSHLog
, Path
>> it
= wals
.entrySet().iterator();
130 Map
.Entry
<FSHLog
, Path
> walEntry
= it
.next();
131 walEntry
.getKey().requestLogRoll();
134 assertNotEquals(walEntry
.getValue(), walEntry
.getKey().getCurrentFileName());
135 walEntry
.setValue(walEntry
.getKey().getCurrentFileName());
136 while (it
.hasNext()) {
137 walEntry
= it
.next();
138 assertEquals(walEntry
.getValue(), walEntry
.getKey().getCurrentFileName());
142 Thread
.sleep(LOG_ROLL_PERIOD
+ 5000);
143 for (Map
.Entry
<FSHLog
, Path
> entry
: wals
.entrySet()) {
144 assertNotEquals(entry
.getValue(), entry
.getKey().getCurrentFileName());
145 entry
.getKey().close();
149 private static String
getWALSuffix(int id
) {