2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.regionserver
;
20 import java
.util
.concurrent
.CompletableFuture
;
21 import java
.util
.concurrent
.TimeUnit
;
22 import java
.util
.function
.Supplier
;
23 import java
.util
.stream
.Stream
;
24 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
25 import org
.apache
.hadoop
.hbase
.client
.RegionInfo
;
26 import org
.apache
.hadoop
.hbase
.client
.RegionInfoBuilder
;
27 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
28 import org
.apache
.hadoop
.hbase
.testclassification
.RegionServerTests
;
29 import org
.junit
.ClassRule
;
30 import org
.junit
.Test
;
31 import org
.junit
.experimental
.categories
.Category
;
34 * Test for the tangled mess that is static initialization of our our {@link RegionInfo} and
35 * {@link RegionInfoBuilder}, as reported on HBASE-24896. The condition being tested can only be
36 * reproduced the first time a JVM loads the classes under test. Thus, this test is marked as a
37 * {@link LargeTests} because, under their current configuration, tests in that category are run
38 * in their own JVM instances.
40 @SuppressWarnings("deprecation")
41 @Category({ RegionServerTests
.class, LargeTests
.class})
42 public class TestRegionInfoStaticInitialization
{
44 public static final HBaseClassTestRule CLASS_RULE
=
45 HBaseClassTestRule
.forClass(TestRegionInfoStaticInitialization
.class);
48 public void testParallelStaticInitialization() throws Exception
{
49 // The JVM loads symbols lazily. These suppliers reference two symbols that, before this patch,
50 // are mutually dependent and expose a deadlock in the loading of symbols from RegionInfo and
52 final Supplier
<RegionInfo
> retrieveUNDEFINED
= () -> RegionInfo
.UNDEFINED
;
53 final Supplier
<RegionInfo
> retrieveMetaRegionInfo
=
54 () -> RegionInfoBuilder
.FIRST_META_REGIONINFO
;
56 // The test runs multiple threads that reference these mutually dependent symbols. In order to
57 // express this bug, these threads need to access these symbols at roughly the same time, so
58 // that the classloader is asked to materialize these symbols concurrently. These Suppliers are
59 // run on threads that have already been allocated, managed by the system's ForkJoin pool.
60 final CompletableFuture
<?
>[] futures
= Stream
.of(
61 retrieveUNDEFINED
, retrieveMetaRegionInfo
, retrieveUNDEFINED
, retrieveMetaRegionInfo
)
62 .map(CompletableFuture
::supplyAsync
)
63 .toArray(CompletableFuture
<?
>[]::new);
65 // Loading classes should be relatively fast. 5 seconds is an arbitrary choice of timeout. It
66 // was chosen under the assumption that loading these symbols should complete much faster than
68 CompletableFuture
.allOf(futures
).get(5, TimeUnit
.SECONDS
);