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 java.util.concurrent.CompletableFuture;
021import java.util.concurrent.TimeUnit;
022import java.util.function.Supplier;
023import java.util.stream.Stream;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HRegionInfo;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.client.RegionInfoBuilder;
028import org.apache.hadoop.hbase.testclassification.LargeTests;
029import org.apache.hadoop.hbase.testclassification.RegionServerTests;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034/**
035 * Test for the tangled mess that is static initialization of our our {@link HRegionInfo} and
036 * {@link RegionInfoBuilder}, as reported on HBASE-24896. The condition being tested can only be
037 * reproduced the first time a JVM loads the classes under test. Thus, this test is marked as a
038 * {@link LargeTests} because, under their current configuration, tests in that category are run in
039 * their own JVM instances.
040 */
041@SuppressWarnings("deprecation")
042@Category({ RegionServerTests.class, LargeTests.class })
043public class TestRegionInfoStaticInitialization {
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestRegionInfoStaticInitialization.class);
047
048  @Test
049  public void testParallelStaticInitialization() throws Exception {
050    // The JVM loads symbols lazily. These suppliers reference two symbols that, before this patch,
051    // are mutually dependent and expose a deadlock in the loading of symbols from RegionInfo and
052    // RegionInfoBuilder.
053    final Supplier<RegionInfo> retrieveUNDEFINED = () -> HRegionInfo.UNDEFINED;
054    final Supplier<RegionInfo> retrieveMetaRegionInfo =
055      () -> RegionInfoBuilder.FIRST_META_REGIONINFO;
056
057    // The test runs multiple threads that reference these mutually dependent symbols. In order to
058    // express this bug, these threads need to access these symbols at roughly the same time, so
059    // that the classloader is asked to materialize these symbols concurrently. These Suppliers are
060    // run on threads that have already been allocated, managed by the system's ForkJoin pool.
061    final CompletableFuture<?>[] futures = Stream
062      .of(retrieveUNDEFINED, retrieveMetaRegionInfo, retrieveUNDEFINED, retrieveMetaRegionInfo)
063      .map(CompletableFuture::supplyAsync).toArray(CompletableFuture<?>[]::new);
064
065    // Loading classes should be relatively fast. 5 seconds is an arbitrary choice of timeout. It
066    // was chosen under the assumption that loading these symbols should complete much faster than
067    // this window.
068    CompletableFuture.allOf(futures).get(5, TimeUnit.SECONDS);
069  }
070}