-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[MINOR] Guard against crashing on invalid key range queries #6521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
1167df5
09ace6b
ac27e85
b961d0a
cef3de5
f2a556b
b2deb14
61e86c8
bf38b11
892b1e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,11 +30,15 @@ | |
| import java.util.concurrent.locks.Lock; | ||
| import java.util.concurrent.locks.ReadWriteLock; | ||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| class CachingKeyValueStore | ||
| extends WrappedStateStore<KeyValueStore<Bytes, byte[]>, byte[], byte[]> | ||
| implements KeyValueStore<Bytes, byte[]>, CachedStateStore<byte[], byte[]> { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(CachingKeyValueStore.class); | ||
|
|
||
| private CacheFlushListener<byte[], byte[]> flushListener; | ||
| private boolean sendOldValues; | ||
| private String cacheName; | ||
|
|
@@ -228,6 +232,12 @@ private byte[] getInternal(final Bytes key) { | |
| @Override | ||
| public KeyValueIterator<Bytes, byte[]> range(final Bytes from, | ||
| final Bytes to) { | ||
| // Make sure this is a valid query | ||
| if (from.compareTo(to) > 0) { | ||
| LOG.debug("Returning empty iterator for range query with invalid range: keyFrom > keyTo."); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: since this represents an invalid range maybe this could be a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ack, good point |
||
| return KeyValueIterators.emptyIterator(); | ||
| } | ||
|
|
||
| validateStoreOpen(); | ||
| final KeyValueIterator<Bytes, byte[]> storeIterator = wrapped().range(from, to); | ||
| final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = cache.range(cacheName, from, to); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,11 +26,15 @@ | |
| import org.apache.kafka.streams.state.SessionStore; | ||
|
|
||
| import java.util.Objects; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| class CachingSessionStore | ||
| extends WrappedStateStore<SessionStore<Bytes, byte[]>, byte[], byte[]> | ||
| implements SessionStore<Bytes, byte[]>, CachedStateStore<byte[], byte[]> { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(CachingSessionStore.class); | ||
|
|
||
| private final SessionKeySchema keySchema; | ||
| private final SegmentedCacheFunction cacheFunction; | ||
| private String cacheName; | ||
|
|
@@ -153,6 +157,12 @@ public KeyValueIterator<Windowed<Bytes>, byte[]> findSessions(final Bytes keyFro | |
| final Bytes keyTo, | ||
| final long earliestSessionEndTime, | ||
| final long latestSessionStartTime) { | ||
| // Make sure this is a valid query | ||
| if (keyFrom.compareTo(keyTo) > 0) { | ||
| LOG.debug("Returning empty iterator for findSessions call with invalid range: keyFrom > keyTo."); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| return KeyValueIterators.emptyIterator(); | ||
| } | ||
|
|
||
| validateStoreOpen(); | ||
|
|
||
| final Bytes cacheKeyFrom = cacheFunction.cacheKey(keySchema.lowerRange(keyFrom, earliestSessionEndTime)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,11 +28,15 @@ | |
| import org.apache.kafka.streams.state.StateSerdes; | ||
| import org.apache.kafka.streams.state.WindowStore; | ||
| import org.apache.kafka.streams.state.WindowStoreIterator; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| class CachingWindowStore | ||
| extends WrappedStateStore<WindowStore<Bytes, byte[]>, byte[], byte[]> | ||
| implements WindowStore<Bytes, byte[]>, CachedStateStore<byte[], byte[]> { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(CachingWindowStore.class); | ||
|
|
||
| private final long windowSize; | ||
| private final SegmentedBytesStore.KeySchema keySchema = new WindowKeySchema(); | ||
|
|
||
|
|
@@ -196,6 +200,12 @@ public KeyValueIterator<Windowed<Bytes>, byte[]> fetch(final Bytes from, | |
| final Bytes to, | ||
| final long timeFrom, | ||
| final long timeTo) { | ||
| // Make sure this is a valid query | ||
| if (from.compareTo(to) > 0) { | ||
| LOG.debug("Returning empty iterator for fetch with invalid range: keyFrom > keyTo."); | ||
| return KeyValueIterators.emptyIterator(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto here and below |
||
| } | ||
|
|
||
| // since this function may not access the underlying inner store, we need to validate | ||
| // if store is open outside as well. | ||
| validateStoreOpen(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I would remove the comment here (and in all occurrences below), because the code itself is clear enough about what it does. Maybe rename
fromandtotofromKeyandtoKey(or similar) to make it even more clearer. Renaming would also apply to some of the changes below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack