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.wal; 019 020import static org.junit.Assert.assertFalse; 021 022import java.util.ArrayList; 023import java.util.List; 024 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.hbase.HBaseTestingUtility; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.client.Put; 029import org.apache.hadoop.hbase.client.Table; 030import org.apache.hadoop.hbase.regionserver.HRegionServer; 031import org.apache.hadoop.hbase.util.Bytes; 032import org.apache.hadoop.hbase.wal.WAL; 033import org.junit.AfterClass; 034import org.junit.BeforeClass; 035import org.junit.Test; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038 039/** 040 * Tests that verifies that the log is forced to be rolled every "hbase.regionserver.logroll.period" 041 */ 042public abstract class AbstractTestLogRollPeriod { 043 private static final Logger LOG = LoggerFactory.getLogger(AbstractTestLogRollPeriod.class); 044 045 protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 046 047 private final static long LOG_ROLL_PERIOD = 4000; 048 049 @BeforeClass 050 public static void setUpBeforeClass() throws Exception { 051 // disable the ui 052 TEST_UTIL.getConfiguration().setInt("hbase.regionsever.info.port", -1); 053 054 TEST_UTIL.getConfiguration().setLong("hbase.regionserver.logroll.period", LOG_ROLL_PERIOD); 055 056 TEST_UTIL.startMiniCluster(); 057 } 058 059 @AfterClass 060 public static void tearDownAfterClass() throws Exception { 061 TEST_UTIL.shutdownMiniCluster(); 062 } 063 064 /** 065 * Tests that the LogRoller perform the roll even if there are no edits 066 */ 067 @Test 068 public void testNoEdits() throws Exception { 069 TableName tableName = TableName.valueOf("TestLogRollPeriodNoEdits"); 070 TEST_UTIL.createTable(tableName, "cf"); 071 try { 072 Table table = TEST_UTIL.getConnection().getTable(tableName); 073 try { 074 HRegionServer server = TEST_UTIL.getRSForFirstRegionInTable(tableName); 075 WAL log = server.getWAL(null); 076 checkMinLogRolls(log, 5); 077 } finally { 078 table.close(); 079 } 080 } finally { 081 TEST_UTIL.deleteTable(tableName); 082 } 083 } 084 085 /** 086 * Tests that the LogRoller perform the roll with some data in the log 087 */ 088 @Test 089 public void testWithEdits() throws Exception { 090 final TableName tableName = TableName.valueOf("TestLogRollPeriodWithEdits"); 091 final String family = "cf"; 092 093 TEST_UTIL.createTable(tableName, family); 094 try { 095 HRegionServer server = TEST_UTIL.getRSForFirstRegionInTable(tableName); 096 WAL log = server.getWAL(null); 097 final Table table = TEST_UTIL.getConnection().getTable(tableName); 098 099 Thread writerThread = new Thread("writer") { 100 @Override 101 public void run() { 102 try { 103 long row = 0; 104 while (!interrupted()) { 105 Put p = new Put(Bytes.toBytes(String.format("row%d", row))); 106 p.addColumn(Bytes.toBytes(family), Bytes.toBytes("col"), Bytes.toBytes(row)); 107 table.put(p); 108 row++; 109 110 Thread.sleep(LOG_ROLL_PERIOD / 16); 111 } 112 } catch (Exception e) { 113 LOG.warn(e.toString(), e); 114 } 115 } 116 }; 117 118 try { 119 writerThread.start(); 120 checkMinLogRolls(log, 5); 121 } finally { 122 writerThread.interrupt(); 123 writerThread.join(); 124 table.close(); 125 } 126 } finally { 127 TEST_UTIL.deleteTable(tableName); 128 } 129 } 130 131 private void checkMinLogRolls(final WAL log, final int minRolls) 132 throws Exception { 133 final List<Path> paths = new ArrayList<>(); 134 log.registerWALActionsListener(new WALActionsListener() { 135 @Override 136 public void postLogRoll(Path oldFile, Path newFile) { 137 LOG.debug("postLogRoll: oldFile="+oldFile+" newFile="+newFile); 138 paths.add(newFile); 139 } 140 }); 141 142 // Sleep until we should get at least min-LogRoll events 143 long wtime = System.currentTimeMillis(); 144 Thread.sleep((minRolls + 1) * LOG_ROLL_PERIOD); 145 // Do some extra sleep in case the machine is slow, 146 // and the log-roll is not triggered exactly on LOG_ROLL_PERIOD. 147 final int NUM_RETRIES = 1 + 8 * (minRolls - paths.size()); 148 for (int retry = 0; paths.size() < minRolls && retry < NUM_RETRIES; ++retry) { 149 Thread.sleep(LOG_ROLL_PERIOD / 4); 150 } 151 wtime = System.currentTimeMillis() - wtime; 152 LOG.info(String.format("got %d rolls after %dms (%dms each) - expected at least %d rolls", 153 paths.size(), wtime, wtime / paths.size(), minRolls)); 154 assertFalse(paths.size() < minRolls); 155 } 156}