github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/fakejob/status.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 fakejob
    15  
    16  import (
    17  	"encoding/json"
    18  	"sync"
    19  	"time"
    20  )
    21  
    22  // DummyWorkerStatus is used in fake worker as status.
    23  type DummyWorkerStatus struct {
    24  	rwm        sync.RWMutex
    25  	BusinessID int               `json:"business-id"`
    26  	Tick       int64             `json:"tick"`
    27  	Checkpoint *WorkerCheckpoint `json:"checkpoint"`
    28  }
    29  
    30  // DoTick is a periodically tick function.
    31  func (s *DummyWorkerStatus) DoTick() {
    32  	s.rwm.Lock()
    33  	defer s.rwm.Unlock()
    34  	s.Tick++
    35  }
    36  
    37  // GetEtcdCheckpoint returns cached checkpoint stored in etcd.
    38  func (s *DummyWorkerStatus) GetEtcdCheckpoint() WorkerCheckpoint {
    39  	s.rwm.RLock()
    40  	defer s.rwm.RUnlock()
    41  	return *s.Checkpoint
    42  }
    43  
    44  // SetEtcdCheckpoint sets checkpoint to memory cache.
    45  func (s *DummyWorkerStatus) SetEtcdCheckpoint(ckpt *WorkerCheckpoint) {
    46  	s.rwm.Lock()
    47  	defer s.rwm.Unlock()
    48  	s.Checkpoint = ckpt
    49  }
    50  
    51  // Marshal returns the JSON encoding of DummyWorkerStatus.
    52  func (s *DummyWorkerStatus) Marshal() ([]byte, error) {
    53  	s.rwm.RLock()
    54  	defer s.rwm.RUnlock()
    55  	return json.Marshal(s)
    56  }
    57  
    58  // Unmarshal parses the JSON-encoded data and stores the result in DummyWorkerStatus.
    59  func (s *DummyWorkerStatus) Unmarshal(data []byte) error {
    60  	return json.Unmarshal(data, s)
    61  }
    62  
    63  // WorkerCheckpoint is used to resume a new worker from old checkpoint
    64  type WorkerCheckpoint struct {
    65  	Tick      int64  `json:"tick"`
    66  	Revision  int64  `json:"revision"`
    67  	MvccCount int    `json:"mvcc-count"`
    68  	Value     string `json:"value"`
    69  }
    70  
    71  // WorkerConfig defines config of fake worker
    72  type WorkerConfig struct {
    73  	ID         int   `json:"id"`
    74  	TargetTick int64 `json:"target-tick"`
    75  
    76  	EtcdWatchEnable     bool          `json:"etcd-watch-enable"`
    77  	EtcdEndpoints       []string      `json:"etcd-endpoints"`
    78  	EtcdWatchPrefix     string        `json:"etcd-watch-prefix"`
    79  	InjectErrorInterval time.Duration `json:"inject-error-interval"`
    80  
    81  	Checkpoint WorkerCheckpoint `json:"checkpoint"`
    82  }