One application that consumes data from 2 Kafka Topics and joins related events is continuously failing whenever the list state is cleaned by TTL config. The logic of the flatMap is a little bit different from the majority I have seen around. Every event from the first stream may join multiple times with events from the second stream, so this is a one to many context.
This is the code of the join:
val join = streamA
.connect(streamB)
.flatMap(
new JoinOneToManyStreams[StreamA, StreamB](
"streamA",
"streamB",
Time.minutes(3)
)
).uid("join")
.keyBy(_.streamB.id)
This is the class that implements the logic:
class JoinOneToManyStreams[A, B] (stateDescriptorA: String, stateDescriptorB: String, time: Time) extends RichCoFlatMapFunction[A, B, StateOut[A, B]] {
val ttlConfig: StateTtlConfig = StateTtlConfig
.newBuilder(time)
// Set TTL just once
.setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite)
.setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired)
.cleanupInRocksdbCompactFilter(1000)
.build
val streamAStateDescriptor = new ValueStateDescriptor[StateIn[A]](stateDescriptorA, classOf[StateIn[A]])
streamAStateDescriptor.enableTimeToLive(ttlConfig)
lazy val streamAState: ValueState[StateIn[A]] = getRuntimeContext.getState(streamAStateDescriptor)
val streamBStateDescriptor = new ListStateDescriptor[StateIn[B]](stateDescriptorB, classOf[StateIn[B]])
streamBStateDescriptor.enableTimeToLive(ttlConfig)
lazy val streamBState: ListState[StateIn[B]] = getRuntimeContext.getListState(streamBStateDescriptor)
override def flatMap1(streamA: A, out: Collector[StateOut[A, B]]): Unit = {
streamAState.update(StateIn[A](stateValue = streamA))
val stateB = streamBState.get
if (stateB.nonEmpty) {
streamBState.clear()
stateB.forEach(row => {
out.collect(StateOut(streamA, row.stateValue))
})
}
}
override def flatMap2(streamB: B, out: Collector[StateOut[A, B]]): Unit = {
streamBState.add(StateIn(streamB))
val stateA = streamAState.value
if (stateA != null) {
val stateB = streamBState.get
streamBState.clear()
stateB.forEach(row => {
out.collect(StateOut(stateA.stateValue, row.stateValue))
})
}
}
}
As you can see, cleanupInRocksdbCompactFilter is used, but I tried fullSnapshot as well and the same behavior was noticed.
Error:
Exception in thread "Thread-19" java.lang.IllegalArgumentException: classLoader cannot be null.
at com.esotericsoftware.kryo.Kryo.setClassLoader(Kryo.java:975)
at org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer.checkKryoInitialized(KryoSerializer.java:477)
at org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer.deserialize(KryoSerializer.java:337)
at org.apache.flink.api.common.typeutils.CompositeSerializer.deserialize(CompositeSerializer.java:151)
at org.apache.flink.contrib.streaming.state.ttl.RocksDbTtlCompactFiltersManager$ListElementFilter.nextElementLastAccessTimestamp(RocksDbTtlCompactFiltersManager.java:193)
at org.apache.flink.contrib.streaming.state.ttl.RocksDbTtlCompactFiltersManager$ListElementFilter.nextUnexpiredOffset(RocksDbTtlCompactFiltersManager.java:180)
I know that the list state is the problem because I disabled the tll for this type of state and the problem stopped. Is there any way to make it work?