private static void initShardRange(Storage storage, String counterId, int startIndex, int endIndex) { try { storage.runInTransaction(tx -> { for (int index = startIndex; index < endIndex; index++) { final Optional<Shard> shard = tx.shard(counterId, index); if (!shard.isPresent()) { tx.store(Shard.create(counterId, index, 0)); } } return null; }); } catch (IOException e) { LOG.warn("Error when trying to create a group of shards in Datastore: ", e); } } }
void deleteActiveState(WorkflowInstance workflowInstance) throws IOException { storeWithRetries(() -> runInTransaction(tx -> tx.deleteActiveState(workflowInstance))); }
WorkflowInstance writeActiveState(WorkflowInstance workflowInstance, RunState state) throws IOException { return storeWithRetries(() -> runInTransaction(tx -> tx.writeActiveState(workflowInstance, state))); }
public <T, E extends Exception> T runInTransaction(TransactionFunction<T, E> f) throws IOException, E { final StorageTransaction tx = newTransaction(); try { final T value = f.apply(tx); tx.commit(); return value; } catch (DatastoreException e) { tx.rollback(); throw new TransactionException(e); } finally { if (tx.isActive()) { tx.rollback(); } } }
final Optional<Workflow> previous; try { previous = tx.workflow(workflow.id()); } catch (IOException e) { LOG.warn("failed to read workflow {} from storage", workflow.id(), e); tx.storeWorkflowWithNextNaturalTrigger(workflow, nextSpec.get()); } else { tx.store(workflow);
private Response<Backfill> updateBackfill(AuthContext ac, String id, EditableBackfillInput backfillInput) { if (!backfillInput.id().equals(id)) { return Response.forStatus( Status.BAD_REQUEST.withReasonPhrase("ID of payload does not match ID in uri.")); } final Backfill backfill; try { backfill = storage.runInTransaction(tx -> { final Backfill oldBackfill = tx.backfill(id) .orElseThrow(() -> new ResourceNotFoundException(String.format("Backfill %s not found.", id))); workflowActionAuthorizer.authorizeWorkflowAction(ac, oldBackfill.workflowId()); final BackfillBuilder backfillBuilder = oldBackfill.builder(); backfillInput.concurrency().ifPresent(backfillBuilder::concurrency); backfillInput.description().ifPresent(backfillBuilder::description); return tx.store(backfillBuilder.build()); }); } catch (ResourceNotFoundException e) { return Response.forStatus(Status.NOT_FOUND.withReasonPhrase(e.getMessage())); } catch (IOException e) { return Response.forStatus( Status.INTERNAL_SERVER_ERROR.withReasonPhrase("Failed to store backfill.")); } return Response.forStatus(Status.OK).withPayload(backfill); }
void storeResource(Resource resource) throws IOException { storeWithRetries(() -> runInTransaction(transaction -> { transaction.store(resource); return null; })); }
try { storage.runInTransaction(tx -> { final Optional<Workflow> workflow = tx.workflow(workflowInstance.workflowId()); if (!workflow.isPresent()) { throw new IllegalArgumentException( "Workflow not found: " + workflowInstance.workflowId().toKey()); return tx.writeActiveState(workflowInstance, runState); }); } catch (TransactionException e) {
tx.readActiveState(event.workflowInstance()); if (!currentRunState.isPresent()) { String message = "Received event for unknown workflow instance: " + event; tx.deleteActiveState(event.workflowInstance()); } else { tx.updateActiveState(event.workflowInstance(), nextRunState);
void patchState(WorkflowId workflowId, WorkflowState state) throws IOException { storeWithRetries(() -> runInTransaction(tx -> tx.patchState(workflowId, state))); }
int remainingCapacity, boolean reversed) throws IOException { final Backfill backfill = tx.backfill(id).orElseThrow(() -> new RuntimeException("Error while fetching backfill " + id)); tx.store(backfill.builder() .allTriggered(true) .build()); tx.store(backfill.builder() .nextTrigger(nextPartition) .build()); tx.store(backfill.builder() .nextTrigger(backfill.reverse() ? previousInstant(backfill.start(), backfill.schedule())
void store(Workflow workflow) throws IOException { storeWithRetries(() -> runInTransaction(tx -> tx.store(workflow))); }
@VisibleForTesting void updateCounterShard(StorageTransaction transaction, String counterId, long delta, int shardIndex, long shardCapacity) throws IOException { final Optional<Shard> shard = transaction.shard(counterId, shardIndex); transaction.store(Shard.create(counterId, shardIndex, (int) newShardValue)); } else if (delta > 0 && Range.closed(0L, shardCapacity).contains(newShardValue)) { transaction.store(Shard.create(counterId, shardIndex, (int) newShardValue)); } else { final String message = String.format(
void storeBackfill(Backfill backfill) throws IOException { storeWithRetries(() -> runInTransaction(tx -> tx.store(backfill))); }