|
| 1 | +// Copyright 2026 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 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package logpuller |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/pingcap/ticdc/heartbeatpb" |
| 22 | + "github.com/pingcap/ticdc/logservice/logpuller/regionlock" |
| 23 | + "github.com/pingcap/ticdc/pkg/common" |
| 24 | + "github.com/pingcap/ticdc/pkg/pdutil" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + "github.com/tikv/client-go/v2/tikv" |
| 27 | +) |
| 28 | + |
| 29 | +func TestScheduleRegionRequestUpdatesRuntimeRegistry(t *testing.T) { |
| 30 | + client := &subscriptionClient{ |
| 31 | + regionTaskQueue: NewPriorityQueue(), |
| 32 | + regionRuntimeRegistry: newRegionRuntimeRegistry(), |
| 33 | + pdClock: pdutil.NewClock4Test(), |
| 34 | + } |
| 35 | + |
| 36 | + rawSpan := heartbeatpb.TableSpan{ |
| 37 | + TableID: 1, |
| 38 | + StartKey: []byte{'a'}, |
| 39 | + EndKey: []byte{'z'}, |
| 40 | + } |
| 41 | + consumeKVEvents := func(_ []common.RawKVEntry, _ func()) bool { return false } |
| 42 | + advanceResolvedTs := func(uint64) {} |
| 43 | + subSpan := client.newSubscribedSpan(SubscriptionID(1), rawSpan, 100, consumeKVEvents, advanceResolvedTs, 0, false) |
| 44 | + |
| 45 | + regionSpan := heartbeatpb.TableSpan{ |
| 46 | + TableID: 1, |
| 47 | + StartKey: []byte{'b'}, |
| 48 | + EndKey: []byte{'c'}, |
| 49 | + } |
| 50 | + region := newRegionInfo(tikv.NewRegionVerID(10, 1, 1), regionSpan, nil, subSpan, false) |
| 51 | + |
| 52 | + client.scheduleRegionRequest(context.Background(), region, TaskLowPrior) |
| 53 | + |
| 54 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 55 | + defer cancel() |
| 56 | + task, err := client.regionTaskQueue.Pop(ctx) |
| 57 | + require.NoError(t, err) |
| 58 | + queued := task.GetRegionInfo() |
| 59 | + require.True(t, queued.runtimeKey.isValid()) |
| 60 | + |
| 61 | + state, ok := client.regionRuntimeRegistry.get(queued.runtimeKey) |
| 62 | + require.True(t, ok) |
| 63 | + require.Equal(t, regionPhaseQueued, state.phase) |
| 64 | + require.Equal(t, uint64(10), state.verID.GetID()) |
| 65 | + require.False(t, state.rangeLockAcquiredTime.IsZero()) |
| 66 | +} |
| 67 | + |
| 68 | +func TestOnRegionFailUpdatesRuntimeRegistry(t *testing.T) { |
| 69 | + client := &subscriptionClient{ |
| 70 | + regionRuntimeRegistry: newRegionRuntimeRegistry(), |
| 71 | + errCache: newErrCache(), |
| 72 | + } |
| 73 | + client.ctx, client.cancel = context.WithCancel(context.Background()) |
| 74 | + defer client.cancel() |
| 75 | + |
| 76 | + rawSpan := heartbeatpb.TableSpan{ |
| 77 | + TableID: 1, |
| 78 | + StartKey: []byte{'a'}, |
| 79 | + EndKey: []byte{'z'}, |
| 80 | + } |
| 81 | + consumeKVEvents := func(_ []common.RawKVEntry, _ func()) bool { return false } |
| 82 | + advanceResolvedTs := func(uint64) {} |
| 83 | + subSpan := client.newSubscribedSpan(SubscriptionID(1), rawSpan, 100, consumeKVEvents, advanceResolvedTs, 0, false) |
| 84 | + |
| 85 | + lockRes := subSpan.rangeLock.LockRange(context.Background(), []byte{'b'}, []byte{'c'}, 10, 1) |
| 86 | + require.Equal(t, regionlock.LockRangeStatusSuccess, lockRes.Status) |
| 87 | + |
| 88 | + region := newRegionInfo(tikv.NewRegionVerID(10, 1, 1), heartbeatpb.TableSpan{ |
| 89 | + TableID: 1, |
| 90 | + StartKey: []byte{'b'}, |
| 91 | + EndKey: []byte{'c'}, |
| 92 | + }, nil, subSpan, false) |
| 93 | + region.lockedRangeState = lockRes.LockedRangeState |
| 94 | + |
| 95 | + client.ensureRegionRuntime(®ion, time.Now()) |
| 96 | + require.True(t, region.runtimeKey.isValid()) |
| 97 | + |
| 98 | + client.onRegionFail(newRegionErrorInfo(region, &sendRequestToStoreErr{})) |
| 99 | + |
| 100 | + state, ok := client.regionRuntimeRegistry.get(region.runtimeKey) |
| 101 | + require.True(t, ok) |
| 102 | + require.Equal(t, regionPhaseRetryPending, state.phase) |
| 103 | + require.Equal(t, "send request to store error", state.lastError) |
| 104 | + require.Equal(t, 1, state.retryCount) |
| 105 | +} |
| 106 | + |
| 107 | +func TestHandleResolvedTsUpdatesRuntimeRegistry(t *testing.T) { |
| 108 | + client := &subscriptionClient{ |
| 109 | + regionRuntimeRegistry: newRegionRuntimeRegistry(), |
| 110 | + } |
| 111 | + worker := ®ionRequestWorker{client: client} |
| 112 | + |
| 113 | + rawSpan := heartbeatpb.TableSpan{ |
| 114 | + TableID: 1, |
| 115 | + StartKey: []byte{'a'}, |
| 116 | + EndKey: []byte{'z'}, |
| 117 | + } |
| 118 | + subSpan := &subscribedSpan{ |
| 119 | + subID: SubscriptionID(1), |
| 120 | + startTs: 100, |
| 121 | + span: rawSpan, |
| 122 | + rangeLock: regionlock.NewRangeLock(1, rawSpan.StartKey, rawSpan.EndKey, 100), |
| 123 | + filterLoop: false, |
| 124 | + } |
| 125 | + subSpan.resolvedTs.Store(100) |
| 126 | + subSpan.resolvedTsUpdated.Store(time.Now().Unix()) |
| 127 | + subSpan.advanceInterval = 0 |
| 128 | + |
| 129 | + lockRes := subSpan.rangeLock.LockRange(context.Background(), rawSpan.StartKey, rawSpan.EndKey, 10, 1) |
| 130 | + require.Equal(t, regionlock.LockRangeStatusSuccess, lockRes.Status) |
| 131 | + lockRes.LockedRangeState.Initialized.Store(true) |
| 132 | + |
| 133 | + region := newRegionInfo(tikv.NewRegionVerID(10, 1, 1), rawSpan, nil, subSpan, false) |
| 134 | + region.lockedRangeState = lockRes.LockedRangeState |
| 135 | + region.runtimeKey = client.regionRuntimeRegistry.allocKey(subSpan.subID, region.verID.GetID()) |
| 136 | + client.regionRuntimeRegistry.updateRegionInfo(region.runtimeKey, region) |
| 137 | + |
| 138 | + state := newRegionFeedState(region, uint64(subSpan.subID), worker) |
| 139 | + state.start() |
| 140 | + |
| 141 | + resolvedTs := uint64(200) |
| 142 | + handleResolvedTs(subSpan, state, resolvedTs) |
| 143 | + |
| 144 | + stored, ok := client.regionRuntimeRegistry.get(region.runtimeKey) |
| 145 | + require.True(t, ok) |
| 146 | + require.Equal(t, resolvedTs, stored.lastResolvedTs) |
| 147 | + require.False(t, stored.lastEventTime.IsZero()) |
| 148 | +} |
0 commit comments