forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 2
[WIP][REVIEW-DONE] Apply StateStore's scan operation to optimize the pattern of range scan for TWS #25
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
Closed
Closed
[WIP][REVIEW-DONE] Apply StateStore's scan operation to optimize the pattern of range scan for TWS #25
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
137c25b
WIP introduce scan to scope the iteration in State Store
HeartSaVioR 07e241d
fix
HeartSaVioR ef84c49
change the scan's endKey to option
HeartSaVioR 158dff8
Roll back the usage on stream-stream join, add test for range scan
HeartSaVioR a494b31
Reflect self-review comments
HeartSaVioR 0aa4813
Second round of self review comments
HeartSaVioR 710466b
Change the API name to rangeScan
HeartSaVioR 63892c7
Apply scan to transformWithState operators (TTL and timers)
HeartSaVioR d0e852a
empty commit to retrigger CI
HeartSaVioR e635cac
Reflect review comments, yet to run tests
HeartSaVioR 44f7350
Bugfix on edge case of the value for watermark on late event filterin…
HeartSaVioR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,11 @@ trait TTLState { | |
| // an expiration at or before this timestamp must be cleaned up. | ||
| private[sql] def batchTimestampMs: Long | ||
|
|
||
| // The batch timestamp from the previous micro-batch, used to derive the startKey | ||
| // for scan-based TTL eviction. Entries at or below prevBatchTimestampMs were already | ||
| // cleaned up in the previous batch. | ||
| private[sql] def prevBatchTimestampMs: Option[Long] | ||
|
|
||
| // The configuration for this run of the streaming query. It may change between runs | ||
| // (e.g. user sets ttlConfig1, stops their query, updates to ttlConfig2, and then | ||
| // resumes their query). | ||
|
|
@@ -105,6 +110,8 @@ trait TTLState { | |
|
|
||
| private final val TTL_ENCODER = new TTLEncoder(elementKeySchema) | ||
|
|
||
| private final val ELEMENT_KEY_PROJECTION = UnsafeProjection.create(elementKeySchema) | ||
|
|
||
| // Empty row used for values | ||
| private final val TTL_EMPTY_VALUE_ROW = | ||
| UnsafeProjection.create(Array[DataType](NullType)).apply(InternalRow.apply(null)) | ||
|
|
@@ -161,10 +168,25 @@ trait TTLState { | |
| // | ||
| // The schema of the UnsafeRow returned by this iterator is (expirationMs, elementKey). | ||
| private[sql] def ttlEvictionIterator(): Iterator[UnsafeRow] = { | ||
| val ttlIterator = store.iterator(TTL_INDEX) | ||
| val dummyElementKey = ELEMENT_KEY_PROJECTION | ||
| .apply(new GenericInternalRow(elementKeySchema.length)) | ||
| val startKey = prevBatchTimestampMs.flatMap { prevTs => | ||
| if (prevTs < Long.MaxValue) { | ||
| Some(TTL_ENCODER.encodeTTLRow(prevTs + 1, dummyElementKey).copy()) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
| val endKey = if (batchTimestampMs < Long.MaxValue) { | ||
| Some(TTL_ENCODER.encodeTTLRow(batchTimestampMs + 1, dummyElementKey).copy()) | ||
| } else { | ||
| None | ||
| } | ||
| val ttlIterator = store.rangeScan(startKey, endKey, TTL_INDEX) | ||
|
|
||
| // Recall that the format is (expirationMs, elementKey) -> TTL_EMPTY_VALUE_ROW, so | ||
|
Owner
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. Self-review: maybe this code comment is still valid? If then let's just leave it as it is. |
||
| // kv.value doesn't ever need to be used. | ||
| // Safety filter: keep only truly expired entries | ||
| ttlIterator.takeWhile { kv => | ||
| val expirationMs = kv.key.getLong(0) | ||
| StateTTL.isExpired(expirationMs, batchTimestampMs) | ||
|
|
@@ -223,12 +245,14 @@ abstract class OneToOneTTLState( | |
| elementKeySchemaArg: StructType, | ||
| ttlConfigArg: TTLConfig, | ||
| batchTimestampMsArg: Long, | ||
| prevBatchTimestampMsArg: Option[Long], | ||
| metricsArg: Map[String, SQLMetric]) extends TTLState { | ||
| override private[sql] def stateName: String = stateNameArg | ||
| override private[sql] def store: StateStore = storeArg | ||
| override private[sql] def elementKeySchema: StructType = elementKeySchemaArg | ||
| override private[sql] def ttlConfig: TTLConfig = ttlConfigArg | ||
| override private[sql] def batchTimestampMs: Long = batchTimestampMsArg | ||
| override private[sql] def prevBatchTimestampMs: Option[Long] = prevBatchTimestampMsArg | ||
| override private[sql] def metrics: Map[String, SQLMetric] = metricsArg | ||
|
|
||
| /** | ||
|
|
@@ -340,12 +364,14 @@ abstract class OneToManyTTLState( | |
| elementKeySchemaArg: StructType, | ||
| ttlConfigArg: TTLConfig, | ||
| batchTimestampMsArg: Long, | ||
| prevBatchTimestampMsArg: Option[Long], | ||
| metricsArg: Map[String, SQLMetric]) extends TTLState { | ||
| override private[sql] def stateName: String = stateNameArg | ||
| override private[sql] def store: StateStore = storeArg | ||
| override private[sql] def elementKeySchema: StructType = elementKeySchemaArg | ||
| override private[sql] def ttlConfig: TTLConfig = ttlConfigArg | ||
| override private[sql] def batchTimestampMs: Long = batchTimestampMsArg | ||
| override private[sql] def prevBatchTimestampMs: Option[Long] = prevBatchTimestampMsArg | ||
| override private[sql] def metrics: Map[String, SQLMetric] = metricsArg | ||
|
|
||
| // Schema of the min-expiry index: elementKey -> minExpirationMs | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.