static Key activeWorkflowInstanceKey(KeyFactory keyFactory, WorkflowInstance workflowInstance) { final String name = workflowInstance.toKey(); return activeWorkflowInstanceKey(keyFactory, name); }
private Key activeWorkflowInstanceKey(WorkflowInstance workflowInstance) { return activeWorkflowInstanceKey(datastore.newKeyFactory(), workflowInstance); }
Optional<RunState> readActiveState(WorkflowInstance instance) throws IOException { final Entity entity = datastore.get(activeWorkflowInstanceKey(instance)); if (entity == null) { return Optional.empty(); } else { return Optional.of(entityToRunState(entity, instance)); } }
@Override public Optional<RunState> readActiveState(WorkflowInstance instance) throws IOException { final Entity entity = tx.get(activeWorkflowInstanceKey(tx.getDatastore().newKeyFactory(), instance)); if (entity == null) { return Optional.empty(); } else { return Optional.of(entityToRunState(entity, instance)); } }
/** * Strongly consistently read all active states */ Map<WorkflowInstance, RunState> readActiveStates() throws IOException { // Strongly read active state keys from index shards in parallel final List<Key> keys = gatherIO(activeWorkflowInstanceIndexShardKeys(datastore.newKeyFactory()).stream() .map(key -> asyncIO(() -> datastore.query(Query.newEntityQueryBuilder() .setFilter(PropertyFilter.hasAncestor(key)) .setKind(KIND_ACTIVE_WORKFLOW_INSTANCE_INDEX_SHARD_ENTRY) .build()))) .collect(toList()), 30, TimeUnit.SECONDS) .stream() .flatMap(Collection::stream) .map(entity -> entity.getKey().getName()) .map(name -> activeWorkflowInstanceKey(datastore.newKeyFactory(), name)) .collect(toList()); // Strongly consistently read values for the above keys in parallel return gatherIO(Lists.partition(keys, MAX_NUMBER_OF_ENTITIES_IN_ONE_BATCH_READ).stream() .map(batch -> asyncIO(() -> readRunStateBatch(batch))) .collect(toList()), 30, TimeUnit.SECONDS) .stream() .flatMap(Collection::stream) .collect(toMap(RunState::workflowInstance, Function.identity())); }
@Override public WorkflowInstance deleteActiveState(WorkflowInstance instance) throws IOException { tx.delete(activeWorkflowInstanceIndexShardEntryKey(tx.getDatastore().newKeyFactory(), instance)); tx.delete(activeWorkflowInstanceKey(tx.getDatastore().newKeyFactory(), instance)); return instance; }
static Entity runStateToEntity(KeyFactory keyFactory, WorkflowInstance wfi, RunState state) throws JsonProcessingException { final Key key = activeWorkflowInstanceKey(keyFactory, wfi); final Entity.Builder entity = Entity.newBuilder(key) .set(PROPERTY_COMPONENT, wfi.workflowId().componentId())