github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/kv/matcher_test.go (about) 1 // Copyright 2020 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 kv 15 16 import ( 17 "testing" 18 19 "github.com/pingcap/kvproto/pkg/cdcpb" 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestMatchRow(t *testing.T) { 24 t.Parallel() 25 matcher := newMatcher() 26 matcher.putPrewriteRow(&cdcpb.Event_Row{ 27 StartTs: 1, 28 Key: []byte("k1"), 29 Value: []byte("v1"), 30 }) 31 matcher.putPrewriteRow(&cdcpb.Event_Row{ 32 StartTs: 2, 33 Key: []byte("k1"), 34 Value: []byte("v2"), 35 OldValue: []byte("v3"), 36 }) 37 38 // test rollback 39 matcher.rollbackRow(&cdcpb.Event_Row{ 40 StartTs: 1, 41 Key: []byte("k1"), 42 }) 43 commitRow1 := &cdcpb.Event_Row{ 44 StartTs: 1, 45 Key: []byte("k1"), 46 } 47 ok := matcher.matchRow(commitRow1, true) 48 require.False(t, ok) 49 require.Equal(t, &cdcpb.Event_Row{ 50 StartTs: 1, 51 Key: []byte("k1"), 52 }, commitRow1) 53 54 // test match commit 55 commitRow2 := &cdcpb.Event_Row{ 56 StartTs: 2, 57 CommitTs: 3, 58 Key: []byte("k1"), 59 } 60 ok = matcher.matchRow(commitRow2, true) 61 require.True(t, ok) 62 require.Equal(t, &cdcpb.Event_Row{ 63 StartTs: 2, 64 CommitTs: 3, 65 Key: []byte("k1"), 66 Value: []byte("v2"), 67 OldValue: []byte("v3"), 68 }, commitRow2) 69 } 70 71 func TestMatchFakePrewrite(t *testing.T) { 72 t.Parallel() 73 matcher := newMatcher() 74 matcher.putPrewriteRow(&cdcpb.Event_Row{ 75 StartTs: 1, 76 Key: []byte("k1"), 77 Value: []byte("v1"), 78 OldValue: []byte("v3"), 79 }) 80 // fake prewrite 81 matcher.putPrewriteRow(&cdcpb.Event_Row{ 82 StartTs: 1, 83 Key: []byte("k1"), 84 OldValue: []byte("v4"), 85 }) 86 87 commitRow1 := &cdcpb.Event_Row{ 88 StartTs: 1, 89 CommitTs: 2, 90 Key: []byte("k1"), 91 } 92 ok := matcher.matchRow(commitRow1, true) 93 require.Equal(t, &cdcpb.Event_Row{ 94 StartTs: 1, 95 CommitTs: 2, 96 Key: []byte("k1"), 97 Value: []byte("v1"), 98 OldValue: []byte("v3"), 99 }, commitRow1) 100 require.True(t, ok) 101 } 102 103 func TestMatchRowUninitialized(t *testing.T) { 104 t.Parallel() 105 matcher := newMatcher() 106 107 // fake prewrite before init. 108 matcher.putPrewriteRow(&cdcpb.Event_Row{ 109 StartTs: 1, 110 Key: []byte("k1"), 111 OldValue: []byte("v4"), 112 }) 113 commitRow1 := &cdcpb.Event_Row{ 114 StartTs: 1, 115 CommitTs: 2, 116 Key: []byte("k1"), 117 } 118 ok := matcher.matchRow(commitRow1, false) 119 require.Equal(t, &cdcpb.Event_Row{ 120 StartTs: 1, 121 CommitTs: 2, 122 Key: []byte("k1"), 123 }, commitRow1) 124 require.False(t, ok) 125 matcher.cacheCommitRow(commitRow1) 126 127 // actual prewrite before init. 128 matcher.putPrewriteRow(&cdcpb.Event_Row{ 129 StartTs: 1, 130 Key: []byte("k1"), 131 Value: []byte("v3"), 132 OldValue: []byte("v4"), 133 }) 134 135 // normal prewrite and commit before init. 136 matcher.putPrewriteRow(&cdcpb.Event_Row{ 137 StartTs: 2, 138 Key: []byte("k2"), 139 Value: []byte("v3"), 140 OldValue: []byte("v4"), 141 }) 142 commitRow2 := &cdcpb.Event_Row{ 143 StartTs: 2, 144 CommitTs: 3, 145 Key: []byte("k2"), 146 } 147 ok = matcher.matchRow(commitRow2, false) 148 require.Equal(t, &cdcpb.Event_Row{ 149 StartTs: 2, 150 CommitTs: 3, 151 Key: []byte("k2"), 152 Value: []byte("v3"), 153 OldValue: []byte("v4"), 154 }, commitRow2) 155 require.True(t, ok) 156 157 // match cached row after init. 158 rows := matcher.matchCachedRow(true) 159 require.Len(t, rows, 1) 160 require.Equal(t, &cdcpb.Event_Row{ 161 StartTs: 1, 162 CommitTs: 2, 163 Key: []byte("k1"), 164 Value: []byte("v3"), 165 OldValue: []byte("v4"), 166 }, rows[0]) 167 } 168 169 func TestMatchMatchCachedRow(t *testing.T) { 170 t.Parallel() 171 matcher := newMatcher() 172 require.Equal(t, 0, len(matcher.matchCachedRow(true))) 173 matcher.cacheCommitRow(&cdcpb.Event_Row{ 174 StartTs: 1, 175 CommitTs: 2, 176 Key: []byte("k1"), 177 }) 178 matcher.cacheCommitRow(&cdcpb.Event_Row{ 179 StartTs: 3, 180 CommitTs: 4, 181 Key: []byte("k2"), 182 }) 183 matcher.cacheCommitRow(&cdcpb.Event_Row{ 184 StartTs: 4, 185 CommitTs: 5, 186 Key: []byte("k3"), 187 }) 188 require.Equal(t, 0, len(matcher.matchCachedRow(true))) 189 190 matcher.cacheCommitRow(&cdcpb.Event_Row{ 191 StartTs: 1, 192 CommitTs: 2, 193 Key: []byte("k1"), 194 }) 195 matcher.cacheCommitRow(&cdcpb.Event_Row{ 196 StartTs: 3, 197 CommitTs: 4, 198 Key: []byte("k2"), 199 }) 200 matcher.cacheCommitRow(&cdcpb.Event_Row{ 201 StartTs: 4, 202 CommitTs: 5, 203 Key: []byte("k3"), 204 }) 205 206 matcher.putPrewriteRow(&cdcpb.Event_Row{ 207 StartTs: 1, 208 Key: []byte("k1"), 209 Value: []byte("v1"), 210 OldValue: []byte("ov1"), 211 }) 212 matcher.putPrewriteRow(&cdcpb.Event_Row{ 213 StartTs: 3, 214 Key: []byte("k2"), 215 Value: []byte("v2"), 216 OldValue: []byte("ov2"), 217 }) 218 matcher.putPrewriteRow(&cdcpb.Event_Row{ 219 StartTs: 4, 220 Key: []byte("k2"), 221 Value: []byte("v3"), 222 OldValue: []byte("ov3"), 223 }) 224 225 require.Equal(t, []*cdcpb.Event_Row{{ 226 StartTs: 1, 227 CommitTs: 2, 228 Key: []byte("k1"), 229 Value: []byte("v1"), 230 OldValue: []byte("ov1"), 231 }, { 232 StartTs: 3, 233 CommitTs: 4, 234 Key: []byte("k2"), 235 Value: []byte("v2"), 236 OldValue: []byte("ov2"), 237 }}, matcher.matchCachedRow(true)) 238 } 239 240 func TestMatchMatchCachedRollbackRow(t *testing.T) { 241 t.Parallel() 242 matcher := newMatcher() 243 matcher.matchCachedRollbackRow(true) 244 matcher.cacheRollbackRow(&cdcpb.Event_Row{ 245 StartTs: 1, 246 Key: []byte("k1"), 247 }) 248 matcher.cacheRollbackRow(&cdcpb.Event_Row{ 249 StartTs: 3, 250 Key: []byte("k2"), 251 }) 252 matcher.cacheRollbackRow(&cdcpb.Event_Row{ 253 StartTs: 4, 254 Key: []byte("k3"), 255 }) 256 matcher.matchCachedRollbackRow(true) 257 258 matcher.cacheRollbackRow(&cdcpb.Event_Row{ 259 StartTs: 1, 260 Key: []byte("k1"), 261 }) 262 matcher.cacheRollbackRow(&cdcpb.Event_Row{ 263 StartTs: 3, 264 Key: []byte("k2"), 265 }) 266 matcher.cacheRollbackRow(&cdcpb.Event_Row{ 267 StartTs: 4, 268 Key: []byte("k3"), 269 }) 270 271 matcher.putPrewriteRow(&cdcpb.Event_Row{ 272 StartTs: 1, 273 Key: []byte("k1"), 274 Value: []byte("v1"), 275 OldValue: []byte("ov1"), 276 }) 277 matcher.putPrewriteRow(&cdcpb.Event_Row{ 278 StartTs: 3, 279 Key: []byte("k2"), 280 Value: []byte("v2"), 281 OldValue: []byte("ov2"), 282 }) 283 matcher.putPrewriteRow(&cdcpb.Event_Row{ 284 StartTs: 4, 285 Key: []byte("k3"), 286 Value: []byte("v3"), 287 OldValue: []byte("ov3"), 288 }) 289 290 matcher.matchCachedRollbackRow(true) 291 require.Empty(t, matcher.unmatchedValue) 292 }