pySpark 无法执行保存操作,因为 select 正在锁定 table

pySpark can't perform save operation because select is locking the table

我们经常从 select 到 table,当我们尝试使用 override 选项保存 dataFrame 时,我们收到如下错误:

py4j.protocol.Py4JJavaError: An error occurred while calling o364.save.
: com.mysql.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2730)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1618)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1549)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.truncateTable(JdbcUtils.scala:113)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:56)
at org.apache.spark.sql.execution.datasources.SaveIntoDataSourceCommand.run(SaveIntoDataSourceCommand.scala:45)
at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult$lzycompute(commands.scala:70)
at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult(commands.scala:68)

我确定此问题与 wating_timeoutinterative_timeout 无关。但不幸的是,有很多事情会导致这个问题。问题是当我们试图截断 table 时,有 select 查询锁定了 table.

目前,可以通过手动从数据库中删除会话来修复它,但这不是我想要的解决方案。也许有人知道我可以做些什么来避免这种情况?

经过调查,我发现这个问题本身很愚蠢,所以有时我们会同时使用 TRUNCATE 进行 SELECT 查询,然后两个查询都会与 wait metadata lock 语句叠加。并且这种情况的超时默认设置为50秒。