|
| 1 | +// Copyright 2025 TiKV Project Authors. |
| 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 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package meta |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "strconv" |
| 20 | + "sync" |
| 21 | + |
| 22 | + "github.com/gogo/protobuf/proto" |
| 23 | + "go.etcd.io/etcd/api/v3/mvccpb" |
| 24 | + clientv3 "go.etcd.io/etcd/client/v3" |
| 25 | + "go.uber.org/zap" |
| 26 | + |
| 27 | + "github.com/pingcap/kvproto/pkg/metapb" |
| 28 | + "github.com/pingcap/log" |
| 29 | + |
| 30 | + "github.com/tikv/pd/pkg/core" |
| 31 | + "github.com/tikv/pd/pkg/statistics" |
| 32 | + "github.com/tikv/pd/pkg/utils/etcdutil" |
| 33 | + "github.com/tikv/pd/pkg/utils/keypath" |
| 34 | +) |
| 35 | + |
| 36 | +// Watcher is used to watch the PD for any meta changes. |
| 37 | +type Watcher struct { |
| 38 | + wg sync.WaitGroup |
| 39 | + ctx context.Context |
| 40 | + cancel context.CancelFunc |
| 41 | + |
| 42 | + etcdClient *clientv3.Client |
| 43 | + basicCluster *core.BasicCluster |
| 44 | + storeWatcher *etcdutil.LoopWatcher |
| 45 | +} |
| 46 | + |
| 47 | +// NewWatcher creates a new watcher to watch the meta change from PD. |
| 48 | +func NewWatcher( |
| 49 | + ctx context.Context, |
| 50 | + etcdClient *clientv3.Client, |
| 51 | + basicCluster *core.BasicCluster, |
| 52 | +) (*Watcher, error) { |
| 53 | + ctx, cancel := context.WithCancel(ctx) |
| 54 | + w := &Watcher{ |
| 55 | + ctx: ctx, |
| 56 | + cancel: cancel, |
| 57 | + etcdClient: etcdClient, |
| 58 | + basicCluster: basicCluster, |
| 59 | + } |
| 60 | + err := w.initializeStoreWatcher() |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + return w, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (w *Watcher) initializeStoreWatcher() error { |
| 68 | + putFn := func(kv *mvccpb.KeyValue) error { |
| 69 | + store := &metapb.Store{} |
| 70 | + if err := proto.Unmarshal(kv.Value, store); err != nil { |
| 71 | + log.Warn("failed to unmarshal store entry", |
| 72 | + zap.String("event-kv-key", string(kv.Key)), zap.Error(err)) |
| 73 | + return err |
| 74 | + } |
| 75 | + log.Debug("update store meta", zap.Stringer("store", store)) |
| 76 | + origin := w.basicCluster.GetStore(store.GetId()) |
| 77 | + if origin == nil { |
| 78 | + w.basicCluster.PutStore(core.NewStoreInfo(store)) |
| 79 | + } else { |
| 80 | + w.basicCluster.PutStore(origin.Clone(core.SetStoreMeta(store))) |
| 81 | + } |
| 82 | + |
| 83 | + if store.GetNodeState() == metapb.NodeState_Removed { |
| 84 | + statistics.ResetStoreStatistics(store.GetAddress(), strconv.FormatUint(store.GetId(), 10)) |
| 85 | + // TODO: remove hot stats |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | + } |
| 90 | + deleteFn := func(kv *mvccpb.KeyValue) error { |
| 91 | + key := string(kv.Key) |
| 92 | + storeID, err := keypath.ExtractStoreIDFromPath(key) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + origin := w.basicCluster.GetStore(storeID) |
| 97 | + if origin != nil { |
| 98 | + statistics.DeleteClusterStatusMetrics(origin) |
| 99 | + w.basicCluster.DeleteStore(origin) |
| 100 | + log.Info("delete store meta", zap.Uint64("store-id", storeID)) |
| 101 | + } |
| 102 | + return nil |
| 103 | + } |
| 104 | + w.storeWatcher = etcdutil.NewLoopWatcher( |
| 105 | + w.ctx, &w.wg, |
| 106 | + w.etcdClient, |
| 107 | + "router-store-watcher", |
| 108 | + // Watch meta store proto |
| 109 | + keypath.StorePathPrefix(), |
| 110 | + func([]*clientv3.Event) error { return nil }, |
| 111 | + putFn, deleteFn, |
| 112 | + func([]*clientv3.Event) error { return nil }, |
| 113 | + true, /* withPrefix */ |
| 114 | + ) |
| 115 | + w.storeWatcher.StartWatchLoop() |
| 116 | + return w.storeWatcher.WaitLoad() |
| 117 | +} |
| 118 | + |
| 119 | +// Close closes the watcher. |
| 120 | +func (w *Watcher) Close() { |
| 121 | + w.cancel() |
| 122 | + w.wg.Wait() |
| 123 | +} |
| 124 | + |
| 125 | +// GetStoreWatcher returns the store watcher. |
| 126 | +func (w *Watcher) GetStoreWatcher() *etcdutil.LoopWatcher { |
| 127 | + return w.storeWatcher |
| 128 | +} |
0 commit comments