What follows is a solution for the given problem, using a mutable reference to a 'state' RDD that we update with new results each time.
We use transform
to tag the incoming event stream with the unique global id by doing a similarity join. This is a join "by hand" where we use a product of the two datasets and compare each entry pair-wise.
Note that this is an expensive process. There are many parts that could be changed, depending on specific characteristics of the expected stream. For example, we could replace the global state RDD by a local map
and apply map-side
joins for a faster similarity join, but that very much depends on the expected cardinality of the set of unique ids.
This was trickier than I originally expected. Take it only as a starting point towards a more robust solution. For example, the union
operation on the state RDD needs regular checkpointing to avoid the DAG to grow beyond control.
(There's a lot of room for improvement - but that's beyond a reasonable effort to provide an answer.)
Here I sketch the core of the solution, for the complete test notebook see UniqueGlobalStateChains.snb
// this mutable reference points to the `states` that we keep across interations
@transient var states: RDD[(String, (Int, Long))] = sparkContext.emptyRDD
// we assume an incoming Event stream. Here we prepare it for the global id-process
@transient val eventsById = eventStream.map(event => (event.id, event))
@transient val groupedEvents = eventsById.groupByKey()
// this is the core of the solution.
// We transform the incoming events into tagged events.
// As a by-product, the mutable `states` reference will get updated with the latest state mapping.
// the "chain" of events can be reconstructed ordering the states by timestamp
@transient val taggedEvents = groupedEvents.transform{ (events, currentTime) =>
val currentTransitions = states.reduceByKey{case (event1, event2) => Seq(event1, event2).maxBy{case (id, ts) => ts}}
val currentMappings = currentTransitions.map{case (globalId, (currentId, maxTx)) => (currentId, globalId)}
val newEventIds = events.keys // let's extract the ids of the incoming (grouped) events
val similarityJoinMap = newEventIds.cartesian(currentMappings)
.collect{case (eventId, (currentId, globalId)) if (isSimilar(currentId)(eventId)) => (eventId, globalId)}
.collectAsMap
//val similarityBC = sparkContext.broadcast(similarityJoinMap)
val newGlobalKeys = newEventIds.map(id => (id, similarityJoinMap.getOrElse(id, genGlobalId())))
newGlobalKeys.cache() //avoid lazy evaluation to generate multiple global ids
val newTaggedEvents = events.join(newGlobalKeys).flatMap{case (eventId, (events, globalKey)) =>
events.map(event => (event.id,event.payload, globalKey))
}
val newStates = newGlobalKeys.map{case (eventId, globalKey) => (globalKey, (eventId, currentTime.milliseconds))}
currentState = newStates
states.unpersist(false)
states = newStates.union(states)
states.cache()
newTaggedEvents
}
Given this input sequence:
"1|a,1|b,3|c", "2|d,2|e,2|f", "3|g,3|h,3|i,4|j", "5|k", "4|f,1|g", "6|h"
We get:
Tagged Events with a global id:
---
1|a: gen-4180,1|b: gen-4180,3|c: gen-5819
---
2|d: gen-4180,2|e: gen-4180,2|f: gen-4180
---
3|g: gen-4180,3|h: gen-4180,3|i: gen-4180,4|j: gen-5819
---
5|k: gen-5819
---
1|g: gen-2635,4|f: gen-4180
---
6|h: gen-5819
And we can reconstruct the chain of events that are derived from a global id:
gen-4180: 1<-2<-3<-4
gen-2635: 1
gen-5819: 3<-4<-5<-6
-o-
1b
and2c
are processed in parallel? – maasg Jul 23 '17 at 9:55groupBy
.1b
and2c
are grouped and we know that1b
and2c
are connected and they are similar. so we can update1b
with this information (same for2c
where we know2c
is similar with1b
). But problem is with2d
because we know2
has parent1
(for the previous event this we store instate
) but we have to know that1
has parentZ
(andZ
is also "parent" for2
) which is "root". – VladoDemcak Jul 23 '17 at 10:03Z <-1 <-2 <-3
? What happens if -for example- we process1a, 3f, 2c
? Will we haveZ <- 1 <- 2
andX <- 3
b/c we didn't have the2->3
relation at the time 3 arrives? – maasg Jul 23 '17 at 10:271a
,2c
,3f
for processing and we should have sequentially1-2
and2-3
pairs. Example in the question assumes2e and 3f
are similar. But if we have similarity for2e-3g
pair and3f
(with earlier timestamp) has been already processed we can assumeX <- 3f
as our path. but3g, 3h, 3i etc
should have3->Z
. But this is edge case I would say. – VladoDemcak Jul 23 '17 at 10:47