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