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;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.testclassification.MediumTests;
023import org.apache.hadoop.hbase.testclassification.MiscTests;
024import org.junit.After;
025import org.junit.Before;
026import org.junit.ClassRule;
027import org.junit.Test;
028import org.junit.experimental.categories.Category;
029
030/**
031 * Test whether background cleanup of MovedRegion entries is happening
032 */
033@Category({ MiscTests.class, MediumTests.class })
034public class TestMovedRegionsCleaner {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038      HBaseClassTestRule.forClass(TestMovedRegionsCleaner.class);
039
040  private final HBaseTestingUtility UTIL = new HBaseTestingUtility();
041
042  public static int numCalls = 0;
043
044  private static class TestMockRegionServer extends MiniHBaseCluster.MiniHBaseClusterRegionServer {
045
046    public TestMockRegionServer(Configuration conf) throws IOException, InterruptedException {
047      super(conf);
048    }
049
050    @Override
051    protected int movedRegionCleanerPeriod() {
052      return 500;
053    }
054
055    @Override protected void cleanMovedRegions() {
056      // count the number of calls that are being made to this
057      //
058      numCalls++;
059      super.cleanMovedRegions();
060    }
061  }
062
063  @After public void after() throws Exception {
064    UTIL.shutdownMiniCluster();
065  }
066
067  @Before public void before() throws Exception {
068    UTIL.getConfiguration()
069        .setStrings(HConstants.REGION_SERVER_IMPL, TestMockRegionServer.class.getName());
070    UTIL.startMiniCluster(1);
071  }
072
073  /**
074   * Start the cluster, wait for some time and verify that the background
075   * MovedRegion cleaner indeed gets called
076   *
077   * @throws IOException
078   * @throws InterruptedException
079   */
080  @Test public void testMovedRegionsCleaner() throws IOException, InterruptedException {
081    // We need to sleep long enough to trigger at least one round of background calls
082    // to MovedRegionCleaner happen. Currently the period is set to 500ms.
083    // Setting the sleep here for 2s just to be safe
084    //
085    UTIL.waitFor(2000, new Waiter.Predicate<IOException>() {
086      @Override
087      public boolean evaluate() throws IOException {
088
089        // verify that there was at least one call to the cleanMovedRegions function
090        //
091        return numCalls > 0;
092      }
093    });
094  }
095}