github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/orchestrator/batch_test.go (about)

     1  // Copyright 2021 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 orchestrator
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  
    20  	"github.com/pingcap/tiflow/pkg/orchestrator/util"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestGetBatchChangeState(t *testing.T) {
    25  	t.Parallel()
    26  	patchGroupSize := 1000
    27  	patchGroup := make([][]DataPatch, patchGroupSize)
    28  	for i := 0; i < patchGroupSize; i++ {
    29  		i := i
    30  		patches := []DataPatch{&SingleDataPatch{
    31  			Key: util.NewEtcdKey(fmt.Sprintf("/key%d", i)),
    32  			Func: func(old []byte) (newValue []byte, changed bool, err error) {
    33  				newValue = []byte(fmt.Sprintf("abc%d", i))
    34  				return newValue, true, nil
    35  			},
    36  		}}
    37  		patchGroup[i] = patches
    38  	}
    39  	rawState := make(map[util.EtcdKey][]byte)
    40  	changedState, n, size, err := getBatchChangedState(rawState, patchGroup)
    41  	require.Nil(t, err)
    42  	require.LessOrEqual(t, n, len(patchGroup))
    43  	require.LessOrEqual(t, size, etcdTxnMaxSize)
    44  	require.LessOrEqual(t, len(changedState), etcdTxnMaxOps)
    45  	require.Equal(t, []byte(fmt.Sprintf("abc%d", 0)), changedState[util.NewEtcdKey("/key0")])
    46  
    47  	// test single patch exceed txn max size
    48  	largeSizePatches := []DataPatch{&SingleDataPatch{
    49  		Key: util.NewEtcdKey("largePatch"),
    50  		Func: func(old []byte) (newValue []byte, changed bool, err error) {
    51  			newValue = make([]byte, etcdTxnMaxSize)
    52  			return newValue, true, nil
    53  		},
    54  	}}
    55  	patchGroup = [][]DataPatch{largeSizePatches}
    56  	_, _, _, err = getBatchChangedState(rawState, patchGroup)
    57  	require.NotNil(t, err)
    58  	require.Contains(t, err.Error(), "a single changefeed exceed etcd txn max size")
    59  
    60  	// test single patch exceed txn max ops
    61  	manyOpsPatches := make([]DataPatch, 0)
    62  	for i := 0; i <= etcdTxnMaxOps*2; i++ {
    63  		manyOpsPatches = append(manyOpsPatches, &SingleDataPatch{
    64  			Key: util.NewEtcdKey(fmt.Sprintf("/key%d", i)),
    65  			Func: func(old []byte) (newValue []byte, changed bool, err error) {
    66  				newValue = []byte(fmt.Sprintf("abc%d", i))
    67  				return newValue, true, nil
    68  			},
    69  		})
    70  	}
    71  	patchGroup = [][]DataPatch{manyOpsPatches}
    72  	_, _, _, err = getBatchChangedState(rawState, patchGroup)
    73  	require.NotNil(t, err)
    74  	require.Contains(t, err.Error(), "a single changefeed exceed etcd txn max ops")
    75  }