@Override public boolean hostHasContainerLabel(long hostId, String labelKey, String labelValue) { return create() .select(LABEL.ID) .from(LABEL) .join(INSTANCE_LABEL_MAP) .on(LABEL.ID.eq(INSTANCE_LABEL_MAP.LABEL_ID)) .join(INSTANCE_HOST_MAP) .on(INSTANCE_LABEL_MAP.INSTANCE_ID.eq(INSTANCE_HOST_MAP.INSTANCE_ID)) .join(INSTANCE) .on(INSTANCE_HOST_MAP.INSTANCE_ID.eq(INSTANCE.ID)) .leftOuterJoin(SERVICE_EXPOSE_MAP) .on(SERVICE_EXPOSE_MAP.INSTANCE_ID.eq(INSTANCE.ID)) .where(INSTANCE_HOST_MAP.HOST_ID.eq(hostId)) .and(LABEL.REMOVED.isNull()) .and(INSTANCE_LABEL_MAP.REMOVED.isNull()) .and(INSTANCE_HOST_MAP.REMOVED.isNull()) .and(INSTANCE.REMOVED.isNull()) .and( INSTANCE.STATE.notIn( InstanceConstants.STATE_ERROR, InstanceConstants.STATE_ERRORING)) .and(LABEL.KEY.equalIgnoreCase(labelKey)) .and(LABEL.VALUE.equalIgnoreCase(labelValue)) .and(SERVICE_EXPOSE_MAP.UPGRADE.eq(false).or(SERVICE_EXPOSE_MAP.UPGRADE.isNull())) .fetchInto(Long.class) .size() > 0; }
@Override public boolean isVolumeInUseOnHost(long volumeId, long hostId) { return create() .select(INSTANCE.ID) .from(INSTANCE) .join(MOUNT) .on(MOUNT.INSTANCE_ID.eq(INSTANCE.ID).and(MOUNT.VOLUME_ID.eq(volumeId))) .join(INSTANCE_HOST_MAP) .on( INSTANCE_HOST_MAP .INSTANCE_ID .eq(INSTANCE.ID) .and(INSTANCE_HOST_MAP.HOST_ID.eq(hostId))) .join(HOST) .on(HOST.ID.eq(INSTANCE_HOST_MAP.HOST_ID)) .where( INSTANCE .REMOVED .isNull() .and(INSTANCE_HOST_MAP.STATE.notIn(IHM_STATES)) .and( (INSTANCE .HEALTH_STATE .isNull() .or( INSTANCE.HEALTH_STATE.eq( HealthcheckConstants.HEALTH_STATE_HEALTHY))))) .fetch() .size() > 0; }
@Override public List<Port> getUsedPortsForHostExcludingInstance(long hostId, long instanceId) { return create() .select(PORT.fields()) .from(PORT) .join(INSTANCE_HOST_MAP) .on(PORT.INSTANCE_ID.eq(INSTANCE_HOST_MAP.INSTANCE_ID)) .join(INSTANCE) .on(INSTANCE_HOST_MAP.INSTANCE_ID.eq(INSTANCE.ID)) .leftOuterJoin(SERVICE_EXPOSE_MAP) .on(SERVICE_EXPOSE_MAP.INSTANCE_ID.eq(INSTANCE.ID)) .where( INSTANCE_HOST_MAP .HOST_ID .eq(hostId) .and(INSTANCE.REMOVED.isNull()) .and(INSTANCE.ID.ne(instanceId)) .and( INSTANCE.STATE.in( InstanceConstants.STATE_STARTING, InstanceConstants.STATE_RESTARTING, InstanceConstants.STATE_RUNNING)) .and(INSTANCE_HOST_MAP.REMOVED.isNull()) .and(PORT.REMOVED.isNull()) .and(SERVICE_EXPOSE_MAP.UPGRADE.eq(false).or(SERVICE_EXPOSE_MAP.UPGRADE.isNull()))) .fetchInto(Port.class); }
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()); } }
@Override public Host getHost(Instance instance) { Condition cond = getInstanceHostConstraint(instance); try { return create() .selectDistinct(HOST.fields()) .from(HOST) .join(INSTANCE_HOST_MAP) .on(INSTANCE_HOST_MAP.HOST_ID.eq(HOST.ID)) .join(INSTANCE) .on(INSTANCE_HOST_MAP.INSTANCE_ID.eq(INSTANCE.ID)) .where(INSTANCE_HOST_MAP.REMOVED.isNull().and(cond)) .fetchOneInto(HostRecord.class); } catch (InvalidResultException e) { throw new FailedToAllocate("Instances to allocate assigned to different hosts."); } }
@Override public List<? extends Instance> findUnallocatedInstanceByDeploymentUnitUuid( long accountId, String deploymentUnitUuid) { return create() .select(INSTANCE.fields()) .from(INSTANCE) .leftOuterJoin(INSTANCE_HOST_MAP) .on(INSTANCE_HOST_MAP.INSTANCE_ID.eq(INSTANCE.ID)) .where( INSTANCE .REMOVED .isNull() .and(INSTANCE_HOST_MAP.ID.isNull()) .and(INSTANCE.DEPLOYMENT_UNIT_UUID.eq(deploymentUnitUuid)) .and(INSTANCE.ALLOCATION_STATE.eq(CommonStatesConstants.INACTIVE))) .fetchInto(InstanceRecord.class); }
@Override public List<? extends InstanceHostMap> findBadInstanceHostMaps(int count) { return create() .select(INSTANCE_HOST_MAP.fields()) .from(INSTANCE_HOST_MAP) .join(INSTANCE) .on(INSTANCE_HOST_MAP.INSTANCE_ID.eq(INSTANCE.ID)) .where( INSTANCE_HOST_MAP .REMOVED .isNull() .and(INSTANCE.STATE.eq(CommonStatesConstants.PURGED)) .and( INSTANCE_HOST_MAP.STATE.notIn( CommonStatesConstants.DEACTIVATING, CommonStatesConstants.REMOVING))) .limit(count) .fetchInto(InstanceHostMapRecord.class); }
@Override public List<? extends Instance> getNonRemovedInstanceOn(Long hostId) { return create() .select(INSTANCE.fields()) .from(INSTANCE) .join(INSTANCE_HOST_MAP) .on(INSTANCE_HOST_MAP.HOST_ID.eq(hostId).and(INSTANCE_HOST_MAP.INSTANCE_ID.eq(INSTANCE.ID))) .where( INSTANCE .REMOVED .isNull() .and( INSTANCE.STATE.notIn( InstanceConstants.STATE_ERROR, InstanceConstants.STATE_ERRORING, CommonStatesConstants.REMOVING))) .fetchInto(InstanceRecord.class); }
@Override public List<? extends Instance> findBadInstances(int count) { return create() .select(INSTANCE.fields()) .from(INSTANCE) .join(INSTANCE_HOST_MAP) .on(INSTANCE_HOST_MAP.INSTANCE_ID.eq(INSTANCE.ID)) .join(HOST) .on(INSTANCE_HOST_MAP.HOST_ID.eq(HOST.ID)) .where( HOST.REMOVED .isNotNull() .and(INSTANCE.REMOVED.isNull()) .and( INSTANCE.STATE.notIn( InstanceConstants.STATE_STOPPING, CommonStatesConstants.REMOVING))) .limit(count) .fetchInto(InstanceRecord.class); }
public List<Instance> getUnmappedDeploymentUnitInstances(String deploymentUnitUuid) { List<? extends Instance> instanceRecords = create() .select(INSTANCE.fields()) .from(INSTANCE) .leftOuterJoin(INSTANCE_HOST_MAP) .on( INSTANCE_HOST_MAP .INSTANCE_ID .eq(INSTANCE.ID) .and(INSTANCE_HOST_MAP.REMOVED.isNull())) .where(INSTANCE.REMOVED.isNull()) .and(INSTANCE.DEPLOYMENT_UNIT_UUID.eq(deploymentUnitUuid)) .and(INSTANCE_HOST_MAP.ID.isNull()) .fetchInto(InstanceRecord.class); List<Instance> instances = new ArrayList<Instance>(); for (Instance i : instanceRecords) { instances.add(i); } return instances; }
@Override public List<Long> getInstancesWithVolumeMounted(long volumeId, long currentInstanceId) { return create() .select(INSTANCE.ID) .from(INSTANCE) .join(MOUNT) .on(MOUNT.INSTANCE_ID.eq(INSTANCE.ID).and(MOUNT.VOLUME_ID.eq(volumeId))) .join(INSTANCE_HOST_MAP) .on(INSTANCE_HOST_MAP.INSTANCE_ID.eq(INSTANCE.ID)) .where( INSTANCE .REMOVED .isNull() .and(INSTANCE.ID.ne(currentInstanceId)) .and(INSTANCE_HOST_MAP.STATE.notIn(IHM_STATES)) .and( (INSTANCE .HEALTH_STATE .isNull() .or(INSTANCE.HEALTH_STATE.eq(HealthcheckConstants.HEALTH_STATE_HEALTHY))))) .fetchInto(Long.class); }
@Override public Map<String, List<InstanceHostMap>> getInstanceHostMapsWithHostUuid(long instanceId) { Map<Record, List<InstanceHostMap>> result = create() .select(INSTANCE_HOST_MAP.fields()) .select(HOST.UUID) .from(INSTANCE_HOST_MAP) .join(HOST) .on(INSTANCE_HOST_MAP.HOST_ID.eq(HOST.ID)) .where( INSTANCE_HOST_MAP .INSTANCE_ID .eq(instanceId) .and(INSTANCE_HOST_MAP.REMOVED.isNull())) .fetchGroups(new Field[] {HOST.UUID}, InstanceHostMap.class); Map<String, List<InstanceHostMap>> maps = new HashMap<>(); for (Map.Entry<Record, List<InstanceHostMap>> entry : result.entrySet()) { String uuid = (String) entry.getKey().getValue(0); maps.put(uuid, entry.getValue()); } return maps; }
private List<PublicEndpoint> getPublicEndpointsInternal( long accountId, Long serviceId, Long hostId) { MultiRecordMapper<PublicEndpoint> mapper = new MultiRecordMapper<PublicEndpoint>() { @Override protected PublicEndpoint map(List<Object> input) { Instance instance = (Instance) input.get(0); Port port = (Port) input.get(1); Host host = (Host) input.get(2); String address = ""; IpAddress ip = (IpAddress) input.get(3); if (ip != null) { address = ip.getAddress(); } else { address = DataAccessor.fieldString(port, PortConstants.FIELD_BIND_ADDR); } ServiceExposeMap exposeMap = (ServiceExposeMap) input.get(4); Long serviceId = exposeMap != null ? exposeMap.getServiceId() : null; PublicEndpoint data = new PublicEndpoint( address, port.getPublicPort(), host.getId(), instance.getId(), serviceId); return data; } }; InstanceTable instance = mapper.add(INSTANCE, INSTANCE.ID, INSTANCE.ACCOUNT_ID); PortTable port = mapper.add(PORT); HostTable host = mapper.add(HOST, HOST.ID); IpAddressTable ipAddress = mapper.add(IP_ADDRESS, IP_ADDRESS.ID, IP_ADDRESS.ADDRESS); ServiceExposeMapTable exposeMap = mapper.add( SERVICE_EXPOSE_MAP, SERVICE_EXPOSE_MAP.INSTANCE_ID, SERVICE_EXPOSE_MAP.SERVICE_ID); Condition condition = null; if (serviceId != null && hostId != null) { condition = host.ID.eq(hostId).and(exposeMap.SERVICE_ID.eq(serviceId)); } else if (hostId != null) { condition = host.ID.eq(hostId); } else if (serviceId != null) { condition = (exposeMap.SERVICE_ID.eq(serviceId)); } return create() .select(mapper.fields()) .from(instance) .join(port) .on(port.INSTANCE_ID.eq(instance.ID)) .join(INSTANCE_HOST_MAP) .on(INSTANCE_HOST_MAP.INSTANCE_ID.eq(instance.ID)) .join(host) .on(INSTANCE_HOST_MAP.HOST_ID.eq(host.ID)) .leftOuterJoin(ipAddress) .on(port.PUBLIC_IP_ADDRESS_ID.eq(ipAddress.ID)) .leftOuterJoin(exposeMap) .on(exposeMap.INSTANCE_ID.eq(instance.ID)) .where(instance.ACCOUNT_ID.eq(accountId)) .and(instance.REMOVED.isNull()) .and(port.REMOVED.isNull()) .and(host.REMOVED.isNull()) .and(ipAddress.REMOVED.isNull()) .and(exposeMap.REMOVED.isNull()) .and(port.PUBLIC_PORT.isNotNull()) .and( port.STATE.in( CommonStatesConstants.ACTIVATING, CommonStatesConstants.ACTIVE, CommonStatesConstants.UPDATING_ACTIVE)) .and(condition) .fetch() .map(mapper); }