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 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); } } }