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.wal;
019
020import static org.junit.jupiter.api.Assertions.assertNotNull;
021import static org.junit.jupiter.api.Assertions.assertSame;
022
023import java.io.IOException;
024import java.util.List;
025import java.util.concurrent.ExecutionException;
026import java.util.concurrent.ForkJoinPool;
027import java.util.concurrent.Future;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.hbase.regionserver.wal.AbstractFSWAL;
030import org.apache.hadoop.hbase.testclassification.RegionServerTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.Threads;
033import org.junit.jupiter.api.Tag;
034import org.junit.jupiter.api.Test;
035import org.mockito.Mockito;
036
037import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
038
039/**
040 * Testcase for HBASE-21503.
041 */
042@Tag(RegionServerTests.TAG)
043@Tag(SmallTests.TAG)
044public class TestRaceBetweenGetWALAndGetWALs {
045
046  private static Future<List<WAL>> GET_WALS_FUTURE;
047
048  private static final class FSWALProvider extends AbstractFSWALProvider<AbstractFSWAL<?>> {
049
050    @Override
051    protected AbstractFSWAL<?> createWAL() throws IOException {
052      // just like what may do in the WALListeners, schedule an asynchronous task to call the
053      // getWALs method.
054      GET_WALS_FUTURE = ForkJoinPool.commonPool().submit(this::getWALs);
055      // sleep a while to make the getWALs arrive before we return
056      Threads.sleep(2000);
057      return Mockito.mock(AbstractFSWAL.class);
058    }
059
060    @Override
061    protected void doInit(Configuration conf) throws IOException {
062    }
063  }
064
065  @Test
066  public void testRace() throws IOException, InterruptedException, ExecutionException {
067    FSWALProvider p = new FSWALProvider();
068    WAL wal = p.getWAL(null);
069    assertNotNull(GET_WALS_FUTURE);
070    List<WAL> wals = GET_WALS_FUTURE.get();
071    assertSame(wal, Iterables.getOnlyElement(wals));
072  }
073}