【HDFS】hdfs的fsck是咋回事

本文探讨了HDFS中的fsck工具的工作机制。fsck是通过http的servlet(FsckServlet)与NameNode交互,检查文件系统的健康状况。NameNode使用FsNameSystem对象来获取活的DataNode数量和最小副本数,然后通过NamenodeFsck执行fsck方法。fsck返回的结果可能因依赖于NameNode内存中的信息而存在延迟,不立即反映实际的块状态变化。然而,对于删除操作,fsck能提供实时反馈,因为删除会立即在NameNode的命名空间和内存中进行标记。
摘要由CSDN通过智能技术生成

有一次使用hadoop客户端运行hadoop的fsck,客户端报错,显示socket错误,连不上,rd童鞋耻笑说没有配置http地址,教训我等fsck是依靠http的一个工具,好吧,谁让咱他妈的不懂呢,现在就来看看fsck到底是咋实现的。

elif [ "$COMMAND" = "fsck" ] ; then
  CLASS=org.apache.hadoop.hdfs.tools.DFSck
还是查看hadoop脚本,fsck工具的启动入口在org.apache.hadoop.hdfs.tools.DFSck,
  public static void main(String[] args) throws Exception {
    // -files option is also used by GenericOptionsParser
    // Make sure that is not the first argument for fsck
    int res = -1;
    if ((args.length == 0 ) || ("-files".equals(args[0]))) 
      printUsage();
    else
      res = ToolRunner.run(new DFSck(new Configuration()), args);
    System.exit(res);
  }
从main可以看到,fsck的实现是DFSck,它实现工具类接口,重写run方法。从构造方法看,它运行需要hdfs的配置文件,告诉它namenode的信息,还要告诉它用户传入的参数。这些废话就不多说了,进去看看吧。

String proto = "http://";
          if(UserGroupInformation.isSecurityEnabled()) { 
             System.setProperty("https.cipherSuites", Krb5AndCertsSslSocketConnector.KRB5_CIPHER_SUITES.get(0));
             proto = "https://";
          }//根据是否开启了安全属性,选择http协议或者https,果然是通过web
          
          final StringBuffer url = new StringBuffer(proto);
          url.append(NameNode.getInfoServer(getConf())).append("/fsck?ugi=").append(ugi.getShortUserName()).append("&path=");
	//从这里/fsck可以看出,namenode实现了fsck的servlet,这个fsck命令行脚本只不过去向这个接口提交请求。
          String dir = "/";
          // find top-level dir first
          for (int idx = 0; idx < args.length; idx++) {
            if (!args[idx].startsWith("-")) { dir = args[idx]; break; }
          }
          url.append(URLEncoder.encode(dir, "UTF-8"));
          for (int idx = 0; idx < args.length; idx++) {
            if (args[idx].equals("-move")) { url.append("&move=1"); }
            else if (args[idx].equals("-delete")) { url.append("&delete=1"); }
            else if (args[idx].equals("-files")) { url.append("&files=1"); }
            else if (args[idx].equals("-openforwrite")) { url.append("&openforwrite=1"); }
            else if (args[idx].equals("-blocks")) { url.append("&blocks=1"); }
            else if (args[idx].equals("-locations")) { url.append("&locations=1"); }
            else if (args[idx].equals("-racks")) { url.append("&racks=1"); }
          }
          URL path = new URL(url.toString());
          SecurityUtil.fetchServiceTicket(path);
          URLConnection connection = path.openConnection();
          InputStream stream = connection.getInputStream();
从上面的代码可以看出,这个工具类实现比较简单,就是调用namenode的fsck这个接口,采用servlet的方式,然后把用户的参数拼成url去提交request。然后拿到结果打印到屏幕上。我擦,当时不配http地址当然没法执行了啊!看一下getInfoServer方法吧就知道了。

  public static String getInfoServer(Configuration conf) {
    String http = UserGroupInformation.isSecurityEnabled() ? "dfs.https.address"
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值