github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/orchestrator/interfaces.go (about)

     1  // Copyright 2020 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  	"context"
    18  
    19  	"github.com/pingcap/ticdc/pkg/orchestrator/util"
    20  )
    21  
    22  // Reactor is a stateful transform of states.
    23  // It models Owner and Processor, which reacts according to updates in Etcd.
    24  type Reactor interface {
    25  	Tick(ctx context.Context, state ReactorState) (nextState ReactorState, err error)
    26  }
    27  
    28  // DataPatch represents an update of state
    29  type DataPatch interface {
    30  	Patch(valueMap map[util.EtcdKey][]byte, changedSet map[util.EtcdKey]struct{}) error
    31  }
    32  
    33  // ReactorState models the Etcd state of a reactor
    34  type ReactorState interface {
    35  	// Update is called by EtcdWorker to notify the Reactor of a latest change to the Etcd state.
    36  	Update(key util.EtcdKey, value []byte, isInit bool) error
    37  
    38  	// GetPatches is called by EtcdWorker, and should return many slices of data patches that represents the changes
    39  	// that a Reactor wants to apply to Etcd.
    40  	// a slice of DataPatch will be committed as one ETCD txn
    41  	GetPatches() [][]DataPatch
    42  }
    43  
    44  // SingleDataPatch represents an update to a given Etcd key
    45  type SingleDataPatch struct {
    46  	Key util.EtcdKey
    47  	// Func should be a pure function that returns a new value given the old value.
    48  	// The function is called each time the EtcdWorker initiates an Etcd transaction.
    49  	Func func(old []byte) (newValue []byte, changed bool, err error)
    50  }
    51  
    52  // Patch implements the DataPatch interface
    53  func (s *SingleDataPatch) Patch(valueMap map[util.EtcdKey][]byte, changedSet map[util.EtcdKey]struct{}) error {
    54  	value := valueMap[s.Key]
    55  	newValue, changed, err := s.Func(value)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	if !changed {
    60  		return nil
    61  	}
    62  	changedSet[s.Key] = struct{}{}
    63  	if newValue == nil {
    64  		delete(valueMap, s.Key)
    65  	} else {
    66  		valueMap[s.Key] = newValue
    67  	}
    68  	return nil
    69  }
    70  
    71  // MultiDatePatch represents an update to many keys
    72  type MultiDatePatch func(valueMap map[util.EtcdKey][]byte, changedSet map[util.EtcdKey]struct{}) error
    73  
    74  // Patch implements the DataPatch interface
    75  func (m MultiDatePatch) Patch(valueMap map[util.EtcdKey][]byte, changedSet map[util.EtcdKey]struct{}) error {
    76  	return m(valueMap, changedSet)
    77  }