View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase;
20  
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.testclassification.MediumTests;
26  import org.junit.After;
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  import java.io.IOException;
32  
33  
34  /**
35   * Test whether background cleanup of MovedRegion entries is happening
36   */
37  @Category( MediumTests.class ) public class TestMovedRegionsCleaner {
38  
39    public static final Log LOG = LogFactory.getLog(TestRegionRebalancing.class);
40    private final HBaseTestingUtility UTIL = new HBaseTestingUtility();
41  
42    public static int numCalls = 0;
43  
44    private static class TestMockRegionServer extends MiniHBaseCluster.MiniHBaseClusterRegionServer {
45  
46      public TestMockRegionServer(Configuration conf, CoordinatedStateManager cp)
47          throws IOException, InterruptedException {
48        super(conf, cp);
49      }
50  
51      protected int movedRegionCleanerPeriod() {
52        return 500;
53      }
54  
55      @Override protected void cleanMovedRegions() {
56        // count the number of calls that are being made to this
57        //
58        numCalls++;
59        super.cleanMovedRegions();
60      }
61    }
62  
63    @After public void after() throws Exception {
64      UTIL.shutdownMiniCluster();
65    }
66  
67    @Before public void before() throws Exception {
68      UTIL.getConfiguration()
69          .setStrings(HConstants.REGION_SERVER_IMPL, TestMockRegionServer.class.getName());
70      UTIL.startMiniCluster(1);
71    }
72  
73    /**
74     * Start the cluster, wait for some time and verify that the background
75     * MovedRegion cleaner indeed gets called
76     *
77     * @throws IOException
78     * @throws InterruptedException
79     */
80    @Test public void testMovedRegionsCleaner() throws IOException, InterruptedException {
81      // We need to sleep long enough to trigger at least one round of background calls
82      // to MovedRegionCleaner happen. Currently the period is set to 500ms.
83      // Setting the sleep here for 2s just to be safe
84      //
85      UTIL.waitFor(2000, new Waiter.Predicate<IOException>() {
86        @Override
87        public boolean evaluate() throws IOException {
88  
89          // verify that there was at least one call to the cleanMovedRegions function
90          //
91          return numCalls > 0;
92        }
93      });
94    }
95  }