forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartbeat_streams.go
159 lines (138 loc) · 4.17 KB
/
heartbeat_streams.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"context"
"strconv"
"sync"
"time"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/pd/pkg/logutil"
"github.com/pingcap/pd/server/core"
log "github.com/sirupsen/logrus"
)
const heartbeatStreamKeepAliveInterval = time.Minute
type heartbeatStream interface {
Send(*pdpb.RegionHeartbeatResponse) error
}
type streamUpdate struct {
storeID uint64
stream heartbeatStream
}
type heartbeatStreams struct {
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
clusterID uint64
streams map[uint64]heartbeatStream
msgCh chan *pdpb.RegionHeartbeatResponse
streamCh chan streamUpdate
}
func newHeartbeatStreams(clusterID uint64) *heartbeatStreams {
ctx, cancel := context.WithCancel(context.Background())
hs := &heartbeatStreams{
ctx: ctx,
cancel: cancel,
clusterID: clusterID,
streams: make(map[uint64]heartbeatStream),
msgCh: make(chan *pdpb.RegionHeartbeatResponse, regionheartbeatSendChanCap),
streamCh: make(chan streamUpdate, 1),
}
hs.wg.Add(1)
go hs.run()
return hs
}
func (s *heartbeatStreams) run() {
defer logutil.LogPanic()
defer s.wg.Done()
keepAliveTicker := time.NewTicker(heartbeatStreamKeepAliveInterval)
defer keepAliveTicker.Stop()
keepAlive := &pdpb.RegionHeartbeatResponse{Header: &pdpb.ResponseHeader{ClusterId: s.clusterID}}
for {
select {
case update := <-s.streamCh:
s.streams[update.storeID] = update.stream
case msg := <-s.msgCh:
storeID := msg.GetTargetPeer().GetStoreId()
storeLabel := strconv.FormatUint(storeID, 10)
if stream, ok := s.streams[storeID]; ok {
if err := stream.Send(msg); err != nil {
log.Errorf("[region %v] send heartbeat message fail: %v", msg.RegionId, err)
delete(s.streams, storeID)
regionHeartbeatCounter.WithLabelValues(storeLabel, "push", "err").Inc()
} else {
regionHeartbeatCounter.WithLabelValues(storeLabel, "push", "ok").Inc()
}
} else {
log.Debugf("[region %v] heartbeat stream not found for store %v, skip send message", msg.RegionId, storeID)
regionHeartbeatCounter.WithLabelValues(storeLabel, "push", "skip").Inc()
}
case <-keepAliveTicker.C:
for storeID, stream := range s.streams {
storeLabel := strconv.FormatUint(storeID, 10)
if err := stream.Send(keepAlive); err != nil {
log.Errorf("[store %v] send keepalive message fail: %v", storeID, err)
delete(s.streams, storeID)
regionHeartbeatCounter.WithLabelValues(storeLabel, "keepalive", "err").Inc()
} else {
regionHeartbeatCounter.WithLabelValues(storeLabel, "keepalive", "ok").Inc()
}
}
case <-s.ctx.Done():
return
}
}
}
func (s *heartbeatStreams) Close() {
s.cancel()
s.wg.Wait()
}
func (s *heartbeatStreams) bindStream(storeID uint64, stream heartbeatStream) {
update := streamUpdate{
storeID: storeID,
stream: stream,
}
select {
case s.streamCh <- update:
case <-s.ctx.Done():
}
}
func (s *heartbeatStreams) sendMsg(region *core.RegionInfo, msg *pdpb.RegionHeartbeatResponse) {
if region.Leader == nil {
return
}
msg.Header = &pdpb.ResponseHeader{ClusterId: s.clusterID}
msg.RegionId = region.GetId()
msg.RegionEpoch = region.GetRegionEpoch()
msg.TargetPeer = region.Leader
select {
case s.msgCh <- msg:
case <-s.ctx.Done():
}
}
func (s *heartbeatStreams) sendErr(region *core.RegionInfo, errType pdpb.ErrorType, errMsg string, storeLabel string) {
regionHeartbeatCounter.WithLabelValues(storeLabel, "report", "err").Inc()
msg := &pdpb.RegionHeartbeatResponse{
Header: &pdpb.ResponseHeader{
ClusterId: s.clusterID,
Error: &pdpb.Error{
Type: errType,
Message: errMsg,
},
},
}
select {
case s.msgCh <- msg:
case <-s.ctx.Done():
}
}