代码之家  ›  专栏  ›  技术社区  ›  DilTeam

Hadoop的TooRunner线程安全吗?

  •  4
  • DilTeam  · 技术社区  · 11 年前

    我想同时触发几个Hadoop作业。我使用Executors.newFixedThreadPool创建了一个线程池。想法是,如果池大小为2,我的代码将使用ToolRunner.run同时触发两个Hadoop作业。在我的测试中,我注意到这两个线程一直在互相踩。

    当我查看引擎盖时,我注意到ToolRunner创建了GenericOptionsParser,后者反过来调用静态方法buildGeneralOptions。此方法使用OptionBuilder.withArgName,后者使用名为argName的实例变量。这对我来说并不安全,我相信这是我遇到问题的根本原因。

    有什么想法吗?

    1 回复  |  直到 11 年前
        1
  •  2
  •   DilTeam    11 年前

    已确认ToolRunner不是线程安全的:

    原始代码(遇到问题):

      public static int run(Configuration conf, Tool tool, String[] args) 
    throws Exception{
    if(conf == null) {
      conf = new Configuration();
    }
    GenericOptionsParser parser = new GenericOptionsParser(conf, args);
    //set the configuration back, so that Tool can configure itself
    tool.setConf(conf);
    
    //get the args w/o generic hadoop args
    String[] toolArgs = parser.getRemainingArgs();
    return tool.run(toolArgs);
    

    }

    新代码(有效):

        public static int run(Configuration conf, Tool tool, String[] args)
            throws Exception{
        if(conf == null) {
            conf = new Configuration();
        }
        GenericOptionsParser parser = getParser(conf, args);
    
        tool.setConf(conf);
    
        //get the args w/o generic hadoop args
        String[] toolArgs = parser.getRemainingArgs();
        return tool.run(toolArgs);
    }
    
    private static synchronized GenericOptionsParser getParser(Configuration conf, String[] args) throws Exception {
        return new GenericOptionsParser(conf, args);
    }