Program: 28345
Project: 28345
Buggy source:
/** 
 * Loop through all instances in a cluster and execute a command for each one.
 * @param command       The string of the command to run. The instancename will be used as the operand for the command.
 * @param map           A map of parameters to use for the command. May benull if no parameters. When the command is executed for a server instance, the instance name is set as the DEFAULT parameter (operand)
 * @param clusterName   The name of the cluster containing the instancesto run the command against.
 * @param context       The AdminCommandContext to use when executing thecommand.
 * @param verbose       true for more verbose output
 * @return              An ActionReport containing the results
 * @throws CommandException
 */
ActionReport runCommand(String command,ParameterMap map,String clusterName,AdminCommandContext context,boolean verbose) throws CommandException {
  final long startTime=System.currentTimeMillis();
  Logger logger=context.getLogger();
  ActionReport report=context.getActionReport();
  Cluster cluster=domain.getClusterNamed(clusterName);
  if (cluster == null) {
    String msg=Strings.get("String_Node_Str",clusterName);
    throw new CommandException(msg);
  }
  List<Server> targetServers=domain.getServersInTarget(clusterName);
  if (targetServers == null || targetServers.isEmpty()) {
    report.setActionExitCode(ExitCode.SUCCESS);
    report.setMessage(Strings.get("String_Node_Str",clusterName));
    return report;
  }
  int nInstances=targetServers.size();
  StringBuilder failedServerNames=new StringBuilder();
  StringBuilder succeededServerNames=new StringBuilder();
  List<String> waitingForServerNames=new ArrayList<String>();
  String msg;
  ReportResult reportResult=new ReportResult();
  boolean failureOccurred=false;
  progress=context.getProgressStatus();
  StringBuilder output=new StringBuilder();
  logger.fine(String.format("String_Node_Str",serverListToString(targetServers)));
  targetServers=optimizeServerListOrder(targetServers);
  ArrayBlockingQueue<CommandRunnable> responseQueue=new ArrayBlockingQueue<CommandRunnable>(nInstances);
  int adminThreadPoolSize=getAdminThreadPoolSize(logger);
  int threadPoolSize=Math.min(nInstances,adminThreadPoolSize / 2);
  if (threadPoolSize < 1)   threadPoolSize=1;
  ExecutorService threadPool=Executors.newFixedThreadPool(threadPoolSize);
  if (map == null) {
    map=new ParameterMap();
  }
  msg=String.format("String_Node_Str",command,nInstances,threadPoolSize,serverListToString(targetServers));
  logger.info(msg);
  msg=Strings.get("String_Node_Str",command,Integer.toString(nInstances));
  progress.setTotalStepCount(nInstances);
  progress.progress(msg);
  for (  Server server : targetServers) {
    String iname=server.getName();
    waitingForServerNames.add(iname);
    ParameterMap instanceParameterMap=new ParameterMap(map);
    instanceParameterMap.set("String_Node_Str",iname);
    ActionReport instanceReport=runner.getActionReport("String_Node_Str");
    instanceReport.setActionExitCode(ExitCode.SUCCESS);
    CommandInvocation invocation=runner.getCommandInvocation(command,instanceReport,context.getSubject());
    invocation.parameters(instanceParameterMap);
    msg=command + "String_Node_Str" + iname;
    logger.info(msg);
    if (verbose) {
      output.append(msg).append(NL);
    }
    CommandRunnable cmdRunnable=new CommandRunnable(invocation,instanceReport,responseQueue);
    cmdRunnable.setName(iname);
    threadPool.execute(cmdRunnable);
  }
  logger.fine(String.format("String_Node_Str",command));
  long adminTimeout=RemoteRestAdminCommand.getReadTimeout() - 3000;
  if (adminTimeout <= 0) {
    adminTimeout=57 * 1000;
  }
  logger.fine(String.format("String_Node_Str",adminTimeout));
  for (int n=0; n < nInstances; n++) {
    long timeLeft=adminTimeout - (System.currentTimeMillis() - startTime);
    if (timeLeft < 0) {
      timeLeft=0;
    }
    CommandRunnable cmdRunnable=null;
    try {
      cmdRunnable=responseQueue.poll(timeLeft,TimeUnit.MILLISECONDS);
    }
 catch (    InterruptedException e) {
      threadPool.shutdownNow();
      msg=Strings.get("String_Node_Str",clusterName,Integer.toString(n),Integer.toString(nInstances),command);
      logger.warning(msg);
      output.append(msg).append(NL);
      failureOccurred=true;
      Thread.currentThread().interrupt();
      break;
    }
    if (cmdRunnable == null) {
      break;
    }
    String iname=cmdRunnable.getName();
    waitingForServerNames.remove(iname);
    ActionReport instanceReport=cmdRunnable.getActionReport();
    logger.fine(String.format("String_Node_Str",n + 1,nInstances,iname,instanceReport.getActionExitCode()));
    if (instanceReport.getActionExitCode() != ExitCode.SUCCESS) {
      failureOccurred=true;
      failedServerNames.append(iname).append("String_Node_Str");
      reportResult.failedServerNames.add(iname);
      msg=iname + "String_Node_Str" + instanceReport.getMessage();
      logger.severe(msg);
      output.append(msg).append(NL);
      msg=Strings.get("String_Node_Str",command,iname);
      progress.progress(1,msg);
    }
 else {
      succeededServerNames.append(iname).append("String_Node_Str");
      reportResult.succeededServerNames.add(iname);
      progress.progress(1,iname);
    }
  }
  report.setActionExitCode(ExitCode.SUCCESS);
  if (failureOccurred) {
    report.setResultType(List.class,reportResult.failedServerNames);
  }
 else {
    report.setResultType(List.class,reportResult.succeededServerNames);
  }
  if (succeededServerNames.length() > 0 && (verbose || failureOccurred)) {
    output.append(NL + Strings.get("String_Node_Str",command,succeededServerNames));
  }
  if (failureOccurred) {
    output.append(NL + Strings.get("String_Node_Str",command,failedServerNames));
    if (succeededServerNames.length() > 0) {
      report.setActionExitCode(ExitCode.WARNING);
    }
 else {
      report.setActionExitCode(ExitCode.FAILURE);
    }
  }
  if (!waitingForServerNames.isEmpty()) {
    msg=Strings.get("String_Node_Str",command,listToString(waitingForServerNames));
    logger.warning(msg);
    if (output.length() > 0) {
      output.append(NL);
    }
    output.append(msg);
    report.setActionExitCode(ExitCode.WARNING);
  }
  report.setMessage(output.toString());
  threadPool.shutdown();
  return report;
}

Fixed version (ground truth):
/** 
 * Loop through all instances in a cluster and execute a command for each one.
 * @param command       The string of the command to run. The instancename will be used as the operand for the command.
 * @param map           A map of parameters to use for the command. May benull if no parameters. When the command is executed for a server instance, the instance name is set as the DEFAULT parameter (operand)
 * @param clusterName   The name of the cluster containing the instancesto run the command against.
 * @param context       The AdminCommandContext to use when executing thecommand.
 * @param verbose       true for more verbose output
 * @return              An ActionReport containing the results
 * @throws CommandException
 */
ActionReport runCommand(String command,ParameterMap map,String clusterName,AdminCommandContext context,boolean verbose) throws CommandException {
  final long startTime=System.currentTimeMillis();
  Logger logger=context.getLogger();
  ActionReport report=context.getActionReport();
  Cluster cluster=domain.getClusterNamed(clusterName);
  if (cluster == null) {
    String msg=Strings.get("String_Node_Str",clusterName);
    throw new CommandException(msg);
  }
  List<Server> targetServers=domain.getServersInTarget(clusterName);
  if (targetServers == null || targetServers.isEmpty()) {
    report.setActionExitCode(ExitCode.SUCCESS);
    report.setMessage(Strings.get("String_Node_Str",clusterName));
    return report;
  }
  int nInstances=targetServers.size();
  StringBuilder failedServerNames=new StringBuilder();
  StringBuilder succeededServerNames=new StringBuilder();
  List<String> waitingForServerNames=new ArrayList<String>();
  String msg;
  ReportResult reportResult=new ReportResult();
  boolean failureOccurred=false;
  progress=context.getProgressStatus();
  StringBuilder output=new StringBuilder();
  if (logger.isLoggable(Level.FINE))   logger.fine(String.format("String_Node_Str",serverListToString(targetServers)));
  targetServers=optimizeServerListOrder(targetServers);
  ArrayBlockingQueue<CommandRunnable> responseQueue=new ArrayBlockingQueue<CommandRunnable>(nInstances);
  int adminThreadPoolSize=getAdminThreadPoolSize(logger);
  int threadPoolSize=Math.min(nInstances,adminThreadPoolSize / 2);
  if (threadPoolSize < 1)   threadPoolSize=1;
  ExecutorService threadPool=Executors.newFixedThreadPool(threadPoolSize);
  if (map == null) {
    map=new ParameterMap();
  }
  msg=String.format("String_Node_Str",command,nInstances,threadPoolSize,serverListToString(targetServers));
  logger.info(msg);
  msg=Strings.get("String_Node_Str",command,Integer.toString(nInstances));
  progress.setTotalStepCount(nInstances);
  progress.progress(msg);
  for (  Server server : targetServers) {
    String iname=server.getName();
    waitingForServerNames.add(iname);
    ParameterMap instanceParameterMap=new ParameterMap(map);
    instanceParameterMap.set("String_Node_Str",iname);
    ActionReport instanceReport=runner.getActionReport("String_Node_Str");
    instanceReport.setActionExitCode(ExitCode.SUCCESS);
    CommandInvocation invocation=runner.getCommandInvocation(command,instanceReport,context.getSubject());
    invocation.parameters(instanceParameterMap);
    msg=command + "String_Node_Str" + iname;
    logger.info(msg);
    if (verbose) {
      output.append(msg).append(NL);
    }
    CommandRunnable cmdRunnable=new CommandRunnable(invocation,instanceReport,responseQueue);
    cmdRunnable.setName(iname);
    threadPool.execute(cmdRunnable);
  }
  if (logger.isLoggable(Level.FINE))   logger.fine(String.format("String_Node_Str",command));
  long adminTimeout=RemoteRestAdminCommand.getReadTimeout() - 3000;
  if (adminTimeout <= 0) {
    adminTimeout=57 * 1000;
  }
  if (logger.isLoggable(Level.FINE))   logger.fine(String.format("String_Node_Str",adminTimeout));
  for (int n=0; n < nInstances; n++) {
    long timeLeft=adminTimeout - (System.currentTimeMillis() - startTime);
    if (timeLeft < 0) {
      timeLeft=0;
    }
    CommandRunnable cmdRunnable=null;
    try {
      cmdRunnable=responseQueue.poll(timeLeft,TimeUnit.MILLISECONDS);
    }
 catch (    InterruptedException e) {
      threadPool.shutdownNow();
      msg=Strings.get("String_Node_Str",clusterName,Integer.toString(n),Integer.toString(nInstances),command);
      logger.warning(msg);
      output.append(msg).append(NL);
      failureOccurred=true;
      Thread.currentThread().interrupt();
      break;
    }
    if (cmdRunnable == null) {
      break;
    }
    String iname=cmdRunnable.getName();
    waitingForServerNames.remove(iname);
    ActionReport instanceReport=cmdRunnable.getActionReport();
    if (logger.isLoggable(Level.FINE))     logger.fine(String.format("String_Node_Str",n + 1,nInstances,iname,instanceReport.getActionExitCode()));
    if (instanceReport.getActionExitCode() != ExitCode.SUCCESS) {
      failureOccurred=true;
      failedServerNames.append(iname).append("String_Node_Str");
      reportResult.failedServerNames.add(iname);
      msg=iname + "String_Node_Str" + instanceReport.getMessage();
      logger.severe(msg);
      output.append(msg).append(NL);
      msg=Strings.get("String_Node_Str",command,iname);
      progress.progress(1,msg);
    }
 else {
      succeededServerNames.append(iname).append("String_Node_Str");
      reportResult.succeededServerNames.add(iname);
      progress.progress(1,iname);
    }
  }
  report.setActionExitCode(ExitCode.SUCCESS);
  if (failureOccurred) {
    report.setResultType(List.class,reportResult.failedServerNames);
  }
 else {
    report.setResultType(List.class,reportResult.succeededServerNames);
  }
  if (succeededServerNames.length() > 0 && (verbose || failureOccurred)) {
    output.append(NL + Strings.get("String_Node_Str",command,succeededServerNames));
  }
  if (failureOccurred) {
    output.append(NL + Strings.get("String_Node_Str",command,failedServerNames));
    if (succeededServerNames.length() > 0) {
      report.setActionExitCode(ExitCode.WARNING);
    }
 else {
      report.setActionExitCode(ExitCode.FAILURE);
    }
  }
  if (!waitingForServerNames.isEmpty()) {
    msg=Strings.get("String_Node_Str",command,listToString(waitingForServerNames));
    logger.warning(msg);
    if (output.length() > 0) {
      output.append(NL);
    }
    output.append(msg);
    report.setActionExitCode(ExitCode.WARNING);
  }
  report.setMessage(output.toString());
  threadPool.shutdown();
  return report;
}