@@ -73,7 +73,7 @@ func createMysqlSuite(t *testing.T) *mysqlSuite {
73
73
cfg .Conflict .Strategy = config .ReplaceOnDup
74
74
cfg .Conflict .Threshold = math .MaxInt64
75
75
cfg .Conflict .MaxRecordRows = 100
76
- backendObj := tidb .NewTiDBBackend (context .Background (), db , cfg . Conflict , errormanager .New (nil , cfg , log .L ()))
76
+ backendObj := tidb .NewTiDBBackend (context .Background (), db , cfg , errormanager .New (nil , cfg , log .L ()))
77
77
return & mysqlSuite {
78
78
dbHandle : db ,
79
79
mockDB : mock ,
@@ -166,7 +166,7 @@ func TestWriteRowsIgnoreOnDup(t *testing.T) {
166
166
cfg .Conflict .Strategy = config .IgnoreOnDup
167
167
cfg .Conflict .Threshold = math .MaxInt64
168
168
cfg .Conflict .MaxRecordRows = 0
169
- ignoreBackend := tidb .NewTiDBBackend (ctx , s .dbHandle , cfg . Conflict , errormanager .New (nil , cfg , logger ))
169
+ ignoreBackend := tidb .NewTiDBBackend (ctx , s .dbHandle , cfg , errormanager .New (nil , cfg , logger ))
170
170
engine , err := backend .MakeEngineManager (ignoreBackend ).OpenEngine (ctx , & backend.EngineConfig {}, "`foo`.`bar`" , 1 )
171
171
require .NoError (t , err )
172
172
@@ -193,7 +193,7 @@ func TestWriteRowsIgnoreOnDup(t *testing.T) {
193
193
// test conflict.strategy == ignore and not 0 conflict.max-record-rows will use ErrorOnDup
194
194
195
195
cfg .Conflict .MaxRecordRows = 10
196
- ignoreBackend = tidb .NewTiDBBackend (ctx , s .dbHandle , cfg . Conflict , errormanager .New (nil , cfg , logger ))
196
+ ignoreBackend = tidb .NewTiDBBackend (ctx , s .dbHandle , cfg , errormanager .New (nil , cfg , logger ))
197
197
engine , err = backend .MakeEngineManager (ignoreBackend ).OpenEngine (ctx , & backend.EngineConfig {}, "`foo`.`bar`" , 1 )
198
198
require .NoError (t , err )
199
199
@@ -246,7 +246,7 @@ func TestWriteRowsErrorOnDup(t *testing.T) {
246
246
cfg .Conflict .Strategy = config .ErrorOnDup
247
247
cfg .Conflict .Threshold = math .MaxInt64
248
248
cfg .Conflict .MaxRecordRows = 0
249
- ignoreBackend := tidb .NewTiDBBackend (ctx , s .dbHandle , cfg . Conflict , errormanager .New (nil , cfg , logger ))
249
+ ignoreBackend := tidb .NewTiDBBackend (ctx , s .dbHandle , cfg , errormanager .New (nil , cfg , logger ))
250
250
engine , err := backend .MakeEngineManager (ignoreBackend ).OpenEngine (ctx , & backend.EngineConfig {}, "`foo`.`bar`" , 1 )
251
251
require .NoError (t , err )
252
252
@@ -536,9 +536,7 @@ func TestWriteRowsErrorNoRetry(t *testing.T) {
536
536
cfg .Conflict .Strategy = config .ErrorOnDup
537
537
cfg .Conflict .Threshold = 0
538
538
cfg .Conflict .MaxRecordRows = 0
539
- ignoreBackend := tidb .NewTiDBBackend (context .Background (), s .dbHandle , cfg .Conflict ,
540
- errormanager .New (s .dbHandle , cfg , log .L ()),
541
- )
539
+ ignoreBackend := tidb .NewTiDBBackend (context .Background (), s .dbHandle , cfg , errormanager .New (s .dbHandle , cfg , log .L ()))
542
540
encBuilder := tidb .NewEncodingBuilder ()
543
541
dataRows := encodeRowsTiDB (t , encBuilder , s .tbl )
544
542
ctx := context .Background ()
@@ -602,9 +600,7 @@ func TestWriteRowsErrorDowngradingAll(t *testing.T) {
602
600
cfg .Conflict .MaxRecordRows = 10
603
601
cfg .App .TaskInfoSchemaName = "tidb_lightning_errors"
604
602
cfg .App .MaxError .Type = * atomic .NewInt64 (10 )
605
- ignoreBackend := tidb .NewTiDBBackend (context .Background (), s .dbHandle , cfg .Conflict ,
606
- errormanager .New (s .dbHandle , cfg , log .L ()),
607
- )
603
+ ignoreBackend := tidb .NewTiDBBackend (context .Background (), s .dbHandle , cfg , errormanager .New (s .dbHandle , cfg , log .L ()))
608
604
encBuilder := tidb .NewEncodingBuilder ()
609
605
dataRows := encodeRowsTiDB (t , encBuilder , s .tbl )
610
606
ctx := context .Background ()
@@ -657,9 +653,7 @@ func TestWriteRowsErrorDowngradingExceedThreshold(t *testing.T) {
657
653
cfg .Conflict .MaxRecordRows = 10
658
654
cfg .App .TaskInfoSchemaName = "tidb_lightning_errors"
659
655
cfg .App .MaxError .Type = * atomic .NewInt64 (3 )
660
- ignoreBackend := tidb .NewTiDBBackend (context .Background (), s .dbHandle , cfg .Conflict ,
661
- errormanager .New (s .dbHandle , cfg , log .L ()),
662
- )
656
+ ignoreBackend := tidb .NewTiDBBackend (context .Background (), s .dbHandle , cfg , errormanager .New (s .dbHandle , cfg , log .L ()))
663
657
encBuilder := tidb .NewEncodingBuilder ()
664
658
dataRows := encodeRowsTiDB (t , encBuilder , s .tbl )
665
659
ctx := context .Background ()
@@ -699,9 +693,7 @@ func TestWriteRowsRecordOneError(t *testing.T) {
699
693
cfg .Conflict .Threshold = 0
700
694
cfg .Conflict .MaxRecordRows = 0
701
695
cfg .App .TaskInfoSchemaName = "tidb_lightning_errors"
702
- ignoreBackend := tidb .NewTiDBBackend (context .Background (), s .dbHandle , cfg .Conflict ,
703
- errormanager .New (s .dbHandle , cfg , log .L ()),
704
- )
696
+ ignoreBackend := tidb .NewTiDBBackend (context .Background (), s .dbHandle , cfg , errormanager .New (s .dbHandle , cfg , log .L ()))
705
697
encBuilder := tidb .NewEncodingBuilder ()
706
698
dataRows := encodeRowsTiDB (t , encBuilder , s .tbl )
707
699
ctx := context .Background ()
@@ -728,9 +720,7 @@ func TestDuplicateThreshold(t *testing.T) {
728
720
cfg .Conflict .Strategy = config .IgnoreOnDup
729
721
cfg .Conflict .Threshold = 5
730
722
cfg .Conflict .MaxRecordRows = 0
731
- ignoreBackend := tidb .NewTiDBBackend (context .Background (), s .dbHandle , cfg .Conflict ,
732
- errormanager .New (s .dbHandle , cfg , log .L ()),
733
- )
723
+ ignoreBackend := tidb .NewTiDBBackend (context .Background (), s .dbHandle , cfg , errormanager .New (s .dbHandle , cfg , log .L ()))
734
724
encBuilder := tidb .NewEncodingBuilder ()
735
725
dataRows := encodeRowsTiDB (t , encBuilder , s .tbl )
736
726
ctx := context .Background ()
@@ -851,3 +841,65 @@ func TestEncodeRowForRecord(t *testing.T) {
851
841
}, []int {0 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , 1 , 2 , 3 , - 1 , - 1 , - 1 })
852
842
require .Equal (t , row , "(5, \" test test\" , \x00 \x00 \x00 \xab \xcd \xef )" )
853
843
}
844
+
845
+ // TestLogicalImportBatch tests that each INSERT statement is limited by both
846
+ // logical-import-batch-size and logical-import-batch-rows configurations. Here
847
+ // we ensure each INSERT statement has up to 5 rows *and* ~30 bytes of values.
848
+ func TestLogicalImportBatch (t * testing.T ) {
849
+ s := createMysqlSuite (t )
850
+ defer s .TearDownTest (t )
851
+
852
+ s .mockDB .
853
+ ExpectExec ("\\ QINSERT INTO `foo`.`bar`(`a`) VALUES(1),(2),(4),(8),(16)\\ E" ).
854
+ WillReturnResult (sqlmock .NewResult (5 , 5 ))
855
+ s .mockDB .
856
+ ExpectExec ("\\ QINSERT INTO `foo`.`bar`(`a`) VALUES(32),(64),(128),(256),(512)\\ E" ).
857
+ WillReturnResult (sqlmock .NewResult (5 , 5 ))
858
+ s .mockDB .
859
+ ExpectExec ("\\ QINSERT INTO `foo`.`bar`(`a`) VALUES(1024),(2048),(4096),(8192)\\ E" ).
860
+ WillReturnResult (sqlmock .NewResult (4 , 4 ))
861
+ s .mockDB .
862
+ ExpectExec ("\\ QINSERT INTO `foo`.`bar`(`a`) VALUES(16384),(32768),(65536),(131072)\\ E" ).
863
+ WillReturnResult (sqlmock .NewResult (4 , 4 ))
864
+ s .mockDB .
865
+ ExpectExec ("\\ QINSERT INTO `foo`.`bar`(`a`) VALUES(262144)\\ E" ).
866
+ WillReturnResult (sqlmock .NewResult (1 , 1 ))
867
+
868
+ ctx := context .Background ()
869
+ logger := log .L ()
870
+
871
+ cfg := config .NewConfig ()
872
+ cfg .Conflict .Strategy = config .ErrorOnDup
873
+ cfg .TikvImporter .LogicalImportBatchSize = 30
874
+ cfg .TikvImporter .LogicalImportBatchRows = 5
875
+ ignoreBackend := tidb .NewTiDBBackend (ctx , s .dbHandle , cfg , errormanager .New (nil , cfg , logger ))
876
+ encBuilder := tidb .NewEncodingBuilder ()
877
+ encoder , err := encBuilder .NewEncoder (context .Background (), & encode.EncodingConfig {
878
+ Path : "1.csv" ,
879
+ Table : s .tbl ,
880
+ Logger : log .L (),
881
+ })
882
+ require .NoError (t , err )
883
+
884
+ dataRows := encBuilder .MakeEmptyRows ()
885
+ dataChecksum := verification .MakeKVChecksum (0 , 0 , 0 )
886
+ indexRows := encBuilder .MakeEmptyRows ()
887
+ indexChecksum := verification .MakeKVChecksum (0 , 0 , 0 )
888
+ for i := int64 (0 ); i < 19 ; i ++ { // encode rows 1, 2, 4, 8, ..., 262144.
889
+ row , err := encoder .Encode (
890
+ []types.Datum {types .NewIntDatum (1 << i )},
891
+ i ,
892
+ []int {0 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 },
893
+ 8 * i ,
894
+ )
895
+ require .NoError (t , err )
896
+ row .ClassifyAndAppend (& dataRows , & dataChecksum , & indexRows , & indexChecksum )
897
+ }
898
+
899
+ engine , err := backend .MakeEngineManager (ignoreBackend ).OpenEngine (ctx , & backend.EngineConfig {}, "`foo`.`bar`" , 1 )
900
+ require .NoError (t , err )
901
+ writer , err := engine .LocalWriter (ctx , & backend.LocalWriterConfig {TableName : "`foo`.`bar`" })
902
+ require .NoError (t , err )
903
+ err = writer .AppendRows (ctx , []string {"a" }, dataRows )
904
+ require .NoError (t , err )
905
+ }
0 commit comments