Hey all! Quick state-compatability question. I cu...
# troubleshooting
r
Hey all! Quick state-compatability question. I currently have an operator that uses a ListState configured with TTL. After some investigation, it was discovered that reading from this state was a HUGE bottleneck accounting for 90%+ of the for the job per the flame graph. I’m looking into replacing this with MapState since I only require checking if a given key exists. What I’m planning on doing is simply: • replacing the ListState with MapState in the operator • replacing the related StateDescriptors in the operator • updating the calls that interact with the state (i.e. reading, inserting values, etc.) Basically replacing this:
Copy code
# 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:
Copy code
# 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).
It seems after some testing this easiest approach, especially if you don’t care about the older state (which in my case was TTL’d) is to just hotswap the state types and just ensure that you update the name of the appropriate
StateDescriptor
. 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.