@Override
  public void put(final Reservation reservation) {

    log.trace("ReservationManagerImpl.put({})", reservation);
    checkRunning();

    reservationsCacheLock.lock();
    try {

      CacheItem<Reservation> item =
          new CacheItem<Reservation>(stopwatchProvider.get(), reservation);

      reservationsBySrk.put(reservation.getSecretReservationKeys(), item);

      for (NodeUrn nodeUrn : reservation.getNodeUrns()) {
        List<CacheItem<Reservation>> entry = reservationsByNodeUrn.get(nodeUrn);
        if (entry == null) {
          entry = Lists.newArrayList();
          reservationsByNodeUrn.put(nodeUrn, entry);
        }
        entry.add(item);
      }

    } finally {
      reservationsCacheLock.unlock();
    }
  }
  /**
   * Pr&uuml;fe, ob eine Ressource f&uuml;r ein bestimmten Appointment reserviert ist.
   *
   * @param alloc
   * @param when
   * @return <code>true</code>, wenn die Ressource reserviert ist. <code>false</code> sonst
   */
  private boolean isReserved(Allocatable alloc, Appointment when) {
    Reservation reservation = when.getReservation();
    Appointment[] restrictions = reservation.getRestriction(alloc);

    for (int restIt = 0, restLen = restrictions.length; restIt < restLen; restIt++) {
      if (when.equals(restrictions[restIt])) return true;
    }

    return (restrictions.length == 0);
  }
示例#3
0
文件: Show.java 项目: nieejl/Projekt1
 public void makeSeatings() {
   int rows = theater.getRows();
   int seatsInRow = theater.getSeatsInRow();
   MySQL.getReservations();
   for (Reservation reservation : MySQL.sendReservations()) {
     for (int i = 0; i < reservation.getSeats().get(0).length - 1; i++) {
       pladser[reservation.getRows().get(0)][reservation.getSeats().get(0)[i]] = true;
     }
   }
 }
 /**
  * F&uuml;gt einem iCal-Event den Termin-Namen aus dem &uuml;bergebenen Appointment-Objekt hinzu.
  *
  * @param appointment
  */
 private void addEventNameToEvent(Appointment appointment, PropertyList properties) {
   Reservation reservation = appointment.getReservation();
   final Locale locale = raplaLocale.getLocale();
   String eventDescription = NameFormatUtil.getExportName(appointment, locale);
   if (reservation
           .getClassification()
           .getType()
           .getAnnotation(DynamicTypeAnnotations.KEY_NAME_FORMAT_EXPORT)
       == null) {
     eventDescription += getAttendeeString(appointment);
   }
   properties.add(new Summary(eventDescription));
 }
 @Override
 public Optional<Reservation> lookup(final Set<SecretReservationKey> srks) {
   log.trace("ReservationCacheImpl.lookup({})", srks);
   checkRunning();
   CacheItem<Reservation> item = reservationsBySrk.get(srks);
   if (item != null) {
     if (item.isOutdated()) {
       return Optional.absent();
     }
     Reservation reservation = item.get();
     reservation.touch();
     return Optional.of(reservation);
   }
   return Optional.absent();
 }
  @Override
  public Optional<Reservation> lookup(final NodeUrn nodeUrn, final DateTime timestamp) {

    log.trace("ReservationCacheImpl.lookup({}, {})", nodeUrn, timestamp);
    checkRunning();

    synchronized (reservationsByNodeUrn) {
      final List<CacheItem<Reservation>> entry = reservationsByNodeUrn.get(nodeUrn);

      if (entry == null) {
        log.trace("ReservationManagerImpl.lookup() CACHE MISS");
        return Optional.absent();
      }

      for (CacheItem<Reservation> item : entry) {

        final Interval effectiveInterval;
        final Reservation reservation = item.get();
        final DateTime reservationStart = reservation.getInterval().getStart();
        final DateTime reservationCancellation = reservation.getCancelled();

        if (reservationCancellation != null) {
          if (reservationCancellation.isBefore(reservationStart)) {
            continue;
          } else {
            effectiveInterval = new Interval(reservationStart, reservationCancellation);
          }
        } else {
          effectiveInterval = reservation.getInterval();
        }

        if (effectiveInterval.contains(timestamp)) {
          if (!item.isOutdated()) {
            item.touch();
            log.trace("ReservationManagerImpl.lookup() CACHE HIT");
            return Optional.of(reservation);
          }
        }
      }
    }
    return Optional.absent();
  }
  private void addDescriptionToEvent(Appointment appointment, PropertyList properties) {

    Reservation reservation = appointment.getReservation();
    String eventDescription;
    if (reservation
            .getClassification()
            .getType()
            .getAnnotation(DynamicTypeAnnotations.KEY_DESCRIPTION_FORMAT_EXPORT)
        != null) {
      eventDescription =
          reservation.format(
              raplaLocale.getLocale(),
              DynamicTypeAnnotations.KEY_DESCRIPTION_FORMAT_EXPORT,
              appointment);
    } else {
      eventDescription = null;
    }
    if (eventDescription != null) {
      properties.add(new Description(eventDescription));
    }
  }
  public String getAttendeeString(Appointment appointment) {

    String attendeeString = "";

    Reservation raplaReservation = appointment.getReservation();
    Allocatable[] raplaPersons = raplaReservation.getPersons();

    for (int i = 0; i < raplaPersons.length; i++) {
      if (!isReserved(raplaPersons[i], appointment)) continue;

      attendeeString += raplaPersons[i].getName(raplaLocale.getLocale());
      attendeeString = attendeeString.trim();

      if (i != raplaPersons.length - 1) {
        attendeeString += ", ";
      } else {
        attendeeString = " [" + attendeeString + "]";
      }
    }

    return attendeeString;
  }
  @Override
  public void remove(Reservation reservation) {
    log.trace("ReservationCacheImpl.remove({})", reservation);
    checkRunning();
    reservationsCacheLock.lock();
    try {

      int removedNodeCacheEntries = 0;

      for (Iterator<Map.Entry<NodeUrn, List<CacheItem<Reservation>>>> cacheIterator =
              reservationsByNodeUrn.entrySet().iterator();
          cacheIterator.hasNext(); ) {

        final Map.Entry<NodeUrn, List<CacheItem<Reservation>>> entry = cacheIterator.next();

        for (Iterator<CacheItem<Reservation>> itemIt = entry.getValue().iterator();
            itemIt.hasNext(); ) {

          final CacheItem<Reservation> item = itemIt.next();
          final Reservation res = item.get();

          if (res == reservation) {
            itemIt.remove();
            removedNodeCacheEntries++;
          }
        }

        if (entry.getValue().isEmpty()) {
          cacheIterator.remove();
        }
      }

      log.trace("ReservationCacheImpl removed {} node cache entries", removedNodeCacheEntries);

      reservationsBySrk.remove(reservation.getSecretReservationKeys());

    } finally {
      reservationsCacheLock.unlock();
    }
  }
示例#10
0
  /**
   * Provisions an On-demand EC2 slave by launching a new instance or starting a previously-stopped
   * instance.
   */
  private EC2AbstractSlave provisionOndemand(
      TaskListener listener, Label requiredLabel, EnumSet<ProvisionOptions> provisionOptions)
      throws AmazonClientException, IOException {
    PrintStream logger = listener.getLogger();
    AmazonEC2 ec2 = getParent().connect();

    try {
      logProvisionInfo(logger, "Considering launching " + ami + " for template " + description);

      KeyPair keyPair = getKeyPair(ec2);

      RunInstancesRequest riRequest = new RunInstancesRequest(ami, 1, 1);
      InstanceNetworkInterfaceSpecification net = new InstanceNetworkInterfaceSpecification();

      riRequest.setEbsOptimized(ebsOptimized);

      if (useEphemeralDevices) {
        setupEphemeralDeviceMapping(riRequest);
      } else {
        setupCustomDeviceMapping(riRequest);
      }

      if (stopOnTerminate) {
        riRequest.setInstanceInitiatedShutdownBehavior(ShutdownBehavior.Stop);
        logProvisionInfo(
            logger, "Setting Instance Initiated Shutdown Behavior : ShutdownBehavior.Stop");
      } else {
        riRequest.setInstanceInitiatedShutdownBehavior(ShutdownBehavior.Terminate);
        logProvisionInfo(
            logger, "Setting Instance Initiated Shutdown Behavior : ShutdownBehavior.Terminate");
      }

      List<Filter> diFilters = new ArrayList<Filter>();
      diFilters.add(new Filter("image-id").withValues(ami));

      if (StringUtils.isNotBlank(getZone())) {
        Placement placement = new Placement(getZone());
        if (getUseDedicatedTenancy()) {
          placement.setTenancy("dedicated");
        }
        riRequest.setPlacement(placement);
        diFilters.add(new Filter("availability-zone").withValues(getZone()));
      }

      if (StringUtils.isNotBlank(getSubnetId())) {
        if (getAssociatePublicIp()) {
          net.setSubnetId(getSubnetId());
        } else {
          riRequest.setSubnetId(getSubnetId());
        }

        diFilters.add(new Filter("subnet-id").withValues(getSubnetId()));

        /*
         * If we have a subnet ID then we can only use VPC security groups
         */
        if (!securityGroupSet.isEmpty()) {
          List<String> groupIds = getEc2SecurityGroups(ec2);

          if (!groupIds.isEmpty()) {
            if (getAssociatePublicIp()) {
              net.setGroups(groupIds);
            } else {
              riRequest.setSecurityGroupIds(groupIds);
            }

            diFilters.add(new Filter("instance.group-id").withValues(groupIds));
          }
        }
      } else {
        /* No subnet: we can use standard security groups by name */
        riRequest.setSecurityGroups(securityGroupSet);
        if (!securityGroupSet.isEmpty()) {
          diFilters.add(new Filter("instance.group-name").withValues(securityGroupSet));
        }
      }

      String userDataString = Base64.encodeBase64String(userData.getBytes(StandardCharsets.UTF_8));
      riRequest.setUserData(userDataString);
      riRequest.setKeyName(keyPair.getKeyName());
      diFilters.add(new Filter("key-name").withValues(keyPair.getKeyName()));
      riRequest.setInstanceType(type.toString());
      diFilters.add(new Filter("instance-type").withValues(type.toString()));

      if (getAssociatePublicIp()) {
        net.setAssociatePublicIpAddress(true);
        net.setDeviceIndex(0);
        riRequest.withNetworkInterfaces(net);
      }

      boolean hasCustomTypeTag = false;
      HashSet<Tag> instTags = null;
      if (tags != null && !tags.isEmpty()) {
        instTags = new HashSet<Tag>();
        for (EC2Tag t : tags) {
          instTags.add(new Tag(t.getName(), t.getValue()));
          diFilters.add(new Filter("tag:" + t.getName()).withValues(t.getValue()));
          if (StringUtils.equals(t.getName(), EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE)) {
            hasCustomTypeTag = true;
          }
        }
      }
      if (!hasCustomTypeTag) {
        if (instTags == null) {
          instTags = new HashSet<Tag>();
        }
        // Append template description as well to identify slaves provisioned per template
        instTags.add(
            new Tag(
                EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE,
                EC2Cloud.getSlaveTypeTagValue(EC2Cloud.EC2_SLAVE_TYPE_DEMAND, description)));
      }

      DescribeInstancesRequest diRequest = new DescribeInstancesRequest();
      diRequest.setFilters(diFilters);

      logProvision(logger, "Looking for existing instances with describe-instance: " + diRequest);

      DescribeInstancesResult diResult = ec2.describeInstances(diRequest);
      EC2AbstractSlave[] ec2Node = new EC2AbstractSlave[1];
      Instance existingInstance = null;
      if (!provisionOptions.contains(ProvisionOptions.FORCE_CREATE)) {
        reservationLoop:
        for (Reservation reservation : diResult.getReservations()) {
          for (Instance instance : reservation.getInstances()) {
            if (checkInstance(logger, instance, requiredLabel, ec2Node)) {
              existingInstance = instance;
              logProvision(
                  logger,
                  "Found existing instance: "
                      + existingInstance
                      + ((ec2Node[0] != null) ? (" node: " + ec2Node[0].getInstanceId()) : ""));
              break reservationLoop;
            }
          }
        }
      }

      if (existingInstance == null) {
        if (!provisionOptions.contains(ProvisionOptions.FORCE_CREATE)
            && !provisionOptions.contains(ProvisionOptions.ALLOW_CREATE)) {
          logProvision(logger, "No existing instance found - but cannot create new instance");
          return null;
        }
        if (StringUtils.isNotBlank(getIamInstanceProfile())) {
          riRequest.setIamInstanceProfile(
              new IamInstanceProfileSpecification().withArn(getIamInstanceProfile()));
        }
        // Have to create a new instance
        Instance inst = ec2.runInstances(riRequest).getReservation().getInstances().get(0);

        /* Now that we have our instance, we can set tags on it */
        if (instTags != null) {
          updateRemoteTags(ec2, instTags, "InvalidInstanceID.NotFound", inst.getInstanceId());

          // That was a remote request - we should also update our
          // local instance data.
          inst.setTags(instTags);
        }
        logProvisionInfo(logger, "No existing instance found - created new instance: " + inst);
        return newOndemandSlave(inst);
      }

      if (existingInstance
              .getState()
              .getName()
              .equalsIgnoreCase(InstanceStateName.Stopping.toString())
          || existingInstance
              .getState()
              .getName()
              .equalsIgnoreCase(InstanceStateName.Stopped.toString())) {

        List<String> instances = new ArrayList<String>();
        instances.add(existingInstance.getInstanceId());
        StartInstancesRequest siRequest = new StartInstancesRequest(instances);
        StartInstancesResult siResult = ec2.startInstances(siRequest);

        logProvisionInfo(
            logger,
            "Found stopped instance - starting it: " + existingInstance + " result:" + siResult);
      } else {
        // Should be pending or running at this point, just let it come up
        logProvisionInfo(
            logger,
            "Found existing pending or running: "
                + existingInstance.getState().getName()
                + " instance: "
                + existingInstance);
      }

      if (ec2Node[0] != null) {
        logProvisionInfo(logger, "Using existing slave: " + ec2Node[0].getInstanceId());
        return ec2Node[0];
      }

      // Existing slave not found
      logProvision(logger, "Creating new slave for existing instance: " + existingInstance);
      return newOndemandSlave(existingInstance);

    } catch (FormException e) {
      throw new AssertionError(e); // we should have discovered all
      // configuration issues upfront
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
  }
  @Override
  public List<DiscoveryNode> buildDynamicNodes() {
    List<DiscoveryNode> discoNodes = new ArrayList<>();

    DescribeInstancesResult descInstances;
    try {
      // Query EC2 API based on AZ, instance state, and tag.

      // NOTE: we don't filter by security group during the describe instances request for two
      // reasons:
      // 1. differences in VPCs require different parameters during query (ID vs Name)
      // 2. We want to use two different strategies: (all security groups vs. any security groups)
      descInstances = client.describeInstances(buildDescribeInstancesRequest());
    } catch (AmazonClientException e) {
      logger.info("Exception while retrieving instance list from AWS API: {}", e.getMessage());
      logger.debug("Full exception:", e);
      return discoNodes;
    }

    logger.trace("building dynamic unicast discovery nodes...");
    for (Reservation reservation : descInstances.getReservations()) {
      for (Instance instance : reservation.getInstances()) {
        // lets see if we can filter based on groups
        if (!groups.isEmpty()) {
          List<GroupIdentifier> instanceSecurityGroups = instance.getSecurityGroups();
          ArrayList<String> securityGroupNames = new ArrayList<String>();
          ArrayList<String> securityGroupIds = new ArrayList<String>();
          for (GroupIdentifier sg : instanceSecurityGroups) {
            securityGroupNames.add(sg.getGroupName());
            securityGroupIds.add(sg.getGroupId());
          }
          if (bindAnyGroup) {
            // We check if we can find at least one group name or one group id in groups.
            if (Collections.disjoint(securityGroupNames, groups)
                && Collections.disjoint(securityGroupIds, groups)) {
              logger.trace(
                  "filtering out instance {} based on groups {}, not part of {}",
                  instance.getInstanceId(),
                  instanceSecurityGroups,
                  groups);
              // continue to the next instance
              continue;
            }
          } else {
            // We need tp match all group names or group ids, otherwise we ignore this instance
            if (!(securityGroupNames.containsAll(groups) || securityGroupIds.containsAll(groups))) {
              logger.trace(
                  "filtering out instance {} based on groups {}, does not include all of {}",
                  instance.getInstanceId(),
                  instanceSecurityGroups,
                  groups);
              // continue to the next instance
              continue;
            }
          }
        }

        String address = null;
        switch (hostType) {
          case PRIVATE_DNS:
            address = instance.getPrivateDnsName();
            break;
          case PRIVATE_IP:
            address = instance.getPrivateIpAddress();
            break;
          case PUBLIC_DNS:
            address = instance.getPublicDnsName();
            break;
          case PUBLIC_IP:
            address = instance.getPublicIpAddress();
            break;
        }
        if (address != null) {
          try {
            TransportAddress[] addresses = transportService.addressesFromString(address);
            // we only limit to 1 addresses, makes no sense to ping 100 ports
            for (int i = 0; (i < addresses.length && i < UnicastZenPing.LIMIT_PORTS_COUNT); i++) {
              logger.trace(
                  "adding {}, address {}, transport_address {}",
                  instance.getInstanceId(),
                  address,
                  addresses[i]);
              discoNodes.add(
                  new DiscoveryNode(
                      "#cloud-" + instance.getInstanceId() + "-" + i,
                      addresses[i],
                      version.minimumCompatibilityVersion()));
            }
          } catch (Exception e) {
            logger.warn("failed ot add {}, address {}", e, instance.getInstanceId(), address);
          }
        } else {
          logger.trace(
              "not adding {}, address is null, host_type {}", instance.getInstanceId(), hostType);
        }
      }
    }

    logger.debug("using dynamic discovery nodes {}", discoNodes);

    return discoNodes;
  }