-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-54443][SS] Partition key extraction for all streaming stateful operators #53355
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
micheal-o
wants to merge
3
commits into
apache:master
Choose a base branch
from
micheal-o:partition_key_extract
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
82 changes: 82 additions & 0 deletions
82
.../spark/sql/execution/streaming/operators/stateful/StatePartitionKeyExtractorFactory.scala
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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.execution.streaming.operators.stateful | ||
|
|
||
| import org.apache.spark.sql.execution.streaming.operators.stateful.StatefulOperatorsUtils | ||
| import org.apache.spark.sql.execution.streaming.operators.stateful.flatmapgroupswithstate.FlatMapGroupsWithStatePartitionKeyExtractor | ||
| import org.apache.spark.sql.execution.streaming.operators.stateful.join.SymmetricHashJoinStateManager | ||
| import org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.{TransformWithStatePartitionKeyExtractorFactory, TransformWithStateVariableInfo} | ||
| import org.apache.spark.sql.execution.streaming.state.{OfflineStateRepartitionErrors, StatePartitionKeyExtractor, StateStore, StateStoreId} | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| /** | ||
| * Factory for creating state partition key extractor for various streaming stateful operators. | ||
| * This is used for offline state repartitioning, when we need to repartition | ||
| * the state for a given operator. If an operator isn't included in this factory, | ||
| * then offline repartitioning will not be supported for it. | ||
| * | ||
| * To support offline repartitioning for a new stateful operator, you need to: | ||
| * 1. Create a state partition key extractor for the operator state. | ||
| * 2. Register the state partition key extractor in this factory. | ||
| */ | ||
| object StatePartitionKeyExtractorFactory { | ||
| import StatefulOperatorsUtils._ | ||
|
|
||
| /** | ||
| * Creates a state partition key extractor for the given operator. | ||
| * An operator may have different extractor for different stores/column families. | ||
| * | ||
| * @param operatorName The name of the operator. | ||
| * @param stateKeySchema The schema of the state key. | ||
| * @param storeName The name of the store. | ||
| * @param colFamilyName The name of the column family. | ||
| * @param stateFormatVersion Optional, the version of the state format. Used by operators | ||
| * that have different extractors for different state formats. | ||
| * @param stateVariableInfo Optional, the state variable info for TransformWithState. | ||
| * @return The state partition key extractor. | ||
| */ | ||
| def create( | ||
| operatorName: String, | ||
| stateKeySchema: StructType, | ||
| storeName: String = StateStoreId.DEFAULT_STORE_NAME, | ||
| colFamilyName: String = StateStore.DEFAULT_COL_FAMILY_NAME, | ||
| stateFormatVersion: Option[Int] = None, | ||
| stateVariableInfo: Option[TransformWithStateVariableInfo] = None | ||
| ): StatePartitionKeyExtractor = { | ||
| operatorName match { | ||
| case STATE_STORE_SAVE_EXEC_OP_NAME => | ||
| new StreamingAggregationStatePartitionKeyExtractor(stateKeySchema) | ||
| case DEDUPLICATE_EXEC_OP_NAME => | ||
| new StreamingDeduplicateStatePartitionKeyExtractor(stateKeySchema) | ||
| case DEDUPLICATE_WITHIN_WATERMARK_EXEC_OP_NAME => | ||
| new StreamingDedupWithinWatermarkStatePartitionKeyExtractor(stateKeySchema) | ||
| case SESSION_WINDOW_STATE_STORE_SAVE_EXEC_OP_NAME => | ||
| new StreamingSessionWindowStatePartitionKeyExtractor(stateKeySchema) | ||
| case SYMMETRIC_HASH_JOIN_EXEC_OP_NAME => | ||
| SymmetricHashJoinStateManager.createPartitionKeyExtractor( | ||
| storeName, colFamilyName, stateKeySchema, stateFormatVersion.get) | ||
| case fmg if FLAT_MAP_GROUPS_OP_NAMES.contains(fmg) => | ||
| new FlatMapGroupsWithStatePartitionKeyExtractor(stateKeySchema) | ||
| case tws if TRANSFORM_WITH_STATE_OP_NAMES.contains(tws) => | ||
| TransformWithStatePartitionKeyExtractorFactory.create( | ||
| storeName, colFamilyName, stateKeySchema, stateVariableInfo.get) | ||
| case _ => throw OfflineStateRepartitionErrors | ||
| .unsupportedStatefulOperatorError(checkpointLocation = "", operatorName) | ||
| } | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -946,7 +946,7 @@ case class StateStoreSaveExec( | |
| } | ||
| } | ||
|
|
||
| override def shortName: String = "stateStoreSave" | ||
| override def shortName: String = StatefulOperatorsUtils.STATE_STORE_SAVE_EXEC_OP_NAME | ||
|
|
||
| override def shouldRunAnotherBatch(newInputWatermark: Long): Boolean = { | ||
| (outputMode.contains(Append) || outputMode.contains(Update)) && | ||
|
|
@@ -1074,7 +1074,8 @@ case class SessionWindowStateStoreSaveExec( | |
|
|
||
| override def keyExpressions: Seq[Attribute] = keyWithoutSessionExpressions | ||
|
|
||
| override def shortName: String = "sessionWindowStateStoreSaveExec" | ||
| override def shortName: String = | ||
| StatefulOperatorsUtils.SESSION_WINDOW_STATE_STORE_SAVE_EXEC_OP_NAME | ||
|
|
||
| private val stateManager = StreamingSessionWindowStateManager.createStateManager( | ||
| keyWithoutSessionExpressions, sessionExpression, child.output, stateFormatVersion) | ||
|
|
@@ -1395,7 +1396,7 @@ case class StreamingDeduplicateExec( | |
| removeKeysOlderThanWatermark(store) | ||
| } | ||
|
|
||
| override def shortName: String = "dedupe" | ||
| override def shortName: String = StatefulOperatorsUtils.DEDUPLICATE_EXEC_OP_NAME | ||
|
|
||
| override protected def withNewChildInternal(newChild: SparkPlan): StreamingDeduplicateExec = | ||
| copy(child = newChild) | ||
|
|
@@ -1416,6 +1417,12 @@ object StreamingDeduplicateExec { | |
| UnsafeProjection.create(Array[DataType](NullType)).apply(InternalRow.apply(null)) | ||
| } | ||
|
|
||
| /** | ||
| * For Deduplicate, the state key is the partition key i.e. the dedup key | ||
| */ | ||
| class StreamingDeduplicateStatePartitionKeyExtractor(stateKeySchema: StructType) | ||
| extends NoopStatePartitionKeyExtractor(stateKeySchema) | ||
|
|
||
| case class StreamingDeduplicateWithinWatermarkExec( | ||
| keyExpressions: Seq[Attribute], | ||
| child: SparkPlan, | ||
|
|
@@ -1478,7 +1485,7 @@ case class StreamingDeduplicateWithinWatermarkExec( | |
| } | ||
| } | ||
|
|
||
| override def shortName: String = "dedupeWithinWatermark" | ||
| override def shortName: String = StatefulOperatorsUtils.DEDUPLICATE_WITHIN_WATERMARK_EXEC_OP_NAME | ||
|
|
||
| override def validateAndMaybeEvolveStateSchema( | ||
| hadoopConf: Configuration, batchId: Long, stateSchemaVersion: Int): | ||
|
|
@@ -1494,6 +1501,12 @@ case class StreamingDeduplicateWithinWatermarkExec( | |
| newChild: SparkPlan): StreamingDeduplicateWithinWatermarkExec = copy(child = newChild) | ||
| } | ||
|
|
||
| /** | ||
| * For DeduplicateWithinWatermark, the state key is the partition key i.e. the dedup key | ||
| */ | ||
| class StreamingDedupWithinWatermarkStatePartitionKeyExtractor(stateKeySchema: StructType) | ||
| extends NoopStatePartitionKeyExtractor(stateKeySchema) | ||
|
|
||
| trait SchemaValidationUtils extends Logging { | ||
|
|
||
| // Determines whether the operator should be able to evolve their schema | ||
|
|
@@ -1561,4 +1574,14 @@ object StatefulOperatorsUtils { | |
| TRANSFORM_WITH_STATE_IN_PYSPARK_EXEC_OP_NAME | ||
| ) | ||
| val SYMMETRIC_HASH_JOIN_EXEC_OP_NAME = "symmetricHashJoin" | ||
| val STATE_STORE_SAVE_EXEC_OP_NAME = "stateStoreSave" | ||
| val DEDUPLICATE_EXEC_OP_NAME = "dedupe" | ||
| val DEDUPLICATE_WITHIN_WATERMARK_EXEC_OP_NAME = "dedupeWithinWatermark" | ||
| val SESSION_WINDOW_STATE_STORE_SAVE_EXEC_OP_NAME = "sessionWindowStateStoreSaveExec" | ||
| val FLAT_MAP_GROUPS_WITH_STATE_EXEC_OP_NAME = "flatMapGroupsWithState" | ||
| val FLAT_MAP_GROUPS_IN_PANDAS_WITH_STATE_EXEC_OP_NAME = "applyInPandasWithState" | ||
|
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. nit: should we rename as |
||
| val FLAT_MAP_GROUPS_OP_NAMES: Seq[String] = Seq( | ||
|
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. Ah TWS node names are already here - ok nice |
||
| FLAT_MAP_GROUPS_WITH_STATE_EXEC_OP_NAME, | ||
| FLAT_MAP_GROUPS_IN_PANDAS_WITH_STATE_EXEC_OP_NAME | ||
| ) | ||
| } | ||
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.
nice - good to move these to utils