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     */
018    package org.apache.hadoop.hdfs.client;
019    
020    import java.io.IOException;
021    import java.net.URI;
022    
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    import org.apache.hadoop.classification.InterfaceAudience;
026    import org.apache.hadoop.classification.InterfaceStability;
027    import org.apache.hadoop.conf.Configuration;
028    import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
029    import org.apache.hadoop.fs.FileSystem;
030    import org.apache.hadoop.hdfs.DFSConfigKeys;
031    import org.apache.hadoop.hdfs.DistributedFileSystem;
032    import org.apache.hadoop.hdfs.protocol.HdfsConstants;
033    import org.apache.hadoop.hdfs.protocol.HdfsConstants.SafeModeAction;
034    import org.apache.hadoop.io.IOUtils;
035    
036    /**
037     * The public utility API for HDFS.
038     */
039    @InterfaceAudience.Public
040    @InterfaceStability.Evolving
041    public class HdfsUtils {
042      private static final Log LOG = LogFactory.getLog(HdfsUtils.class);
043    
044      /**
045       * Is the HDFS healthy?
046       * HDFS is considered as healthy if it is up and not in safemode.
047       *
048       * @param uri the HDFS URI.  Note that the URI path is ignored.
049       * @return true if HDFS is healthy; false, otherwise.
050       */
051      public static boolean isHealthy(URI uri) {
052        //check scheme
053        final String scheme = uri.getScheme();
054        if (!HdfsConstants.HDFS_URI_SCHEME.equalsIgnoreCase(scheme)) {
055          throw new IllegalArgumentException("The scheme is not "
056              + HdfsConstants.HDFS_URI_SCHEME + ", uri=" + uri);
057        }
058        
059        final Configuration conf = new Configuration();
060        //disable FileSystem cache
061        conf.setBoolean(String.format("fs.%s.impl.disable.cache", scheme), true);
062        //disable client retry for rpc connection and rpc calls
063        conf.setBoolean(DFSConfigKeys.DFS_CLIENT_RETRY_POLICY_ENABLED_KEY, false);
064        conf.setInt(
065            CommonConfigurationKeysPublic.IPC_CLIENT_CONNECT_MAX_RETRIES_KEY, 0);
066    
067        DistributedFileSystem fs = null;
068        try {
069          fs = (DistributedFileSystem)FileSystem.get(uri, conf);
070          final boolean safemode = fs.setSafeMode(SafeModeAction.SAFEMODE_GET);
071          if (LOG.isDebugEnabled()) {
072            LOG.debug("Is namenode in safemode? " + safemode + "; uri=" + uri);
073          }
074    
075          fs.close();
076          fs = null;
077          return !safemode;
078        } catch(IOException e) {
079          if (LOG.isDebugEnabled()) {
080            LOG.debug("Got an exception for uri=" + uri, e);
081          }
082          return false;
083        } finally {
084          IOUtils.cleanup(LOG, fs);
085        }
086      }
087    }