github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/servermaster/metastore_manager.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 servermaster 15 16 import ( 17 "sync" 18 19 "github.com/pingcap/log" 20 metaModel "github.com/pingcap/tiflow/engine/pkg/meta/model" 21 "github.com/pingcap/tiflow/pkg/errors" 22 "go.uber.org/zap" 23 ) 24 25 // MetaStoreManager defines an interface to manage metastore 26 type MetaStoreManager interface { 27 // Register register specify backend store to manager with an unique id 28 // id can be some readable identifier, like `meta-test1`. 29 // Duplicate id will return an error 30 Register(id string, store *metaModel.StoreConfig) error 31 // UnRegister delete an existing backend store 32 UnRegister(id string) 33 // GetMetaStore get an existing backend store info 34 GetMetaStore(id string) *metaModel.StoreConfig 35 } 36 37 type metaStoreManagerImpl struct { 38 // From id to metaModel.StoreConfig 39 id2Store sync.Map 40 } 41 42 // NewMetaStoreManager creates a new metaStoreManagerImpl instance 43 func NewMetaStoreManager() MetaStoreManager { 44 return &metaStoreManagerImpl{} 45 } 46 47 // Register implements MetaStoreManager.Register 48 func (m *metaStoreManagerImpl) Register(id string, store *metaModel.StoreConfig) error { 49 if _, exists := m.id2Store.LoadOrStore(id, store); exists { 50 log.Error("register metastore fail", zap.Any("config", store), zap.String("err", "Duplicate storeID")) 51 return errors.ErrMetaStoreIDDuplicate.GenWithStackByArgs() 52 } 53 return nil 54 } 55 56 // Unregister implements MetaStoreManager.Unregister 57 func (m *metaStoreManagerImpl) UnRegister(id string) { 58 m.id2Store.Delete(id) 59 log.Info("unregister metastore", zap.String("storeID", id)) 60 } 61 62 // GetMetaStore implements MetaStoreManager.GetMetaStore 63 func (m *metaStoreManagerImpl) GetMetaStore(id string) *metaModel.StoreConfig { 64 if store, exists := m.id2Store.Load(id); exists { 65 return store.(*metaModel.StoreConfig) 66 } 67 68 return nil 69 }