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  
19  package org.apache.hadoop.hbase.util;
20  
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.io.InterruptedIOException;
24  import java.lang.reflect.Method;
25  import java.net.InetSocketAddress;
26  import java.net.URI;
27  import java.util.HashSet;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import com.google.common.collect.Sets;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.hadoop.classification.InterfaceAudience;
35  import org.apache.hadoop.classification.InterfaceStability;
36  import org.apache.hadoop.conf.Configuration;
37  import org.apache.hadoop.fs.FileSystem;
38  import org.apache.hadoop.fs.Path;
39  import org.apache.hadoop.hdfs.DistributedFileSystem;
40  import org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException;
41  
42  
43  /**
44   * Implementation for hdfs
45   */
46  @InterfaceAudience.Private
47  @InterfaceStability.Evolving
48  public class FSHDFSUtils extends FSUtils {
49    private static final Log LOG = LogFactory.getLog(FSHDFSUtils.class);
50    private static Class dfsUtilClazz;
51    private static Method getNNAddressesMethod;
52  
53    /**
54     * @param fs
55     * @param conf
56     * @return A set containing all namenode addresses of fs
57     */
58    private static Set<InetSocketAddress> getNNAddresses(DistributedFileSystem fs,
59                                                        Configuration conf) {
60      Set<InetSocketAddress> addresses = new HashSet<InetSocketAddress>();
61      String serviceName = fs.getCanonicalServiceName();
62  
63      if (serviceName.startsWith("ha-hdfs")) {
64        try {
65          if (dfsUtilClazz == null) {
66            dfsUtilClazz = Class.forName("org.apache.hadoop.hdfs.DFSUtil");
67          }
68          if (getNNAddressesMethod == null) {
69            getNNAddressesMethod =
70                    dfsUtilClazz.getMethod("getNNServiceRpcAddresses", Configuration.class);
71          }
72  
73          Map<String, Map<String, InetSocketAddress>> addressMap =
74                  (Map<String, Map<String, InetSocketAddress>>) getNNAddressesMethod
75                          .invoke(null, conf);
76          for (Map.Entry<String, Map<String, InetSocketAddress>> entry : addressMap.entrySet()) {
77            Map<String, InetSocketAddress> nnMap = entry.getValue();
78            for (Map.Entry<String, InetSocketAddress> e2 : nnMap.entrySet()) {
79              InetSocketAddress addr = e2.getValue();
80              addresses.add(addr);
81            }
82          }
83        } catch (Exception e) {
84          LOG.warn("DFSUtil.getNNServiceRpcAddresses failed. serviceName=" + serviceName, e);
85        }
86      } else {
87        URI uri = fs.getUri();
88        int port = uri.getPort();
89        if (port < 0) {
90          int idx = serviceName.indexOf(':');
91          port = Integer.parseInt(serviceName.substring(idx+1));
92        }
93        InetSocketAddress addr = new InetSocketAddress(uri.getHost(), port);
94        addresses.add(addr);
95      }
96  
97      return addresses;
98    }
99  
100   /**
101    * @param conf the Configuration of HBase
102    * @param srcFs
103    * @param desFs
104    * @return Whether srcFs and desFs are on same hdfs or not
105    */
106   public static boolean isSameHdfs(Configuration conf, FileSystem srcFs, FileSystem desFs) {
107     // By getCanonicalServiceName, we could make sure both srcFs and desFs
108     // show a unified format which contains scheme, host and port.
109     String srcServiceName = srcFs.getCanonicalServiceName();
110     String desServiceName = desFs.getCanonicalServiceName();
111 
112     if (srcServiceName == null || desServiceName == null) {
113       return false;
114     }
115     if (srcServiceName.equals(desServiceName)) {
116       return true;
117     }
118     if (srcFs instanceof DistributedFileSystem && desFs instanceof DistributedFileSystem) {
119       //If one serviceName is an HA format while the other is a non-HA format,
120       // maybe they refer to the same FileSystem.
121       //For example, srcFs is "ha-hdfs://nameservices" and desFs is "hdfs://activeNamenode:port"
122       Set<InetSocketAddress> srcAddrs = getNNAddresses((DistributedFileSystem) srcFs, conf);
123       Set<InetSocketAddress> desAddrs = getNNAddresses((DistributedFileSystem) desFs, conf);
124       if (Sets.intersection(srcAddrs, desAddrs).size() > 0) {
125         return true;
126       }
127     }
128 
129     return false;
130   }
131 
132   /**
133    * Recover the lease from HDFS, retrying multiple times.
134    */
135   @Override
136   public void recoverFileLease(final FileSystem fs, final Path p,
137       Configuration conf, CancelableProgressable reporter)
138   throws IOException {
139     // lease recovery not needed for local file system case.
140     if (!(fs instanceof DistributedFileSystem)) return;
141     recoverDFSFileLease((DistributedFileSystem)fs, p, conf, reporter);
142   }
143 
144   /*
145    * Run the dfs recover lease. recoverLease is asynchronous. It returns:
146    *    -false when it starts the lease recovery (i.e. lease recovery not *yet* done)
147    *    - true when the lease recovery has succeeded or the file is closed.
148    * But, we have to be careful.  Each time we call recoverLease, it starts the recover lease
149    * process over from the beginning.  We could put ourselves in a situation where we are
150    * doing nothing but starting a recovery, interrupting it to start again, and so on.
151    * The findings over in HBASE-8354 have it that the namenode will try to recover the lease
152    * on the file's primary node.  If all is well, it should return near immediately.  But,
153    * as is common, it is the very primary node that has crashed and so the namenode will be
154    * stuck waiting on a socket timeout before it will ask another datanode to start the
155    * recovery. It does not help if we call recoverLease in the meantime and in particular,
156    * subsequent to the socket timeout, a recoverLease invocation will cause us to start
157    * over from square one (possibly waiting on socket timeout against primary node).  So,
158    * in the below, we do the following:
159    * 1. Call recoverLease.
160    * 2. If it returns true, break.
161    * 3. If it returns false, wait a few seconds and then call it again.
162    * 4. If it returns true, break.
163    * 5. If it returns false, wait for what we think the datanode socket timeout is
164    * (configurable) and then try again.
165    * 6. If it returns true, break.
166    * 7. If it returns false, repeat starting at step 5. above.
167    *
168    * If HDFS-4525 is available, call it every second and we might be able to exit early.
169    */
170   boolean recoverDFSFileLease(final DistributedFileSystem dfs, final Path p,
171       final Configuration conf, final CancelableProgressable reporter)
172   throws IOException {
173     LOG.info("Recovering lease on dfs file " + p);
174     long startWaiting = EnvironmentEdgeManager.currentTimeMillis();
175     // Default is 15 minutes. It's huge, but the idea is that if we have a major issue, HDFS
176     // usually needs 10 minutes before marking the nodes as dead. So we're putting ourselves
177     // beyond that limit 'to be safe'.
178     long recoveryTimeout = conf.getInt("hbase.lease.recovery.timeout", 900000) + startWaiting;
179     // This setting should be a little bit above what the cluster dfs heartbeat is set to.
180     long firstPause = conf.getInt("hbase.lease.recovery.first.pause", 4000);
181     // This should be set to how long it'll take for us to timeout against primary datanode if it
182     // is dead.  We set it to 61 seconds, 1 second than the default READ_TIMEOUT in HDFS, the
183     // default value for DFS_CLIENT_SOCKET_TIMEOUT_KEY.
184     long subsequentPause = conf.getInt("hbase.lease.recovery.dfs.timeout", 61 * 1000);
185     
186     Method isFileClosedMeth = null;
187     // whether we need to look for isFileClosed method
188     boolean findIsFileClosedMeth = true;
189     boolean recovered = false;
190     // We break the loop if we succeed the lease recovery, timeout, or we throw an exception.
191     for (int nbAttempt = 0; !recovered; nbAttempt++) {
192       recovered = recoverLease(dfs, nbAttempt, p, startWaiting);
193       if (recovered) break;
194       checkIfCancelled(reporter);
195       if (checkIfTimedout(conf, recoveryTimeout, nbAttempt, p, startWaiting)) break;
196       try {
197         // On the first time through wait the short 'firstPause'.
198         if (nbAttempt == 0) {
199           Thread.sleep(firstPause);
200         } else {
201           // Cycle here until subsequentPause elapses.  While spinning, check isFileClosed if
202           // available (should be in hadoop 2.0.5... not in hadoop 1 though.
203           long localStartWaiting = EnvironmentEdgeManager.currentTimeMillis();
204           while ((EnvironmentEdgeManager.currentTimeMillis() - localStartWaiting) <
205               subsequentPause) {
206             Thread.sleep(conf.getInt("hbase.lease.recovery.pause", 1000));
207             if (findIsFileClosedMeth) {
208               try {
209                 isFileClosedMeth = dfs.getClass().getMethod("isFileClosed",
210                   new Class[]{ Path.class });
211               } catch (NoSuchMethodException nsme) {
212                 LOG.debug("isFileClosed not available");
213               } finally {
214                 findIsFileClosedMeth = false;
215               }
216             }
217             if (isFileClosedMeth != null && isFileClosed(dfs, isFileClosedMeth, p)) {
218               recovered = true;
219               break;
220             }
221             checkIfCancelled(reporter);
222           }
223         }
224       } catch (InterruptedException ie) {
225         InterruptedIOException iioe = new InterruptedIOException();
226         iioe.initCause(ie);
227         throw iioe;
228       }
229     }
230     return recovered;
231   }
232 
233   boolean checkIfTimedout(final Configuration conf, final long recoveryTimeout,
234       final int nbAttempt, final Path p, final long startWaiting) {
235     if (recoveryTimeout < EnvironmentEdgeManager.currentTimeMillis()) {
236       LOG.warn("Cannot recoverLease after trying for " +
237         conf.getInt("hbase.lease.recovery.timeout", 900000) +
238         "ms (hbase.lease.recovery.timeout); continuing, but may be DATALOSS!!!; " +
239         getLogMessageDetail(nbAttempt, p, startWaiting));
240       return true;
241     }
242     return false;
243   }
244 
245   /**
246    * Try to recover the lease.
247    * @param dfs
248    * @param nbAttempt
249    * @param p
250    * @param startWaiting
251    * @return True if dfs#recoverLease came by true.
252    * @throws FileNotFoundException
253    */
254   boolean recoverLease(final DistributedFileSystem dfs, final int nbAttempt, final Path p,
255       final long startWaiting)
256   throws FileNotFoundException {
257     boolean recovered = false;
258     try {
259       recovered = dfs.recoverLease(p);
260       LOG.info("recoverLease=" + recovered + ", " +
261         getLogMessageDetail(nbAttempt, p, startWaiting));
262     } catch (IOException e) {
263       if (e instanceof LeaseExpiredException && e.getMessage().contains("File does not exist")) {
264         // This exception comes out instead of FNFE, fix it
265         throw new FileNotFoundException("The given HLog wasn't found at " + p);
266       } else if (e instanceof FileNotFoundException) {
267         throw (FileNotFoundException)e;
268       }
269       LOG.warn(getLogMessageDetail(nbAttempt, p, startWaiting), e);
270     }
271     return recovered;
272   }
273 
274   /**
275    * @param nbAttempt
276    * @param p
277    * @param startWaiting
278    * @return Detail to append to any log message around lease recovering.
279    */
280   private String getLogMessageDetail(final int nbAttempt, final Path p, final long startWaiting) {
281     return "attempt=" + nbAttempt + " on file=" + p + " after " +
282       (EnvironmentEdgeManager.currentTimeMillis() - startWaiting) + "ms";
283   }
284 
285   /**
286    * Call HDFS-4525 isFileClosed if it is available.
287    * @param dfs
288    * @param m
289    * @param p
290    * @return True if file is closed.
291    */
292   private boolean isFileClosed(final DistributedFileSystem dfs, final Method m, final Path p) {
293     try {
294       return (Boolean) m.invoke(dfs, p);
295     } catch (SecurityException e) {
296       LOG.warn("No access", e);
297     } catch (Exception e) {
298       LOG.warn("Failed invocation for " + p.toString(), e);
299     }
300     return false;
301   }
302 
303   void checkIfCancelled(final CancelableProgressable reporter)
304   throws InterruptedIOException {
305     if (reporter == null) return;
306     if (!reporter.progress()) throw new InterruptedIOException("Operation cancelled");
307   }
308 }