github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/cyclic/filter_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 cyclic
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/davecgh/go-spew/spew"
    20  	"github.com/pingcap/check"
    21  	"github.com/pingcap/ticdc/cdc/model"
    22  	"github.com/pingcap/ticdc/pkg/cyclic/mark"
    23  	"github.com/pingcap/ticdc/pkg/util/testleak"
    24  )
    25  
    26  type markSuite struct{}
    27  
    28  var _ = check.Suite(&markSuite{})
    29  
    30  func TestCyclic(t *testing.T) { check.TestingT(t) }
    31  
    32  func (s *markSuite) TestFilterAndReduceTxns(c *check.C) {
    33  	defer testleak.AfterTest(c)()
    34  	rID := mark.CyclicReplicaIDCol
    35  	testCases := []struct {
    36  		input     map[model.TableID][]*model.SingleTableTxn
    37  		output    map[model.TableID][]*model.SingleTableTxn
    38  		filterID  []uint64
    39  		replicaID uint64
    40  	}{
    41  		{
    42  			input:     map[model.TableID][]*model.SingleTableTxn{},
    43  			output:    map[model.TableID][]*model.SingleTableTxn{},
    44  			filterID:  []uint64{},
    45  			replicaID: 0,
    46  		},
    47  		{
    48  			input:     map[model.TableID][]*model.SingleTableTxn{1: {{Table: &model.TableName{Table: "a"}, StartTs: 1}}},
    49  			output:    map[model.TableID][]*model.SingleTableTxn{1: {{Table: &model.TableName{Table: "a"}, StartTs: 1, ReplicaID: 1}}},
    50  			filterID:  []uint64{},
    51  			replicaID: 1,
    52  		},
    53  		{
    54  			input: map[model.TableID][]*model.SingleTableTxn{
    55  				2: {
    56  					{
    57  						Table:   &model.TableName{Schema: "tidb_cdc"}, /* cyclic.SchemaName */
    58  						StartTs: 1,
    59  						Rows:    []*model.RowChangedEvent{{StartTs: 1, Columns: []*model.Column{{Name: rID, Value: uint64(10)}}}},
    60  					},
    61  				},
    62  			},
    63  			output:    map[model.TableID][]*model.SingleTableTxn{},
    64  			filterID:  []uint64{},
    65  			replicaID: 1,
    66  		},
    67  		{
    68  			input: map[model.TableID][]*model.SingleTableTxn{
    69  				1: {{Table: &model.TableName{Table: "a"}, StartTs: 1}},
    70  				2: {
    71  					{
    72  						Table:   &model.TableName{Schema: "tidb_cdc"}, /* cyclic.SchemaName */
    73  						StartTs: 1,
    74  						Rows:    []*model.RowChangedEvent{{StartTs: 1, Columns: []*model.Column{{Name: rID, Value: uint64(10)}}}},
    75  					},
    76  				},
    77  			},
    78  			output:    map[model.TableID][]*model.SingleTableTxn{},
    79  			filterID:  []uint64{10},
    80  			replicaID: 1,
    81  		},
    82  		{
    83  			input: map[model.TableID][]*model.SingleTableTxn{
    84  				1: {{Table: &model.TableName{Table: "a"}, StartTs: 1}},
    85  				2: {
    86  					{
    87  						Table:   &model.TableName{Schema: "tidb_cdc", Table: "1"},
    88  						StartTs: 1,
    89  						Rows:    []*model.RowChangedEvent{{StartTs: 1, Columns: []*model.Column{{Name: rID, Value: uint64(10)}}}},
    90  					},
    91  				},
    92  				3: {
    93  					{
    94  						Table:   &model.TableName{Schema: "tidb_cdc", Table: "2"},
    95  						StartTs: 2,
    96  						Rows:    []*model.RowChangedEvent{{StartTs: 2, Columns: []*model.Column{{Name: rID, Value: uint64(10)}}}},
    97  					},
    98  				},
    99  				4: {
   100  					{
   101  						Table:   &model.TableName{Schema: "tidb_cdc", Table: "3"},
   102  						StartTs: 3,
   103  						Rows:    []*model.RowChangedEvent{{StartTs: 3, Columns: []*model.Column{{Name: rID, Value: uint64(10)}}}},
   104  					},
   105  				},
   106  			},
   107  			output:    map[model.TableID][]*model.SingleTableTxn{},
   108  			filterID:  []uint64{10},
   109  			replicaID: 1,
   110  		},
   111  		{
   112  			input: map[model.TableID][]*model.SingleTableTxn{
   113  				1: {{Table: &model.TableName{Table: "a"}, StartTs: 1}},
   114  				2: {{Table: &model.TableName{Table: "b2"}, StartTs: 2}},
   115  				3: {{Table: &model.TableName{Table: "b2_1"}, StartTs: 2}},
   116  				4: {
   117  					{
   118  						Table:   &model.TableName{Schema: "tidb_cdc", Table: "1"},
   119  						StartTs: 1,
   120  						Rows:    []*model.RowChangedEvent{{StartTs: 1, Columns: []*model.Column{{Name: rID, Value: uint64(10)}}}},
   121  					},
   122  				},
   123  			},
   124  			output: map[model.TableID][]*model.SingleTableTxn{
   125  				2: {{Table: &model.TableName{Table: "b2"}, StartTs: 2, ReplicaID: 1}},
   126  				3: {{Table: &model.TableName{Table: "b2_1"}, StartTs: 2, ReplicaID: 1}},
   127  			},
   128  			filterID:  []uint64{10},
   129  			replicaID: 1,
   130  		},
   131  		{
   132  			input: map[model.TableID][]*model.SingleTableTxn{
   133  				1: {{Table: &model.TableName{Table: "a"}, StartTs: 1}},
   134  				2: {{Table: &model.TableName{Table: "b2"}, StartTs: 2}},
   135  				3: {{Table: &model.TableName{Table: "b2_1"}, StartTs: 2}},
   136  				4: {{Table: &model.TableName{Table: "b3"}, StartTs: 3}},
   137  				5: {{Table: &model.TableName{Table: "b3_1"}, StartTs: 3}},
   138  				6: {
   139  					{
   140  						Table:   &model.TableName{Schema: "tidb_cdc", Table: "1"},
   141  						StartTs: 2,
   142  						Rows:    []*model.RowChangedEvent{{StartTs: 2, Columns: []*model.Column{{Name: rID, Value: uint64(10)}}}},
   143  					},
   144  					{
   145  						Table:   &model.TableName{Schema: "tidb_cdc", Table: "1"},
   146  						StartTs: 3,
   147  						Rows:    []*model.RowChangedEvent{{StartTs: 3, Columns: []*model.Column{{Name: rID, Value: uint64(11)}}}},
   148  					},
   149  				},
   150  			},
   151  			output: map[model.TableID][]*model.SingleTableTxn{
   152  				1: {{Table: &model.TableName{Table: "a"}, StartTs: 1, ReplicaID: 1}},
   153  				4: {{Table: &model.TableName{Table: "b3"}, StartTs: 3, ReplicaID: 11}},
   154  				5: {{Table: &model.TableName{Table: "b3_1"}, StartTs: 3, ReplicaID: 11}},
   155  			},
   156  			filterID:  []uint64{10}, // 10 -> 2, filter start ts 2
   157  			replicaID: 1,
   158  		},
   159  		{
   160  			input: map[model.TableID][]*model.SingleTableTxn{
   161  				2: {{Table: &model.TableName{Table: "b2"}, StartTs: 2, CommitTs: 2}},
   162  				3: {
   163  					{Table: &model.TableName{Table: "b3"}, StartTs: 2, CommitTs: 2},
   164  					{Table: &model.TableName{Table: "b3"}, StartTs: 3, CommitTs: 3},
   165  					{Table: &model.TableName{Table: "b3"}, StartTs: 3, CommitTs: 3},
   166  					{Table: &model.TableName{Table: "b3"}, StartTs: 4, CommitTs: 4},
   167  				},
   168  				6: {
   169  					{
   170  						Table:   &model.TableName{Schema: "tidb_cdc", Table: "1"},
   171  						StartTs: 2,
   172  						Rows:    []*model.RowChangedEvent{{StartTs: 2, Columns: []*model.Column{{Name: rID, Value: uint64(10)}}}},
   173  					},
   174  					{
   175  						Table:   &model.TableName{Schema: "tidb_cdc", Table: "1"},
   176  						StartTs: 3,
   177  						Rows:    []*model.RowChangedEvent{{StartTs: 3, Columns: []*model.Column{{Name: rID, Value: uint64(11)}}}},
   178  					},
   179  				},
   180  			},
   181  			output: map[model.TableID][]*model.SingleTableTxn{
   182  				3: {
   183  					{Table: &model.TableName{Table: "b3"}, StartTs: 3, CommitTs: 3, ReplicaID: 11},
   184  					{Table: &model.TableName{Table: "b3"}, StartTs: 3, CommitTs: 3, ReplicaID: 11},
   185  					{Table: &model.TableName{Table: "b3"}, StartTs: 4, CommitTs: 4, ReplicaID: 1},
   186  				},
   187  			},
   188  			filterID:  []uint64{10}, // 10 -> 2, filter start ts 2
   189  			replicaID: 1,
   190  		},
   191  		{
   192  			input: map[model.TableID][]*model.SingleTableTxn{
   193  				2: {{Table: &model.TableName{Table: "b2"}, StartTs: 2}},
   194  				6: {
   195  					{
   196  						Table:   &model.TableName{Schema: "tidb_cdc", Table: "1"},
   197  						StartTs: 2,
   198  						Rows:    []*model.RowChangedEvent{{StartTs: 2, Columns: []*model.Column{{Name: rID, Value: uint64(10)}}}},
   199  					},
   200  					{
   201  						Table:   &model.TableName{Schema: "tidb_cdc", Table: "1"},
   202  						StartTs: 2,
   203  						Rows:    []*model.RowChangedEvent{{StartTs: 2, Columns: []*model.Column{{Name: rID, Value: uint64(10)}}}},
   204  					},
   205  				},
   206  			},
   207  			output:    map[model.TableID][]*model.SingleTableTxn{},
   208  			filterID:  []uint64{10}, // 10 -> 2, filter start ts 2
   209  			replicaID: 1,
   210  		},
   211  	}
   212  
   213  	for i, tc := range testCases {
   214  		FilterAndReduceTxns(tc.input, tc.filterID, tc.replicaID)
   215  		c.Assert(tc.input, check.DeepEquals, tc.output, check.Commentf("case %d %s\n", i, spew.Sdump(tc)))
   216  	}
   217  }