github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/service/service_cn_handler_test.go (about) 1 // Copyright 2021 - 2022 Matrix Origin 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package service 16 17 import ( 18 "context" 19 "math" 20 "os" 21 "sync/atomic" 22 "testing" 23 "time" 24 25 "github.com/matrixorigin/matrixone/pkg/clusterservice" 26 27 "github.com/matrixorigin/matrixone/pkg/common/moerr" 28 "github.com/matrixorigin/matrixone/pkg/common/morpc" 29 "github.com/matrixorigin/matrixone/pkg/common/runtime" 30 "github.com/matrixorigin/matrixone/pkg/lockservice" 31 "github.com/matrixorigin/matrixone/pkg/logutil" 32 "github.com/matrixorigin/matrixone/pkg/pb/lock" 33 "github.com/matrixorigin/matrixone/pkg/pb/metadata" 34 "github.com/matrixorigin/matrixone/pkg/pb/timestamp" 35 "github.com/matrixorigin/matrixone/pkg/pb/txn" 36 "github.com/matrixorigin/matrixone/pkg/txn/rpc" 37 "github.com/matrixorigin/matrixone/pkg/txn/storage/mem" 38 "github.com/stretchr/testify/assert" 39 "github.com/stretchr/testify/require" 40 "go.uber.org/zap" 41 "go.uber.org/zap/zapcore" 42 ) 43 44 func TestReadBasic(t *testing.T) { 45 sender := NewTestSender() 46 defer func() { 47 assert.NoError(t, sender.Close()) 48 }() 49 50 s := NewTestTxnService(t, 1, sender, NewTestClock(0)) 51 assert.NoError(t, s.Start()) 52 defer func() { 53 assert.NoError(t, s.Close(false)) 54 }() 55 56 sender.AddTxnService(s) 57 58 rTxn := NewTestTxn(1, 1) 59 resp := readTestData(t, sender, 1, rTxn, 1) 60 checkReadResponses(t, resp, "") 61 } 62 63 func TestReadWithTNShardNotMatch(t *testing.T) { 64 sender := NewTestSender() 65 defer func() { 66 assert.NoError(t, sender.Close()) 67 }() 68 sender.setFilter(func(req *txn.TxnRequest) bool { 69 req.CNRequest.Target.ReplicaID = 0 70 return true 71 }) 72 73 s := NewTestTxnService(t, 1, sender, NewTestClock(0)) 74 assert.NoError(t, s.Start()) 75 defer func() { 76 assert.NoError(t, s.Close(false)) 77 }() 78 79 sender.AddTxnService(s) 80 81 rTxn := NewTestTxn(1, 1) 82 resp := readTestData(t, sender, 1, rTxn, 1) 83 checkResponses(t, resp, 84 txn.WrapError(moerr.NewTNShardNotFound(context.TODO(), "", 1), 0)) 85 } 86 87 func TestReadWithSelfWrite(t *testing.T) { 88 sender := NewTestSender() 89 defer func() { 90 assert.NoError(t, sender.Close()) 91 }() 92 93 s := NewTestTxnService(t, 1, sender, NewTestClock(0)) 94 assert.NoError(t, s.Start()) 95 defer func() { 96 assert.NoError(t, s.Close(false)) 97 }() 98 sender.AddTxnService(s) 99 100 rwTxn := NewTestTxn(1, 1) 101 checkResponses(t, writeTestData(t, sender, 1, rwTxn, 1)) 102 checkReadResponses(t, readTestData(t, sender, 1, rwTxn, 1), "1-1-1") 103 checkResponses(t, writeTestData(t, sender, 1, rwTxn, 2)) 104 checkReadResponses(t, readTestData(t, sender, 1, rwTxn, 2), "2-1-1") 105 } 106 107 func TestReadBlockWithClock(t *testing.T) { 108 sender := NewTestSender() 109 defer func() { 110 assert.NoError(t, sender.Close()) 111 }() 112 113 ts := int64(0) 114 s := NewTestTxnService(t, 1, sender, NewTestSpecClock(func() int64 { 115 return atomic.AddInt64(&ts, 1) 116 })) 117 assert.NoError(t, s.Start()) 118 defer func() { 119 assert.NoError(t, s.Close(false)) 120 }() 121 sender.AddTxnService(s) 122 123 c := make(chan struct{}) 124 go func() { 125 rwTxn := NewTestTxn(1, 3) 126 checkReadResponses(t, readTestData(t, sender, 1, rwTxn, 1), "") 127 c <- struct{}{} 128 }() 129 <-c 130 assert.Equal(t, int64(3), ts) 131 } 132 133 func TestReadCannotBlockByUncommitted(t *testing.T) { 134 sender := NewTestSender() 135 defer func() { 136 assert.NoError(t, sender.Close()) 137 }() 138 139 s := NewTestTxnService(t, 1, sender, NewTestClock(1)) 140 assert.NoError(t, s.Start()) 141 defer func() { 142 assert.NoError(t, s.Close(false)) 143 }() 144 sender.AddTxnService(s) 145 146 wTxn := NewTestTxn(1, 1) 147 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 148 149 rTxn := NewTestTxn(2, 1) 150 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 1), "") 151 } 152 153 func TestReadCannotBlockByPreparedIfSnapshotTSIsLEPreparedTS(t *testing.T) { 154 sender := NewTestSender() 155 defer func() { 156 assert.NoError(t, sender.Close()) 157 }() 158 159 s := NewTestTxnService(t, 1, sender, NewTestClock(1)) 160 assert.NoError(t, s.Start()) 161 defer func() { 162 assert.NoError(t, s.Close(false)) 163 }() 164 sender.AddTxnService(s) 165 166 wTxn := NewTestTxn(1, 1) 167 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 168 prepareTestTxn(t, sender, wTxn, 1) // prepare at 2 169 170 rTxn := NewTestTxn(2, 1) 171 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 1), "") 172 173 rTxn = NewTestTxn(2, 2) 174 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 1), "") 175 } 176 177 func TestReadWillBlockByPreparedIfSnapshotTSIsGTPreparedTS(t *testing.T) { 178 sender := NewTestSender() 179 defer func() { 180 assert.NoError(t, sender.Close()) 181 }() 182 183 s := NewTestTxnService(t, 1, sender, NewTestClock(1)) 184 assert.NoError(t, s.Start()) 185 defer func() { 186 assert.NoError(t, s.Close(false)) 187 }() 188 sender.AddTxnService(s) 189 190 wTxn := NewTestTxn(1, 1) 191 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 192 prepareTestTxn(t, sender, wTxn, 1) // prepare at 2 193 194 c := make(chan struct{}) 195 go func() { 196 rTxn := NewTestTxn(2, 3) 197 readTestData(t, sender, 1, rTxn, 1) 198 close(c) 199 }() 200 select { 201 case <-c: 202 assert.Fail(t, "cannot read") 203 case <-time.After(time.Second): 204 205 } 206 } 207 208 func TestReadAfterBlockTxnCommitted(t *testing.T) { 209 sender := NewTestSender() 210 defer func() { 211 assert.NoError(t, sender.Close()) 212 }() 213 214 s := NewTestTxnService(t, 1, sender, NewTestClock(1)) 215 assert.NoError(t, s.Start()) 216 defer func() { 217 assert.NoError(t, s.Close(false)) 218 }() 219 sender.AddTxnService(s) 220 221 wTxn := NewTestTxn(1, 1, 1) 222 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 223 prepareTestTxn(t, sender, wTxn, 1) // prepare at 2 224 225 c := make(chan struct{}) 226 go func() { 227 rTxn := NewTestTxn(2, 3) 228 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 1), "1-1-1") 229 close(c) 230 }() 231 go func() { 232 time.Sleep(time.Second) 233 wTxn.CommitTS = NewTestTimestamp(2) // commit at 2 234 checkResponses(t, commitShardWriteData(t, sender, wTxn)) 235 }() 236 237 select { 238 case <-c: 239 case <-time.After(time.Minute): 240 assert.Fail(t, "cannot read") 241 } 242 } 243 244 func TestReadAfterBlockTxnCommittedAndCannotReadCommittedValue(t *testing.T) { 245 sender := NewTestSender() 246 defer func() { 247 assert.NoError(t, sender.Close()) 248 }() 249 250 s := NewTestTxnService(t, 1, sender, NewTestClock(1)) 251 assert.NoError(t, s.Start()) 252 defer func() { 253 assert.NoError(t, s.Close(false)) 254 }() 255 sender.AddTxnService(s) 256 257 wTxn := NewTestTxn(1, 1, 1) 258 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 259 prepareTestTxn(t, sender, wTxn, 1) // prepare at 2 260 261 c := make(chan struct{}) 262 go func() { 263 rTxn := NewTestTxn(2, 3) 264 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 1), "") 265 close(c) 266 }() 267 go func() { 268 time.Sleep(time.Second) 269 wTxn.CommitTS = NewTestTimestamp(3) // commit at 3 270 checkResponses(t, commitShardWriteData(t, sender, wTxn)) 271 }() 272 273 select { 274 case <-c: 275 case <-time.After(time.Minute): 276 assert.Fail(t, "cannot read") 277 } 278 } 279 280 func TestReadAfterBlockTxnAborted(t *testing.T) { 281 sender := NewTestSender() 282 defer func() { 283 assert.NoError(t, sender.Close()) 284 }() 285 286 s := NewTestTxnService(t, 1, sender, NewTestClock(1)) 287 assert.NoError(t, s.Start()) 288 defer func() { 289 assert.NoError(t, s.Close(false)) 290 }() 291 sender.AddTxnService(s) 292 293 wTxn := NewTestTxn(1, 1, 1) 294 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 295 prepareTestTxn(t, sender, wTxn, 1) // prepare at 2 296 297 c := make(chan struct{}) 298 go func() { 299 rTxn := NewTestTxn(2, 3) 300 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 1), "") 301 close(c) 302 }() 303 go func() { 304 time.Sleep(time.Second) 305 checkResponses(t, rollbackShardWriteData(t, sender, wTxn)) 306 }() 307 308 select { 309 case <-c: 310 case <-time.After(time.Minute): 311 assert.Fail(t, "cannot read") 312 } 313 } 314 315 func TestReadCannotBlockByCommittingIfSnapshotTSIsLECommitTS(t *testing.T) { 316 sender := NewTestSender() 317 defer func() { 318 assert.NoError(t, sender.Close()) 319 }() 320 321 s := NewTestTxnService(t, 1, sender, NewTestClock(1)).(*service) 322 assert.NoError(t, s.Start()) 323 defer func() { 324 assert.NoError(t, s.Close(false)) 325 }() 326 sender.AddTxnService(s) 327 328 wTxn := NewTestTxn(1, 1) 329 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 330 prepareTestTxn(t, sender, wTxn, 1) // prepare at 2 331 332 wTxn.CommitTS = NewTestTimestamp(2) 333 assert.NoError(t, s.storage.(*mem.KVTxnStorage).Committing(context.TODO(), wTxn)) 334 335 rTxn := NewTestTxn(2, 1) 336 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 1), "") 337 338 rTxn = NewTestTxn(2, 2) 339 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 1), "") 340 } 341 342 func TestReadWillBlockByCommittingIfSnapshotTSIsGTCommitTS(t *testing.T) { 343 sender := NewTestSender() 344 defer func() { 345 assert.NoError(t, sender.Close()) 346 }() 347 348 s := NewTestTxnService(t, 1, sender, NewTestClock(1)).(*service) 349 assert.NoError(t, s.Start()) 350 defer func() { 351 assert.NoError(t, s.Close(false)) 352 }() 353 sender.AddTxnService(s) 354 355 wTxn := NewTestTxn(1, 1) 356 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 357 prepareTestTxn(t, sender, wTxn, 1) // prepare at 2 358 359 wTxn.CommitTS = NewTestTimestamp(2) 360 assert.NoError(t, s.storage.(*mem.KVTxnStorage).Committing(context.TODO(), wTxn)) 361 362 c := make(chan struct{}) 363 go func() { 364 rTxn := NewTestTxn(2, 3) 365 readTestData(t, sender, 1, rTxn, 1) 366 close(c) 367 }() 368 select { 369 case <-c: 370 assert.Fail(t, "cannot read") 371 case <-time.After(time.Second): 372 373 } 374 } 375 376 func TestReadCommitted(t *testing.T) { 377 sender := NewTestSender() 378 defer func() { 379 assert.NoError(t, sender.Close()) 380 }() 381 382 s := NewTestTxnService(t, 1, sender, NewTestClock(1)) 383 assert.NoError(t, s.Start()) 384 defer func() { 385 assert.NoError(t, s.Close(false)) 386 }() 387 sender.AddTxnService(s) 388 389 wTxn1 := NewTestTxn(1, 1, 1) // commit at 2 390 checkResponses(t, writeTestData(t, sender, 1, wTxn1, 1)) 391 checkResponses(t, commitWriteData(t, sender, wTxn1)) 392 393 wTxn2 := NewTestTxn(2, 1, 1) // commit at 3 394 checkResponses(t, writeTestData(t, sender, 1, wTxn2, 2)) 395 checkResponses(t, commitWriteData(t, sender, wTxn2)) 396 397 rTxn := NewTestTxn(3, 2) 398 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 1), "") 399 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 2), "") 400 401 rTxn = NewTestTxn(3, 3) 402 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 1), string(GetTestValue(1, wTxn1))) 403 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 2), "") 404 405 rTxn = NewTestTxn(3, 4) 406 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 1), string(GetTestValue(1, wTxn1))) 407 checkReadResponses(t, readTestData(t, sender, 1, rTxn, 2), string(GetTestValue(2, wTxn2))) 408 } 409 410 func TestWriteBasic(t *testing.T) { 411 sender := NewTestSender() 412 defer func() { 413 assert.NoError(t, sender.Close()) 414 }() 415 416 s := NewTestTxnService(t, 1, sender, NewTestClock(0)).(*service) 417 assert.NoError(t, s.Start()) 418 defer func() { 419 assert.NoError(t, s.Close(false)) 420 }() 421 422 sender.AddTxnService(s) 423 424 wTxn := NewTestTxn(1, 1) 425 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 426 427 kv := s.storage.(*mem.KVTxnStorage).GetUncommittedKV() 428 v, ok := kv.Get(GetTestKey(1)) 429 assert.True(t, ok) 430 assert.Equal(t, GetTestValue(1, wTxn), v) 431 } 432 433 func TestWriteWithTNShardNotMatch(t *testing.T) { 434 sender := NewTestSender() 435 defer func() { 436 assert.NoError(t, sender.Close()) 437 }() 438 sender.setFilter(func(req *txn.TxnRequest) bool { 439 req.CNRequest.Target.ReplicaID = 0 440 return true 441 }) 442 443 s := NewTestTxnService(t, 1, sender, NewTestClock(0)).(*service) 444 assert.NoError(t, s.Start()) 445 defer func() { 446 assert.NoError(t, s.Close(false)) 447 }() 448 449 sender.AddTxnService(s) 450 451 wTxn := NewTestTxn(1, 1) 452 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1), 453 txn.WrapError(moerr.NewTNShardNotFound(context.TODO(), "", 1), 0)) 454 } 455 456 func TestWriteWithWWConflict(t *testing.T) { 457 sender := NewTestSender() 458 defer func() { 459 assert.NoError(t, sender.Close()) 460 }() 461 462 s := NewTestTxnService(t, 1, sender, NewTestClock(0)) 463 assert.NoError(t, s.Start()) 464 defer func() { 465 assert.NoError(t, s.Close(false)) 466 }() 467 468 sender.AddTxnService(s) 469 470 wTxn := NewTestTxn(1, 1) 471 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 472 473 wTxn2 := NewTestTxn(2, 1) 474 checkResponses(t, writeTestData(t, sender, 1, wTxn2, 1), 475 txn.WrapError(moerr.NewTAEWrite(context.TODO()), 0)) 476 } 477 478 func TestCommitWithSingleTNShard(t *testing.T) { 479 sender := NewTestSender() 480 defer func() { 481 assert.NoError(t, sender.Close()) 482 }() 483 484 s := NewTestTxnService(t, 1, sender, NewTestClock(1)).(*service) 485 assert.NoError(t, s.Start()) 486 defer func() { 487 assert.NoError(t, s.Close(false)) 488 }() 489 490 sender.AddTxnService(s) 491 492 n := byte(10) 493 wTxn := NewTestTxn(1, 1, 1) 494 for i := byte(0); i < n; i++ { 495 checkResponses(t, writeTestData(t, sender, 1, wTxn, i)) 496 } 497 checkResponses(t, commitWriteData(t, sender, wTxn)) 498 499 for i := byte(0); i < n; i++ { 500 var values [][]byte 501 var timestamps []timestamp.Timestamp 502 kv := s.storage.(*mem.KVTxnStorage).GetCommittedKV() 503 504 kv.AscendRange(GetTestKey(i), NewTestTimestamp(0), NewTestTimestamp(math.MaxInt64), func(value []byte, ts timestamp.Timestamp) { 505 values = append(values, value) 506 timestamps = append(timestamps, ts) 507 }) 508 assert.Equal(t, [][]byte{GetTestValue(i, wTxn)}, values) 509 assert.Equal(t, []timestamp.Timestamp{NewTestTimestamp(2)}, timestamps) 510 } 511 } 512 513 func TestCommitWithTNShardNotMatch(t *testing.T) { 514 sender := NewTestSender() 515 defer func() { 516 assert.NoError(t, sender.Close()) 517 }() 518 sender.setFilter(func(req *txn.TxnRequest) bool { 519 if req.CommitRequest != nil { 520 req.Txn.TNShards[0].ReplicaID = 0 521 } 522 return true 523 }) 524 525 s := NewTestTxnService(t, 1, sender, NewTestClock(1)).(*service) 526 assert.NoError(t, s.Start()) 527 defer func() { 528 assert.NoError(t, s.Close(false)) 529 }() 530 531 sender.AddTxnService(s) 532 533 n := byte(10) 534 wTxn := NewTestTxn(1, 1, 1) 535 for i := byte(0); i < n; i++ { 536 checkResponses(t, writeTestData(t, sender, 1, wTxn, i)) 537 } 538 checkResponses(t, commitWriteData(t, sender, wTxn), 539 txn.WrapError(moerr.NewTNShardNotFound(context.TODO(), "", 1), 0)) 540 } 541 542 func TestCommitWithMultiTNShards(t *testing.T) { 543 sender := NewTestSender() 544 defer func() { 545 assert.NoError(t, sender.Close()) 546 }() 547 548 s1 := NewTestTxnService(t, 1, sender, NewTestClock(1)).(*service) 549 assert.NoError(t, s1.Start()) 550 defer func() { 551 assert.NoError(t, s1.Close(false)) 552 }() 553 s2 := NewTestTxnService(t, 2, sender, NewTestClock(1)).(*service) 554 assert.NoError(t, s2.Start()) 555 defer func() { 556 assert.NoError(t, s2.Close(false)) 557 }() 558 559 sender.AddTxnService(s1) 560 sender.AddTxnService(s2) 561 562 wTxn := NewTestTxn(1, 1, 1, 2) 563 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 564 checkResponses(t, writeTestData(t, sender, 2, wTxn, 2)) 565 566 w1 := addTestWaiter(t, s1, wTxn, txn.TxnStatus_Committed) 567 defer w1.close() 568 w2 := addTestWaiter(t, s2, wTxn, txn.TxnStatus_Committed) 569 defer w2.close() 570 571 checkResponses(t, commitWriteData(t, sender, wTxn)) 572 573 checkWaiter(t, w1, txn.TxnStatus_Committed) 574 checkWaiter(t, w2, txn.TxnStatus_Committed) 575 576 checkData(t, wTxn, s1, 2, 1, true) 577 checkData(t, wTxn, s2, 2, 2, true) 578 } 579 580 func TestCommitWithRollbackIfAnyPrepareFailed(t *testing.T) { 581 sender := NewTestSender() 582 defer func() { 583 assert.NoError(t, sender.Close()) 584 }() 585 586 s1 := NewTestTxnService(t, 1, sender, NewTestClock(1)).(*service) 587 assert.NoError(t, s1.Start()) 588 defer func() { 589 assert.NoError(t, s1.Close(false)) 590 }() 591 s2 := NewTestTxnService(t, 2, sender, NewTestClock(1)).(*service) 592 assert.NoError(t, s2.Start()) 593 defer func() { 594 assert.NoError(t, s2.Close(false)) 595 }() 596 597 sender.AddTxnService(s1) 598 sender.AddTxnService(s2) 599 600 wTxn1 := NewTestTxn(1, 1, 1) 601 writeTestData(t, sender, 1, wTxn1, 1) 602 checkResponses(t, commitWriteData(t, sender, wTxn1)) // commit at 2 603 604 wTxn := NewTestTxn(1, 1, 1, 2) 605 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 606 checkResponses(t, writeTestData(t, sender, 2, wTxn, 2)) 607 608 w1 := addTestWaiter(t, s1, wTxn, txn.TxnStatus_Aborted) 609 defer w1.close() 610 w2 := addTestWaiter(t, s2, wTxn, txn.TxnStatus_Aborted) 611 defer w2.close() 612 613 checkResponses(t, commitWriteData(t, sender, wTxn), 614 txn.WrapError(moerr.NewTAEPrepare(context.TODO(), "cannot prepare"), 0)) 615 616 checkWaiter(t, w1, txn.TxnStatus_Aborted) 617 checkWaiter(t, w2, txn.TxnStatus_Aborted) 618 619 checkData(t, wTxn, s1, 2, 2, false) 620 checkData(t, wTxn, s2, 2, 2, false) 621 } 622 623 func TestCommitWithLockTables(t *testing.T) { 624 sender := NewTestSender() 625 defer func() { 626 assert.NoError(t, sender.Close()) 627 }() 628 629 allocator := newTestLockTablesAllocator( 630 t, 631 "/tmp/locktable.sock", 632 time.Second) 633 defer func() { 634 assert.NoError(t, allocator.Close()) 635 }() 636 637 s := NewTestTxnServiceWithAllocator( 638 t, 639 1, 640 sender, 641 NewTestClock(1), 642 allocator).(*service) 643 assert.NoError(t, s.Start()) 644 defer func() { 645 assert.NoError(t, s.Close(false)) 646 }() 647 sender.AddTxnService(s) 648 649 bind := allocator.Get("s1", 0, 10, 0, lock.Sharding_None) 650 wTxn := NewTestTxn(1, 1, 1) 651 wTxn.LockTables = append(wTxn.LockTables, bind) 652 checkResponses(t, writeTestData(t, sender, 1, wTxn, 0)) 653 checkResponses(t, commitWriteData(t, sender, wTxn)) 654 655 var values [][]byte 656 var timestamps []timestamp.Timestamp 657 kv := s.storage.(*mem.KVTxnStorage).GetCommittedKV() 658 659 kv.AscendRange(GetTestKey(0), NewTestTimestamp(0), NewTestTimestamp(math.MaxInt64), func(value []byte, ts timestamp.Timestamp) { 660 values = append(values, value) 661 timestamps = append(timestamps, ts) 662 }) 663 assert.Equal(t, [][]byte{GetTestValue(0, wTxn)}, values) 664 assert.Equal(t, []timestamp.Timestamp{NewTestTimestamp(2)}, timestamps) 665 } 666 667 func TestCommitWithLockTablesBindChanged(t *testing.T) { 668 sender := NewTestSender() 669 defer func() { 670 assert.NoError(t, sender.Close()) 671 }() 672 673 allocator := newTestLockTablesAllocator( 674 t, 675 "/tmp/locktable.sock", 676 time.Second) 677 defer func() { 678 assert.NoError(t, allocator.Close()) 679 }() 680 s := NewTestTxnServiceWithAllocator( 681 t, 682 1, 683 sender, 684 NewTestClock(1), 685 allocator).(*service) 686 assert.NoError(t, s.Start()) 687 defer func() { 688 assert.NoError(t, s.Close(false)) 689 }() 690 sender.AddTxnService(s) 691 692 bind := allocator.Get("s1", 0, 10, 0, lock.Sharding_None) 693 wTxn := NewTestTxn(1, 1, 1) 694 bind.ServiceID = "s2" 695 wTxn.LockTables = append(wTxn.LockTables, bind) 696 checkResponses(t, writeTestData(t, sender, 1, wTxn, 0)) 697 checkResponses(t, commitWriteData(t, sender, wTxn), 698 txn.WrapError(moerr.NewLockTableBindChanged(context.TODO()), 0)) 699 } 700 701 func TestCommitWithOrphan(t *testing.T) { 702 sender := NewTestSender() 703 defer func() { 704 assert.NoError(t, sender.Close()) 705 }() 706 707 allocator := newTestLockTablesAllocator( 708 t, 709 "/tmp/locktable.sock", 710 time.Second) 711 defer func() { 712 assert.NoError(t, allocator.Close()) 713 }() 714 s := NewTestTxnServiceWithAllocator( 715 t, 716 1, 717 sender, 718 NewTestClock(1), 719 allocator).(*service) 720 assert.NoError(t, s.Start()) 721 defer func() { 722 assert.NoError(t, s.Close(false)) 723 }() 724 sender.AddTxnService(s) 725 726 bind := allocator.Get("s1", 0, 10, 0, lock.Sharding_None) 727 wTxn := NewTestTxn(1, 1, 1) 728 wTxn.LockTables = append(wTxn.LockTables, bind) 729 wTxn.LockService = "s1" 730 731 allocator.AddCannotCommit([]lock.OrphanTxn{ 732 { 733 Service: "s1", 734 Txn: [][]byte{wTxn.ID}, 735 }, 736 }) 737 738 checkResponses(t, writeTestData(t, sender, 1, wTxn, 0)) 739 checkResponses(t, commitWriteData(t, sender, wTxn), 740 txn.WrapError(moerr.NewCannotCommitOrphan(context.TODO()), 0)) 741 } 742 743 func TestRollback(t *testing.T) { 744 sender := NewTestSender() 745 defer func() { 746 assert.NoError(t, sender.Close()) 747 }() 748 749 s1 := NewTestTxnService(t, 1, sender, NewTestClock(1)).(*service) 750 assert.NoError(t, s1.Start()) 751 defer func() { 752 assert.NoError(t, s1.Close(false)) 753 }() 754 s2 := NewTestTxnService(t, 2, sender, NewTestClock(1)).(*service) 755 assert.NoError(t, s2.Start()) 756 defer func() { 757 assert.NoError(t, s2.Close(false)) 758 }() 759 760 sender.AddTxnService(s1) 761 sender.AddTxnService(s2) 762 763 wTxn := NewTestTxn(1, 1, 1) 764 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 765 wTxn.TNShards = append(wTxn.TNShards, NewTestTNShard(2)) 766 checkResponses(t, writeTestData(t, sender, 2, wTxn, 2)) 767 768 w1 := addTestWaiter(t, s1, wTxn, txn.TxnStatus_Aborted) 769 defer w1.close() 770 w2 := addTestWaiter(t, s2, wTxn, txn.TxnStatus_Aborted) 771 defer w2.close() 772 773 responses := rollbackWriteData(t, sender, wTxn) 774 checkResponses(t, responses) 775 for _, resp := range responses { 776 assert.Equal(t, txn.TxnStatus_Aborted, resp.Txn.Status) 777 } 778 779 checkWaiter(t, w1, txn.TxnStatus_Aborted) 780 checkWaiter(t, w2, txn.TxnStatus_Aborted) 781 782 checkData(t, wTxn, s1, 2, 0, false) 783 checkData(t, wTxn, s2, 2, 0, false) 784 } 785 786 func TestRollbackWithTNShardNotFound(t *testing.T) { 787 sender := NewTestSender() 788 defer func() { 789 assert.NoError(t, sender.Close()) 790 }() 791 792 sender.setFilter(func(req *txn.TxnRequest) bool { 793 if req.RollbackRequest != nil { 794 req.Txn.TNShards[0].ReplicaID = 0 795 } 796 return true 797 }) 798 799 s1 := NewTestTxnService(t, 1, sender, NewTestClock(1)).(*service) 800 assert.NoError(t, s1.Start()) 801 defer func() { 802 assert.NoError(t, s1.Close(false)) 803 }() 804 s2 := NewTestTxnService(t, 2, sender, NewTestClock(1)).(*service) 805 assert.NoError(t, s2.Start()) 806 defer func() { 807 assert.NoError(t, s2.Close(false)) 808 }() 809 810 sender.AddTxnService(s1) 811 sender.AddTxnService(s2) 812 813 wTxn := NewTestTxn(1, 1, 1) 814 checkResponses(t, writeTestData(t, sender, 1, wTxn, 1)) 815 wTxn.TNShards = append(wTxn.TNShards, NewTestTNShard(2)) 816 checkResponses(t, writeTestData(t, sender, 2, wTxn, 2)) 817 818 checkResponses(t, rollbackWriteData(t, sender, wTxn), 819 txn.WrapError(moerr.NewTNShardNotFound(context.TODO(), "", 1), 0)) 820 } 821 822 func writeTestData(t *testing.T, sender rpc.TxnSender, toShard uint64, wTxn txn.TxnMeta, keys ...byte) []txn.TxnResponse { 823 requests := make([]txn.TxnRequest, 0, len(keys)) 824 for _, k := range keys { 825 requests = append(requests, NewTestWriteRequest(k, wTxn, toShard)) 826 } 827 result, err := sender.Send(context.Background(), requests) 828 assert.NoError(t, err) 829 responses := result.Responses 830 assert.Equal(t, len(keys), len(responses)) 831 return responses 832 } 833 834 func TestDebug(t *testing.T) { 835 sender := NewTestSender() 836 defer func() { 837 assert.NoError(t, sender.Close()) 838 }() 839 840 s := NewTestTxnService(t, 1, sender, NewTestClock(0)) 841 assert.NoError(t, s.Start()) 842 defer func() { 843 assert.NoError(t, s.Close(false)) 844 }() 845 846 sender.AddTxnService(s) 847 848 data := []byte("OK") 849 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 850 defer cancel() 851 852 result, err := sender.Send(ctx, []txn.TxnRequest{ 853 { 854 Method: txn.TxnMethod_DEBUG, 855 CNRequest: &txn.CNOpRequest{ 856 Payload: data, 857 Target: NewTestTNShard(1), 858 }, 859 }, 860 }) 861 require.NoError(t, err) 862 assert.Equal(t, data, result.Responses[0].CNOpResponse.Payload) 863 } 864 865 func commitShardWriteData(t *testing.T, sender rpc.TxnSender, wTxn txn.TxnMeta) []txn.TxnResponse { 866 result, err := sender.Send(context.Background(), []txn.TxnRequest{NewTestCommitShardRequest(wTxn)}) 867 assert.NoError(t, err) 868 responses := result.Responses 869 return responses 870 } 871 872 func rollbackShardWriteData(t *testing.T, sender rpc.TxnSender, wTxn txn.TxnMeta) []txn.TxnResponse { 873 result, err := sender.Send(context.Background(), []txn.TxnRequest{NewTestRollbackShardRequest(wTxn)}) 874 assert.NoError(t, err) 875 responses := result.Responses 876 return responses 877 } 878 879 func commitWriteData(t *testing.T, sender rpc.TxnSender, wTxn txn.TxnMeta) []txn.TxnResponse { 880 result, err := sender.Send(context.Background(), []txn.TxnRequest{NewTestCommitRequest(wTxn)}) 881 assert.NoError(t, err) 882 responses := result.Responses 883 return responses 884 } 885 886 func rollbackWriteData(t *testing.T, sender rpc.TxnSender, wTxn txn.TxnMeta) []txn.TxnResponse { 887 result, err := sender.Send(context.Background(), []txn.TxnRequest{NewTestRollbackRequest(wTxn)}) 888 assert.NoError(t, err) 889 responses := result.Responses 890 return responses 891 } 892 893 func readTestData(t *testing.T, sender rpc.TxnSender, toShard uint64, rTxn txn.TxnMeta, keys ...byte) []txn.TxnResponse { 894 requests := make([]txn.TxnRequest, 0, len(keys)) 895 for _, k := range keys { 896 requests = append(requests, NewTestReadRequest(k, rTxn, toShard)) 897 } 898 result, err := sender.Send(context.Background(), requests) 899 assert.NoError(t, err) 900 responses := result.Responses 901 assert.Equal(t, len(keys), len(responses)) 902 return responses 903 } 904 905 func checkReadResponses(t *testing.T, response []txn.TxnResponse, expectValues ...string) { 906 for idx, resp := range response { 907 values := mem.MustParseGetPayload(resp.CNOpResponse.Payload) 908 assert.Equal(t, expectValues[idx], string(values[0])) 909 assert.NotNil(t, resp.Txn) 910 } 911 } 912 913 func checkResponses(t *testing.T, response []txn.TxnResponse, expectErrors ...*txn.TxnError) { 914 if len(expectErrors) == 0 { 915 expectErrors = make([]*txn.TxnError, len(response)) 916 } 917 for idx, resp := range response { 918 if resp.TxnError == nil { 919 assert.Equal(t, expectErrors[idx], resp.TxnError) 920 } else { 921 assert.Equal(t, expectErrors[idx].TxnErrCode, resp.TxnError.TxnErrCode) 922 } 923 } 924 } 925 926 func checkData(t *testing.T, wTxn txn.TxnMeta, s *service, commitTS int64, k byte, committed bool) { 927 for { 928 v := s.getTxnContext(wTxn.ID) 929 if v != nil { 930 time.Sleep(time.Millisecond * 100) 931 continue 932 } 933 break 934 } 935 936 kv := s.storage.(*mem.KVTxnStorage) 937 938 if committed { 939 kv.RLock() 940 v, ok := kv.GetCommittedKV().Get(GetTestKey(k), NewTestTimestamp(commitTS)) 941 assert.True(t, ok) 942 assert.Equal(t, GetTestValue(k, wTxn), v) 943 kv.RUnlock() 944 } else { 945 kv.RLock() 946 n := 0 947 kv.GetCommittedKV().AscendRange(GetTestKey(k), 948 NewTestTimestamp(commitTS).Next(), 949 NewTestTimestamp(math.MaxInt64), func(_ []byte, _ timestamp.Timestamp) { 950 n++ 951 }) 952 assert.Equal(t, 0, n) 953 kv.RUnlock() 954 } 955 956 kv.RLock() 957 v, ok := kv.GetUncommittedKV().Get(GetTestKey(k)) 958 assert.False(t, ok) 959 assert.Empty(t, v) 960 kv.RUnlock() 961 962 assert.Nil(t, kv.GetUncommittedTxn(wTxn.ID)) 963 } 964 965 func addTestWaiter(t *testing.T, s *service, wTxn txn.TxnMeta, status txn.TxnStatus) *waiter { 966 txnCtx := s.getTxnContext(wTxn.ID) 967 assert.NotNil(t, txnCtx) 968 w := acquireWaiter() 969 assert.True(t, txnCtx.addWaiter(wTxn.ID, w, status)) 970 return w 971 } 972 973 func checkWaiter(t *testing.T, w *waiter, expectStatus txn.TxnStatus) { 974 status, err := w.wait(context.Background()) 975 assert.NoError(t, err) 976 assert.Equal(t, expectStatus, status) 977 } 978 979 func newTestLockTablesAllocator( 980 t *testing.T, 981 address string, 982 keepTimeout time.Duration) lockservice.LockTableAllocator { 983 require.NoError(t, os.RemoveAll(address)) 984 r := runtime.NewRuntime( 985 metadata.ServiceType_TN, 986 "dn-uuid", 987 logutil.GetPanicLoggerWithLevel(zapcore.DebugLevel). 988 With(zap.String("case", t.Name()))) 989 runtime.SetupProcessLevelRuntime(r) 990 c := clusterservice.NewMOCluster(nil, time.Hour, clusterservice.WithDisableRefresh()) 991 r.SetGlobalVariables(runtime.ClusterService, c) 992 return lockservice.NewLockTableAllocator( 993 "unix://"+address, 994 keepTimeout, 995 morpc.Config{}) 996 }