/** @return true if number of instances are evenly distributed across the specified containers */
  public static boolean isEvenlyDistributedAcrossContainers(
      ProcessingUnit pu, GridServiceContainer[] containers) {

    if (!isProcessingUnitIntact(pu, containers)) {
      return false;
    }

    boolean evenlyDistributed = true;
    int numberOfInstances = pu.getTotalNumberOfInstances();
    int numberOfContainers = containers.length;
    if (numberOfInstances < numberOfContainers) {
      evenlyDistributed = false;
    } else {
      double expectedAverageNumberOfInstancesPerContainer =
          1.0 * numberOfInstances / numberOfContainers;
      int numberOfServicesPerContainerUpperBound =
          (int) Math.ceil(expectedAverageNumberOfInstancesPerContainer);
      int numberOfServicesPerContainerLowerBound =
          (int) Math.floor(expectedAverageNumberOfInstancesPerContainer);

      for (GridServiceContainer container : containers) {

        int puNumberOfInstances = container.getProcessingUnitInstances(pu.getName()).length;

        if (puNumberOfInstances < numberOfServicesPerContainerLowerBound
            || puNumberOfInstances > numberOfServicesPerContainerUpperBound) {
          evenlyDistributed = false;
          break;
        }
      }
    }
    return evenlyDistributed;
  }
 public static Machine[] getMachinesHostingContainers(GridServiceContainer[] containers) {
   Set<Machine> machines = new HashSet<Machine>();
   for (GridServiceContainer container : containers) {
     machines.add(container.getMachine());
   }
   return machines.toArray(new Machine[machines.size()]);
 }
 private static List<GridServiceContainer> getUnusedContainers(
     final ProcessingUnit pu, final GridServiceContainer[] containers) {
   // look for free containers
   List<GridServiceContainer> unusedContainers = new ArrayList<GridServiceContainer>();
   for (GridServiceContainer container : containers) {
     if (container.getProcessingUnitInstances(pu.getName()).length == 0) {
       unusedContainers.add(container);
     }
   }
   return unusedContainers;
 }
 public static int getNumberOfPrimaryInstancesOnMachine(ProcessingUnit pu, Machine machine) {
   int numberOfPrimaryInstances = 0;
   for (GridServiceContainer container : machine.getGridServiceContainers()) {
     for (ProcessingUnitInstance instance : container.getProcessingUnitInstances(pu.getName())) {
       if (instance.getSpaceInstance() != null
           && instance.getSpaceInstance().getMode() == SpaceMode.PRIMARY) {
         numberOfPrimaryInstances++;
       }
     }
   }
   return numberOfPrimaryInstances;
 }
 private static ProcessingUnitInstance findProcessingUnitInstance(
     ProcessingUnit pu, int instanceId, int backupId, GridServiceContainer[] containers) {
   for (final GridServiceContainer container : containers) {
     for (final ProcessingUnitInstance instance :
         container.getProcessingUnitInstances(pu.getName())) {
       if (instance.getInstanceId() == instanceId && instance.getBackupId() == backupId) {
         return instance;
       }
     }
   }
   return null;
 }
 public static String processingUnitDeploymentToString(ProcessingUnit pu) {
   StringBuilder deployment = new StringBuilder();
   for (final GridServiceContainer container : pu.getAdmin().getGridServiceContainers()) {
     deployment.append(gscToString(container));
     deployment.append(" { ");
     for (final ProcessingUnitInstance instance :
         container.getProcessingUnitInstances(pu.getName())) {
       deployment.append(puInstanceToString(instance));
       deployment.append(" ");
     }
     deployment.append(" } ");
   }
   return deployment.toString();
 }
  private static GridServiceContainer[] getContainersOnMachines(
      ProcessingUnit pu, Machine[] machines) {
    if (pu.getRequiredZones().length != 1) {
      throw new IllegalStateException(
          "Processing Unit must have exactly one container zone defined.");
    }
    final List<GridServiceContainer> containers = new ArrayList<GridServiceContainer>();
    for (final Machine machine : machines) {
      for (final GridServiceContainer container : machine.getGridServiceContainers()) {
        if (container.getZones().size() == 1
            && container.getZones().containsKey(pu.getRequiredZones()[0])) {

          containers.add(container);
        }
      }
    }
    return containers.toArray(new GridServiceContainer[containers.size()]);
  }
  public static Set<ProcessingUnitInstance> getOtherInstancesFromSamePartitionInContainer(
      GridServiceContainer container, ProcessingUnitInstance instance) {
    Set<ProcessingUnitInstance> puInstancesFromSamePartition =
        new HashSet<ProcessingUnitInstance>();
    for (ProcessingUnitInstance instanceOnContainer :
        container.getProcessingUnitInstances(instance.getName())) {
      if (instanceOnContainer.getInstanceId() == instance.getInstanceId()
          && !instanceOnContainer.equals(instance)) {

        puInstancesFromSamePartition.add(instanceOnContainer);
      }
    }
    return puInstancesFromSamePartition;
  }
Exemplo n.º 9
0
  @Override
  public void perform(String command, String arguments) {
    boolean showAll = false;
    if ("-a".equals(arguments)) {
      showAll = true;
    }
    Admin admin = gash.getWorkingLocation().getCurrentConnection().getAdmin();
    for (Machine machine : admin.getMachines()) {
      Utils.println("%s", machine.getHostName());
      GridServiceContainer[] containers = machine.getGridServiceContainers().getContainers();
      Arrays.sort(
          containers,
          new Comparator<GridServiceContainer>() {
            @Override
            public int compare(GridServiceContainer o1, GridServiceContainer o2) {
              return new Integer(o1.getAgentId()).compareTo(o2.getAgentId());
            }
          });
      int containerCount = 0;
      for (GridServiceContainer container : containers) {
        containerCount++;
        String zones = null;
        for (String zoneName : container.getZones().keySet()) {
          if (zones == null) {
            zones = zoneName;
          } else {
            zones += ", " + zoneName;
          }
        }
        if (zones == null) {
          zones = "";
        } else {
          zones = "Zones [" + zones + "]";
        }
        int i = container.getAgentId();
        String containerTree = BEGIN_VERTICAL_AND_RIGHT;
        char containerVertical = VERTICAL;
        if (containerCount == containers.length) {
          containerTree = BEGIN_ELBOW;
          containerVertical = ' ';
        }
        if (showAll) {
          Utils.println(
              "%s GSC-%s [pid %s] uid[%s] %s",
              containerTree,
              i,
              container.getVirtualMachine().getDetails().getPid(),
              container.getUid(),
              zones);
        } else {
          Utils.println(
              "%s GSC-%s [pid %s] %s",
              containerTree, i, container.getVirtualMachine().getDetails().getPid(), zones);
        }

        int processingUnitCount = 0;
        ProcessingUnitInstance[] processingUnitInstances = container.getProcessingUnitInstances();
        sortProcessingUnitInstance(processingUnitInstances);
        for (ProcessingUnitInstance processingUnitInstance : processingUnitInstances) {
          processingUnitCount++;
          String processingUnitTree = BEGIN_VERTICAL_AND_RIGHT;
          char processingUnitVertical = VERTICAL;
          if (processingUnitCount == processingUnitInstances.length) {
            processingUnitTree = BEGIN_ELBOW;
            processingUnitVertical = ' ';
          }
          String primary =
              (processingUnitInstance.getSpaceInstance() == null
                      || processingUnitInstance
                          .getSpaceInstance()
                          .getMode()
                          .equals(SpaceMode.PRIMARY)
                  ? ""
                  : "Backup");
          StringBuilder puNameAndClusterInfo =
              Utils.getPUNameAndClusterInfo(processingUnitInstance);
          if (processingUnitInstance.isJee()) {
            puNameAndClusterInfo.append(" Web");
          }

          if (showAll) {
            Utils.println(
                "%s   %s %s %s id[%s]",
                containerVertical,
                processingUnitTree,
                puNameAndClusterInfo,
                primary,
                processingUnitInstance.getUid());
          } else {
            Utils.println(
                "%s   %s %s %s",
                containerVertical, processingUnitTree, puNameAndClusterInfo, primary);
          }

          if (processingUnitInstance.isJee()) {
            JeeServiceDetails jeeDetails = processingUnitInstance.getJeeDetails();
            if (jeeDetails.getPort() > 0) {
              String jeeTree = BEGIN_ELBOW;
              if (jeeDetails.getSslPort() > 0) {
                jeeTree = BEGIN_VERTICAL_AND_RIGHT;
              }
              Utils.println(
                  "%s   %s   %s http://%s:%s%s",
                  containerVertical,
                  processingUnitVertical,
                  jeeTree,
                  jeeDetails.getHost(),
                  jeeDetails.getPort(),
                  jeeDetails.getContextPath());
            }
            if (jeeDetails.getSslPort() > 0) {
              Utils.println(
                  "%s   %s   %s https://%s:%s%s",
                  containerVertical,
                  processingUnitVertical,
                  BEGIN_ELBOW,
                  jeeDetails.getHost(),
                  jeeDetails.getSslPort(),
                  jeeDetails.getContextPath());
            }
          } else {
            for (SpaceServiceDetails details : processingUnitInstance.getSpaceDetails()) {

              if (details.getSpaceType().equals(SpaceType.REMOTE)) {
                if (showAll) {
                  Utils.println(
                      "%s   %s   %s %s [%s] %s",
                      containerVertical,
                      processingUnitVertical,
                      BEGIN_ELBOW,
                      details.getId(),
                      details.getLongDescription(),
                      "Remote");
                }
              } else {
                String instancePrimary =
                    (processingUnitInstance.getSpaceInstance().getMode().equals(SpaceMode.PRIMARY)
                        ? " "
                        : " Backup");
                if (showAll) {
                  Utils.println(
                      "%s   %s   %s %s %s serviceId[%s]",
                      containerVertical,
                      processingUnitVertical,
                      BEGIN_ELBOW,
                      details.getId(),
                      instancePrimary,
                      details.getServiceID());
                } else {
                  Utils.println(
                      "%s   %s   %s %s %s",
                      containerVertical,
                      processingUnitVertical,
                      BEGIN_ELBOW,
                      details.getId(),
                      instancePrimary);
                }
              }
            }
          }
        }
      }
    }
  }