-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-56400][SS] Apply rangeScan API in transformWithState Timer/TTL #55265
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
Open
HeartSaVioR
wants to merge
9
commits into
apache:master
Choose a base branch
from
HeartSaVioR:SPARK-56400-on-top-of-SPARK-56369
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 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 d750b04
Apply scan to transformWithState operators (TTL and timers)
HeartSaVioR d09017e
Reflect review comment
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) | ||
|
Contributor
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. Should we update the method comment indicating that we are using range scan now? |
||
|
|
||
| // Recall that the format is (expirationMs, elementKey) -> TTL_EMPTY_VALUE_ROW, so | ||
| // 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.
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.
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.
Is the
dummyElementKeyhere just for usingTTL_ENCODER.encodeTTLRowrequires an elementKey?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.
Yes, correct.