001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.regionserver; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNotEquals; 022 023import java.util.HashMap; 024import java.util.Iterator; 025import java.util.Map; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.fs.FileSystem; 028import org.apache.hadoop.fs.Path; 029import org.apache.hadoop.hbase.HBaseClassTestRule; 030import org.apache.hadoop.hbase.HBaseTestingUtility; 031import org.apache.hadoop.hbase.HConstants; 032import org.apache.hadoop.hbase.regionserver.wal.FSHLog; 033import org.apache.hadoop.hbase.testclassification.MediumTests; 034import org.apache.hadoop.hbase.testclassification.RegionServerTests; 035import org.junit.After; 036import org.junit.Before; 037import org.junit.ClassRule; 038import org.junit.Test; 039import org.junit.experimental.categories.Category; 040import org.mockito.Mockito; 041 042@Category({ RegionServerTests.class, MediumTests.class }) 043public class TestLogRoller { 044 045 @ClassRule 046 public static final HBaseClassTestRule CLASS_RULE = 047 HBaseClassTestRule.forClass(TestLogRoller.class); 048 049 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 050 051 private static final int LOG_ROLL_PERIOD = 20 * 1000; 052 private static final String LOG_DIR = "WALs"; 053 private static final String ARCHIVE_DIR = "archiveWALs"; 054 private static final String WAL_PREFIX = "test-log-roller"; 055 private static Configuration CONF; 056 private static LogRoller ROLLER; 057 private static Path ROOT_DIR; 058 private static FileSystem FS; 059 060 @Before 061 public void setup() throws Exception { 062 CONF = TEST_UTIL.getConfiguration(); 063 CONF.setInt("hbase.regionserver.logroll.period", LOG_ROLL_PERIOD); 064 CONF.setInt(HConstants.THREAD_WAKE_FREQUENCY, 300); 065 ROOT_DIR = TEST_UTIL.getRandomDir(); 066 FS = FileSystem.get(CONF); 067 RegionServerServices services = Mockito.mock(RegionServerServices.class); 068 Mockito.when(services.getConfiguration()).thenReturn(CONF); 069 ROLLER = new LogRoller(services); 070 ROLLER.start(); 071 } 072 073 @After 074 public void tearDown() throws Exception { 075 ROLLER.close(); 076 FS.close(); 077 TEST_UTIL.shutdownMiniCluster(); 078 } 079 080 /** 081 * verify that each wal roll separately 082 */ 083 @Test 084 public void testRequestRollWithMultiWal() throws Exception { 085 // add multiple wal 086 Map<FSHLog, Path> wals = new HashMap<>(); 087 for (int i = 1; i <= 3; i++) { 088 FSHLog wal = 089 new FSHLog(FS, ROOT_DIR, LOG_DIR, ARCHIVE_DIR, CONF, null, true, WAL_PREFIX, "." + i); 090 wal.init(); 091 wals.put(wal, wal.getCurrentFileName()); 092 ROLLER.addWAL(wal); 093 Thread.sleep(1000); 094 } 095 096 // request roll 097 Iterator<Map.Entry<FSHLog, Path>> it = wals.entrySet().iterator(); 098 Map.Entry<FSHLog, Path> walEntry = it.next(); 099 walEntry.getKey().requestLogRoll(); 100 Thread.sleep(5000); 101 102 assertNotEquals(walEntry.getValue(), walEntry.getKey().getCurrentFileName()); 103 walEntry.setValue(walEntry.getKey().getCurrentFileName()); 104 while (it.hasNext()) { 105 walEntry = it.next(); 106 assertEquals(walEntry.getValue(), walEntry.getKey().getCurrentFileName()); 107 } 108 109 // period roll 110 Thread.sleep(LOG_ROLL_PERIOD + 5000); 111 for (Map.Entry<FSHLog, Path> entry : wals.entrySet()) { 112 assertNotEquals(entry.getValue(), entry.getKey().getCurrentFileName()); 113 entry.getKey().close(); 114 } 115 } 116}