Rion Williams
03/27/2024, 3:35 PM# Descriptor
val recentlySeen = ListStateDescriptor(
"recently-seen-keys",
String::class.java
)
# Declaration
recentlySeen = runtimeContext.getListState(States.recentlySeen
.apply {
enableTimeToLive(
StateTtlConfig
.newBuilder(Time.minutes(duplicateBufferMinutes))
.setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite)
.setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired)
.build()
)
}
)
# Usage
val identifier = ...
val recentIdentifiers = recentlySeen.get()
if (recentIdentifiers.contains(identifer)) { ... } else { recentlySeen.put(identifier); }
with this:
# Descriptor
val recentlySeen = MapStateDescriptor(
"recently-seen-keys",
String::class.java,
Boolean::class.java
)
# Declaration
recentlySeen = runtimeContext.getMapState(States.recentlySeen
.apply {
enableTimeToLive(
StateTtlConfig
.newBuilder(Time.minutes(duplicateBufferMinutes))
.setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite)
.setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired)
.build()
)
}
)
# Usage
val identifier = ...
val wasRecentlySeen = recentlySeen.get(identifier) ?: false
if (wasRecentlySeen) { ... } else { recentlySeen.put(identifier, true); }
I’m trying to determine the best way to handle this or if it should “just work” as I’m entirely removing some state in favor of new/different state. In this case, it is acceptable if the existing contents of the ListState are lost (they should be empty anyways since they are seldom populated and only have a TTL of 15 minutes).Rion Williams
03/27/2024, 6:10 PMStateDescriptor. If you try and use the same one, it’ll attempt to restore the old (in this case List) into a Map and you’ll get a type-mismatch.
If you really need to retain it I’m sure you could try implementing CheckpointedFunction and add some logic to the initialize/restore state functions.