github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/model/mounter.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 model 15 16 import ( 17 "context" 18 ) 19 20 // PolymorphicEvent describes a event can be in multiple states 21 type PolymorphicEvent struct { 22 StartTs uint64 23 // Commit or resolved TS 24 CRTs uint64 25 26 RawKV *RawKVEntry 27 Row *RowChangedEvent 28 ReplicaID uint64 29 finished chan struct{} 30 } 31 32 // NewPolymorphicEvent creates a new PolymorphicEvent with a raw KV 33 func NewPolymorphicEvent(rawKV *RawKVEntry) *PolymorphicEvent { 34 if rawKV.OpType == OpTypeResolved { 35 return NewResolvedPolymorphicEvent(rawKV.RegionID, rawKV.CRTs) 36 } 37 return &PolymorphicEvent{ 38 StartTs: rawKV.StartTs, 39 CRTs: rawKV.CRTs, 40 RawKV: rawKV, 41 finished: nil, 42 } 43 } 44 45 // NewResolvedPolymorphicEvent creates a new PolymorphicEvent with the resolved ts 46 func NewResolvedPolymorphicEvent(regionID uint64, resolvedTs uint64) *PolymorphicEvent { 47 return &PolymorphicEvent{ 48 CRTs: resolvedTs, 49 RawKV: &RawKVEntry{CRTs: resolvedTs, OpType: OpTypeResolved, RegionID: regionID}, 50 Row: nil, 51 finished: nil, 52 } 53 } 54 55 // RegionID returns the region ID where the event comes from. 56 func (e *PolymorphicEvent) RegionID() uint64 { 57 return e.RawKV.RegionID 58 } 59 60 // SetUpFinishedChan creates an internal channel to support PrepareFinished and WaitPrepare 61 func (e *PolymorphicEvent) SetUpFinishedChan() { 62 if e.finished == nil { 63 e.finished = make(chan struct{}) 64 } 65 } 66 67 // PrepareFinished marks the prepare process is finished 68 // In prepare process, Mounter will translate raw KV to row data 69 func (e *PolymorphicEvent) PrepareFinished() { 70 if e.finished != nil { 71 close(e.finished) 72 } 73 } 74 75 // WaitPrepare waits for prepare process finished 76 func (e *PolymorphicEvent) WaitPrepare(ctx context.Context) error { 77 if e.finished != nil { 78 select { 79 case <-ctx.Done(): 80 return ctx.Err() 81 case <-e.finished: 82 } 83 } 84 return nil 85 }