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.util;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.testclassification.MiscTests;
029import org.apache.hadoop.hdfs.DistributedFileSystem;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033import org.mockito.Mockito;
034
035/**
036 * Test our recoverLease loop against mocked up filesystem.
037 */
038@Category({ MiscTests.class, MediumTests.class })
039public class TestRecoverLeaseFSUtils {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestRecoverLeaseFSUtils.class);
044
045  private static final HBaseCommonTestingUtility HTU = new HBaseCommonTestingUtility();
046  static {
047    Configuration conf = HTU.getConfiguration();
048    conf.setInt("hbase.lease.recovery.first.pause", 10);
049    conf.setInt("hbase.lease.recovery.pause", 10);
050  }
051
052  private static Path FILE = new Path(HTU.getDataTestDir(), "file.txt");
053
054  /**
055   * Test recover lease eventually succeeding.
056   */
057  @Test
058  public void testRecoverLease() throws IOException {
059    long startTime = EnvironmentEdgeManager.currentTime();
060    HTU.getConfiguration().setInt("hbase.lease.recovery.dfs.timeout", 1000);
061    CancelableProgressable reporter = Mockito.mock(CancelableProgressable.class);
062    Mockito.when(reporter.progress()).thenReturn(true);
063    DistributedFileSystem dfs = Mockito.mock(DistributedFileSystem.class);
064    // Fail four times and pass on the fifth.
065    Mockito.when(dfs.recoverLease(FILE)).thenReturn(false).thenReturn(false).thenReturn(false)
066      .thenReturn(false).thenReturn(true);
067    RecoverLeaseFSUtils.recoverFileLease(dfs, FILE, HTU.getConfiguration(), reporter);
068    Mockito.verify(dfs, Mockito.times(5)).recoverLease(FILE);
069    // Make sure we waited at least hbase.lease.recovery.dfs.timeout * 3 (the first two
070    // invocations will happen pretty fast... the we fall into the longer wait loop).
071    assertTrue((EnvironmentEdgeManager.currentTime() - startTime)
072        > (3 * HTU.getConfiguration().getInt("hbase.lease.recovery.dfs.timeout", 61000)));
073  }
074
075  /**
076   * Test that isFileClosed makes us recover lease faster.
077   */
078  @Test
079  public void testIsFileClosed() throws IOException {
080    // Make this time long so it is plain we broke out because of the isFileClosed invocation.
081    HTU.getConfiguration().setInt("hbase.lease.recovery.dfs.timeout", 100000);
082    CancelableProgressable reporter = Mockito.mock(CancelableProgressable.class);
083    Mockito.when(reporter.progress()).thenReturn(true);
084    IsFileClosedDistributedFileSystem dfs = Mockito.mock(IsFileClosedDistributedFileSystem.class);
085    // Now make it so we fail the first two times -- the two fast invocations, then we fall into
086    // the long loop during which we will call isFileClosed.... the next invocation should
087    // therefore return true if we are to break the loop.
088    Mockito.when(dfs.recoverLease(FILE)).thenReturn(false).thenReturn(false).thenReturn(true);
089    Mockito.when(dfs.isFileClosed(FILE)).thenReturn(true);
090    RecoverLeaseFSUtils.recoverFileLease(dfs, FILE, HTU.getConfiguration(), reporter);
091    Mockito.verify(dfs, Mockito.times(2)).recoverLease(FILE);
092    Mockito.verify(dfs, Mockito.times(1)).isFileClosed(FILE);
093  }
094
095  /**
096   * Version of DFS that has HDFS-4525 in it.
097   */
098  private static class IsFileClosedDistributedFileSystem extends DistributedFileSystem {
099    /**
100     * Close status of a file. Copied over from HDFS-4525
101     * @return true if file is already closed
102     **/
103    @Override
104    public boolean isFileClosed(Path f) throws IOException {
105      return false;
106    }
107  }
108}