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.apache.hadoop.hbase.HBaseTestingUtility.fam1;
021import static org.junit.Assert.fail;
022import static org.mockito.Mockito.spy;
023
024import java.io.IOException;
025import java.lang.reflect.Field;
026import java.util.concurrent.BlockingQueue;
027import java.util.concurrent.ScheduledExecutorService;
028import java.util.concurrent.ThreadPoolExecutor;
029import java.util.concurrent.TimeUnit;
030import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.HBaseTestingUtility;
033import org.apache.hadoop.hbase.ServerName;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
036import org.apache.hadoop.hbase.client.RegionInfo;
037import org.apache.hadoop.hbase.client.RegionInfoBuilder;
038import org.apache.hadoop.hbase.client.TableDescriptor;
039import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
040import org.apache.hadoop.hbase.testclassification.LargeTests;
041import org.apache.hadoop.hbase.testclassification.RegionServerTests;
042import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper;
043import org.apache.hadoop.metrics2.MetricsExecutor;
044import org.junit.AfterClass;
045import org.junit.Assert;
046import org.junit.ClassRule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049import org.slf4j.Logger;
050import org.slf4j.LoggerFactory;
051
052@Category({ RegionServerTests.class, LargeTests.class })
053public class TestOpenRegionFailedMemoryLeak {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057    HBaseClassTestRule.forClass(TestOpenRegionFailedMemoryLeak.class);
058
059  private static final Logger LOG = LoggerFactory.getLogger(TestOpenRegionFailedMemoryLeak.class);
060
061  private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
062
063  @AfterClass
064  public static void tearDown() throws IOException {
065    EnvironmentEdgeManagerTestHelper.reset();
066    LOG.info("Cleaning test directory: " + TEST_UTIL.getDataTestDir());
067    TEST_UTIL.cleanupTestDir();
068  }
069
070  // make sure the region is successfully closed when the coprocessor config is wrong
071  @Test
072  public void testOpenRegionFailedMemoryLeak() throws Exception {
073    final ServerName serverName = ServerName.valueOf("testOpenRegionFailed", 100, 42);
074    final RegionServerServices rss = spy(TEST_UTIL.createMockRegionServerService(serverName));
075
076    TableDescriptor htd =
077      TableDescriptorBuilder.newBuilder(TableName.valueOf("testOpenRegionFailed"))
078        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(fam1))
079        .setValue("COPROCESSOR$1", "hdfs://test/test.jar|test||").build();
080
081    RegionInfo hri = RegionInfoBuilder.newBuilder(htd.getTableName()).build();
082    ScheduledExecutorService executor =
083      CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
084    for (int i = 0; i < 20; i++) {
085      try {
086        HRegion.openHRegion(hri, htd, rss.getWAL(hri), TEST_UTIL.getConfiguration(), rss, null);
087        fail("Should fail otherwise the test will be useless");
088      } catch (Throwable t) {
089        LOG.info("Expected exception, continue", t);
090      }
091    }
092    TimeUnit.SECONDS.sleep(MetricsRegionWrapperImpl.PERIOD);
093    Field[] fields = ThreadPoolExecutor.class.getDeclaredFields();
094    boolean found = false;
095    for (Field field : fields) {
096      if (field.getName().equals("workQueue")) {
097        field.setAccessible(true);
098        BlockingQueue<Runnable> workQueue = (BlockingQueue<Runnable>) field.get(executor);
099        // there are still two task not cancel, can not cause to memory lack
100        Assert.assertTrue("ScheduledExecutor#workQueue should equals 2, now is " +
101          workQueue.size() + ", please check region is close", 2 == workQueue.size());
102        found = true;
103      }
104    }
105    Assert.assertTrue("can not find workQueue, test failed", found);
106  }
107
108}