Exemplo n.º 1
0
  protected void populateResolveInfo(
      final boolean isVIPProvider,
      final Map<Long, IpAddress> instanceIdToHostIpMap,
      ServiceDnsEntryData serviceData,
      Service clientService,
      Map<String, Map<String, String>> resolve,
      Map<String, String> resolveCname,
      Service targetService,
      ServiceInstanceData targetInstance) {
    String targetInstanceName =
        targetInstance.getInstance() == null ? null : targetInstance.getInstance().getName();

    boolean self = clientService.getId().equals(targetService.getId());
    String dnsName =
        getDnsName(targetService, serviceData.getConsumeMap(), targetInstance.getExposeMap(), self);

    String targetIp =
        isVIPProvider
            ? clientService.getVip()
            : getIpAddress(targetInstance, false, instanceIdToHostIpMap);
    if (targetIp != null) {
      Map<String, String> ips = resolve.get(dnsName);
      if (ips == null) {
        ips = new HashMap<>();
      }
      ips.put(targetIp, targetInstanceName);

      resolve.put(dnsName, ips);
    } else {
      String cname = targetInstance.getExposeMap().getHostName();
      if (cname != null) {
        resolveCname.put(dnsName, cname);
      }
    }
  }
Exemplo n.º 2
0
  protected List<DnsEntryData> convertToDnsEntryData(
      final boolean isVIPProvider,
      List<ServiceDnsEntryData> serviceDnsData,
      long accountId,
      long vnetId) {
    final Map<Long, IpAddress> instanceIdToHostIpMap =
        getInstanceWithHostNetworkingToIpMap(accountId);
    Map<Long, List<ServiceInstanceData>> servicesClientInstances =
        getServiceInstancesData(accountId, true, vnetId);
    Map<Long, List<ServiceInstanceData>> servicesTargetInstances =
        getServiceInstancesData(accountId, false, vnetId);
    Map<Long, List<ServiceDnsEntryData>> clientServiceIdToServiceData = new HashMap<>();
    for (ServiceDnsEntryData data : serviceDnsData) {
      Long clientServiceId = data.getClientService().getId();
      List<ServiceDnsEntryData> existingData = clientServiceIdToServiceData.get(clientServiceId);
      if (existingData == null) {
        existingData = new ArrayList<>();
      }
      existingData.add(data);
      clientServiceIdToServiceData.put(clientServiceId, existingData);
    }

    List<DnsEntryData> returnData = new ArrayList<>();

    for (ServiceDnsEntryData serviceData : serviceDnsData) {
      Service clientService = serviceData.getClientService();
      Map<String, Map<String, String>> resolve = new HashMap<>();
      Map<String, String> resolveCname = new HashMap<>();
      Service targetService = serviceData.getTargetService();
      List<ServiceInstanceData> targetInstancesData =
          populateTargetInstancesData(
              servicesTargetInstances, clientServiceIdToServiceData, targetService);

      for (ServiceInstanceData targetInstance : targetInstancesData) {
        populateResolveInfo(
            isVIPProvider,
            instanceIdToHostIpMap,
            serviceData,
            clientService,
            resolve,
            resolveCname,
            targetService,
            targetInstance);
      }

      if (servicesClientInstances.containsKey(clientService.getId())) {
        for (ServiceInstanceData clientInstance :
            servicesClientInstances.get(clientService.getId())) {
          DnsEntryData data = new DnsEntryData();
          String clientIp = getIpAddress(clientInstance, true, instanceIdToHostIpMap);
          data.setSourceIpAddress(clientIp);
          data.setResolveServicesAndContainers(resolve);
          data.setInstance(clientInstance.getInstance());
          data.setResolveCname(resolveCname);
          returnData.add(data);
        }
      }
    }
    return returnData;
  }
Exemplo n.º 3
0
 protected List<ServiceInstanceData> populateTargetInstancesData(
     Map<Long, List<ServiceInstanceData>> servicesTargetInstances,
     Map<Long, List<ServiceDnsEntryData>> clientServiceIdToServiceData,
     Service targetService) {
   List<ServiceInstanceData> targetInstancesData = new ArrayList<>();
   if (targetService
       .getKind()
       .equalsIgnoreCase(ServiceDiscoveryConstants.KIND.DNSSERVICE.name())) {
     List<ServiceDnsEntryData> behindAlias =
         clientServiceIdToServiceData.get(targetService.getId());
     if (behindAlias != null) {
       for (ServiceDnsEntryData behindAliasEntry : behindAlias) {
         List<ServiceInstanceData> toAdd =
             servicesTargetInstances.get(behindAliasEntry.getTargetService().getId());
         if (toAdd != null) {
           targetInstancesData.addAll(toAdd);
         }
       }
     }
   } else {
     List<ServiceInstanceData> toAdd = servicesTargetInstances.get(targetService.getId());
     if (toAdd != null) {
       targetInstancesData.addAll(toAdd);
     }
   }
   return targetInstancesData;
 }
  @Override
  public void removeServiceMaps(Service service) {
    // 1. remove all maps to the services consumed by service specified
    for (ServiceConsumeMap map : consumeMapDao.findConsumedMapsToRemove(service.getId())) {
      objectProcessManager.scheduleProcessInstance(
          ServiceDiscoveryConstants.PROCESS_SERVICE_CONSUME_MAP_REMOVE, map, null);
    }

    // 2. remove all maps to the services consuming service specified
    for (ServiceConsumeMap map : consumeMapDao.findConsumingMapsToRemove(service.getId())) {
      objectProcessManager.scheduleProcessInstance(
          ServiceDiscoveryConstants.PROCESS_SERVICE_CONSUME_MAP_REMOVE, map, null);
    }
  }
  protected void createListener(
      Service service,
      Map<Integer, LoadBalancerListener> listeners,
      LoadBalancerListenerPort port) {
    LoadBalancerListener listenerObj =
        objectManager.findOne(
            LoadBalancerListener.class,
            LOAD_BALANCER_LISTENER.SERVICE_ID,
            service.getId(),
            LOAD_BALANCER_LISTENER.SOURCE_PORT,
            port.getSourcePort(),
            LOAD_BALANCER_LISTENER.PRIVATE_PORT,
            port.getPrivatePort(),
            LOAD_BALANCER_LISTENER.TARGET_PORT,
            port.getTargetPort(),
            LOAD_BALANCER_LISTENER.REMOVED,
            null,
            LOAD_BALANCER_LISTENER.ACCOUNT_ID,
            service.getAccountId());

    if (listenerObj == null) {
      listenerObj =
          objectManager.create(
              LoadBalancerListener.class,
              LOAD_BALANCER_LISTENER.NAME,
              getLoadBalancerName(service) + "_" + port.getPrivatePort(),
              LOAD_BALANCER_LISTENER.ACCOUNT_ID,
              service.getAccountId(),
              LOAD_BALANCER_LISTENER.SOURCE_PORT,
              port.getSourcePort(),
              LOAD_BALANCER_LISTENER.PRIVATE_PORT,
              port.getPrivatePort(),
              LOAD_BALANCER_LISTENER.TARGET_PORT,
              port.getTargetPort(),
              LOAD_BALANCER_LISTENER.SOURCE_PROTOCOL,
              port.getProtocol(),
              LOAD_BALANCER_LISTENER.TARGET_PROTOCOL,
              port.getProtocol(),
              LoadBalancerConstants.FIELD_LB_LISTENER_ALGORITHM,
              "roundrobin",
              LOAD_BALANCER_LISTENER.ACCOUNT_ID,
              service.getAccountId(),
              LOAD_BALANCER_LISTENER.SERVICE_ID,
              service.getId());
    }
    objectProcessManager.executeProcess(
        LoadBalancerConstants.PROCESS_LB_LISTENER_CREATE, listenerObj, null);

    listeners.put(listenerObj.getPrivatePort(), listenerObj);
  }
  @Override
  public List<Integer> getServiceInstanceUsedOrderIds(Service service, String launchConfigName) {
    Environment env =
        objectManager.findOne(Environment.class, ENVIRONMENT.ID, service.getEnvironmentId());
    // get all existing instances to check if the name is in use by the instance of the same service
    List<Integer> usedIds = new ArrayList<>();
    // list all the instances
    List<? extends ServiceExposeMap> instanceServiceMaps =
        exposeMapDao.getNonRemovedServiceInstanceMap(service.getId());

    for (ServiceExposeMap instanceServiceMap : instanceServiceMaps) {
      Instance instance =
          objectManager.loadResource(Instance.class, instanceServiceMap.getInstanceId());
      if (ServiceDiscoveryUtil.isServiceGeneratedName(env, service, instance)) {

        String configName =
            launchConfigName == null
                    || launchConfigName.equals(ServiceDiscoveryConstants.PRIMARY_LAUNCH_CONFIG_NAME)
                ? ""
                : launchConfigName + "_";

        String id =
            instance
                .getName()
                .replace(
                    String.format("%s_%s_%s", env.getName(), service.getName(), configName), "");
        if (id.matches("\\d+")) {
          usedIds.add(Integer.valueOf(id));
        }
      }
    }
    return usedIds;
  }
  protected Map<String, Object> populateLBData(
      Service service,
      String lbName,
      LoadBalancerConfig lbConfig,
      Map<String, Object> launchConfigData,
      List<? extends Long> certIds,
      Long defaultCertId) {
    Map<String, Object> data = new HashMap<>();
    data.put("name", lbName);
    data.put(LoadBalancerConstants.FIELD_LB_CONFIG_ID, lbConfig.getId());
    data.put(LoadBalancerConstants.FIELD_LB_SERVICE_ID, service.getId());
    data.put(LoadBalancerConstants.FIELD_LB_NETWORK_ID, getServiceNetworkId(service));
    data.put(
        LoadBalancerConstants.FIELD_LB_INSTANCE_IMAGE_UUID,
        launchConfigData.get(InstanceConstants.FIELD_IMAGE_UUID));
    data.put(
        LoadBalancerConstants.FIELD_LB_INSTANCE_URI_PREDICATE,
        DataAccessor.fields(service)
            .withKey(LoadBalancerConstants.FIELD_LB_INSTANCE_URI_PREDICATE)
            .withDefault("delegate:///")
            .as(String.class));
    data.put("accountId", service.getAccountId());
    if (defaultCertId != null) {
      data.put(LoadBalancerConstants.FIELD_LB_DEFAULT_CERTIFICATE_ID, defaultCertId);
    }

    if (certIds != null) {
      data.put(LoadBalancerConstants.FIELD_LB_CERTIFICATE_IDS, certIds);
    }
    return data;
  }
 @Override
 public void updateLoadBalancerService(
     Service service, List<? extends Long> certIds, Long defaultCertId) {
   LoadBalancer lb =
       objectManager.findOne(
           LoadBalancer.class,
           LOAD_BALANCER.SERVICE_ID,
           service.getId(),
           LOAD_BALANCER.REMOVED,
           null);
   if (lb != null) {
     Map<String, Object> data = new HashMap<>();
     if (certIds == null) {
       certIds =
           DataAccessor.fields(service)
               .withKey(LoadBalancerConstants.FIELD_LB_CERTIFICATE_IDS)
               .asList(jsonMapper, Long.class);
     }
     if (defaultCertId == null) {
       defaultCertId =
           DataAccessor.fieldLong(service, LoadBalancerConstants.FIELD_LB_DEFAULT_CERTIFICATE_ID);
     }
     data.put(LoadBalancerConstants.FIELD_LB_CERTIFICATE_IDS, certIds);
     data.put(LoadBalancerConstants.FIELD_LB_DEFAULT_CERTIFICATE_ID, defaultCertId);
     DataUtils.getWritableFields(lb).putAll(data);
     objectManager.persist(lb);
     objectProcessManager.scheduleStandardProcess(StandardProcess.UPDATE, lb, data);
   }
 }
 protected long getServiceNetworkId(Service service) {
   Network network = ntwkDao.getNetworkForObject(service, NetworkConstants.KIND_HOSTONLY);
   if (network == null) {
     throw new RuntimeException(
         "Unable to find a network to activate a service " + service.getId());
   }
   long networkId = network.getId();
   return networkId;
 }
Exemplo n.º 10
0
 @Override
 public void reconcileServices(Collection<? extends Service> services) {
   for (Service service : services) {
     ConfigUpdateRequest request = ConfigUpdateRequest.forResource(Service.class, service.getId());
     request.addItem(RECONCILE);
     request.withDeferredTrigger(true);
     itemManager.updateConfig(request);
   }
 }
  @Override
  public void removeServiceLink(Service service, ServiceLink serviceLink) {
    ServiceConsumeMap map =
        consumeMapDao.findMapToRemove(service.getId(), serviceLink.getServiceId());

    if (map != null) {
      objectProcessManager.scheduleProcessInstance(
          ServiceDiscoveryConstants.PROCESS_SERVICE_CONSUME_MAP_REMOVE, map, null);
    }
  }
  @Override
  public void cloneConsumingServices(Service fromService, Service toService) {
    List<ServiceLink> linksToCreate = new ArrayList<>();

    for (ServiceConsumeMap map : consumeMapDao.findConsumingServices(fromService.getId())) {
      ServiceLink link;
      List<String> ports =
          DataAccessor.fieldStringList(map, LoadBalancerConstants.FIELD_LB_TARGET_PORTS);
      if (ports == null) {
        link = new ServiceLink(toService.getId(), map.getName());
      } else {
        link = new LoadBalancerServiceLink(toService.getId(), map.getName(), ports);
      }

      link.setConsumingServiceId(map.getServiceId());
      linksToCreate.add(link);
    }

    consumeMapDao.createServiceLinks(linksToCreate);
  }
Exemplo n.º 13
0
 private Map<Long, DeploymentUnitInstanceIdGenerator> populateUsedNames(List<Service> services) {
   Map<Long, DeploymentUnitInstanceIdGenerator> generator = new HashMap<>();
   for (Service service : services) {
     Map<String, List<Integer>> launchConfigUsedIds = new HashMap<>();
     for (String launchConfigName : ServiceDiscoveryUtil.getServiceLaunchConfigNames(service)) {
       List<Integer> usedIds = sdSvc.getServiceInstanceUsedSuffixes(service, launchConfigName);
       launchConfigUsedIds.put(launchConfigName, usedIds);
     }
     generator.put(
         service.getId(), new DeploymentUnitInstanceIdGeneratorImpl(launchConfigUsedIds));
   }
   return generator;
 }
  @Override
  public void addToLoadBalancerService(Service lbSvc, ServiceExposeMap instanceToRegister) {
    if (!lbSvc
        .getKind()
        .equalsIgnoreCase(ServiceDiscoveryConstants.KIND.LOADBALANCERSERVICE.name())) {
      return;
    }

    if (!isActiveService(lbSvc)) {
      return;
    }

    if (!exposeMapDao.isActiveMap(instanceToRegister)) {
      return;
    }

    LoadBalancer lb =
        objectManager.findOne(
            LoadBalancer.class,
            LOAD_BALANCER.SERVICE_ID,
            lbSvc.getId(),
            LOAD_BALANCER.REMOVED,
            null);
    if (lb == null) {
      return;
    }

    ServiceConsumeMap map =
        consumeMapDao.findNonRemovedMap(lbSvc.getId(), instanceToRegister.getServiceId(), null);
    if (map == null) {
      return;
    }

    LoadBalancerTargetInput target =
        new LoadBalancerTargetInput(instanceToRegister, map, jsonMapper);
    lbService.addTargetToLoadBalancer(lb, target);
  }
 private void createNewServiceMaps(Service service, Map<Long, String> newServiceLinks) {
   for (Long consumedServiceId : newServiceLinks.keySet()) {
     String linkName = newServiceLinks.get(consumedServiceId);
     ServiceConsumeMap map =
         consumeMapDao.findNonRemovedMap(service.getId(), consumedServiceId, linkName);
     if (map == null) {
       map =
           objectManager.create(
               ServiceConsumeMap.class,
               SERVICE_CONSUME_MAP.SERVICE_ID,
               service.getId(),
               SERVICE_CONSUME_MAP.CONSUMED_SERVICE_ID,
               consumedServiceId,
               SERVICE_CONSUME_MAP.ACCOUNT_ID,
               service.getAccountId(),
               SERVICE_CONSUME_MAP.NAME,
               linkName);
     }
     if (map.getState().equalsIgnoreCase(CommonStatesConstants.REQUESTED)) {
       objectProcessManager.scheduleProcessInstanceAsync(
           ServiceDiscoveryConstants.PROCESS_SERVICE_CONSUME_MAP_CREATE, map, null);
     }
   }
 }
 @Override
 public void cleanupLoadBalancerService(Service service) {
   // 1) remove load balancer
   LoadBalancer lb =
       objectManager.findOne(
           LoadBalancer.class,
           LOAD_BALANCER.SERVICE_ID,
           service.getId(),
           LOAD_BALANCER.REMOVED,
           null);
   if (lb != null) {
     objectProcessManager.scheduleProcessInstance(
         LoadBalancerConstants.PROCESS_LB_REMOVE, lb, null);
   }
 }
  private LoadBalancerConfig createDefaultLoadBalancerConfig(
      String defaultName, Map<String, Object> lbConfigData, Service service) {
    String name =
        lbConfigData.get("name") == null ? defaultName : lbConfigData.get("name").toString();
    LoadBalancerConfig lbConfig =
        objectManager.findOne(
            LoadBalancerConfig.class,
            LOAD_BALANCER_CONFIG.REMOVED,
            null,
            LOAD_BALANCER_CONFIG.ACCOUNT_ID,
            service.getAccountId(),
            LOAD_BALANCER_CONFIG.SERVICE_ID,
            service.getId());

    if (lbConfig == null) {
      lbConfigData.put("accountId", service.getAccountId());
      lbConfigData.put("name", name);
      lbConfigData.put("serviceId", service.getId());
      lbConfig = objectManager.create(LoadBalancerConfig.class, lbConfigData);
    }
    objectProcessManager.executeProcess(
        LoadBalancerConstants.PROCESS_LB_CONFIG_CREATE, lbConfig, null);
    return lbConfig;
  }
  private void removeOldServiceMaps(Service service, Map<String, ServiceLink> newServiceLinks) {
    List<? extends ServiceConsumeMap> existingMaps =
        consumeMapDao.findConsumedMapsToRemove(service.getId());
    List<ServiceLink> linksToRemove = new ArrayList<>();

    for (ServiceConsumeMap existingMap : existingMaps) {
      ServiceLink existingLink =
          new ServiceLink(existingMap.getConsumedServiceId(), existingMap.getName());
      if (!newServiceLinks.containsKey(existingLink.getUuid())) {
        linksToRemove.add(existingLink);
      }
    }

    for (ServiceLink linkToRemove : linksToRemove) {
      sdService.removeServiceLink(service, linkToRemove);
    }
  }
Exemplo n.º 19
0
  @Override
  public List<IpAddressToServiceIndex> getIpToIndex(Service service) {
    MultiRecordMapper<IpAddressToServiceIndex> mapper =
        new MultiRecordMapper<IpAddressToServiceIndex>() {
          @Override
          protected IpAddressToServiceIndex map(List<Object> input) {
            ServiceIndex index = (ServiceIndex) input.get(0);
            IpAddress ip = (IpAddress) input.get(1);
            Subnet subnet = (Subnet) input.get(2);
            IpAddressToServiceIndex data = new IpAddressToServiceIndex(index, ip, subnet);
            return data;
          }
        };

    ServiceIndexTable serviceIndex = mapper.add(SERVICE_INDEX);
    IpAddressTable ipAddress = mapper.add(IP_ADDRESS);
    SubnetTable subnet = mapper.add(SUBNET);
    ServiceExposeMapTable exposeMap = mapper.add(SERVICE_EXPOSE_MAP, SERVICE_EXPOSE_MAP.REMOVED);

    return create()
        .select(mapper.fields())
        .from(INSTANCE)
        .join(exposeMap)
        .on(exposeMap.INSTANCE_ID.eq(INSTANCE.ID))
        .join(NIC)
        .on(NIC.INSTANCE_ID.eq(exposeMap.INSTANCE_ID))
        .join(IP_ADDRESS_NIC_MAP)
        .on(IP_ADDRESS_NIC_MAP.NIC_ID.eq(NIC.ID))
        .join(ipAddress)
        .on(IP_ADDRESS_NIC_MAP.IP_ADDRESS_ID.eq(ipAddress.ID))
        .join(serviceIndex)
        .on(serviceIndex.ID.eq(INSTANCE.SERVICE_INDEX_ID))
        .join(subnet)
        .on(ipAddress.SUBNET_ID.eq(subnet.ID))
        .where(exposeMap.SERVICE_ID.eq(service.getId()))
        .and(exposeMap.REMOVED.isNull())
        .and(NIC.REMOVED.isNull())
        .and(ipAddress.REMOVED.isNull())
        .and(ipAddress.ADDRESS.isNotNull())
        .and(INSTANCE.REMOVED.isNull())
        .and(ipAddress.ROLE.eq(IpAddressConstants.ROLE_PRIMARY))
        .fetch()
        .map(mapper);
  }
  @SuppressWarnings("unchecked")
  protected void populateLoadBalancerServiceLabels(
      Service service, String launchConfigName, Map<String, Object> composeServiceData) {
    if (!service
        .getKind()
        .equalsIgnoreCase(ServiceDiscoveryConstants.KIND.LOADBALANCERSERVICE.name())) {
      return;
    }

    Map<String, String> labels = new HashMap<>();
    if (composeServiceData.get(InstanceConstants.FIELD_LABELS) != null) {
      labels.putAll(
          (HashMap<String, String>) composeServiceData.get(InstanceConstants.FIELD_LABELS));
    }
    // get all consumed services maps
    List<? extends ServiceConsumeMap> consumedServiceMaps =
        consumeMapDao.findConsumedServices(service.getId());
    // for each port, populate the label
    for (ServiceConsumeMap map : consumedServiceMaps) {
      Service consumedService =
          objectManager.loadResource(Service.class, map.getConsumedServiceId());
      List<String> ports =
          DataAccessor.fieldStringList(map, LoadBalancerConstants.FIELD_LB_TARGET_PORTS);
      String consumedServiceName = consumedService.getName();
      if (!service.getEnvironmentId().equals(consumedService.getEnvironmentId())) {
        Environment env =
            objectManager.loadResource(Environment.class, consumedService.getEnvironmentId());
        consumedServiceName = env.getName() + "/" + consumedServiceName;
      }
      String labelName = ServiceDiscoveryConstants.LABEL_LB_TARGET + consumedServiceName;
      StringBuilder bldr = new StringBuilder();
      for (String port : ports) {
        bldr.append(port).append(",");
      }
      if (bldr.length() > 0) {
        labels.put(labelName, bldr.toString().substring(0, bldr.length() - 1));
      }
    }

    if (!labels.isEmpty()) {
      composeServiceData.put(InstanceConstants.FIELD_LABELS, labels);
    }
  }
Exemplo n.º 21
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;
 }
Exemplo n.º 22
0
 protected void waitForHealthyState(Service service) {
   List<? extends Instance> serviceInstances =
       exposeMapDao.listServiceManagedInstances(service.getId());
   final List<String> healthyStates =
       Arrays.asList(
           HealthcheckConstants.HEALTH_STATE_HEALTHY,
           HealthcheckConstants.HEALTH_STATE_UPDATING_HEALTHY);
   for (final Instance instance : serviceInstances) {
     resourceMntr.waitFor(
         instance,
         DEFAULT_WAIT_TIMEOUT,
         new ResourcePredicate<Instance>() {
           @Override
           public boolean evaluate(Instance obj) {
             return instance.getHealthState() == null
                 || healthyStates.contains(obj.getHealthState());
           }
         });
   }
 }
  private void removeOldServiceMaps(Service service, Map<Long, String> newServiceLinks) {
    List<? extends ServiceConsumeMap> existingMaps =
        consumeMapDao.findConsumedMapsToRemove(service.getId());
    List<ServiceConsumeMap> mapsToRemove = new ArrayList<>();

    for (ServiceConsumeMap existingMap : existingMaps) {
      if (!newServiceLinks.containsKey(existingMap.getConsumedServiceId())) {
        mapsToRemove.add(existingMap);
      } else {
        String newName = newServiceLinks.get(existingMap.getConsumedServiceId());
        String existingName = existingMap.getName();

        if (!StringUtils.equalsIgnoreCase(newName, existingName)) {
          mapsToRemove.add(existingMap);
        }
      }
    }

    for (ServiceConsumeMap mapToRemove : mapsToRemove) {
      objectProcessManager.scheduleProcessInstanceAsync(
          ServiceDiscoveryConstants.PROCESS_SERVICE_CONSUME_MAP_REMOVE, mapToRemove, null);
    }
  }
  private void populateLinksForService(
      Service service,
      Collection<Long> servicesToExportIds,
      Map<String, Object> composeServiceData) {
    List<String> serviceLinksWithNames = new ArrayList<>();
    List<String> externalLinksServices = new ArrayList<>();
    List<? extends ServiceConsumeMap> consumedServiceMaps =
        consumeMapDao.findConsumedServices(service.getId());
    for (ServiceConsumeMap consumedServiceMap : consumedServiceMaps) {
      Service consumedService =
          objectManager.findOne(
              Service.class, SERVICE.ID, consumedServiceMap.getConsumedServiceId());

      String linkName =
          consumedService.getName()
              + ":"
              + (consumedServiceMap.getName() != null
                  ? consumedServiceMap.getName()
                  : consumedService.getName());
      if (servicesToExportIds.contains(consumedServiceMap.getConsumedServiceId())) {
        serviceLinksWithNames.add(linkName);
      } else if (!consumedService.getEnvironmentId().equals(service.getEnvironmentId())) {
        Environment externalEnvironment =
            objectManager.loadResource(Environment.class, consumedService.getEnvironmentId());
        externalLinksServices.add(externalEnvironment.getName() + "/" + linkName);
      }
    }
    if (!serviceLinksWithNames.isEmpty()) {
      composeServiceData.put(
          ServiceDiscoveryConfigItem.LINKS.getDockerName(), serviceLinksWithNames);
    }

    if (!externalLinksServices.isEmpty()) {
      composeServiceData.put(
          ServiceDiscoveryConfigItem.EXTERNALLINKS.getDockerName(), externalLinksServices);
    }
  }
Exemplo n.º 25
0
  public void cleanupUpgradedInstances(Service service) {
    List<? extends ServiceExposeMap> maps = exposeMapDao.getInstancesSetForUpgrade(service.getId());
    List<Instance> waitList = new ArrayList<>();
    for (ServiceExposeMap map : maps) {
      Instance instance = objectManager.loadResource(Instance.class, map.getInstanceId());
      if (instance == null
          || instance.getState().equals(CommonStatesConstants.REMOVED)
          || instance.getState().equals(CommonStatesConstants.REMOVING)) {
        continue;
      }
      try {
        objectProcessMgr.scheduleProcessInstanceAsync(
            InstanceConstants.PROCESS_REMOVE, instance, null);
      } catch (ProcessCancelException ex) {
        // in case instance was manually restarted
        objectProcessMgr.scheduleProcessInstanceAsync(
            InstanceConstants.PROCESS_STOP,
            instance,
            ProcessUtils.chainInData(
                new HashMap<String, Object>(),
                InstanceConstants.PROCESS_STOP,
                InstanceConstants.PROCESS_REMOVE));
      }
    }

    for (Instance instance : waitList) {
      resourceMntr.waitFor(
          instance,
          new ResourcePredicate<Instance>() {
            @Override
            public boolean evaluate(Instance obj) {
              return CommonStatesConstants.REMOVED.equals(obj.getState());
            }
          });
    }
  }
Exemplo n.º 26
0
 @Override
 public List<? extends Instance> findInstancesFor(Service service) {
   return create()
       .select(INSTANCE.fields())
       .from(INSTANCE)
       .join(SERVICE_EXPOSE_MAP)
       .on(
           SERVICE_EXPOSE_MAP
               .INSTANCE_ID
               .eq(INSTANCE.ID)
               .and(SERVICE_EXPOSE_MAP.SERVICE_ID.eq(service.getId()))
               .and(
                   SERVICE_EXPOSE_MAP.STATE.in(
                       CommonStatesConstants.ACTIVATING,
                       CommonStatesConstants.ACTIVE,
                       CommonStatesConstants.REQUESTED))
               .and(
                   INSTANCE.STATE.notIn(
                       CommonStatesConstants.PURGING,
                       CommonStatesConstants.PURGED,
                       CommonStatesConstants.REMOVED,
                       CommonStatesConstants.REMOVING)))
       .fetchInto(InstanceRecord.class);
 }
  private void createLoadBalancer(
      Service service,
      String lbName,
      LoadBalancerConfig lbConfig,
      Map<String, Object> launchConfigData,
      List<? extends Long> certIds,
      Long defaultCertId) {
    LoadBalancer lb =
        objectManager.findOne(
            LoadBalancer.class,
            LOAD_BALANCER.SERVICE_ID,
            service.getId(),
            LOAD_BALANCER.REMOVED,
            null,
            LOAD_BALANCER.ACCOUNT_ID,
            service.getAccountId());
    Map<String, Object> data =
        populateLBData(service, lbName, lbConfig, launchConfigData, certIds, defaultCertId);
    if (lb == null) {
      lb = objectManager.create(LoadBalancer.class, data);
    }

    objectProcessManager.executeProcess(LoadBalancerConstants.PROCESS_LB_CREATE, lb, data);
  }
Exemplo n.º 28
0
 public ServiceEndpointsUpdateLock(Service service) {
   super("SERVICE." + service.getId() + "ENDPOINTS.UPDATE");
 }