github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/causality/tests/worker.go (about)

     1  // Copyright 2022 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 tests
    15  
    16  import (
    17  	"sync"
    18  
    19  	"github.com/pingcap/errors"
    20  	"github.com/pingcap/tiflow/engine/pkg/containers"
    21  	"github.com/pingcap/tiflow/pkg/causality"
    22  )
    23  
    24  type txnForTest struct {
    25  	keys []uint64
    26  	done func()
    27  }
    28  
    29  func (t *txnForTest) OnConflictResolved() {}
    30  
    31  func (t *txnForTest) ConflictKeys() []uint64 {
    32  	return t.keys
    33  }
    34  
    35  func (t *txnForTest) Finish(err error) {
    36  	if t.done != nil {
    37  		t.done()
    38  	}
    39  }
    40  
    41  type txnWithUnlock struct {
    42  	*txnForTest
    43  }
    44  
    45  type workerForTest struct {
    46  	txnQueue *containers.SliceQueue[txnWithUnlock]
    47  	wg       sync.WaitGroup
    48  	closeCh  chan struct{}
    49  	execFunc func(*txnForTest) error
    50  }
    51  
    52  func newWorkerForTest(txnCh <-chan causality.TxnWithNotifier[*txnForTest]) *workerForTest {
    53  	ret := &workerForTest{
    54  		txnQueue: containers.NewSliceQueue[txnWithUnlock](),
    55  		closeCh:  make(chan struct{}),
    56  	}
    57  
    58  	ret.wg.Add(1)
    59  	go func() {
    60  		defer ret.wg.Done()
    61  		ret.run(txnCh)
    62  	}()
    63  
    64  	return ret
    65  }
    66  
    67  func (w *workerForTest) Close() {
    68  	close(w.closeCh)
    69  	w.wg.Wait()
    70  }
    71  
    72  func (w *workerForTest) run(txnCh <-chan causality.TxnWithNotifier[*txnForTest]) {
    73  	for {
    74  		select {
    75  		case <-w.closeCh:
    76  			return
    77  		case txn := <-txnCh:
    78  			var err error
    79  			if w.execFunc != nil {
    80  				err = errors.Trace(w.execFunc(txn.TxnEvent))
    81  			}
    82  			txn.PostTxnExecuted()
    83  
    84  			// Finish must be called after unlock,
    85  			// because the conflictTestDriver needs to make sure
    86  			// that all conflicts have been resolved before exiting.
    87  			txn.TxnEvent.Finish(err)
    88  		}
    89  	}
    90  }