为提高效率,请提供以下信息,问题描述清晰能够更快得到解决:
[TiDB 版本]
5.7.25-TiDB-v4.0.7
schema
CREATE TABLE `tasks_queue` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`bucket` varchar(32) DEFAULT NULL,
`key` varchar(512) DEFAULT NULL,
`identity` varchar(255) DEFAULT NULL,
`length` bigint(20) DEFAULT NULL,
`bytesTransferred` bigint(20) DEFAULT NULL,
`state` enum('WAITING','RUNNING','SUCCESS','FAILED') DEFAULT NULL,
`host` varchar(255) DEFAULT NULL,
`errorMessage` longtext DEFAULT NULL,
`multiPartUploadParallelism` int(11) DEFAULT NULL,
`runCount` int(11) DEFAULT NULL,
`submitTime` timestamp NULL DEFAULT NULL,
`startTime` timestamp NULL DEFAULT NULL,
`finishTime` timestamp NULL DEFAULT NULL,
`heartbeatTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY (`bucket`,`key`)
) ;
SQL
# 50 个节点每隔 1s 执行以下 sql
START TRANSACTION;
SELECT *
FROM `tasks_queue`
WHERE `state` = 'WAITING'
OR (`state` = 'RUNNING' AND
TIME_TO_SEC(CURRENT_TIMESTAMP) - TIME_TO_SEC(`heartbeatTime`) > 60)
ORDER BY `submitTime`
LIMIT 1 FOR UPDATE;
UPDATE `tasks_queue` AS table1,
(SELECT count(*) AS c
FROM `tasks_queue`
WHERE `state` = 'RUNNING'
AND TIME_TO_SEC(`heartbeatTime`) - TIME_TO_SEC(`startTime`) <= 60
) AS table2
SET `state` = 'RUNNING',
`host` = #{task.host},
`heartbeatTime` = CURRENT_TIMESTAMP()
WHERE `updateTime` = #{task.updateTime}
AND `bucket` = #{task.bucket}
AND `key` = #{task.key}
AND table2.c < #{maxCount};
COMMIT;
Exception
org.apache.ibatis.exceptions.PersistenceException:
### Error committing transaction. Cause: java.sql.SQLException: Write conflict, txnStartTS=422372140194730836, conflictStartTS=422372140194729828, conflictCommitTS=422372140247157492, key={tableID=871, handle=5174890} primary={tableID=871, handle=5174890} [try again later]
### Cause: java.sql.SQLException: Write conflict, txnStartTS=422372140194730836, conflictStartTS=422372140194729828, conflictCommitTS=422372140247157492, key={tableID=871, handle=5174890} primary={tableID=871, handle=5174890} [try again later]
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:226)
at org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:217)
at com.zhihu.unionstore.task.mysql.ReadTasksThread.run(ReadTasksThread.java:50)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Caused by: java.sql.SQLException: Write conflict, txnStartTS=422372140194730836, conflictStartTS=422372140194729828, conflictCommitTS=422372140247157492, key={tableID=871, handle=5174890} primary={tableID=871, handle=5174890} [try again later]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3978)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3914)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2491)
at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1555)
at com.zaxxer.hikari.pool.ProxyConnection.commit(ProxyConnection.java:387)
at com.zaxxer.hikari.pool.HikariProxyConnection.commit(HikariProxyConnection.java)
at org.apache.ibatis.transaction.jdbc.JdbcTransaction.commit(JdbcTransaction.java:72)
at org.apache.ibatis.executor.BaseExecutor.commit(BaseExecutor.java:244)
at org.apache.ibatis.executor.CachingExecutor.commit(CachingExecutor.java:119)
at org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:223)
... 4 more