Example #1
0
  /** Updates the list of instance type descriptions based on the currently registered hosts. */
  private void updateInstaceTypeDescriptionMap() {

    // this.registeredHosts.values().iterator()
    this.instanceTypeDescriptionMap.clear();

    final List<InstanceTypeDescription> instanceTypeDescriptionList =
        new ArrayList<InstanceTypeDescription>();

    // initialize array which stores the availability counter for each instance type
    final int[] numberOfInstances = new int[this.availableInstanceTypes.length];
    for (int i = 0; i < numberOfInstances.length; i++) {
      numberOfInstances[i] = 0;
    }

    // Shuffle through instance types
    for (int i = 0; i < this.availableInstanceTypes.length; i++) {

      final InstanceType currentInstanceType = this.availableInstanceTypes[i];
      int numberOfMatchingInstances = 0;
      int minNumberOfCPUCores = Integer.MAX_VALUE;
      long minSizeOfPhysicalMemory = Long.MAX_VALUE;
      long minSizeOfFreeMemory = Long.MAX_VALUE;
      final Iterator<ClusterInstance> it = this.registeredHosts.values().iterator();
      while (it.hasNext()) {
        final ClusterInstance clusterInstance = it.next();
        if (clusterInstance.getType().equals(currentInstanceType)) {
          ++numberOfMatchingInstances;
          final HardwareDescription hardwareDescription = clusterInstance.getHardwareDescription();
          minNumberOfCPUCores =
              Math.min(minNumberOfCPUCores, hardwareDescription.getNumberOfCPUCores());
          minSizeOfPhysicalMemory =
              Math.min(minSizeOfPhysicalMemory, hardwareDescription.getSizeOfPhysicalMemory());
          minSizeOfFreeMemory =
              Math.min(minSizeOfFreeMemory, hardwareDescription.getSizeOfFreeMemory());
        }
      }

      // Update number of instances
      int highestAccommodationNumber = -1;
      int highestAccommodationIndex = -1;
      for (int j = 0; j < this.availableInstanceTypes.length; j++) {
        final int accommodationNumber = canBeAccommodated(j, i);
        // LOG.debug(this.availableInstanceTypes[j].getIdentifier() + " fits into "
        // + this.availableInstanceTypes[i].getIdentifier() + " " + accommodationNumber + " times");
        if (accommodationNumber > 0) {
          numberOfInstances[j] += numberOfMatchingInstances * accommodationNumber;
          if (accommodationNumber > highestAccommodationNumber) {
            highestAccommodationNumber = accommodationNumber;
            highestAccommodationIndex = j;
          }
        }
      }

      // Calculate hardware description
      HardwareDescription pessimisticHardwareDescription = null;
      if (minNumberOfCPUCores < Integer.MAX_VALUE
          && minSizeOfPhysicalMemory < Long.MAX_VALUE
          && minSizeOfFreeMemory < Long.MAX_VALUE) {

        pessimisticHardwareDescription =
            HardwareDescriptionFactory.construct(
                minNumberOfCPUCores, minSizeOfPhysicalMemory, minSizeOfFreeMemory);

      } else {

        if (highestAccommodationIndex
            < i) { // Since highestAccommodationIndex smaller than my index, the
          // target instance must be more powerful

          final InstanceTypeDescription descriptionOfLargerInstanceType =
              instanceTypeDescriptionList.get(highestAccommodationIndex);
          if (descriptionOfLargerInstanceType.getHardwareDescription() != null) {
            final HardwareDescription hardwareDescriptionOfLargerInstanceType =
                descriptionOfLargerInstanceType.getHardwareDescription();

            final int numCores =
                hardwareDescriptionOfLargerInstanceType.getNumberOfCPUCores()
                    / highestAccommodationNumber;
            final long physMem =
                hardwareDescriptionOfLargerInstanceType.getSizeOfPhysicalMemory()
                    / highestAccommodationNumber;
            final long freeMem =
                hardwareDescriptionOfLargerInstanceType.getSizeOfFreeMemory()
                    / highestAccommodationNumber;

            pessimisticHardwareDescription =
                HardwareDescriptionFactory.construct(numCores, physMem, freeMem);
          }
        }
      }

      instanceTypeDescriptionList.add(
          InstanceTypeDescriptionFactory.construct(
              currentInstanceType, pessimisticHardwareDescription, numberOfInstances[i]));
    }

    final Iterator<InstanceTypeDescription> it = instanceTypeDescriptionList.iterator();
    while (it.hasNext()) {

      final InstanceTypeDescription itd = it.next();
      this.instanceTypeDescriptionMap.put(itd.getInstanceType(), itd);
    }
  }