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;
  }
Пример #2
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;
  }