View Javadoc

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      CancelableProgressable reporter = Mockito.mock(CancelableProgressable.class);
62      Mockito.when(reporter.progress()).thenReturn(true);
63      DistributedFileSystem dfs = Mockito.mock(DistributedFileSystem.class);
64      // Fail four times and pass on the fifth.
65      Mockito.when(dfs.recoverLease(FILE)).
66        thenReturn(false).thenReturn(false).thenReturn(false).thenReturn(false).thenReturn(true);
67      assertTrue(this.fsHDFSUtils.recoverDFSFileLease(dfs, FILE, HTU.getConfiguration(), reporter));
68      Mockito.verify(dfs, Mockito.times(5)).recoverLease(FILE);
69      // Make sure we waited at least hbase.lease.recovery.dfs.timeout * 3 (the first two
70      // invocations will happen pretty fast... the we fall into the longer wait loop).
71      assertTrue((EnvironmentEdgeManager.currentTimeMillis() - this.startTime) >
72        (3 * HTU.getConfiguration().getInt("hbase.lease.recovery.dfs.timeout", 61000)));
73    }
74  
75    /**
76     * Test that isFileClosed makes us recover lease faster.
77     * @throws IOException
78     */
79    @Test (timeout = 30000)
80    public void testIsFileClosed() throws IOException {
81      // Make this time long so it is plain we broke out because of the isFileClosed invocation.
82      HTU.getConfiguration().setInt("hbase.lease.recovery.dfs.timeout", 100000);
83      CancelableProgressable reporter = Mockito.mock(CancelableProgressable.class);
84      Mockito.when(reporter.progress()).thenReturn(true);
85      IsFileClosedDistributedFileSystem dfs = Mockito.mock(IsFileClosedDistributedFileSystem.class);
86      // Now make it so we fail the first two times -- the two fast invocations, then we fall into
87      // the long loop during which we will call isFileClosed.... the next invocation should
88      // therefore return true if we are to break the loop.
89      Mockito.when(dfs.recoverLease(FILE)).
90        thenReturn(false).thenReturn(false).thenReturn(true);
91      Mockito.when(dfs.isFileClosed(FILE)).thenReturn(true);
92      assertTrue(this.fsHDFSUtils.recoverDFSFileLease(dfs, FILE, HTU.getConfiguration(), reporter));
93      Mockito.verify(dfs, Mockito.times(2)).recoverLease(FILE);
94      Mockito.verify(dfs, Mockito.times(1)).isFileClosed(FILE);
95    }
96  
97    /**
98     * Version of DFS that has HDFS-4525 in it.
99     */
100   class IsFileClosedDistributedFileSystem extends DistributedFileSystem {
101     /**
102      * Close status of a file. Copied over from HDFS-4525
103      * @return true if file is already closed
104      **/
105     public boolean isFileClosed(Path f) throws IOException{
106       return false;
107     }
108   }
109 }