-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-29461][SQL] Measure the number of records being updated for JDBC writer #26109
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
11852c7
[SPARK-29461][SQL] Measure records being written for JDBC writer
HeartSaVioR 298d968
Fix compilation
HeartSaVioR 5f4c9e6
Fix UT: remove unnecessary registering dialect to fix missing unregister
HeartSaVioR 7bae87e
Reflect review comments
HeartSaVioR 620d111
Reflect missing review comment
HeartSaVioR 6e908d1
add explanation of policy around recording metrics
HeartSaVioR 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,10 +21,12 @@ import java.sql.DriverManager | |
| import java.util.Properties | ||
|
|
||
| import scala.collection.JavaConverters.propertiesAsScalaMapConverter | ||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
| import org.scalatest.BeforeAndAfter | ||
|
|
||
| import org.apache.spark.SparkException | ||
| import org.apache.spark.scheduler.{SparkListener, SparkListenerTaskEnd} | ||
| import org.apache.spark.sql.{AnalysisException, DataFrame, Row, SaveMode} | ||
| import org.apache.spark.sql.catalyst.parser.ParseException | ||
| import org.apache.spark.sql.execution.datasources.jdbc.{JDBCOptions, JdbcUtils} | ||
|
|
@@ -543,4 +545,57 @@ class JDBCWriteSuite extends SharedSparkSession with BeforeAndAfter { | |
| }.getMessage | ||
| assert(errMsg.contains("Statement was canceled or the session timed out")) | ||
| } | ||
|
|
||
| test("metrics") { | ||
| val df = spark.createDataFrame(sparkContext.parallelize(arr2x2), schema2) | ||
| val df2 = spark.createDataFrame(sparkContext.parallelize(arr1x2), schema2) | ||
|
|
||
| runAndVerifyRecordsWritten(2) { | ||
| df.write.mode(SaveMode.Append).jdbc(url, "TEST.BASICCREATETEST", new Properties()) | ||
| } | ||
|
|
||
| runAndVerifyRecordsWritten(1) { | ||
| df2.write.mode(SaveMode.Overwrite).jdbc(url, "TEST.BASICCREATETEST", new Properties()) | ||
| } | ||
|
|
||
| runAndVerifyRecordsWritten(1) { | ||
| df2.write.mode(SaveMode.Overwrite).option("truncate", true) | ||
| .jdbc(url, "TEST.BASICCREATETEST", new Properties()) | ||
| } | ||
|
|
||
| runAndVerifyRecordsWritten(0) { | ||
| intercept[AnalysisException] { | ||
| df2.write.mode(SaveMode.ErrorIfExists).jdbc(url, "TEST.BASICCREATETEST", new Properties()) | ||
| } | ||
| } | ||
|
|
||
| runAndVerifyRecordsWritten(0) { | ||
| df.write.mode(SaveMode.Ignore).jdbc(url, "TEST.BASICCREATETEST", new Properties()) | ||
| } | ||
| } | ||
|
|
||
| private def runAndVerifyRecordsWritten(expected: Long)(job: => Unit): Unit = { | ||
| assert(expected === runAndReturnMetrics(job, _.taskMetrics.outputMetrics.recordsWritten)) | ||
| } | ||
|
|
||
| private def runAndReturnMetrics(job: => Unit, collector: (SparkListenerTaskEnd) => Long): Long = { | ||
|
Contributor
Author
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. This is copied from InputOutputMetricsSuite - please let me know if it should be extracted with some utility class/object. |
||
| val taskMetrics = new ArrayBuffer[Long]() | ||
|
|
||
| // Avoid receiving earlier taskEnd events | ||
| sparkContext.listenerBus.waitUntilEmpty() | ||
|
|
||
| val listener = new SparkListener() { | ||
| override def onTaskEnd(taskEnd: SparkListenerTaskEnd): Unit = { | ||
| taskMetrics += collector(taskEnd) | ||
| } | ||
| } | ||
| sparkContext.addSparkListener(listener) | ||
|
|
||
| job | ||
|
|
||
| sparkContext.listenerBus.waitUntilEmpty() | ||
|
|
||
| sparkContext.removeSparkListener(listener) | ||
| taskMetrics.sum | ||
| } | ||
| } | ||
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.
We cannot move
rowCountoutsidetrythen just use it for the metric?Uh oh!
There was an error while loading. Please reload this page.
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.
It's used to determine whether it needs one more flush or not at the end of iterating. It can just be a boolean flag, but we should have one specific variable for taking this into account anyway.
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.
Ur, I see.
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 leave some comments somewhere about the policy to collect metrics?
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.
Just added it. 6e908d1
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.
Thanks!