github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/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/tiflow/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  	// UpdatePendingChange is called by EtcdWorker to notify the Reactor to apply the pending changes.
    39  	UpdatePendingChange()
    40  
    41  	// GetPatches is called by EtcdWorker, and should return many slices of data patches that represents the changes
    42  	// that a Reactor wants to apply to Etcd.
    43  	// a slice of DataPatch will be committed as one ETCD txn
    44  	GetPatches() [][]DataPatch
    45  }
    46  
    47  // SingleDataPatch represents an update to a given Etcd key
    48  type SingleDataPatch struct {
    49  	Key util.EtcdKey
    50  	// Func should be a pure function that returns a new value given the old value.
    51  	// The function is called each time the EtcdWorker initiates an Etcd transaction.
    52  	Func func(old []byte) (newValue []byte, changed bool, err error)
    53  }
    54  
    55  // Patch implements the DataPatch interface
    56  func (s *SingleDataPatch) Patch(valueMap map[util.EtcdKey][]byte, changedSet map[util.EtcdKey]struct{}) error {
    57  	value := valueMap[s.Key]
    58  	newValue, changed, err := s.Func(value)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	if !changed {
    63  		return nil
    64  	}
    65  	changedSet[s.Key] = struct{}{}
    66  	if newValue == nil {
    67  		delete(valueMap, s.Key)
    68  	} else {
    69  		valueMap[s.Key] = newValue
    70  	}
    71  	return nil
    72  }