public static Set<WorkflowInstance> getTimedOutInstances(Map<WorkflowId, Workflow> workflows, List<InstanceState> activeStates, Instant instant, TimeoutConfig ttl) { return activeStates.parallelStream() .filter(entry -> { final Optional<Workflow> workflowOpt = Optional.ofNullable(workflows.get(entry.workflowInstance().workflowId())); return hasTimedOut(workflowOpt, entry.runState(), instant, ttl.ttlOf(entry.runState().state())); }) .map(InstanceState::workflowInstance) .collect(toSet()); }
public static ConcurrentMap<String, Long> getResourceUsage(boolean globalConcurrencyEnabled, List<InstanceState> activeStates, Set<WorkflowInstance> timedOutInstances, WorkflowResourceDecorator resourceDecorator, Map<WorkflowId, Workflow> workflows) { return activeStates.parallelStream() .filter(entry -> !timedOutInstances.contains(entry.workflowInstance())) .filter(entry -> isConsumingResources(entry.runState().state())) .flatMap(instanceState -> pairWithResources(globalConcurrencyEnabled, instanceState, workflows, resourceDecorator)) .collect(groupingByConcurrent( ResourceWithInstance::resource, ConcurrentHashMap::new, counting())); }
private static Stream<ResourceWithInstance> pairWithResources(boolean globalConcurrencyEnabled, InstanceState instanceState, Map<WorkflowId, Workflow> workflows, WorkflowResourceDecorator resourceDecorator) { final Optional<Workflow> workflowOpt = Optional.ofNullable(workflows.get(instanceState.workflowInstance().workflowId())); final Set<String> workflowResources = workflowResources(globalConcurrencyEnabled, workflowOpt); return workflowOpt .map(workflow -> resourceDecorator.decorateResources( instanceState.runState(), workflow.configuration(), workflowResources)) .orElse(workflowResources).stream() .map(resource -> ResourceWithInstance.create(resource, instanceState)); }
final List<InstanceState> activeStates = getActiveInstanceStates(activeStatesMap); final Set<WorkflowInstance> timedOutInstances = getTimedOutInstances(workflows, activeStates, time.get(), ttls); .collect(toMap( workflowId -> workflowId, workflowId -> workflowResources(globalConcurrency.isPresent(), Optional.ofNullable(workflows.get(workflowId))))); getResourceUsage(globalConcurrency.isPresent(), activeStates, timedOutInstances, resourceDecorator, workflows); final AtomicLongMap<String> currentResourceDemand = AtomicLongMap.create();
private void updateResourceCounters(StorageTransaction tx, Event event, RunState currentRunState, RunState nextRunState) throws IOException { // increment counters if event is dequeue if (isDequeue(event) && nextRunState.data().resourceIds().isPresent()) { tryUpdatingCounter(currentRunState, tx, nextRunState.data().resourceIds().get()); } // decrement counters if transitioning from a state that consumes resources // to a state that doesn't consume any resources if (isConsumingResources(currentRunState.state()) && !isConsumingResources(nextRunState.state())) { if (nextRunState.data().resourceIds().isPresent()) { for (String resource : nextRunState.data().resourceIds().get()) { tx.updateCounter(shardedCounter, resource, -1); } } else { log.error("Resource ids are missing for {} when transitioning from {} to {}.", nextRunState.workflowInstance(), currentRunState, nextRunState); } } }