关注 spark技术分享,
撸spark源码 玩spark最佳实践

StateStoreRestoreExec Unary Physical Operator — Restoring State of Streaming Aggregates

StateStoreRestoreExec Unary Physical Operator — Restoring State of Streaming Aggregates

StateStoreRestoreExec is a unary physical operator that restores a state from a state store (for the keys from the child physical operator).

Note
A unary physical operator is a physical operator with a single child physical operator.

StateStoreRestoreExec is created exclusively when StatefulAggregationStrategy execution planning strategy is requested to plan a logical query plan with an Aggregate logical operator in a streaming structured query.

StateStoreRestoreExec StatefulAggregationStrategy.png
Figure 1. StateStoreRestoreExec and StatefulAggregationStrategy
Note

Aggregate logical operator is the result of:

  • RelationalGroupedDataset aggregations, i.e. agg and pivot operators

  • KeyValueGroupedDataset aggregations, i.e. mapGroups, flatMapGroups, mapGroupsWithState, flatMapGroupsWithState, reduceGroups, and agg, cogroup operators

  • SQL’s GROUP BY clause (possibly with WITH CUBE or WITH ROLLUP)

The optional property StatefulOperatorStateInfo is initially undefined (i.e. when StateStoreRestoreExec is created). StateStoreRestoreExec is updated to hold the streaming batch-specific execution property when IncrementalExecution prepares a streaming physical plan for execution (and state preparation rule is executed when StreamExecution plans a streaming query for a streaming batch).

StateStoreRestoreExec IncrementalExecution.png
Figure 2. StateStoreRestoreExec and IncrementalExecution

Table 1. StateStoreRestoreExec’s SQLMetrics
Key Name (in UI) Description

numOutputRows

number of output rows

The number of input rows from the child physical operator (for which StateStoreRestoreExec tried to find the state)

StateStoreRestoreExec webui query details.png
Figure 3. StateStoreRestoreExec in web UI (Details for Query)

When executed, StateStoreRestoreExec executes the child physical operator and creates a StateStoreRDD to map over partitions with storeUpdateFunction that restores the saved state for the keys in input rows if available.

The output schema of StateStoreRestoreExec is exactly the child‘s output schema.

The output partitioning of StateStoreRestoreExec is exactly the child‘s output partitioning.

StateStoreRestoreExec uses a StreamingAggregationStateManager (that is created for the keyExpressions, the output of the child physical operator and the stateFormatVersion).

Executing Physical Operator — doExecute Method

Note
doExecute is a part of SparkPlan contract to produce the result of a physical operator as an RDD of internal binary rows (i.e. InternalRow).

Internally, doExecute executes child physical operator and creates a StateStoreRDD with storeUpdateFunction that does the following per child operator’s RDD partition:

  1. Generates an unsafe projection to access the key field (using keyExpressions and the output schema of child operator).

  2. For every input row (as InternalRow)

    • Extracts the key from the row (using the unsafe projection above)

    • Gets the saved state in StateStore for the key if available (it might not be if the key appeared in the input the first time)

    • Increments numOutputRows metric (that in the end is the number of rows from the child operator)

    • Generates collection made up of the current row and possibly the state for the key if available

Note
The number of rows from StateStoreRestoreExec is the number of rows from the child operator with additional rows for the saved state.
Note
There is no way in StateStoreRestoreExec to find out how many rows had associated state available in a state store. You would have to use the corresponding StateStoreSaveExec operator’s metrics (most likely number of total state rows but that could depend on the output mode).

Creating StateStoreRestoreExec Instance

StateStoreRestoreExec takes the following to be created:

赞(0) 打赏
未经允许不得转载:spark技术分享 » StateStoreRestoreExec Unary Physical Operator — Restoring State of Streaming Aggregates
分享到: 更多 (0)

关注公众号:spark技术分享

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏