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