private void defaultInit() {

    setTimes();
    try {
      this.setProperty(
          InMemoInformation.INFO_ID_PROPERTY_NAME, java.util.UUID.randomUUID().toString());
    } catch (SharkKBException ex) {
      // cannot happen
    }
  }
  /** create it and set status */
  protected InMemoInformation(
      String contentType, long lastModified, long creationTime, PeerSTSet recipientSet) {

    try {
      this.setProperty(InMemoInformation.INFO_CONTENT_TYPE, contentType);
      this.setProperty(InMemoInformation.INFO_LAST_MODIFED, Long.toString(lastModified));
      this.setProperty(InMemoInformation.INFO_CREATION_TIME, Long.toString(creationTime));
      this.setProperty(
          InMemoInformation.INFO_ID_PROPERTY_NAME, java.util.UUID.randomUUID().toString());
    } catch (SharkKBException ex) {
      // cannot happen
    }
  }
  public static void main(String[] args) throws Exception {
    System.out.println("Beginning job...");
    // Start phase 1
    Configuration conf = new Configuration();

    conf.set("mapred.job.tracker", "local");
    conf.set("fs.default.name", "local");

    String[] inputArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

    Path xmlPath = null;
    List<String> other_args = new ArrayList<String>();
    for (int i = 0; i < args.length; ++i) {
      if ("-setup".equals(inputArgs[i])) {
        xmlPath = new Path(inputArgs[++i]);
        DistributedCache.addCacheFile(xmlPath.toUri(), conf);
        conf.setBoolean("minibatch.job.setup", true);

      } else {
        other_args.add(inputArgs[i]);
      }
    }

    String[] tool_args = other_args.toArray(new String[0]);
    int result = ToolRunner.run(conf, new BatchGenerationEngine(), tool_args);
    // End phase 1

    // get example count and size from xml file
    // count = count_size[0];
    // size = count_size[1];
    int[] count_size = parseJobSetup(xmlPath);

    // distribute those output from phase 1 into different directories
    String outputPhase1 = tool_args[1];
    FileSystem fs = FileSystem.get(new Configuration());
    Path outputPhase1Path = new Path(outputPhase1);
    fs.setWorkingDirectory(outputPhase1Path);
    FileStatus[] outputP1AllFiles = fs.listStatus(outputPhase1Path);
    for (int i = 0; i < outputP1AllFiles.length; i++) {
      int batchNum = i / count_size[1];
      Path batchPath = new Path(outputPhase1 + "/batch" + batchNum);

      // if batch# directory not exists, mkdir
      if (!fs.exists(batchPath)) FileSystem.mkdirs(fs, batchPath, new FsPermission("777"));
      // move file into the batch# directory
      fs.rename(
          outputP1AllFiles[i].getPath(),
          new Path(
              outputPhase1 + "/batch" + batchNum + "/" + outputP1AllFiles[i].getPath().getName()));
    }
    //

    // Generate dictionary of jobs
    int numberOfJobs = count_size[0] * count_size[1];
    JobConfig[] dictionary = new JobConfig[numberOfJobs];

    // Add job 0 to dictionary
    Configuration conf0 = new Configuration();
    DistributedCache.addCacheFile(xmlPath.toUri(), conf0);
    JobConfig job0 = new JobConfig(conf0, "input go here", java.util.UUID.randomUUID().toString());
    dictionary[0] = job0;

    // Add the rest of jobs into dictionary
    for (int i = 1; i < dictionary.length; i++) {
      Configuration newConf = new Configuration();
      DistributedCache.addCacheFile(xmlPath.toUri(), newConf);
      JobConfig newJob =
          new JobConfig(newConf, dictionary[i - 1].args[1], java.util.UUID.randomUUID().toString());
      dictionary[i] = newJob;
    }

    // running the jobs
    logger.info("Start " + dictionary.length + " jobs!");
    for (int i = 0; i < dictionary.length; i++) {
      int runResult =
          ToolRunner.run(dictionary[i].conf, new BatchGenerationEngine(), dictionary[i].args);
      if (runResult == 1) {
        logger.info("Job " + i + "-th Re-run once!");
        dictionary[i].args[1] = java.util.UUID.randomUUID().toString();
        runResult =
            ToolRunner.run(dictionary[i].conf, new BatchGenerationEngine(), dictionary[i].args);
      }
      if (runResult == 1) {
        logger.info("Job " + i + "-th Re-run twice!");
        dictionary[i].args[1] = java.util.UUID.randomUUID().toString();
        runResult =
            ToolRunner.run(dictionary[i].conf, new BatchGenerationEngine(), dictionary[i].args);
      }
      if (runResult == 1) {
        logger.info("Job " + i + "-th Failed!");
        break;
      } else {
        if (i - 1 < dictionary.length) dictionary[i + 1].args[0] = dictionary[i].args[1];
      }
    }

    System.exit(1);
  }
Exemple #4
0
  public static void main(String args[]) throws Exception {
    boolean usePersistConnection = JokeClient.usePersistConnection;
    // default server name to connect
    String serverName = "localhost";

    // read optional serverName param from args if there is args.
    if (args.length > 0) {
      serverName = args[0];
    }

    String userName = null;

    String clientId = java.util.UUID.randomUUID().toString();
    /** user can pass a clientId as arg. */
    if (args.length > 1) {
      clientId = args[1];
    }
    /** user can pass usePersistConnection as arg. */
    if (args.length > 2) {
      usePersistConnection = Boolean.parseBoolean(args[2]);
    }
    // print configuration information
    printConfiguration(serverName, clientId, usePersistConnection);
    /**
     * we need to close resources after use them, so declared two named object, and auto close them
     * by jdk7.
     */
    try (InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader); ) {
      /** create connection to server .I do not want reconnect socket each time, so I re-use it. */
      Socket server = null;
      /** when use PersistConnection, will not re connect each time */
      if (usePersistConnection) {
        server = new Socket(serverName, SERVER_PORT);
      }
      // continue do job until break by user's quit command
      while (true) {
        if (!usePersistConnection) {
          server = new Socket(serverName, SERVER_PORT);
        }
        if (userName == null) {
          System.out.print(
              "Enter \""
                  + ClientCommandType.SetName
                  + " yourName\" to set your name,("
                  + QUIT_COMMAND
                  + ") to end: ");
        } else {
          System.out.print(
              "Enter "
                  + ClientCommandType.QueryNext
                  + " to query next joke/proverb, ("
                  + QUIT_COMMAND
                  + ") to end: ");
        }
        System.out.flush();

        String clientCommand = bufferedReader.readLine();
        /** validate input.must be not null and not empty. */
        if (clientCommand == null || clientCommand.isEmpty()) {
          System.out.println("command must not be null or empty");
          continue;
        }

        /**
         * More strict condition to quit. Must exactly equals quit command to quit, other than only
         * contains quit command in the host name.
         */
        if (clientCommand.equals(QUIT_COMMAND)) {
          System.out.println("Cancelled by user request.");
          // break is to quit here.
          break;
        }
        /** if userName is not set ,do not allow query command. */
        if (userName == null) {
          if (!clientCommand.startsWith(ClientCommandType.SetName.toString())) {
            System.out.println("You must enter name before QueryNext joke or proverb.");
            continue;
          } else {
            /**
             * client don't store input userName, instead ,it store verified and accepted userName
             * by server.
             */
            userName = getResponse(clientId, clientCommand, server)[0];
            continue;
          }
        }
        // didn't quit. userName is set. so do query and print.
        getResponse(clientId, clientCommand, server);

        if (!usePersistConnection) {
          server.close();
        }
      }
    } catch (IOException x) {
      System.out.println("Socket error. Client Terminated");
      x.printStackTrace();
    }
  }