Skip to content

Conversation

@gwaramadze
Copy link
Contributor

@gwaramadze gwaramadze commented Apr 18, 2025

Despite key_deserializer being set to json, after windowing, the deserialization is gone, and the key is served back as bytes. Affected windows:

  • sliding
  • tumbling and hopping in closing strategy "partition" mode

Tumbling and hopping windows with the closing strategy "key" are unaffected.

  • The fix for the sliding window was rather trivial. All we need is to use the deserialized key coming directly from process_window instead of the serialized one coming from the store.
    • As a side effect, turns out nothing is utilizing the key from state.expire_windows, so it doesn't need to return it.
  • The fix for the "partition" expiration mode is less elegant; refer to the docstring of _deserialize_prefix for a detailed explanation.

@gwaramadze gwaramadze force-pushed the fix/deserialization-propagation branch from dd3bf22 to 62d6b4e Compare April 18, 2025 12:49
@gwaramadze gwaramadze marked this pull request as ready for review April 21, 2025 08:35
@gwaramadze gwaramadze added the bug Something isn't working label Apr 21, 2025
Comment on lines +413 to +416
When retrieving a prefix during partition-level windows expiration, we
don't know its original type due to this conditional serialization.
Therefore, we must first *try* to deserialize it using the configured
`loads` function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we store an indicator about the prefix type, if it's a byte or something else ?

For migration, existing windows missing that information can try to deserialize

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it's considerably more code, not sure if it's worth it.

Let's see what @daniil-quix thinks. But even if we decide to do this, it may be done as a separate optimisation; in the meantime, this will fix the bug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm mostly worried about the performance hit of trying to deserialise every keys

Copy link

@ghost ghost May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some quick tests locally, and the performance hit actually depends on the value itself.

Two almost identical values b'123123' and b'{123123 take very different amounts of time:

### Successful deserialization

%%timeit

try:
    orjson.loads(b'123123')
except Exception:
    ...

34.4 ns ± 0.182 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
### Deserialization failed
%%timeit

try:
    orjson.loads(b'{123123')
except Exception:
    ...

765 ns ± 4.51 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

@ghost ghost force-pushed the fix/deserialization-propagation branch from 67f0bf5 to fb1045b Compare May 9, 2025 10:30
Comment on lines -331 to -333
assert expired[0] == ((0, 10), 1, [], prefix)
assert expired[1] == ((10, 20), 1, [], prefix)
assert expired[2] == ((20, 30), 1, [], prefix)
Copy link

@ghost ghost May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was kinda testing that the same prefix is returned :)
Let's return the key from expire_windows() to make the tests legit, even though we don't use it in the actual code.

@gwaramadze gwaramadze closed this May 9, 2025
@gwaramadze gwaramadze deleted the fix/deserialization-propagation branch May 9, 2025 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants