github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/sink/tablesink/state/state.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 state 15 16 import "sync/atomic" 17 18 // TableSinkState is state of the table sink. 19 type TableSinkState int32 20 21 // State for table sink 22 const ( 23 TableSinkStateUnknown TableSinkState = iota 24 // TableSinkSinking indicate that the table is sinking. 25 TableSinkSinking 26 // TableSinkStopping means the table sink is stopping, but not guaranteed yet. 27 TableSinkStopping 28 // TableSinkStopped means sink stop all works. 29 TableSinkStopped 30 ) 31 32 var stateStringMap = map[TableSinkState]string{ 33 TableSinkStateUnknown: "Unknown", 34 TableSinkSinking: "Sinking", 35 TableSinkStopping: "Stopping", 36 TableSinkStopped: "Stopped", 37 } 38 39 func (s TableSinkState) String() string { 40 return stateStringMap[s] 41 } 42 43 // Load state. 44 // This is THREAD-SAFE. 45 func (s *TableSinkState) Load() TableSinkState { 46 return TableSinkState(atomic.LoadInt32((*int32)(s))) 47 } 48 49 // Store state. 50 // This is THREAD-SAFE. 51 func (s *TableSinkState) Store(new TableSinkState) { 52 atomic.StoreInt32((*int32)(s), int32(new)) 53 } 54 55 // CompareAndSwap is just like sync/atomic.Atomic*.CompareAndSwap. 56 func (s *TableSinkState) CompareAndSwap(old, new TableSinkState) bool { 57 oldx := int32(old) 58 newx := int32(new) 59 return atomic.CompareAndSwapInt32((*int32)(s), oldx, newx) 60 }