Пример #1
0
  /**
   * Creates a new Cluster, with a number of nodes and a resource manager
   *
   * <p>
   *
   * <DL>
   *   <DT><B>Preconditions:</B>
   *   <DD>parameter <CODE>name</CODE> cannot be null<br>
   *   <DD>parameter <CODE>gridSchedulerHostname</CODE> cannot be null<br>
   *   <DD>parameter <CODE>gridSchedulerPort</CODE> cannot be inferior or equal to 0<br>
   *   <DD>parameter <CODE>nrNodes</code> must be greater than 0
   * </DL>
   *
   * @param name the name of this cluster
   * @param nrNodes the number of nodes in this cluster
   */
  public Cluster(
      final int id,
      int nEntities,
      int nodeCount,
      final int nJobsToExecute,
      String hostname,
      int port,
      String gridSchedulerHostname,
      int gridSchedulerPort,
      boolean restart) {
    // Preconditions
    assert (hostname != null) : "parameter 'hostname' cannot be null";
    assert (port > 0) : "parameter 'port' cannot be inferior or equal to 0";
    assert (gridSchedulerHostname != null) : "parameter 'gridSchedulerHostname' cannot be null";
    assert (gridSchedulerPort > 0)
        : "parameter 'gridSchedulerPort' cannot be inferior or equal to 0";
    assert (nodeCount > 0) : "parameter 'nodeCount' cannot be smaller or equal to zero";

    // Initialize members
    this.id = id;
    this.rmHostname = hostname;
    this.rmPort = port;
    this.nodes = new ArrayList<Node>(nodeCount);

    // Initialize the resource manager for this cluster
    resourceManager =
        new ResourceManager(id, nEntities, this, restart, gridSchedulerHostname, gridSchedulerPort);

    // Initialize the nodes
    for (int i = 0; i < nodeCount; i++) {
      Node n = new Node();
      // Make nodes report their status to the resource manager
      n.addNodeEventHandler(resourceManager);
      nodes.add(n);
    }

    resourceManager.connectToGridScheduler(gridSchedulerHostname, gridSchedulerPort);

    try {
      System.out.println(resourceManager.getGsList() + "\n5 seconds to start generating jobs...");
      Thread.sleep(5000L);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    System.out.println(resourceManager.getGsList());

    if (restart) {
      // Get the Jobs that need to be executed from the analysis of the Log
      final HashSet<Long> outsideJobsToExecute = resourceManager.getOutsideJobsToExecute();
      final HashSet<Long> ownJobsToIgnore = resourceManager.getOwnJobsToIgnore();
      System.out.println("Internal Jobs to be ignored (already compeleted)\n" + ownJobsToIgnore);
      System.out.println("External Jobs to be generated:\n" + outsideJobsToExecute);
      // Create the thread that will add the local Jobs that were not completed
      Thread createOwnJobs =
          new Thread(
              new Runnable() {
                public void run() {
                  int jobId = id * JOBID_MULTIPLICATION_FACTOR;
                  for (int i = 0; i < nJobsToExecute; i++) {
                    jobId++;
                    if (ownJobsToIgnore.contains(new Long(jobId))) {
                      continue;
                    }
                    Job job = new Job(8000 + (int) (Math.random() * 5000), jobId);
                    job.setOriginalRM(new InetSocketAddress(rmHostname, rmPort));
                    getResourceManager().addJob(job);
                    // Sleep a while before creating a new job
                    try {
                      Thread.sleep(JOB_CREATION_DELAY);
                    } catch (InterruptedException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                    }
                  }
                }
              });
      // Create the thread that will add the outside jobs that were not completed
      Thread createOutsideJobs =
          new Thread(
              new Runnable() {
                public void run() {
                  for (Long id : outsideJobsToExecute) {
                    Job job = new Job(8000 + (int) (Math.random() * 5000), id);
                    job.setOriginalRM(new InetSocketAddress(rmHostname, rmPort));
                    getResourceManager().addJob(job);
                    // Sleep a while before creating a new job
                    try {
                      Thread.sleep(JOB_CREATION_DELAY);
                    } catch (InterruptedException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                    }
                  }
                }
              });
      // Start the two threads that will generate the needed Jobs
      createOwnJobs.start();
      createOutsideJobs.start();
    }
    // Launch the thread normally.
    else {
      Thread createJobs =
          new Thread(
              new Runnable() {
                public void run() {
                  int jobId = id * JOBID_MULTIPLICATION_FACTOR;
                  for (int i = 0; i < nJobsToExecute; i++) {
                    Job job = new Job(8000 + (int) (Math.random() * 5000), jobId++);
                    job.setOriginalRM(new InetSocketAddress(rmHostname, rmPort));
                    getResourceManager().addJob(job);
                    // Sleep a while before creating a new job
                    try {
                      Thread.sleep(JOB_CREATION_DELAY);
                    } catch (InterruptedException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                    }
                  }
                }
              });
      createJobs.start();
    }

    // Start the polling thread
    running = true;
    pollingThread = new Thread(this);
    pollingThread.start();
  }