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