Example #1
0
 public Condition getInstanceHostConstraint(Instance instance) {
   if (StringUtils.isEmpty(instance.getDeploymentUnitUuid())) {
     return INSTANCE_HOST_MAP.INSTANCE_ID.eq(instance.getId());
   } else {
     return INSTANCE.DEPLOYMENT_UNIT_UUID.eq(instance.getDeploymentUnitUuid());
   }
 }
Example #2
0
  /**
   * Supported macros ${service_name} ${stack_name} LEGACY: ${project_name}
   *
   * @param valueStr
   * @param instance
   * @return
   */
  private String evaluateMacros(String valueStr, Instance instance) {
    if (valueStr.indexOf(SERVICE_NAME_MACRO) != -1
        || valueStr.indexOf(STACK_NAME_MACRO) != -1
        || valueStr.indexOf(PROJECT_NAME_MACRO) != -1) {

      List<Label> labels = labelsDao.getLabelsForInstance(instance.getId());
      String serviceLaunchConfigName = "";
      String stackName = "";
      for (Label label : labels) {
        if (LABEL_STACK_NAME.equals(label.getKey())) {
          stackName = label.getValue();
        } else if (LABEL_STACK_SERVICE_NAME.equals(label.getKey())) {
          if (label.getValue() != null) {
            int i = label.getValue().indexOf('/');
            if (i != -1) {
              serviceLaunchConfigName = label.getValue().substring(i + 1);
            }
          }
        }
      }
      if (!StringUtils.isBlank(stackName)) {
        valueStr = valueStr.replace(STACK_NAME_MACRO, stackName);

        // LEGACY: ${project_name} rename ${stack_name}
        valueStr = valueStr.replace(PROJECT_NAME_MACRO, stackName);
      }

      if (!StringUtils.isBlank(serviceLaunchConfigName)) {
        valueStr = valueStr.replace(SERVICE_NAME_MACRO, serviceLaunchConfigName);
      }
    }

    return valueStr;
  }
Example #3
0
  protected void modifyDisk(long hostId, Instance instance, boolean add) {
    CacheManager cm = CacheManager.getCacheManagerInstance(this.objectManager);
    HostInfo hostInfo = cm.getHostInfo(hostId, false);
    if (hostInfo == null) {
      // we never tried to schedule disks at all in the past
      return;
    }
    InstanceInfo instanceInfo = hostInfo.getInstanceInfo(instance.getId());
    if (instanceInfo == null) {
      // we never tried to schedule disks at all during constraint scheduling
      return;
    }
    if (add) {
      Map<Pair<String, Long>, DiskInfo> volumeToDiskMapping =
          AllocatorUtils.allocateDiskForVolumes(hostId, instance, this.objectManager);
      if (volumeToDiskMapping == null) {
        return;
      }

      for (Entry<Pair<String, Long>, DiskInfo> mapping : volumeToDiskMapping.entrySet()) {
        Pair<String, Long> vol = mapping.getKey();
        DiskInfo disk = mapping.getValue();
        Long allocated = disk.getAllocatedSize();
        disk.addAllocatedSize(vol.getRight());
        log.info(
            "allocated disk space on disk [{}] with total = {}, {} {} {} = {} as used",
            disk.getDiskDevicePath(),
            disk.getCapacity(),
            allocated,
            "+",
            vol.getRight(),
            allocated + vol.getRight());

        // record to cache for deletion purpose
        instanceInfo.addReservedSize(disk.getDiskDevicePath(), vol.getRight());
      }

    } else {
      for (Entry<String, Long> diskAllocated : instanceInfo.getAllocatedDisks()) {
        String diskDevicePath = diskAllocated.getKey();
        Long reserveSize = diskAllocated.getValue();
        DiskInfo diskInfo = hostInfo.getDiskInfo(diskDevicePath);
        diskInfo.freeAllocatedSize(reserveSize);
        log.info(
            "freed disk space on disk [{}] with total = {}, {} {} {} = {} as used",
            diskInfo.getDiskDevicePath(),
            diskInfo.getCapacity(),
            diskInfo.getAllocatedSize(),
            "-",
            reserveSize,
            diskInfo.getAllocatedSize() - reserveSize);

        // release the reserved disk for this instance
        instanceInfo.releaseDisk(diskDevicePath);
      }
    }
  }
Example #4
0
 @Override
 public List<? extends Service> findServicesFor(Instance instance) {
   return create()
       .select(SERVICE.fields())
       .from(SERVICE)
       .join(SERVICE_EXPOSE_MAP)
       .on(SERVICE_EXPOSE_MAP.SERVICE_ID.eq(SERVICE.ID))
       .where(SERVICE_EXPOSE_MAP.INSTANCE_ID.eq(instance.getId()))
       .fetchInto(ServiceRecord.class);
 }
Example #5
0
 protected List<? extends Instance> getServiceInstancesToRestart(Service service) {
   // get all instances of the service
   List<? extends Instance> instances = exposeMapDao.listServiceManagedInstances(service.getId());
   List<Instance> toRestart = new ArrayList<>();
   ServiceRestart svcRestart =
       DataAccessor.field(
           service, ServiceDiscoveryConstants.FIELD_RESTART, jsonMapper, ServiceRestart.class);
   RollingRestartStrategy strategy = svcRestart.getRollingRestartStrategy();
   Map<Long, Long> instanceToStartCount = strategy.getInstanceToStartCount();
   // compare its start_count with one set on the service restart field
   for (Instance instance : instances) {
     if (instanceToStartCount.containsKey(instance.getId())) {
       Long previousStartCount = instanceToStartCount.get(instance.getId());
       if (previousStartCount == instance.getStartCount()) {
         toRestart.add(instance);
       }
     }
   }
   return toRestart;
 }
Example #6
0
  protected void processPorts(Instance instance) {
    Set<String> portSpecs = new HashSet<>();
    for (Port port : objectManager.children(instance, Port.class)) {
      if (port.getRemoved() != null) {
        continue;
      }

      portSpecs.add(new PortSpec(port).toSpec());
    }
    objectManager.setFields(instance, InstanceConstants.FIELD_PORTS, new ArrayList<>(portSpecs));
    instanceDao.clearCacheInstanceData(instance.getId());
  }
  @Override
  protected void postProcessEvent(
      EventVO<?> event,
      Event reply,
      ProcessState state,
      ProcessInstance process,
      Object eventResource,
      Object dataResource,
      Object agentResource) {
    Map<String, String> labels =
        CollectionUtils.toMap(
            CollectionUtils.getNestedValue(
                reply.getData(), "instance", "+data", "+fields", "+labels"));
    if (labels.size() == 0) {
      labels =
          CollectionUtils.toMap(
              CollectionUtils.getNestedValue(
                  reply.getData(), "instanceHostMap", "instance", "+data", "+fields", "+labels"));
    } else {
      CollectionUtils.setNestedValue(
          CollectionUtils.toMap(reply.getData()),
          labels,
          "instanceHostMap",
          "instance",
          "+data",
          "+fields",
          "+labels");
    }

    Instance instance = getInstance(state);

    for (Map.Entry<String, String> label : labels.entrySet()) {
      labelsService.createContainerLabel(
          instance.getAccountId(), instance.getId(), label.getKey(), label.getValue());
    }
  }
Example #8
0
  @Override
  public boolean recordCandidate(AllocationAttempt attempt, AllocationCandidate candidate) {
    Long newHost = candidate.getHost();
    if (newHost != null) {
      for (Instance instance : attempt.getInstances()) {
        log.info("Associating instance [{}] to host [{}]", instance.getId(), newHost);
        objectManager.create(
            InstanceHostMap.class,
            INSTANCE_HOST_MAP.HOST_ID,
            newHost,
            INSTANCE_HOST_MAP.INSTANCE_ID,
            instance.getId());

        modifyDisk(newHost, instance, true);

        List<Volume> vols = InstanceHelpers.extractVolumesFromMounts(instance, objectManager);
        for (Volume v : vols) {
          if (VolumeConstants.ACCESS_MODE_SINGLE_HOST_RW.equals(v.getAccessMode())) {
            objectManager.setFields(v, VOLUME.HOST_ID, newHost);
          }
        }
      }
    }

    Map<Long, Set<Long>> existingPools = attempt.getPoolIds();
    Map<Long, Set<Long>> newPools = candidate.getPools();

    if (!existingPools.keySet().equals(newPools.keySet())) {
      throw new IllegalStateException(
          String.format(
              "Volumes don't match. currently %s, new %s",
              existingPools.keySet(), newPools.keySet()));
    }

    for (Map.Entry<Long, Set<Long>> entry : newPools.entrySet()) {
      long volumeId = entry.getKey();
      Set<Long> existingPoolsForVol = existingPools.get(entry.getKey());
      Set<Long> newPoolsForVol = entry.getValue();
      if (existingPoolsForVol == null || existingPoolsForVol.size() == 0) {
        for (long poolId : newPoolsForVol) {
          log.info("Associating volume [{}] to storage pool [{}]", volumeId, poolId);
          objectManager.create(
              VolumeStoragePoolMap.class,
              VOLUME_STORAGE_POOL_MAP.VOLUME_ID,
              volumeId,
              VOLUME_STORAGE_POOL_MAP.STORAGE_POOL_ID,
              poolId);
        }
      } else if (!existingPoolsForVol.equals(newPoolsForVol)) {
        throw new IllegalStateException(
            String.format(
                "Can not move volume %s, currently: %s, new: %s",
                volumeId, existingPools, newPools));
      }
    }

    for (Nic nic : attempt.getNics()) {
      Long subnetId = candidate.getSubnetIds().get(nic.getId());

      if (subnetId == null
          || (nic.getSubnetId() != null && subnetId.longValue() == nic.getSubnetId())) {
        continue;
      }

      log.info("Associating nic [{}] to subnet [{}]", nic.getId(), subnetId);
      int i =
          create().update(NIC).set(NIC.SUBNET_ID, subnetId).where(NIC.ID.eq(nic.getId())).execute();

      if (i != 1) {
        throw new IllegalStateException(
            "Expected to update nic id ["
                + nic.getId()
                + "] with subnet ["
                + subnetId
                + "] but update ["
                + i
                + "] rows");
      }
    }

    return true;
  }
Example #9
0
  @Override
  public List<DnsEntryData> getInstanceLinksDnsData(final Instance instance) {
    MultiRecordMapper<DnsEntryData> mapper =
        new MultiRecordMapper<DnsEntryData>() {
          @Override
          protected DnsEntryData map(List<Object> input) {
            DnsEntryData data = new DnsEntryData();
            Map<String, Map<String, String>> resolve = new HashMap<>();
            Map<String, String> ips = new HashMap<>();
            String targetInstanceName =
                input.get(4) == null ? null : ((Instance) input.get(4)).getName();
            ips.put(((IpAddress) input.get(1)).getAddress(), targetInstanceName);
            // add all instance links
            resolve.put(((InstanceLink) input.get(0)).getLinkName(), ips);
            data.setSourceIpAddress(((IpAddress) input.get(2)).getAddress());
            data.setResolveServicesAndContainers(resolve);
            data.setInstance((Instance) input.get(3));
            return data;
          }
        };

    InstanceLinkTable instanceLink = mapper.add(INSTANCE_LINK);
    IpAddressTable targetIpAddress = mapper.add(IP_ADDRESS);
    IpAddressTable clientIpAddress = mapper.add(IP_ADDRESS);
    InstanceTable clientInstance = mapper.add(INSTANCE);
    InstanceTable targetInstance = mapper.add(INSTANCE);
    NicTable clientNic = NIC.as("client_nic");
    NicTable targetNic = NIC.as("target_nic");
    IpAddressNicMapTable clientNicIpTable = IP_ADDRESS_NIC_MAP.as("client_nic_ip");
    IpAddressNicMapTable targetNicIpTable = IP_ADDRESS_NIC_MAP.as("target_nic_ip");

    return create()
        .select(mapper.fields())
        .from(NIC)
        .join(clientNic)
        .on(NIC.VNET_ID.eq(clientNic.VNET_ID))
        .join(instanceLink)
        .on(instanceLink.INSTANCE_ID.eq(clientNic.INSTANCE_ID))
        .join(targetNic)
        .on(targetNic.INSTANCE_ID.eq(instanceLink.TARGET_INSTANCE_ID))
        .join(targetInstance)
        .on(targetNic.INSTANCE_ID.eq(targetInstance.ID))
        .join(targetNicIpTable)
        .on(targetNicIpTable.NIC_ID.eq(targetNic.ID))
        .join(targetIpAddress)
        .on(targetNicIpTable.IP_ADDRESS_ID.eq(targetIpAddress.ID))
        .join(clientNicIpTable)
        .on(clientNicIpTable.NIC_ID.eq(clientNic.ID))
        .join(clientIpAddress)
        .on(clientNicIpTable.IP_ADDRESS_ID.eq(clientIpAddress.ID))
        .join(clientInstance)
        .on(clientNic.INSTANCE_ID.eq(clientInstance.ID))
        .where(
            NIC.INSTANCE_ID
                .eq(instance.getId())
                .and(NIC.VNET_ID.isNotNull())
                .and(NIC.REMOVED.isNull())
                .and(targetIpAddress.ROLE.eq(IpAddressConstants.ROLE_PRIMARY))
                .and(targetIpAddress.REMOVED.isNull())
                .and(clientIpAddress.ROLE.eq(IpAddressConstants.ROLE_PRIMARY))
                .and(clientIpAddress.REMOVED.isNull())
                .and(targetNicIpTable.REMOVED.isNull())
                .and(clientNic.REMOVED.isNull())
                .and(targetNic.REMOVED.isNull())
                .and(instanceLink.REMOVED.isNull())
                .and(instanceLink.SERVICE_CONSUME_MAP_ID.isNull())
                .and(
                    targetInstance.STATE.in(
                        InstanceConstants.STATE_RUNNING, InstanceConstants.STATE_STARTING))
                .and(
                    targetInstance
                        .HEALTH_STATE
                        .isNull()
                        .or(
                            targetInstance.HEALTH_STATE.eq(
                                HealthcheckConstants.HEALTH_STATE_HEALTHY))))
        .fetch()
        .map(mapper);
  }
Example #10
0
  @Override
  public List<DnsEntryData> getServiceDnsData(
      final Instance instance, final boolean isVIPProvider) {
    MultiRecordMapper<ServiceDnsEntryData> mapper =
        new MultiRecordMapper<ServiceDnsEntryData>() {
          @Override
          protected ServiceDnsEntryData map(List<Object> input) {
            Service clientService = (Service) input.get(0);
            Service targetService = (Service) input.get(1);
            ServiceConsumeMap consumeMap = (ServiceConsumeMap) input.get(2);
            ServiceDnsEntryData data =
                new ServiceDnsEntryData(clientService, targetService, consumeMap);
            return data;
          }
        };

    ServiceTable clientService = mapper.add(SERVICE);
    ServiceTable targetService = mapper.add(SERVICE);
    ServiceConsumeMapTable serviceConsumeMap = mapper.add(SERVICE_CONSUME_MAP);

    // there are 2 conditions below linked with OR clause
    // first condition - means to return all non-dns clientService + target service map within the
    // same stack
    // that are not linked
    // second condition - return only clientService + targetService with explicit links
    Condition condition =
        (clientService
                .KIND
                .ne(ServiceDiscoveryConstants.KIND.DNSSERVICE.name())
                .and(targetService.ENVIRONMENT_ID.eq(clientService.ENVIRONMENT_ID))
                .and(serviceConsumeMap.ID.isNull()))
            .or(
                serviceConsumeMap
                    .ID
                    .isNotNull()
                    .and(serviceConsumeMap.REMOVED.isNull())
                    .and(
                        serviceConsumeMap.STATE.in(
                            CommonStatesConstants.ACTIVATING, CommonStatesConstants.ACTIVE)));

    List<ServiceDnsEntryData> serviceDnsEntries =
        create()
            .select(mapper.fields())
            .from(clientService)
            .join(targetService)
            .on(targetService.ACCOUNT_ID.eq(clientService.ACCOUNT_ID))
            .leftOuterJoin(serviceConsumeMap)
            .on(
                serviceConsumeMap
                    .SERVICE_ID
                    .eq(clientService.ID)
                    .and(serviceConsumeMap.CONSUMED_SERVICE_ID.eq(targetService.ID))
                    .and(serviceConsumeMap.REMOVED.isNull()))
            .where(targetService.REMOVED.isNull())
            .and(clientService.REMOVED.isNull())
            .and(condition)
            .fetch()
            .map(mapper);

    Nic nic = ntwkDao.getPrimaryNic(instance.getId());
    long vnetId = nic.getVnetId();
    return convertToDnsEntryData(isVIPProvider, serviceDnsEntries, instance.getAccountId(), vnetId);
  }