@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;
  }
Exemplo n.º 2
0
 protected void setUpgrade(ServiceExposeMap map, boolean upgrade) {
   if (upgrade) {
     map.setUpgrade(true);
     map.setManaged(false);
   } else {
     map.setUpgrade(false);
     map.setManaged(true);
   }
   objectManager.persist(map);
 }
Exemplo n.º 3
0
  protected String getIpAddress(
      ServiceInstanceData serviceInstanceData,
      boolean isSource,
      Map<Long, IpAddress> instanceIdToHostIpMap) {
    Nic nic = serviceInstanceData.getNic();
    ServiceExposeMap exposeMap = serviceInstanceData.getExposeMap();
    IpAddress ipAddr = serviceInstanceData.getIpAddress();
    String ip = null;

    if (isSource
        && serviceInstanceData
            .getService()
            .getKind()
            .equalsIgnoreCase(ServiceDiscoveryConstants.KIND.SERVICE.name())) {
      if (ipAddr != null) {
        ip = ipAddr.getAddress();
      }
    } else {
      if (ipAddr != null) {
        ip = ipAddr.getAddress();
      } else {
        ip = exposeMap.getIpAddress();
      }
    }

    if (nic == null || nic.getDeviceNumber().equals(0)) {
      return ip;
    } else {
      IpAddress hostIp = instanceIdToHostIpMap.get(nic.getInstanceId());
      if (hostIp != null) {
        if (isSource) {
          return "default";
        }
        return hostIp.getAddress();
      }
    }
    return null;
  }
Exemplo n.º 4
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());
            }
          });
    }
  }
  @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);
  }