github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/syncer/safe-mode/mode.go (about) 1 // Copyright 2019 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 mode 15 16 import ( 17 "sync" 18 19 "github.com/pingcap/tidb/pkg/util/filter" 20 tcontext "github.com/pingcap/tiflow/dm/pkg/context" 21 "github.com/pingcap/tiflow/dm/pkg/terror" 22 "github.com/pingcap/tiflow/dm/pkg/utils" 23 "go.uber.org/zap" 24 ) 25 26 // SafeMode controls whether enable safe-mode through a mechanism similar to reference-count 27 // indicates enabled excepting the count is 0. 28 type SafeMode struct { 29 mu sync.RWMutex 30 count int32 31 tables map[string]struct{} 32 } 33 34 // NewSafeMode creates a new SafeMode instance. 35 func NewSafeMode() *SafeMode { 36 return &SafeMode{ 37 tables: make(map[string]struct{}), 38 } 39 } 40 41 // Add adds n to the count, n can be negative. 42 func (m *SafeMode) Add(tctx *tcontext.Context, n int32) error { 43 m.mu.Lock() 44 defer m.mu.Unlock() 45 return m.setCount(tctx, m.count+n) 46 } 47 48 // IncrForTable tries to add 1 on the count if the table not added before 49 // can only be desc with DescForTable. 50 func (m *SafeMode) IncrForTable(tctx *tcontext.Context, table *filter.Table) error { 51 tableID := utils.GenTableID(table) 52 53 m.mu.Lock() 54 defer m.mu.Unlock() 55 if _, ok := m.tables[tableID]; !ok { 56 m.tables[tableID] = struct{}{} 57 return m.setCount(tctx, m.count+1) 58 } 59 return nil 60 } 61 62 // DescForTable tries to add -1 on the count if the table added before. 63 func (m *SafeMode) DescForTable(tctx *tcontext.Context, table *filter.Table) error { 64 tableID := utils.GenTableID(table) 65 66 m.mu.Lock() 67 defer m.mu.Unlock() 68 if _, ok := m.tables[tableID]; ok { 69 delete(m.tables, tableID) 70 return m.setCount(tctx, m.count-1) 71 } 72 return nil 73 } 74 75 // Reset resets to the state of not-enable. 76 func (m *SafeMode) Reset(tctx *tcontext.Context) { 77 m.mu.Lock() 78 defer m.mu.Unlock() 79 80 //nolint:errcheck 81 m.setCount(tctx, 0) 82 m.tables = make(map[string]struct{}) 83 } 84 85 // Enable returns whether is enabled currently. 86 func (m *SafeMode) Enable() bool { 87 m.mu.RLock() 88 defer m.mu.RUnlock() 89 return m.count != 0 90 } 91 92 // setCount sets the count, called internal. 93 func (m *SafeMode) setCount(tctx *tcontext.Context, n int32) error { 94 if n < 0 { 95 return terror.ErrSyncUnitSafeModeSetCount.Generatef("set negative count (%d) for safe-mode not valid", m.count) 96 } 97 98 prev := m.count 99 m.count = n 100 tctx.L().Info("change count", zap.Int32("previous count", prev), zap.Int32("new count", m.count)) 101 return nil 102 }