@Override
 public Instance update(Instance existingEntity, Instance requestedEntity) {
   LOG.debug(
       String.format(
           "update(Existing Instance - %s,Requested Instance - %s)",
           existingEntity, requestedEntity));
   InstanceStateTransition transition = new InstanceStateTransition();
   Instance entityToReturn = null;
   if (existingEntity != null) {
     transition.setPreviousState(existingEntity.getState());
     // we set this here in case we can't write the state want.
     // this way the user doesn't get back a null. :D.
     transition.setNextState(existingEntity.getState());
     instanceStatusMap.put(existingEntity.getInstanceId(), transition);
     if (existingEntity.getNodeId() != null
         && existingEntity.getState() != InstanceState.SHUTTING_DOWN
         && existingEntity.getState() != InstanceState.TERMINATED) {
       existingEntity.setState(InstanceState.SHUTTING_DOWN);
       entityToReturn = existingEntity;
     } else if (existingEntity.getState() != InstanceState.TERMINATED) {
       LOG.info(
           String.format(
               "Instance %s is provisionally being terminated before an Instance Manager has claimed it by stamping a node id onto it",
               existingEntity.getInstanceId()));
       existingEntity.setState(InstanceState.TERMINATED);
       entityToReturn = existingEntity;
     }
   } else {
     LOG.warn(String.format("Expected non-null instance"));
   }
   return entityToReturn;
 }
  protected Instance putInstanceIntoDht(String instanceID) {
    int globalAvailabilityZoneCode = piIdBuilder.getGlobalAvailabilityZoneCodeFromEc2Id(instanceID);
    instance = new Instance(instanceID, "test", "default", ImagePlatform.linux);
    instance.setAvailabilityZoneCode(
        PId.getAvailabilityZoneCodeWithinRegionFromGlobalAvailabilityZoneCode(
            globalAvailabilityZoneCode));
    instance.setRegionCode(
        PId.getRegionCodeFromGlobalAvailabilityZoneCode(globalAvailabilityZoneCode));
    instance.setImageId(IMAGE_ID);
    instance.setKeyName(KEY);
    instance.setImageSizeInMB(1024);
    instance.setPrivateMacAddress("D0:0D:12:34:56:78");
    instance.setMemoryInKB("512");
    instance.setVcpus(1);
    instance.setVlanId(getRandomVlanId());
    instance.setInstanceType("m1.small");
    instance.setUserId(USER_NAME);
    instance.setState(InstanceState.PENDING);

    instancePastryId = piIdBuilder.getPIdForEc2AvailabilityZone(instance);
    System.err.println(
        "Instance pastryId: "
            + instancePastryId
            + " gAvz code embeded in instanceID: "
            + globalAvailabilityZoneCode);
    System.err.println("Instance pastryId: " + instancePastryId.toStringFull());
    putPiEntityIntoDht(instance, instancePastryId);

    return instance;
  }
Пример #3
0
  public ReservationInstances runInstances(final Reservation reservation) {
    LOG.debug(String.format("runInstances(%s)", reservation));

    String securityGroupUrl =
        SecurityGroup.getUrl(reservation.getUserId(), reservation.getSecurityGroupName());
    PId securityGroupId = getPiIdBuilder().getPId(securityGroupUrl).forLocalRegion();
    SecurityGroup securityGroup =
        (SecurityGroup) getDhtClientFactory().createBlockingReader().get(securityGroupId);

    validateReservation(reservation, securityGroup);

    reservation.setReservationId(getIdFactory().createNewReservationId());

    AvailabilityZone availabilityZone;
    if (StringUtils.isNotEmpty(reservation.getAvailabilityZone())) {
      try {
        availabilityZone = getAvailabilityZoneByName(reservation.getAvailabilityZone());
      } catch (AvailabilityZoneNotFoundException e) {
        throw new IllegalArgumentException(
            String.format("Unknown availability zone: %s", reservation.getAvailabilityZone()));
      }
    } else {
      availabilityZone = getLocalAvailabilityZone();
      reservation.setAvailabilityZone(availabilityZone.getAvailabilityZoneName());
    }

    // setup return object
    ReservationInstances reservationInstances = new ReservationInstances();
    reservationInstances.setReservation(reservation);

    for (int i = 0; i < reservation.getMaxCount(); i++) {
      String instanceId =
          getIdFactory().createNewInstanceId(availabilityZone.getGlobalAvailabilityZoneCode());
      // create instance
      Instance instance = new Instance(reservation);
      instance.setInstanceType(reservation.getInstanceType());
      instance.setUserId(reservation.getUserId());
      instance.setInstanceId(instanceId);
      instance.setState(InstanceState.PENDING);
      instance.setLaunchTime(System.currentTimeMillis());
      instance.setAvailabilityZoneCode(availabilityZone.getAvailabilityZoneCodeWithinRegion());
      instance.setRegionCode(availabilityZone.getRegionCode());
      LOG.info(String.format("Requesting new %s", instance));
      reservationInstances.getInstances().add(instance);

      // create instance in dht
      PId instanceDhtId =
          getPiIdBuilder().getPIdForEc2AvailabilityZone(Instance.getUrl(instanceId));
      BlockingDhtWriter blockingDhtWriter = getDhtClientFactory().createBlockingWriter();
      AddNewInstanceResolver addNewInstanceResolver = new AddNewInstanceResolver();
      blockingDhtWriter.update(instanceDhtId, instance, addNewInstanceResolver);

      reservation.addInstanceId(instance.getInstanceId());
    }

    getUserService()
        .addInstancesToUser(
            reservation.getUserId(), reservation.getInstanceIds(), reservation.getInstanceType());

    // write security group to DHT
    getDhtClientFactory()
        .createBlockingWriter()
        .update(
            securityGroupId,
            null,
            new AddInstanceToSecurityGroupResolver(reservation.getInstanceIds()));

    // add to task processing queue
    PId runInstanceQueueId =
        getPiIdBuilder()
            .getPId(PiQueue.RUN_INSTANCE.getUrl())
            .forGlobalAvailablityZoneCode(availabilityZone.getGlobalAvailabilityZoneCode());
    for (String instanceId : reservation.getInstanceIds()) {
      getTaskProcessingQueueHelper()
          .addUrlToQueue(runInstanceQueueId, Instance.getUrl(instanceId), instanceTaskQueueRetries);
    }

    // anycast message
    PubSubMessageContext pubSubMessageContext =
        getApiApplicationManager()
            .newPubSubMessageContextFromGlobalAvzCode(
                PiTopics.RUN_INSTANCE, availabilityZone.getGlobalAvailabilityZoneCode());
    pubSubMessageContext.randomAnycast(EntityMethod.CREATE, reservation);

    return reservationInstances;
  }