-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-23052][SS] Migrate ConsoleSink to data source V2 api. #20243
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
Changes from all commits
3abe75c
71cc6e4
e3af17c
45df30a
7154f34
be880b1
e4c6429
fac17a4
8ce6f38
d7b4571
c0ec93f
c3abd70
516fd4a
7abd2b2
da13f37
a9d6b82
2916010
99109a4
278eeb4
f3c170e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,16 +54,13 @@ class ContinuousExecution( | |
sparkSession, name, checkpointRoot, analyzedPlan, sink, | ||
trigger, triggerClock, outputMode, deleteCheckpointOnStop) { | ||
|
||
@volatile protected var continuousSources: Seq[ContinuousReader] = _ | ||
@volatile protected var continuousSources: Seq[ContinuousReader] = Seq() | ||
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. why this change. is it related to this PR? 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. Yes. As mentioned in an earlier comment, initializing to null means the StreamingQueryException won't construct if it happens before sources are set. |
||
override protected def sources: Seq[BaseStreamingSource] = continuousSources | ||
|
||
// For use only in test harnesses. | ||
private[sql] var currentEpochCoordinatorId: String = _ | ||
|
||
override lazy val logicalPlan: LogicalPlan = { | ||
assert(queryExecutionThread eq Thread.currentThread, | ||
"logicalPlan must be initialized in StreamExecutionThread " + | ||
s"but the current thread was ${Thread.currentThread}") | ||
override val logicalPlan: LogicalPlan = { | ||
val toExecutionRelationMap = MutableMap[StreamingRelationV2, ContinuousExecutionRelation]() | ||
analyzedPlan.transform { | ||
case r @ StreamingRelationV2( | ||
|
@@ -72,7 +69,7 @@ class ContinuousExecution( | |
ContinuousExecutionRelation(source, extraReaderOptions, output)(sparkSession) | ||
}) | ||
case StreamingRelationV2(_, sourceName, _, _, _) => | ||
throw new AnalysisException( | ||
throw new UnsupportedOperationException( | ||
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. Why this change? An incorrect data source is not an operation. 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. I think there's an argument that it is - you're asking the data source (which is correct in the sense that it's a real, existing source) to do a type of read/write it doesn't support. The primary motivation is that the existing code has already made the choice to throw an UnsupportedOperationException when you try to stream from a source that only outputs in batch mode. |
||
s"Data source $sourceName does not support continuous processing.") | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.sources | ||
|
||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.sql.{Row, SparkSession} | ||
import org.apache.spark.sql.sources.v2.DataSourceV2Options | ||
import org.apache.spark.sql.sources.v2.writer.{DataSourceV2Writer, DataWriterFactory, WriterCommitMessage} | ||
import org.apache.spark.sql.types.StructType | ||
|
||
/** | ||
* A [[DataSourceV2Writer]] that collects results to the driver and prints them in the console. | ||
* Generated by [[org.apache.spark.sql.execution.streaming.ConsoleSinkProvider]]. | ||
* | ||
* This sink should not be used for production, as it requires sending all rows to the driver | ||
* and does not support recovery. | ||
*/ | ||
class ConsoleWriter(batchId: Long, schema: StructType, options: DataSourceV2Options) | ||
extends DataSourceV2Writer with Logging { | ||
// Number of rows to display, by default 20 rows | ||
private val numRowsToShow = options.getInt("numRows", 20) | ||
|
||
// Truncate the displayed data if it is too long, by default it is true | ||
private val isTruncated = options.getBoolean("truncate", true) | ||
|
||
assert(SparkSession.getActiveSession.isDefined) | ||
private val spark = SparkSession.getActiveSession.get | ||
|
||
override def createWriterFactory(): DataWriterFactory[Row] = PackedRowWriterFactory | ||
|
||
override def commit(messages: Array[WriterCommitMessage]): Unit = synchronized { | ||
val batch = messages.collect { | ||
case PackedRowCommitMessage(rows) => rows | ||
}.flatten | ||
|
||
// scalastyle:off println | ||
println("-------------------------------------------") | ||
println(s"Batch: $batchId") | ||
println("-------------------------------------------") | ||
// scalastyle:off println | ||
spark.createDataFrame( | ||
spark.sparkContext.parallelize(batch), schema) | ||
.show(numRowsToShow, isTruncated) | ||
} | ||
|
||
override def abort(messages: Array[WriterCommitMessage]): Unit = {} | ||
|
||
override def toString(): String = s"ConsoleWriter[numRows=$numRowsToShow, truncate=$isTruncated]" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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.sources | ||
|
||
import scala.collection.mutable | ||
|
||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.sql.Row | ||
import org.apache.spark.sql.sources.v2.writer.{DataWriter, DataWriterFactory, WriterCommitMessage} | ||
|
||
/** | ||
* A simple [[DataWriterFactory]] whose tasks just pack rows into the commit message for delivery | ||
* to a [[org.apache.spark.sql.sources.v2.writer.DataSourceV2Writer]] on the driver. | ||
* | ||
* Note that, because it sends all rows to the driver, this factory will generally be unsuitable | ||
* for production-quality sinks. It's intended for use in tests. | ||
*/ | ||
case object PackedRowWriterFactory extends DataWriterFactory[Row] { | ||
def createDataWriter(partitionId: Int, attemptNumber: Int): DataWriter[Row] = { | ||
new PackedRowDataWriter() | ||
} | ||
} | ||
|
||
/** | ||
* Commit message for a [[PackedRowDataWriter]], containing all the rows written in the most | ||
* recent interval. | ||
*/ | ||
case class PackedRowCommitMessage(rows: Array[Row]) extends WriterCommitMessage | ||
|
||
/** | ||
* A simple [[DataWriter]] that just sends all the rows it's received as a commit message. | ||
*/ | ||
class PackedRowDataWriter() extends DataWriter[Row] with Logging { | ||
private val data = mutable.Buffer[Row]() | ||
|
||
override def write(row: Row): Unit = data.append(row) | ||
|
||
override def commit(): PackedRowCommitMessage = { | ||
val msg = PackedRowCommitMessage(data.toArray) | ||
data.clear() | ||
msg | ||
} | ||
|
||
override def abort(): Unit = data.clear() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ import org.apache.spark.sql.execution.datasources.DataSource | |
import org.apache.spark.sql.execution.streaming._ | ||
import org.apache.spark.sql.execution.streaming.continuous.ContinuousTrigger | ||
import org.apache.spark.sql.execution.streaming.sources.{MemoryPlanV2, MemorySinkV2} | ||
import org.apache.spark.sql.sources.v2.streaming.ContinuousWriteSupport | ||
import org.apache.spark.sql.sources.v2.streaming.{ContinuousWriteSupport, MicroBatchWriteSupport} | ||
|
||
/** | ||
* Interface used to write a streaming `Dataset` to external storage systems (e.g. file systems, | ||
|
@@ -280,14 +280,12 @@ final class DataStreamWriter[T] private[sql](ds: Dataset[T]) { | |
useTempCheckpointLocation = true, | ||
trigger = trigger) | ||
} else { | ||
val sink = trigger match { | ||
case _: ContinuousTrigger => | ||
val ds = DataSource.lookupDataSource(source, df.sparkSession.sessionState.conf) | ||
ds.newInstance() match { | ||
case w: ContinuousWriteSupport => w | ||
case _ => throw new AnalysisException( | ||
s"Data source $source does not support continuous writing") | ||
} | ||
val ds = DataSource.lookupDataSource(source, df.sparkSession.sessionState.conf) | ||
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. We are checking for the same conditions here as well as in the StreamingQueryManager.createQuery. I think we need to refactor this, probably sometime in the future once we get rid of v1 completely. Either way, we should immediately add a general test suite (say StreamingDataSourceV2Suite) that tests these cases with various fake data sources. |
||
val sink = (ds.newInstance(), trigger) match { | ||
case (w: ContinuousWriteSupport, _: ContinuousTrigger) => w | ||
case (_, _: ContinuousTrigger) => throw new UnsupportedOperationException( | ||
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. AnalysisException. 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. as above |
||
s"Data source $source does not support continuous writing") | ||
case (w: MicroBatchWriteSupport, _) => w | ||
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. Isnt there a case where it does not have MicroBatchWriteSupport, but the trigger is ProcessingTime/OneTime? That should have a different error message. 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. In that case, we have to just fall back to the V1 path, because V1 sinks don't have MicroBatchWriteSupport. |
||
case _ => | ||
val ds = DataSource( | ||
df.sparkSession, | ||
|
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.
can you move this file into the sources subdirectory to make it consistent with other v2 sources?
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.
in fact this file can be merged into the ConsoleWriter.scala. The combined file will be named console.scala
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.
I can do this in a followup PR. It's not as simple as just moving it; we have to add an alias so that .format("org.apache.spark.sql.execution.streaming.ConsoleSinkProvider") continues to work.
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.
argh. okay. later then.