github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/servermaster/serverutil/watch_executors.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 serverutil 15 16 import ( 17 "context" 18 "time" 19 20 "github.com/pingcap/log" 21 "github.com/pingcap/tiflow/engine/model" 22 "github.com/pingcap/tiflow/engine/pkg/notifier" 23 "github.com/pingcap/tiflow/pkg/errors" 24 "github.com/pingcap/tiflow/pkg/logutil" 25 "go.uber.org/zap" 26 ) 27 28 // executorWatcher represents an object that can provide a snapshot 29 // of the executor list followed by a stream of updates to the list. 30 type executorWatcher interface { 31 WatchExecutors(ctx context.Context) ( 32 snap map[model.ExecutorID]string, 33 stream *notifier.Receiver[model.ExecutorStatusChange], 34 err error, 35 ) 36 } 37 38 // executorInfoUser represents an object that uses the information provides 39 // by executorWatcher. 40 type executorInfoUser interface { 41 UpdateExecutorList(executors map[model.ExecutorID]string) error 42 AddExecutor(executorID model.ExecutorID, addr string) error 43 RemoveExecutor(executorID model.ExecutorID) error 44 } 45 46 // WatchExecutors updates the state of `user` with information provides 47 // by `watcher`. It blocks until canceled or an error is encountered. 48 func WatchExecutors(ctx context.Context, watcher executorWatcher, user executorInfoUser) (retErr error) { 49 defer func() { 50 log.Info("WatchExecutors exited", logutil.ShortError(retErr)) 51 }() 52 53 watchStart := time.Now() 54 snap, updates, err := watcher.WatchExecutors(ctx) 55 56 if duration := time.Since(watchStart); duration >= 100*time.Millisecond { 57 log.Warn("WatchExecutors took too long", 58 zap.Duration("duration", duration), 59 zap.Any("snap", snap)) 60 } 61 if err != nil { 62 return errors.Annotate(err, "watch executors") 63 } 64 defer updates.Close() 65 66 log.Info("update executor list", zap.Any("list", snap)) 67 if err := user.UpdateExecutorList(snap); err != nil { 68 return errors.Annotate(err, "watch executors") 69 } 70 71 for { 72 var ( 73 change model.ExecutorStatusChange 74 ok bool 75 ) 76 select { 77 case <-ctx.Done(): 78 return errors.Trace(ctx.Err()) 79 case change, ok = <-updates.C: 80 } 81 82 if !ok { 83 return errors.ErrExecutorWatcherClosed.GenWithStackByArgs() 84 } 85 if change.Tp == model.EventExecutorOnline { 86 err := user.AddExecutor(change.ID, change.Addr) 87 if err != nil { 88 if errors.Is(err, errors.ErrExecutorAlreadyExists) { 89 log.Warn("unexpected EventExecutorOnline", 90 zap.Error(err)) 91 continue 92 } 93 return err 94 } 95 log.Info("add executor", 96 zap.String("id", string(change.ID)), 97 zap.String("addr", change.Addr)) 98 } else if change.Tp == model.EventExecutorOffline { 99 err := user.RemoveExecutor(change.ID) 100 if err != nil { 101 if errors.Is(err, errors.ErrExecutorNotFound) || errors.Is(err, errors.ErrTombstoneExecutor) { 102 log.Warn("unexpected EventExecutorOffline", 103 zap.Error(err)) 104 continue 105 } 106 return errors.Annotate(err, "watch executors") 107 } 108 log.Info("remove executor", 109 zap.String("id", string(change.ID))) 110 } else { 111 log.Panic("unknown ExecutorStatusChange type", 112 zap.Any("change", change)) 113 } 114 } 115 }