@Override public Result run(List<Object> input) { Metadata metadata = metadataManager.getMetadataForAccount(accountId); ListValuedMap<Long, String> serviceStates = calculateInstanceHealth(metadata); ListValuedMap<Long, String> stackStates = calculateServiceHealth(serviceStates, metadata); calculateStackHealth(stackStates, metadata); return Result.DONE; }
private void calculateStackHealth(ListValuedMap<Long, String> stackStates, Metadata metadata) { for (StackInfo stackInfo : metadata.getStacks()) { String stackState = aggregate(stackStates.get(stackInfo.getId())); if (!Objects.equals(stackState, stackInfo.getHealthState())) { writeStackHealthState(metadata, stackInfo.getId(), stackState); } } }
private ListValuedMap<Long, String> calculateServiceHealth(ListValuedMap<Long, String> serviceStates, Metadata metadata) { ListValuedMap<Long, String> stackState = new ArrayListValuedHashMap<>(); for (ServiceInfo serviceInfo : metadata.getServices()) { Long stackId = serviceInfo.getStackId(); List<String> healthStates = serviceStates.get(serviceInfo.getId()); if (healthStates == null) { healthStates = new ArrayList<>(); } if (SUPPORTED_SERVICE_KINDS.contains(serviceInfo.getKind())) { // Haven't met the scale yet boolean scaleNotMet = !serviceInfo.isGlobal() && serviceInfo.getScale() != null && healthStates.size() != (serviceInfo.getScale() * (1 + serviceInfo.getSidekicks().size())); if (healthStates.isEmpty() || scaleNotMet) { healthStates = Collections.singletonList(HEALTH_STATE_DEGRADED); } } else { healthStates = Collections.singletonList(null); } String serviceState = aggregate(healthStates); if (!Objects.equals(serviceState, serviceInfo.getHealthState())) { writeServiceHealthState(metadata, serviceInfo.getId(), serviceState); } stackState.put(stackId, serviceState); } return stackState; }
@Override public Map<Long, Collection<MountEntry>> getMountsForInstances(List<Long> ids, final IdFormatter idF) { ListValuedMap<Long, MountEntry> result = new ArrayListValuedHashMap<>(); create().select(INSTANCE.NAME, VOLUME.NAME, VOLUME.ID, MOUNT.PERMISSIONS, MOUNT.PATH, MOUNT.INSTANCE_ID) .from(MOUNT) .join(VOLUME) .on(VOLUME.ID.eq(MOUNT.VOLUME_ID)) .join(INSTANCE) .on(INSTANCE.ID.eq(MOUNT.INSTANCE_ID)) .where(MOUNT.REMOVED.isNull() .and(VOLUME.REMOVED.isNull()) .and(MOUNT.INSTANCE_ID.in(ids))) .fetchInto((RecordHandler<Record6<String, String, Long, String, String, Long>>) record -> { Long instanceId = record.getValue(MOUNT.INSTANCE_ID); MountEntry mount = new MountEntry(); mount.setInstanceName(record.getValue(INSTANCE.NAME)); mount.setInstanceId(idF.formatId(InstanceConstants.TYPE, instanceId)); mount.setPath(record.getValue(MOUNT.PATH)); mount.setPermission(record.getValue(MOUNT.PERMISSIONS)); mount.setVolumeId(idF.formatId(VolumeConstants.TYPE, record.getValue(VOLUME.ID))); mount.setVolumeName(record.getValue(VOLUME.NAME)); result.put(instanceId, mount); }); return result.asMap(); }
@Override public Map<Long, Collection<MountEntry>> getMountsForVolumes(List<Long> ids, final IdFormatter idF) { ListValuedMap<Long, MountEntry> result = new ArrayListValuedHashMap<>(); create().select(VOLUME.NAME, MOUNT.PERMISSIONS, MOUNT.PATH, MOUNT.INSTANCE_ID, MOUNT.VOLUME_ID, INSTANCE.NAME) .from(MOUNT) .join(INSTANCE) .on(INSTANCE.ID.eq(MOUNT.INSTANCE_ID)) .join(VOLUME) .on(VOLUME.ID.eq(MOUNT.VOLUME_ID)) .where(INSTANCE.REMOVED.isNull() .and(VOLUME.REMOVED.isNull()) .and(MOUNT.VOLUME_ID.in(ids))) .fetchInto((RecordHandler<Record6<String, String, String, Long, Long, String>>) record -> { Long volumeId = record.getValue(MOUNT.VOLUME_ID); MountEntry mount = new MountEntry(); mount.setInstanceName(record.getValue(INSTANCE.NAME)); mount.setInstanceId(idF.formatId(InstanceConstants.TYPE, record.getValue(MOUNT.INSTANCE_ID))); mount.setPath(record.getValue(MOUNT.PATH)); mount.setPermission(record.getValue(MOUNT.PERMISSIONS)); mount.setVolumeId(idF.formatId(VolumeConstants.TYPE, volumeId)); mount.setVolumeName(record.getValue(VOLUME.NAME)); result.put(volumeId, mount); }); return result.asMap(); }
private ListValuedMap<Long, String> calculateInstanceHealth(Metadata metadata) { ListValuedMap<Long, String> serviceState = new ArrayListValuedHashMap<>(); for (InstanceInfo instanceInfo : metadata.getInstances()) { String instanceState = instanceInfo.getHealthState(); switch(instanceInfo.getState()) { case InstanceConstants.STATE_RUNNING: if (instanceInfo.getHealthCheck() != null) { String newInstanceState = aggregate(healthStates(instanceInfo)); if (STATE_TRANSITIONS.get(instanceInfo.getHealthState()).contains(newInstanceState)) { instanceState = newInstanceState; } } break; case InstanceConstants.STATE_STOPPED: if (instanceInfo.isShouldRestart()) { instanceState = HEALTH_STATE_UNHEALTHY; } else { instanceState = HEALTH_STATE_HEALTHY; } break; case CommonStatesConstants.ERROR: instanceState = HEALTH_STATE_UNHEALTHY; break; default: continue; } Set<Long> serviceIds = instanceInfo.getServiceIds(); for (Long serviceId : serviceIds) { if (instanceInfo.getDesired()) { serviceState.put(serviceId, instanceState); } } if (!Objects.equals(instanceState, instanceInfo.getHealthState())) { writeInstanceHealthState(metadata, instanceInfo.getId(), instanceState); } } return serviceState; }