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

StreamExecution — Base of Streaming Query Executions

StreamExecution — Base of Streaming Query Executions

StreamExecution is the base of streaming query executions that can execute the structured query continuously on a stream execution thread.

Note
Continuous query, streaming query, continuous Dataset, streaming Dataset are synonyms, and StreamExecution uses analyzed logical plan internally to refer to it.
Table 1. StreamExecution Contract
Property Description

logicalPlan

Analyzed logical plan of the streaming query

Note
logicalPlan is part of ProgressReporter Contract and the only purpose of the logicalPlan property is to change the access level from protected to public.

Used when StreamExecution is requested to run stream processing

runActivatedStream

Runs the activated streaming query

Used exclusively when StreamExecution is requested to run the streaming query (when transitioning from INITIALIZING to ACTIVE state)

Table 2. StreamExecutions
StreamExecution Description

ContinuousExecution

MicroBatchExecution

StreamExecution is the execution environment of a single continuous query (aka streaming Dataset) that is executed every trigger and in the end adds the results to a sink.

Note
StreamExecution corresponds to a single streaming query with one or more streaming sources and exactly one streaming sink.

StreamExecution creating instance.png
Figure 1. Creating Instance of StreamExecution
Note
DataStreamWriter describes how the results of executing batches of a streaming query are written to a streaming sink.

StreamExecution starts a thread of execution that runs the streaming query continuously and concurrently (and polls for new records in the streaming data sources to create a batch every trigger).

StreamExecution start.png
Figure 2. StreamExecution’s Starting Streaming Query (on Execution Thread)

StreamExecution can be in three states:

  • INITIALIZED when the instance was created.

  • ACTIVE when batches are pulled from the sources.

  • TERMINATED when executing streaming batches has been terminated due to an error, all batches were successfully processed or StreamExecution has been stopped.

StreamExecution is a ProgressReporter and reports status of the streaming query (i.e. when it starts, progresses and terminates) by posting StreamingQueryListener events.

StreamExecution tracks streaming data sources in uniqueSources internal registry.

StreamExecution uniqueSources.png
Figure 3. StreamExecution’s uniqueSources Registry of Streaming Data Sources

StreamExecution collects durationMs for the execution units of streaming batches.

StreamExecution durationMs.png
Figure 4. StreamExecution’s durationMs

StreamExecution uses OffsetSeqLog and BatchCommitLog metadata logs for write-ahead log (to record offsets to be processed) and that have already been processed and committed to a streaming sink, respectively.

Tip
Monitor offsets and commits metadata logs to know the progress of a streaming query.

StreamExecution delays polling for new data for 10 milliseconds (when no data was available to process in a batch). Use spark.sql.streaming.pollingDelay Spark property to control the delay.

Table 3. StreamExecution’s Internal Registries and Counters (in alphabetical order)
Name Description

availableOffsets

StreamProgress that tracks the offsets that are available to be processed, but have not yet be committed to the sink.

Note
availableOffsets is part of the ProgressReporter Contract.
Note
StreamProgress is an enhanced immutable.Map from Scala with streaming sources as keys and their Offsets as values.

Set when (in order):

  1. StreamExecution resumes and populates the start offsets with the latest offsets from the offset log that may have already been processed (and committed to the batch commit log so they are used as the current committed offsets)

  2. StreamExecution constructs the next streaming batch (and gets offsets from the sources)

Note

You can see availableOffsets in the DEBUG message in the logs when StreamExecution resumes and populates the start offsets.

Used when:

Note
availableOffsets works in tandem with committedOffsets registry.

awaitProgressLock

Java’s fair reentrant mutual exclusion java.util.concurrent.locks.ReentrantLock (that favors granting access to the longest-waiting thread under contention).

awaitProgressLockCondition

callSite

commitLog

CommitLog with commits metadata checkpoint directory for completed streaming batches (with a single file per batch with a file name being the batch id).

Note
Metadata log or metadata checkpoint are synonyms and are often used interchangeably.

Used exclusively by the extensions for the following:

committedOffsets

StreamProgress of the streaming sources and the committed offsets (i.e. processed already).

Note
committedOffsets is a part of ProgressReporter Contract.

currentBatchId

Current batch number

id

Unique identifier of the streaming query

Set as the id of streamMetadata when StreamExecution is created.

Note
id can get fetched from checkpoint metadata if available and thus recovered when a query is resumed (i.e. restarted after a failure or a planned stop).

initializationLatch

lastExecution

Last IncrementalExecution

newData

Registry of the streaming sources (in the logical query plan) that have new data available in the current batch. The new data is a streaming DataFrame.

Note
newData is part of the ProgressReporter Contract.

Set exclusively when StreamExecution is requested to requests unprocessed data from streaming sources (while running a single streaming batch).

noNewData

Flag whether there are any new offsets available for processing or not.

Turned on (i.e. enabled) when constructing the next streaming batch when no new offsets are available.

offsetLog

OffsetSeqLog with offsets metadata checkpoint directory for write-ahead log to record offsets in when ready for processing.

Note
Metadata log or metadata checkpoint are synonyms and are often used interchangeably.

Used when StreamExecution populates the start offsets and constructs the next streaming batch (first to store the current batch’s offsets in a write-ahead log and retrieve the previous batch’s offsets right afterwards).

Note
StreamExecution discards offsets from the offset metadata log when the current batch id is above spark.sql.streaming.minBatchesToRetain Spark property (which defaults to 100).

offsetSeqMetadata

Note
offsetSeqMetadata is a part of ProgressReporter Contract.
  • Initialized with 0 for batchWatermarkMs and batchTimestampMs when StreamExecution is created.

  • Updated with 0 for batchWatermarkMs and batchTimestampMs and SparkSession with spark.sql.adaptive.enabled disabled when StreamExecution runs streaming batches.

  • Used in…​FIXME

  • Copied with batchTimestampMs updated with the current time (in milliseconds) when StreamExecution constructs the next streaming batch.

pollingDelayMs

Time delay before polling new data again when no data was available

Set to spark.sql.streaming.pollingDelay Spark property.

Used when StreamExecution has started running streaming batches (and no data was available to process in a trigger).

prettyIdString

Pretty-identified string for identification in logs (with name if defined).

resolvedCheckpointRoot

Qualified path of the checkpoint directory (as defined using checkpointRoot when StreamExecution is created).

Note

checkpointRoot is defined using checkpointLocation option or spark.sql.streaming.checkpointLocation Spark property with queryName option.

checkpointLocation and queryName options are defined when StreamingQueryManager creates a streaming query.

Used when creating the path to the checkpoint directory and when StreamExecution finishes running streaming batches.

Used for logicalPlan (while transforming analyzedPlan and planning StreamingRelation logical operators to corresponding StreamingExecutionRelation physical operators with the streaming data sources created passing in the path to sources directory to store checkpointing metadata).

Note

You can see resolvedCheckpointRoot in the INFO message when StreamExecution is started.

Internally, resolvedCheckpointRoot creates a Hadoop org.apache.hadoop.fs.Path for checkpointRoot and makes it qualified.

Note
resolvedCheckpointRoot uses SparkSession to access SessionState for a Hadoop configuration.

runId

Current run id

sources

All streaming Sources in logical query plan (that are the sources from StreamingExecutionRelation).

startLatch

Java’s java.util.concurrent.CountDownLatch with count 1.

Used when StreamExecution is requested to start to pause the main thread until StreamExecution was requested to run the streaming query.

state

Java’s java.util.concurrent.atomic.AtomicReference for the three different states a streaming query execution can be:

  • INITIALIZING (default)

  • ACTIVE (after the first execution of runBatches)

  • TERMINATED

streamDeathCause

StreamingQueryException

streamMetadata

StreamMetadata from the metadata file from checkpoint directory. If the metadata file is not available it is created (with a new random id).

uniqueSources

Unique streaming data sources in a streaming Dataset (after being collected as StreamingExecutionRelation from the corresponding logical query plan).

Note
StreamingExecutionRelation is a leaf logical operator (i.e. LogicalPlan) that represents a streaming data source (and corresponds to a single StreamingRelation in analyzed logical query plan of a streaming Dataset).

Used when StreamExecution:

Tip

Enable INFO or DEBUG logging levels for org.apache.spark.sql.execution.streaming.StreamExecution to see what happens inside.

Add the following line to conf/log4j.properties:

Refer to Logging.

stop Method

Caution
FIXME

stopSources Internal Method

Caution
FIXME

Running Stream Processing — runStream Internal Method

runBatches runs streaming batches of data (that are datasets from every streaming source).

Internally, runBatches assigns the group id (to all the Spark jobs started by this thread) as runId (with the group description to display in web UI as getBatchDescriptionString and interruptOnCancel flag enabled).

Note

runBatches uses SparkSession to access SparkContext and assign the group id.

You can find the details on SparkContext.setJobGroup method in the Mastering Apache Spark 2 gitbook.

runBatches sets a local property sql.streaming.queryId as id.

runBatches registers a metric source when spark.sql.streaming.metricsEnabled property is enabled (which is disabled by default).

Caution
FIXME Metrics

runBatches notifies StreamingQueryListeners that a streaming query has been started (by posting a QueryStartedEvent with id, runId and name).

StreamingQueryListener onQueryStarted.png
Figure 5. StreamingQueryListener Notified about Query’s Start (onQueryStarted)

runBatches unblocks the main starting thread (by decrementing the count of startLatch that goes to 0 and lets the starting thread continue).

Caution
FIXME A picture with two parallel lanes for the starting thread and daemon one for the query.

runBatches updates the status message to Initializing sources followed by initialization of the logical plan (of the streaming Dataset).

runBatches disables adaptive query execution (using spark.sql.adaptive.enabled property which is disabled by default) as it could change the number of shuffle partitions.

runBatches initializes offsetSeqMetadata internal variable.

runBatches sets state to ACTIVE (only when the current state is INITIALIZING that prevents from repeating the initialization)

Note
runBatches does the work only when first started (i.e. when state is INITIALIZING).

runBatches decrements the count of initializationLatch.

Caution
FIXME initializationLatch so what?

Once TriggerExecutor has finished executing batches, runBatches updates the status message to Stopped.

Note
TriggerExecutor finishes executing batches when batch runner returns whether the streaming query is stopped or not (which is when the internal state is not TERMINATED).
Caution
FIXME Describe catch block for exception handling
Caution
FIXME Describe finally block for query termination
Note
runStream is used exclusively when the stream execution thread is requested to start.

TriggerExecutor’s Batch Runner

Batch Runner (aka batchRunner) is an executable block executed by TriggerExecutor in runBatches.

As long as the query is not stopped (i.e. state is not TERMINATED), batchRunner executes the streaming batch for the trigger.

In triggerExecution time-tracking section, runBatches branches off per currentBatchId.

Table 4. Current Batch Execution per currentBatchId
currentBatchId < 0 currentBatchId >= 0
  1. populateStartOffsets

  2. Setting Job Description as getBatchDescriptionString

1. Constructing the next streaming batch

If there is data available in the sources, batchRunner marks currentStatus with isDataAvailable enabled.

Note

You can check out the status of a streaming query using status method.

batchRunner then updates the status message to Processing new data and runs the current streaming batch.

StreamExecution runBatches.png
Figure 6. StreamExecution’s Running Batches (on Execution Thread)

After triggerExecution section has finished, batchRunner finishes the streaming batch for the trigger (and collects query execution statistics).

When there was data available in the sources, batchRunner updates committed offsets (by adding the current batch id to BatchCommitLog and adding availableOffsets to committedOffsets).

You should see the following DEBUG message in the logs:

batchRunner increments the current batch id and sets the job description for all the following Spark jobs to include the new batch id.

When no data was available in the sources to process, batchRunner does the following:

  1. Marks currentStatus with isDataAvailable disabled

  2. Updates the status message to Waiting for data to arrive

  3. Sleeps the current thread for pollingDelayMs milliseconds.

batchRunner updates the status message to Waiting for next trigger and returns whether the query is currently active or not (so TriggerExecutor can decide whether to finish executing the batches or not)

getBatchDescriptionString Internal Method

Caution
FIXME

toDebugString Internal Method

toDebugString…​FIXME

Note
toDebugString is used exclusively when StreamExecution is requested to run stream processing (and a streaming query terminated with an exception).

Starting Streaming Query (on Stream Execution Thread) — start Method

When called, start prints out the following INFO message to the logs:

start then starts the queryExecutionThread as a daemon thread.

Note
start uses Java’s java.lang.Thread.start to run the streaming query on a separate execution thread.
Note
When started, a streaming query runs in its own execution thread on JVM.

In the end, start pauses the main thread (using the startLatch until StreamExecution was requested to run the streaming query).

Note
start is used exclusively when StreamingQueryManager is requested to start a streaming query.

Creating StreamExecution Instance

StreamExecution takes the following when created:

  • SparkSession

  • Query name

  • Path of the checkpoint directory (aka metadata directory)

  • Analyzed logical query plan (i.e. LogicalPlan)

  • Streaming sink

  • Trigger

  • Clock

  • Output mode (that is only used when creating IncrementalExecution for a streaming batch in query planning)

  • deleteCheckpointOnStop flag to control whether to delete the checkpoint directory on stop

StreamExecution initializes the internal registries and counters.

Note
StreamExecution is a Scala abstract class and cannot be created directly. It is created indirectly when the concrete StreamExecutions are.

Creating Path to Checkpoint Directory — checkpointFile Internal Method

checkpointFile gives the path of a directory with name in checkpoint directory.

Note
checkpointFile uses Hadoop’s org.apache.hadoop.fs.Path.
Note
checkpointFile is used for streamMetadata, OffsetSeqLog, BatchCommitLog, and lastExecution (for runBatch).

Posting StreamingQueryListener Event — postEvent Method

Note
postEvent is a part of ProgressReporter Contract.

postEvent simply requests the StreamingQueryManager to post the input event (to the StreamingQueryListenerBus in the current SparkSession).

Note
postEvent uses SparkSession to access the current StreamingQueryManager.
Note

postEvent is used when:

Waiting Until No Data Available in Sources or Query Has Been Terminated — processAllAvailable Method

Note
processAllAvailable is a part of StreamingQuery Contract.

processAllAvailable reports streamDeathCause exception if defined (and returns).

Note
streamDeathCause is defined exclusively when StreamExecution runs streaming batches (and terminated with an exception).

processAllAvailable returns when isActive flag is turned off (which is when StreamExecution is in TERMINATED state).

processAllAvailable acquires a lock on awaitProgressLock and turns noNewData flag off.

processAllAvailable keeps waiting 10 seconds for awaitProgressLockCondition until noNewData flag is turned on or StreamExecution is no longer active.

Note
noNewData flag is turned on exclusively when StreamExecution constructs the next streaming batch (and finds that no data is available).

In the end, processAllAvailable releases awaitProgressLock lock.

Stream Execution Thread — queryExecutionThread Property

queryExecutionThread is a Java thread of execution (java.util.Thread) that runs the structured query when started.

queryExecutionThread uses the name stream execution thread for [id] (that uses prettyIdString for the id, i.e. queryName [id = [id], runId = [runId]]).

queryExecutionThread is a QueryExecutionThread (that is a Spark UninterruptibleThread with runUninterruptibly method for running a block of code without being interrupted by Thread.interrupt()).

queryExecutionThread is started (as a daemon thread) when StreamExecution is requested to start.

When started, queryExecutionThread sets the thread-local properties as the call site and runs the streaming query.

Tip

Use Java’s jconsole or jstack to monitor the streaming threads.

赞(0) 打赏
未经允许不得转载:spark技术分享 » StreamExecution — Base of Streaming Query Executions
分享到: 更多 (0)

关注公众号:spark技术分享

联系我们联系我们

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

支付宝扫一扫打赏

微信扫一扫打赏