@@ -1393,3 +1393,43 @@ func TestFinishError(t *testing.T) {
1393
1393
m .UpdateHeartBeat (context .Background (), se , now )
1394
1394
tk .MustQuery ("select count(*) from mysql.tidb_ttl_task" ).Check (testkit .Rows ("0" ))
1395
1395
}
1396
+
1397
+ func TestDisableTTLAfterLoseHeartbeat (t * testing.T ) {
1398
+ store , dom := testkit .CreateMockStoreAndDomain (t )
1399
+ waitAndStopTTLManager (t , dom )
1400
+ tk := testkit .NewTestKit (t , store )
1401
+
1402
+ sessionFactory := sessionFactory (t , store )
1403
+ se := sessionFactory ()
1404
+
1405
+ tk .MustExec ("use test" )
1406
+ tk .MustExec ("CREATE TABLE t (id INT PRIMARY KEY, created_at DATETIME) TTL = created_at + INTERVAL 1 HOUR" )
1407
+ testTable , err := dom .InfoSchema ().TableByName (model .NewCIStr ("test" ), model .NewCIStr ("t" ))
1408
+ require .NoError (t , err )
1409
+
1410
+ ctx := context .Background ()
1411
+ m1 := ttlworker .NewJobManager ("test-ttl-job-manager-1" , nil , store , nil , nil )
1412
+ require .NoError (t , m1 .InfoSchemaCache ().Update (se ))
1413
+ require .NoError (t , m1 .TableStatusCache ().Update (ctx , se ))
1414
+
1415
+ now := se .Now ()
1416
+ _ , err = m1 .LockJob (context .Background (), se , m1 .InfoSchemaCache ().Tables [testTable .Meta ().ID ], now , uuid .NewString (), false )
1417
+ require .NoError (t , err )
1418
+ tk .MustQuery ("select current_job_status from mysql.tidb_ttl_table_status" ).Check (testkit .Rows ("running" ))
1419
+
1420
+ // lose heartbeat. Simulate the situation that m1 doesn't update the hearbeat for 8 hours.
1421
+ now = now .Add (time .Hour * 8 )
1422
+
1423
+ // stop the tidb_ttl_job_enable
1424
+ tk .MustExec ("set global tidb_ttl_job_enable = 'OFF'" )
1425
+ defer tk .MustExec ("set global tidb_ttl_job_enable = 'ON'" )
1426
+
1427
+ // reschedule and try to get the job
1428
+ m2 := ttlworker .NewJobManager ("test-ttl-job-manager-2" , nil , store , nil , nil )
1429
+ require .NoError (t , m2 .InfoSchemaCache ().Update (se ))
1430
+ require .NoError (t , m2 .TableStatusCache ().Update (ctx , se ))
1431
+ m2 .RescheduleJobs (se , now )
1432
+
1433
+ // the job should have been cancelled
1434
+ tk .MustQuery ("select current_job_status from mysql.tidb_ttl_table_status" ).Check (testkit .Rows ("<nil>" ))
1435
+ }
0 commit comments