public static SequenceEvent parseKey(String key, Event event, long timestamp) { final int lastHashPos = key.lastIndexOf('#'); if (lastHashPos < 1) { throw new IllegalArgumentException("Key must contain a hash '#' sign on position > 0"); } final long counter = Long.parseLong(key.substring(lastHashPos + 1)); return create(event, counter, timestamp); } }
void writeEvent(SequenceEvent sequenceEvent) throws IOException { storeWithRetries(() -> { try (final Table eventsTable = connection.getTable(EVENTS_TABLE_NAME)) { final String workflowInstanceKey = sequenceEvent.event().workflowInstance().toKey(); final String keyString = String.format("%s#%08d", workflowInstanceKey, sequenceEvent.counter()); final byte[] key = Bytes.toBytes(keyString); final Put put = new Put(key, sequenceEvent.timestamp()); final byte[] eventBytes = serialize(sequenceEvent.event()).toByteArray(); put.addColumn(EVENT_CF, EVENT_QUALIFIER, eventBytes); eventsTable.put(put); } }); }
WorkflowInstanceExecutionData executionInfo(Iterable<SequenceEvent> events) { for (SequenceEvent sequenceEvent : events) { eventTs = Instant.ofEpochMilli(sequenceEvent.timestamp()); sequenceEvent.event().accept(visitor); } if (!completed) { closeTrigger(); } return WorkflowInstanceExecutionData.create(currWorkflowInstance, triggerList); } }
@Override public SortedSet<SequenceEvent> readEvents(WorkflowInstance workflowInstance) { final SortedSet<SequenceEvent> events = Sets.newTreeSet(SequenceEvent.COUNTER_COMPARATOR); writtenEvents.stream() .filter(e -> e.event().workflowInstance().equals(workflowInstance)) .forEach(events::add); return events; }
Optional<Long> getLatestStoredCounter(WorkflowInstance workflowInstance) throws IOException { final Set<SequenceEvent> storedEvents = readEvents(workflowInstance); final Optional<SequenceEvent> lastStoredEvent = storedEvents.stream().reduce((a, b) -> b); if (lastStoredEvent.isPresent()) { return Optional.of(lastStoredEvent.get().counter()); } else { return Optional.empty(); } }
private SequenceEvent parseEventResult(Result r) throws IOException { final String key = new String(r.getRow()); final long timestamp = r.getColumnLatestCell(EVENT_CF, EVENT_QUALIFIER).getTimestamp(); final byte[] value = r.getValue(EVENT_CF, EVENT_QUALIFIER); final Event event = deserializeEvent(ByteString.of(value)); return SequenceEvent.parseKey(key, event, timestamp); }
private EventsPayload eventsForWorkflowInstance(String cid, String eid, String iid) { final WorkflowId workflowId = WorkflowId.create(cid, eid); final WorkflowInstance workflowInstance = WorkflowInstance.create(workflowId, iid); try { final Set<SequenceEvent> sequenceEvents = storage.readEvents(workflowInstance); final List<EventsPayload.TimestampedEvent> timestampedEvents = sequenceEvents.stream() .map(sequenceEvent -> EventsPayload.TimestampedEvent.create( sequenceEvent.event(), sequenceEvent.timestamp())) .collect(toList()); return EventsPayload.create(timestampedEvents); } catch (IOException e) { throw new RuntimeException(e); } } }
private Meter eventConsumerErrorMeter(SequenceEvent sequenceEvent) { final String eventType = EventUtil.name(sequenceEvent.event()); return eventConsumerErrorMeters.computeIfAbsent( eventType, (op) -> registry.meter(EVENT_CONSUMER_ERROR_RATE.tagged("event-type", eventType))); }
@Override public Optional<Long> getLatestStoredCounter(WorkflowInstance workflowInstance) throws IOException { final SortedSet<SequenceEvent> storedEvents = readEvents(workflowInstance); if (storedEvents.isEmpty()) { return Optional.empty(); } else { final SequenceEvent lastStoredEvent = storedEvents.last(); return Optional.of(lastStoredEvent.counter()); } }
private Meter eventConsumerMeter(SequenceEvent sequenceEvent) { final String eventType = EventUtil.name(sequenceEvent.event()); return eventConsumerMeters.computeIfAbsent( eventType, (op) -> registry.meter(EVENT_CONSUMER_RATE.tagged("event-type", eventType))); }
SequenceEvent.create(event, nextRunState.counter(), nextRunState.timestamp());