Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syncer: exit broadcast as soon as possible #9018

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions pkg/syncer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/tikv/pd/pkg/storage/kv"
"github.com/tikv/pd/pkg/utils/grpcutil"
"github.com/tikv/pd/pkg/utils/keypath"
"github.com/tikv/pd/pkg/utils/logutil"
"github.com/tikv/pd/pkg/utils/syncutil"
)

Expand Down Expand Up @@ -79,11 +80,12 @@ type RegionSyncer struct {
clientCtx context.Context
clientCancel context.CancelFunc
}
server Server
wg sync.WaitGroup
history *historyBuffer
limit *ratelimit.RateLimiter
tlsConfig *grpcutil.TLSConfig
broadcastDone chan struct{}
server Server
wg sync.WaitGroup
history *historyBuffer
limit *ratelimit.RateLimiter
tlsConfig *grpcutil.TLSConfig
// status when as client
streamingRunning atomic.Bool
}
Expand All @@ -96,10 +98,11 @@ func NewRegionSyncer(s Server) *RegionSyncer {
return nil
}
syncer := &RegionSyncer{
server: s,
history: newHistoryBuffer(defaultHistoryBufferSize, regionStorage.(kv.Base)),
limit: ratelimit.NewRateLimiter(defaultBucketRate, defaultBucketCapacity),
tlsConfig: s.GetTLSConfig(),
server: s,
history: newHistoryBuffer(defaultHistoryBufferSize, regionStorage.(kv.Base)),
limit: ratelimit.NewRateLimiter(defaultBucketRate, defaultBucketCapacity),
tlsConfig: s.GetTLSConfig(),
broadcastDone: make(chan struct{}, 1),
}
syncer.mu.streams = make(map[string]ServerStream)
return syncer
Expand Down Expand Up @@ -160,13 +163,13 @@ func (s *RegionSyncer) RunServer(ctx context.Context, regionNotifier <-chan *cor
RegionLeaders: leaders,
Buckets: buckets,
}
s.broadcast(regions)
s.broadcast(ctx, regions)
case <-ticker.C:
alive := &pdpb.SyncRegionResponse{
Header: &pdpb.ResponseHeader{ClusterId: keypath.ClusterID()},
StartIndex: s.history.getNextIndex(),
}
s.broadcast(alive)
s.broadcast(ctx, alive)
}
requests = requests[:0]
stats = stats[:0]
Expand Down Expand Up @@ -344,23 +347,31 @@ func (s *RegionSyncer) bindStream(name string, stream ServerStream) {
s.mu.streams[name] = stream
}

func (s *RegionSyncer) broadcast(regions *pdpb.SyncRegionResponse) {
var failed []string
s.mu.RLock()
for name, sender := range s.mu.streams {
err := sender.Send(regions)
if err != nil {
log.Error("region syncer send data meet error", errs.ZapError(errs.ErrGRPCSend, err))
failed = append(failed, name)
func (s *RegionSyncer) broadcast(ctx context.Context, regions *pdpb.SyncRegionResponse) {
go func() {
defer logutil.LogPanic()
var failed []string
s.mu.RLock()
for name, sender := range s.mu.streams {
err := sender.Send(regions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check ctx.Done here as well?

if err != nil {
log.Error("region syncer send data meet error", errs.ZapError(errs.ErrGRPCSend, err))
failed = append(failed, name)
}
}
}
s.mu.RUnlock()
if len(failed) > 0 {
s.mu.Lock()
for _, name := range failed {
delete(s.mu.streams, name)
log.Info("region syncer delete the stream", zap.String("stream", name))
s.mu.RUnlock()
if len(failed) > 0 {
s.mu.Lock()
for _, name := range failed {
delete(s.mu.streams, name)
log.Info("region syncer delete the stream", zap.String("stream", name))
}
s.mu.Unlock()
}
s.mu.Unlock()
s.broadcastDone <- struct{}{}
nolouch marked this conversation as resolved.
Show resolved Hide resolved
}()
select {
case <-s.broadcastDone:
case <-ctx.Done():
}
}
Loading