-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-24525][SS] Provide an option to limit number of rows in a MemorySink #21559
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
Closed
Closed
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ac7eb2f
Add in logic to determine the max rows a sink can have
8dc89cc
Make MemorySink and MemorySinkV2 respect row and byte limits
8ddf566
Make tests compile
d82c7d5
Make microbatch memory writer work with limits
7fefe87
Test MemorySinkV2 with limits
58c5044
Add MemorySink test with limit
392f05f
rename method
9097dd5
Don't use byte limit, and log if we truncate rows
a28fb38
Update tests
f981cb8
minor refactor
74d5b6b
fixed indenting a bit
4ab9bda
make helper methods private
b2ef59c
Add additional safeguard that we don't call take() on a negative number
25d6de1
Move truncate method to parent class
e5b6175
Address Burak's comments
0402b60
fix documentation
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ import org.apache.spark.sql.catalyst.expressions.{Attribute, UnsafeRow} | |
| import org.apache.spark.sql.catalyst.plans.logical.{LeafNode, LogicalPlan, Statistics} | ||
| import org.apache.spark.sql.catalyst.plans.logical.statsEstimation.EstimationUtils | ||
| import org.apache.spark.sql.catalyst.streaming.InternalOutputModes._ | ||
| import org.apache.spark.sql.sources.v2.DataSourceOptions | ||
| import org.apache.spark.sql.sources.v2.reader.{InputPartition, InputPartitionReader, SupportsScanUnsafeRow} | ||
| import org.apache.spark.sql.sources.v2.reader.streaming.{MicroBatchReader, Offset => OffsetV2} | ||
| import org.apache.spark.sql.streaming.OutputMode | ||
|
|
@@ -221,26 +222,72 @@ class MemoryStreamInputPartition(records: Array[UnsafeRow]) | |
| } | ||
|
|
||
| /** A common trait for MemorySinks with methods used for testing */ | ||
| trait MemorySinkBase extends BaseStreamingSink { | ||
| trait MemorySinkBase extends BaseStreamingSink with Logging { | ||
| def allData: Seq[Row] | ||
| def latestBatchData: Seq[Row] | ||
| def dataSinceBatch(sinceBatchId: Long): Seq[Row] | ||
| def latestBatchId: Option[Long] | ||
|
|
||
| /** | ||
| * Truncates the given rows to return at most maxRows rows. | ||
| * @param rows The data that may need to be truncated. | ||
| * @param batchLimit Number of rows to keep in this batch; the rest will be truncated | ||
| * @param sinkLimit Total number of rows kept in this sink, for logging purposes. | ||
| * @param batchId The ID of the batch that sent these rows, for logging purposes. | ||
| * @return Truncated rows. | ||
| */ | ||
| protected def truncateRowsIfNeeded( | ||
| rows: Array[Row], | ||
| batchLimit: Int, | ||
| sinkLimit: Int, | ||
| batchId: Long): Array[Row] = { | ||
| if (rows.length > batchLimit && batchLimit >= 0) { | ||
| logWarning(s"Truncating batch $batchId to $batchLimit rows because of sink limit $sinkLimit") | ||
| rows.take(batchLimit) | ||
| } else { | ||
| rows | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Companion object to MemorySinkBase. | ||
| */ | ||
| object MemorySinkBase { | ||
| val MAX_MEMORY_SINK_ROWS = "maxRows" | ||
| val MAX_MEMORY_SINK_ROWS_DEFAULT = -1 | ||
|
|
||
| /** | ||
| * Gets the max number of rows a MemorySink should store. This number is based on the memory | ||
| * sink row limit if it is set. If not, there is no limit. | ||
| * @param options Options for writing from which we get the max rows option | ||
| * @return The maximum number of rows a memorySink should store, or None for no limit. | ||
|
||
| */ | ||
| def getMemorySinkCapacity(options: DataSourceOptions): Int = { | ||
| val maxRows = options.getInt(MAX_MEMORY_SINK_ROWS, MAX_MEMORY_SINK_ROWS_DEFAULT) | ||
| if (maxRows >= 0) maxRows else Int.MaxValue - 10 | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A sink that stores the results in memory. This [[Sink]] is primarily intended for use in unit | ||
| * tests and does not provide durability. | ||
| */ | ||
| class MemorySink(val schema: StructType, outputMode: OutputMode) extends Sink | ||
| with MemorySinkBase with Logging { | ||
| class MemorySink(val schema: StructType, outputMode: OutputMode, options: DataSourceOptions) | ||
| extends Sink with MemorySinkBase with Logging { | ||
|
|
||
| private case class AddedData(batchId: Long, data: Array[Row]) | ||
|
|
||
| /** An order list of batches that have been written to this [[Sink]]. */ | ||
| @GuardedBy("this") | ||
| private val batches = new ArrayBuffer[AddedData]() | ||
|
|
||
| /** The number of rows in this MemorySink. */ | ||
| private var numRows = 0 | ||
|
|
||
| /** The capacity in rows of this sink. */ | ||
| val sinkCapacity: Int = MemorySinkBase.getMemorySinkCapacity(options) | ||
|
|
||
| /** Returns all rows that are stored in this [[Sink]]. */ | ||
| def allData: Seq[Row] = synchronized { | ||
| batches.flatMap(_.data) | ||
|
|
@@ -273,14 +320,23 @@ class MemorySink(val schema: StructType, outputMode: OutputMode) extends Sink | |
| logDebug(s"Committing batch $batchId to $this") | ||
| outputMode match { | ||
| case Append | Update => | ||
| val rows = AddedData(batchId, data.collect()) | ||
| synchronized { batches += rows } | ||
| var rowsToAdd = data.collect() | ||
| synchronized { | ||
| rowsToAdd = | ||
| truncateRowsIfNeeded(rowsToAdd, sinkCapacity - numRows, sinkCapacity, batchId) | ||
| val rows = AddedData(batchId, rowsToAdd) | ||
| batches += rows | ||
| numRows += rowsToAdd.length | ||
| } | ||
|
|
||
| case Complete => | ||
| val rows = AddedData(batchId, data.collect()) | ||
| var rowsToAdd = data.collect() | ||
| synchronized { | ||
| rowsToAdd = truncateRowsIfNeeded(rowsToAdd, sinkCapacity, sinkCapacity, batchId) | ||
| val rows = AddedData(batchId, rowsToAdd) | ||
| batches.clear() | ||
| batches += rows | ||
| numRows = rowsToAdd.length | ||
| } | ||
|
|
||
| case _ => | ||
|
|
@@ -294,6 +350,7 @@ class MemorySink(val schema: StructType, outputMode: OutputMode) extends Sink | |
|
|
||
| def clear(): Unit = synchronized { | ||
| batches.clear() | ||
| numRows = 0 | ||
| } | ||
|
|
||
| override def toString(): String = "MemorySink" | ||
|
|
||
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
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.
nit: not sure if these sinks get used by Continuous processing too. If so I would rename
batchtotrigger version.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.
This piece is shared by MemorySink and MemorySinkV2, and the MemorySinkV2 (continuous processing) sink still calls them batches.