linhongliu-db commented on code in PR #59962:
URL: https://github.com/apache/spark/pull/59962#discussion_r4554422106
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -722,75 +772,89 @@ object JdbcUtils extends Logging with SQLConfHelper {
}
val supportsTransactions = finalIsolationLevel !=
Connection.TRANSACTION_NONE
var totalRowCount = 0L
+ def sourceHasNext: Boolean = {
+ try {
+ iterator.hasNext
+ } catch {
+ case e: SQLException =>
+ linkNextException(e)
+ throw e
+ }
+ }
+
+ def sourceNext(): Row = {
+ try {
+ iterator.next()
+ } catch {
+ case e: SQLException =>
+ linkNextException(e)
+ throw e
+ }
+ }
+
try {
- if (supportsTransactions) {
- conn.setAutoCommit(false) // Everything in the same db transaction.
- conn.setTransactionIsolation(finalIsolationLevel)
+ withWriteException {
+ if (supportsTransactions) {
+ conn.setAutoCommit(false) // Everything in the same db transaction.
+ conn.setTransactionIsolation(finalIsolationLevel)
+ }
}
- val stmt = conn.prepareStatement(insertStmt)
- val setters =
+ val stmt = withWriteException { conn.prepareStatement(insertStmt) }
+ val setters = withWriteException {
rddSchema.fields.map(f => makeSetter(conn, dialect, f.dataType,
options))
- val nullTypes = rddSchema.fields.map(f => getJdbcType(f.dataType,
dialect).jdbcNullType)
+ }
+ val nullTypes = withWriteException {
+ rddSchema.fields.map(f => getJdbcType(f.dataType,
dialect).jdbcNullType)
+ }
val numFields = rddSchema.fields.length
try {
var rowCount = 0
- stmt.setQueryTimeout(options.queryTimeout)
+ withWriteException { stmt.setQueryTimeout(options.queryTimeout) }
- while (iterator.hasNext) {
- val row = iterator.next()
- var i = 0
- while (i < numFields) {
- if (row.isNullAt(i)) {
- stmt.setNull(i + 1, nullTypes(i))
- } else {
- setters(i).apply(stmt, row, i)
+ while (sourceHasNext) {
+ val row = sourceNext()
+ try {
+ var i = 0
+ while (i < numFields) {
+ if (row.isNullAt(i)) {
+ stmt.setNull(i + 1, nullTypes(i))
+ } else {
+ setters(i).apply(stmt, row, i)
+ }
+ i = i + 1
}
- i = i + 1
+ stmt.addBatch()
+ } catch {
+ case e: SQLException => classifyWriteException(e)
}
- stmt.addBatch()
rowCount += 1
totalRowCount += 1
if (rowCount % batchSize == 0) {
// Hot spot for native blocking reads; TaskInterruptListener
(registered after
// opening the connection in this method) closes conn to unblock.
JDBC 4.0 section 9.6:
// methods on a closed Connection throw SQLException (expected for
major drivers).
// Mid-batch kill may drop the in-flight batch; still better than
hanging forever.
- stmt.executeBatch()
+ withWriteException { stmt.executeBatch() }
rowCount = 0
}
}
if (rowCount > 0) {
- stmt.executeBatch()
+ withWriteException { stmt.executeBatch() }
}
} finally {
- stmt.close()
+ // Cleanup must not replace a binding/batch failure or abort a pending
commit.
Review Comment:
Addressed: connection close on failure is now best-effort, with a regression.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -670,7 +670,57 @@ object JdbcUtils extends Logging with SQLConfHelper {
val outMetrics = TaskContext.get().taskMetrics().outputMetrics
- val conn = dialect.createConnectionFactory(options)(-1)
+ def linkNextException(e: SQLException): Unit = {
+ val cause = e.getNextException
+ if (cause != null && e.getCause != cause) {
+ // If there is no cause already, set 'next exception' as cause. If
cause is null,
+ // it *may* be because no cause was set yet
+ if (e.getCause == null) {
+ try {
+ e.initCause(cause)
+ } catch {
+ // Or it may be null because the cause *was* explicitly
initialized, to *null*,
+ // in which case this fails. There is no other way to detect it.
+ // addSuppressed in this case as well.
+ case _: IllegalStateException => e.addSuppressed(cause)
+ }
+ } else {
+ e.addSuppressed(cause)
+ }
+ }
+ }
+
+ def classifyWriteException(e: SQLException): Nothing = {
+ linkNextException(e)
+ // Closing the connection on task cancellation can cause a SQLException.
Do not
+ // report that cancellation side effect as a JDBC write failure.
+ if (TaskContext.get().isInterrupted() || e.isInstanceOf[SparkThrowable])
{
+ throw e
+ }
+ val parameters = Map("url" -> options.getRedactUrl(), "tableName" ->
table)
+ val classified = dialect.classifyException(
+ e,
+ condition = "FAILED_JDBC.WRITE",
+ messageParameters = parameters,
+ description = s"Write rows to the table $table.",
+ isRuntime = true)
+ // The legacy classifier ignores the supplied condition and uses a
placeholder URL.
+ // Normalize only its generic fallback so specific dialect mappings
still take precedence.
+ if (classified.getCondition == "FAILED_JDBC.UNCLASSIFIED") {
+ throw new SparkRuntimeException("FAILED_JDBC.WRITE", parameters, e)
+ }
+ throw classified
+ }
+
+ def withWriteException[T](f: => T): T = {
Review Comment:
Addressed: marked the v1 and v2 Spark-driver setup boundaries in code.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]