|
| 1 | +// Copyright 2022 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package ttlworker_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "math/rand" |
| 21 | + "sync" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/pingcap/tidb/pkg/parser/model" |
| 26 | + "github.com/pingcap/tidb/pkg/testkit" |
| 27 | + "github.com/pingcap/tidb/pkg/ttl/cache" |
| 28 | + "github.com/pingcap/tidb/pkg/ttl/ttlworker" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +func TestCancelWhileScan(t *testing.T) { |
| 33 | + store, dom := testkit.CreateMockStoreAndDomain(t) |
| 34 | + tk := testkit.NewTestKit(t, store) |
| 35 | + |
| 36 | + tk.MustExec("create table test.t (id int, created_at datetime) TTL= created_at + interval 1 hour") |
| 37 | + testTable, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t")) |
| 38 | + require.NoError(t, err) |
| 39 | + for i := 0; i < 10000; i++ { |
| 40 | + tk.MustExec(fmt.Sprintf("insert into test.t values (%d, NOW() - INTERVAL 24 HOUR)", i)) |
| 41 | + } |
| 42 | + testPhysicalTableCache, err := cache.NewPhysicalTable(model.NewCIStr("test"), testTable.Meta(), model.NewCIStr("")) |
| 43 | + require.NoError(t, err) |
| 44 | + |
| 45 | + delCh := make(chan *ttlworker.TTLDeleteTask) |
| 46 | + wg := &sync.WaitGroup{} |
| 47 | + wg.Add(1) |
| 48 | + go func() { |
| 49 | + defer wg.Done() |
| 50 | + for range delCh { |
| 51 | + // do nothing |
| 52 | + } |
| 53 | + }() |
| 54 | + |
| 55 | + testStart := time.Now() |
| 56 | + testDuration := time.Second |
| 57 | + for time.Since(testStart) < testDuration { |
| 58 | + ctx, cancel := context.WithCancel(context.Background()) |
| 59 | + ttlTask := ttlworker.NewTTLScanTask(ctx, testPhysicalTableCache, &cache.TTLTask{ |
| 60 | + JobID: "test", |
| 61 | + TableID: 1, |
| 62 | + ScanID: 1, |
| 63 | + ScanRangeStart: nil, |
| 64 | + ScanRangeEnd: nil, |
| 65 | + ExpireTime: time.Now().Add(-12 * time.Hour), |
| 66 | + OwnerID: "test", |
| 67 | + OwnerAddr: "test", |
| 68 | + OwnerHBTime: time.Now(), |
| 69 | + Status: cache.TaskStatusRunning, |
| 70 | + StatusUpdateTime: time.Now(), |
| 71 | + State: &cache.TTLTaskState{}, |
| 72 | + CreatedTime: time.Now(), |
| 73 | + }) |
| 74 | + |
| 75 | + wg := &sync.WaitGroup{} |
| 76 | + wg.Add(1) |
| 77 | + go func() { |
| 78 | + defer wg.Done() |
| 79 | + |
| 80 | + ttlTask.DoScan(ctx, delCh, dom.SysSessionPool()) |
| 81 | + }() |
| 82 | + |
| 83 | + // randomly sleep for a while and cancel the scan |
| 84 | + time.Sleep(time.Duration(rand.Int() % int(time.Millisecond*10))) |
| 85 | + startCancel := time.Now() |
| 86 | + cancel() |
| 87 | + |
| 88 | + wg.Wait() |
| 89 | + // make sure the scan is canceled in time |
| 90 | + require.Less(t, time.Since(startCancel), time.Second) |
| 91 | + } |
| 92 | + |
| 93 | + close(delCh) |
| 94 | + wg.Wait() |
| 95 | +} |
0 commit comments