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