I have made job with Hazelcast Jet that converts a stream of IoT measurements into a stream of alarms.
So, whenever the humidity level of one sensor goes above a threshold, an alarm is raised. When it falls again below the threshold, the alarm is cleared. There can be up to 3 levels of thresholds (severity).
Currently, I have issues when the job is started. It will flush all buffered events from my RabbitMQ source. So, far events are ordered because local parallelism is one (let's assume a single member cluster here). But we the events are dispatched to the pool of cooperative threads, there is no garantee on the order. Can I instruct Jet to process all events with the same sensor ID in order?
Here is the current definition of my pipeline:
StreamStage<Notification> ss = l
.drawFrom(
Sources.<SimpleEntry<String, String>> streamFromProcessor("rabbitmq", ReadRabbitMQP.readRabbitMQ()))
.map(e -> makeMeasurement(e))
.flatMap(e -> checkThresholds(e))
.flatMap(e -> checkNotification(e));
ss.drainTo(Sinks.logger());
checkNotification compares the severity of the event with the latest severity for this sensor. That's why order is important.
I tried to implement to solution suggested by Gokhan Oner: I modifed the source to ouput SimpleMeasurement objects. This way I can add the timestamp just after the source.
StreamStage<Notification> ss = l
.drawFrom(Sources.<SimpleEntry<Integer, SimpleMeasurement>> streamFromProcessor("rabbitmq",
ReadRabbitMQP.readRabbitMQ(mGroupNames, mLocalParallelism)))
.addTimestamps(e -> e.getValue().getTimestamp().toEpochMilli(), 1000)
.flatMap(e -> checkThresholds(e))
.groupingKey(e -> e.getSensorId())
.window(WindowDefinition.tumbling(1))
.aggregate(AggregateOperations.sorting(DistributedComparator.comparing(e -> e.getPeakTime())))
.flatMap(e -> checkNotification(e));
ss.drainTo(Sinks.logger());
With this code, events are still not processed in order for the same sensor ID. Moreover, there is a 20 seconds delay from the moment the event is read from the source till it is processed in 'checkNotification'.