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