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