1   /**
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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.util;
19  
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.fs.Path;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.MediumTests;
28  import org.apache.hadoop.hdfs.DistributedFileSystem;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  import org.mockito.Mockito;
33  
34  /**
35   * Test our recoverLease loop against mocked up filesystem.
36   */
37  @Category(MediumTests.class)
38  public class TestFSHDFSUtils {
39    private static final HBaseTestingUtility HTU = new HBaseTestingUtility();
40    static {
41      Configuration conf = HTU.getConfiguration();
42      conf.setInt("hbase.lease.recovery.first.pause", 10);
43      conf.setInt("hbase.lease.recovery.pause", 10);
44    };
45    private FSHDFSUtils fsHDFSUtils = new FSHDFSUtils();
46    private static Path FILE = new Path(HTU.getDataTestDir(), "file.txt");
47    long startTime = -1;
48  
49    @Before
50    public void setup() {
51      this.startTime = EnvironmentEdgeManager.currentTimeMillis();
52    }
53  
54    /**
55     * Test recover lease eventually succeeding.
56     * @throws IOException
57     */
58    @Test (timeout = 30000)
59    public void testRecoverLease() throws IOException {
60      HTU.getConfiguration().setInt("hbase.lease.recovery.dfs.timeout", 1000);
61      DistributedFileSystem dfs = Mockito.mock(DistributedFileSystem.class);
62      // Fail four times and pass on the fifth.
63      Mockito.when(dfs.recoverLease(FILE)).
64        thenReturn(false).thenReturn(false).thenReturn(false).thenReturn(false).thenReturn(true);
65      assertTrue(this.fsHDFSUtils.recoverDFSFileLease(dfs, FILE, HTU.getConfiguration()));
66      Mockito.verify(dfs, Mockito.times(5)).recoverLease(FILE);
67      // Make sure we waited at least hbase.lease.recovery.dfs.timeout * 3 (the first two
68      // invocations will happen pretty fast... the we fall into the longer wait loop).
69      assertTrue((EnvironmentEdgeManager.currentTimeMillis() - this.startTime) >
70        (3 * HTU.getConfiguration().getInt("hbase.lease.recovery.dfs.timeout", 61000)));
71    }
72  
73    /**
74     * Test that isFileClosed makes us recover lease faster.
75     * @throws IOException
76     */
77    @Test (timeout = 30000)
78    public void testIsFileClosed() throws IOException {
79      // Make this time long so it is plain we broke out because of the isFileClosed invocation.
80      HTU.getConfiguration().setInt("hbase.lease.recovery.dfs.timeout", 100000);
81      IsFileClosedDistributedFileSystem dfs = Mockito.mock(IsFileClosedDistributedFileSystem.class);
82      // Now make it so we fail the first two times -- the two fast invocations, then we fall into
83      // the long loop during which we will call isFileClosed.... the next invocation should
84      // therefore return true if we are to break the loop.
85      Mockito.when(dfs.recoverLease(FILE)).
86        thenReturn(false).thenReturn(false).thenReturn(true);
87      Mockito.when(dfs.isFileClosed(FILE)).thenReturn(true);
88      assertTrue(this.fsHDFSUtils.recoverDFSFileLease(dfs, FILE, HTU.getConfiguration()));
89      Mockito.verify(dfs, Mockito.times(2)).recoverLease(FILE);
90      Mockito.verify(dfs, Mockito.times(1)).isFileClosed(FILE);
91    }
92  
93    /**
94     * Version of DFS that has HDFS-4525 in it.
95     */
96    class IsFileClosedDistributedFileSystem extends DistributedFileSystem {
97      /**
98       * Close status of a file. Copied over from HDFS-4525
99       * @return true if file is already closed
100      **/
101     public boolean isFileClosed(Path f) throws IOException{
102       return false;
103     }
104   }
105 }