Ejemplo n.º 1
0
    @Override
    public boolean apply(Allocation allocInfo)
        throws MetadataException, AuthException, VerificationException {
      RunInstancesType msg = allocInfo.getRequest();
      String imageId = msg.getImageId();
      VmType vmType = allocInfo.getVmType();
      try {
        BootableSet bootSet = Emis.newBootableSet(imageId);
        allocInfo.setBootableSet(bootSet);

        // Add (1024L * 1024L * 10) to handle NTFS min requirements.
        if (!bootSet.isBlockStorage()) {
          if (Platform.windows.equals(bootSet.getMachine().getPlatform())
              && bootSet.getMachine().getImageSizeBytes()
                  > ((1024L * 1024L * 1024L * vmType.getDisk()) + (1024L * 1024L * 10))) {
            throw new ImageInstanceTypeVerificationException(
                "Unable to run instance "
                    + bootSet.getMachine().getDisplayName()
                    + " in which the size "
                    + bootSet.getMachine().getImageSizeBytes()
                    + " bytes of the instance is greater than the vmType "
                    + vmType.getDisplayName()
                    + " size "
                    + vmType.getDisk()
                    + " GB.");
          } else if (bootSet.getMachine().getImageSizeBytes()
              > ((1024L * 1024L * 1024L * vmType.getDisk()))) {
            throw new ImageInstanceTypeVerificationException(
                "Unable to run instance "
                    + bootSet.getMachine().getDisplayName()
                    + " in which the size "
                    + bootSet.getMachine().getImageSizeBytes()
                    + " bytes of the instance is greater than the vmType "
                    + vmType.getDisplayName()
                    + " size "
                    + vmType.getDisk()
                    + " GB.");
          }
        }
      } catch (VerificationException e) {
        throw e;
      } catch (MetadataException ex) {
        LOG.error(ex);
        throw ex;
      } catch (RuntimeException ex) {
        LOG.error(ex);
        throw new VerificationException(
            "Failed to verify references for request: "
                + msg.toSimpleString()
                + " because of: "
                + ex.getMessage(),
            ex);
      }
      return true;
    }
  @Override
  public void fireEvent(final ClockTick event) {
    if (Bootstrap.isFinished() && Hosts.isCoordinator()) {

      final List<ResourceAvailabilityEvent> resourceAvailabilityEvents = Lists.newArrayList();
      final Map<ResourceAvailabilityEvent.ResourceType, AvailabilityAccumulator> availabilities =
          Maps.newEnumMap(ResourceAvailabilityEvent.ResourceType.class);
      final Iterable<VmType> vmTypes = Lists.newArrayList(VmTypes.list());
      for (final Cluster cluster : Clusters.getInstance().listValues()) {
        availabilities.put(Core, new AvailabilityAccumulator(VmType.SizeProperties.Cpu));
        availabilities.put(Disk, new AvailabilityAccumulator(VmType.SizeProperties.Disk));
        availabilities.put(Memory, new AvailabilityAccumulator(VmType.SizeProperties.Memory));

        for (final VmType vmType : vmTypes) {
          final ResourceState.VmTypeAvailability va =
              cluster.getNodeState().getAvailability(vmType.getName());

          resourceAvailabilityEvents.add(
              new ResourceAvailabilityEvent(
                  Instance,
                  new ResourceAvailabilityEvent.Availability(
                      va.getMax(),
                      va.getAvailable(),
                      Lists.<ResourceAvailabilityEvent.Tag>newArrayList(
                          new ResourceAvailabilityEvent.Dimension(
                              "availabilityZone", cluster.getPartition()),
                          new ResourceAvailabilityEvent.Dimension("cluster", cluster.getName()),
                          new ResourceAvailabilityEvent.Type("vm-type", vmType.getName())))));

          for (final AvailabilityAccumulator availability : availabilities.values()) {
            availability.total =
                Math.max(
                    availability.total, va.getMax() * availability.valueExtractor.apply(vmType));
            availability.available =
                Math.max(
                    availability.available,
                    va.getAvailable() * availability.valueExtractor.apply(vmType));
          }
        }

        for (final AvailabilityAccumulator availability : availabilities.values()) {
          availability.rollUp(
              Lists.<ResourceAvailabilityEvent.Tag>newArrayList(
                  new ResourceAvailabilityEvent.Dimension(
                      "availabilityZone", cluster.getPartition()),
                  new ResourceAvailabilityEvent.Dimension("cluster", cluster.getName())));
        }
      }

      for (final Map.Entry<ResourceAvailabilityEvent.ResourceType, AvailabilityAccumulator> entry :
          availabilities.entrySet()) {
        resourceAvailabilityEvents.add(
            new ResourceAvailabilityEvent(entry.getKey(), entry.getValue().availabilities));
      }

      for (final ResourceAvailabilityEvent resourceAvailabilityEvent : resourceAvailabilityEvents)
        try {
          ListenerRegistry.getInstance().fireEvent(resourceAvailabilityEvent);
        } catch (Exception ex) {
          logger.error(ex, ex);
        }
    }
  }