Example #1
0
  public static void init(String[] args, Object... objects) {
    // Parse options
    parser = new OptionsParser();
    parser.doRegister("log", LogInfo.class);
    parser.doRegister("exec", Execution.class);
    parser.doRegisterAll(objects);
    // These options are specific to the execution, so we don't want to overwrite them
    // with a previous execution's.
    parser.setDefaultDirFileName("options.map");
    parser.setIgnoreOptsFromFileName(
        "options.map",
        ListUtils.newList(
            "log.file",
            "exec.execDir",
            "exec.execPoolDir",
            "exec.actualPoolDir",
            "exec.makeThunk"));
    if (!parser.doParse(args)) System.exit(1);

    // Load classes
    if (jarFiles.size() > 0) {
      List<String> names = new ArrayList();
      for (String jarFile : jarFiles) names.add(new File(jarFile).getName());
      stderr.println("Loading JAR files: " + StrUtils.join(names));
      for (String jarFile : jarFiles) // Load classes
      ClassInitializer.initializeJar(jarFile);
    }
    // Set character encoding
    if (charEncoding != null) CharEncUtils.setCharEncoding(charEncoding);

    if (printOptionsAndExit) { // Just print options and exit
      parser.doGetOptionPairs().print(stdout);
      System.exit(0);
    }

    // Create a new directory
    if (create) {
      createVirtualExecDir();
      stderr.println(virtualExecDir);
      if (!makeThunk) LogInfo.file = getFile("log");

      // Copy the Jar files for reference
      if (!makeThunk) {
        for (String jarFile : jarFiles)
          Utils.systemHard(String.format("cp %s %s", jarFile, virtualExecDir));
      }
    } else {
      LogInfo.file = "";
    }

    if (!makeThunk) {
      LogInfo.init();
      if (startMainTrack) track("main()", true);
    }

    // Output options
    if (!makeThunk && virtualExecDir != null) logs("Execution directory: " + virtualExecDir);
    if (!makeThunk) getInfo().printEasy(getFile("info.map"));
    printOptions();
    if (create && addToView.size() > 0)
      IOUtils.printLinesHard(Execution.getFile("addToView"), addToView);

    // Start monitoring
    if (!makeThunk && monitor) {
      monitorThread = new MonitorThread();
      monitorThread.start();
    }

    if (!makeThunk) Record.init(Execution.getFile("record"));
  }
Example #2
0
  /** Return an unused directory in the execution pool directory. Set virtualExecDir */
  public static String createVirtualExecDir() {
    if (useStandardExecPoolDirStrategy) {
      // Assume we are in the run directory, so set the standard paths
      execPoolDir = new File(SysInfoUtils.getcwd(), "state/execs").toString();
      actualExecPoolDir =
          new File(SysInfoUtils.getcwd(), "state/hosts/" + SysInfoUtils.getShortHostName())
              .toString();
      if (!new File(actualExecPoolDir).isDirectory()) actualExecPoolDir = null;
    }
    if (!StrUtils.isEmpty(execPoolDir) && !new File(execPoolDir).isDirectory())
      throw Exceptions.bad("Execution pool directory '" + execPoolDir + "' doesn't exist");
    if (!StrUtils.isEmpty(actualExecPoolDir) && !new File(actualExecPoolDir).isDirectory())
      throw Exceptions.bad(
          "Actual execution pool directory '" + actualExecPoolDir + "' doesn't exist");

    if (!StrUtils.isEmpty(execDir)) { // Use specified execDir
      boolean exists = new File(execDir).isDirectory();
      if (exists && !overwriteExecDir)
        throw Exceptions.bad("Directory %s already exists and overwrite flag is false", execDir);
      if (!exists) mkdirHard(new File(execDir));
      else {
        // This part looks at actualExecPoolDir
        // This case is overwriting an existing execution directory, which
        // happens when we are executing a thunk.  We have to be careful here
        // because the actual symlinked directory that was created when thunking
        // might be using a different actualPoolDir.  If this happens, we need
        // to move the actual thunked symlinked directory into the actual
        // execution pool directory requested.  In fact, we always do this for simplicity.
        String oldActualExecDir = Utils.systemGetStringOutputEasy("readlink " + execDir);
        if (oldActualExecDir == null) { // Not symlink
          if (!StrUtils.isEmpty(actualExecPoolDir))
            throw Exceptions.bad(
                "The old execution directory was not created with actualExecPoolDir but now we want an actualExecPoolDir");
          // Do nothing, just use the directory as is
        } else { // Symlink
          oldActualExecDir = oldActualExecDir.trim();
          if (StrUtils.isEmpty(actualExecPoolDir))
            throw Exceptions.bad(
                "The old execution directory was created with actualExecPoolDir but now we don't want an actualExecPoolDir");
          // Note that now the execution numbers might not correspond between the
          // actual and virtual execution pool directories.
          File newActualExecDir = null;
          for (int i = 0; ; i++) {
            newActualExecDir = new File(actualExecPoolDir, i + "a.exec");
            if (!newActualExecDir.exists()) break;
          }
          // Move the old directory to the new directory
          Utils.systemHard(String.format("mv %s %s", oldActualExecDir, newActualExecDir));
          // Update the symlink (execDir -> newActualExecDir)
          Utils.systemHard(
              String.format("ln -sf %s %s", newActualExecDir.getAbsolutePath(), execDir));
        }
      }
      return virtualExecDir = execDir;
    }

    // execDir hasn't been specified, so we need to pick one from a pool directory
    // execPoolDir must exist; actualExecPoolDir is optional

    // Get a list of files that already exists
    Set<String> files = new HashSet<String>();
    for (String f : new File(execPoolDir).list()) files.add(f);

    // Go through and pick out a file that doesn't exist
    int numFailures = 0;
    for (int i = 0; numFailures < 3; i++) {
      // Either the virtual file (a link) or the actual file
      File f = new File(execPoolDir, i + ".exec");
      // Actual file
      File g =
          StrUtils.isEmpty(actualExecPoolDir) ? null : new File(actualExecPoolDir, i + ".exec");

      if (!files.contains(i + ".exec") && (g == null || !g.exists())) {
        if (g == null || g.equals(f)) {
          mkdirHard(f);
          return virtualExecDir = f.toString();
        }
        // Create symlink before mkdir to try to reserve the name and avoid race conditions
        if (Utils.createSymLink(g.getAbsolutePath(), f.getAbsolutePath())) {
          mkdirHard(g);
          return virtualExecDir = f.toString();
        }

        // Probably because someone else already linked to it
        // in the race condition: so try again
        stderr.println("Cannot create symlink from " + f + " to " + g);
        numFailures++;
      }
    }
    throw Exceptions.bad("Failed many times to create execution directory");
  }
Example #3
0
 public static void linkFileFromExec(String file, String realFileName) {
   if (StrUtils.isEmpty(realFileName) || StrUtils.isEmpty(file)) return;
   File f = new File(realFileName);
   Utils.createSymLink(getFile(file), f.getAbsolutePath());
 }