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