-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeer_store.go
275 lines (224 loc) · 5.67 KB
/
peer_store.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package prophet
import (
"context"
"sync"
"time"
"github.com/fagongzi/util/task"
)
// ResourceStore is a container of resources, which maintains a set of resources
type ResourceStore interface {
Start()
Meta() Container
GetTransport() ReplicaTransport
GetContainerAddr(uint64) (string, error)
HandleReplicaMsg(interface{}) interface{}
ForeachReplica(func(*PeerReplica) bool)
// GetPeerReplica returns a peer replicatation from the store,
// when `leader` is true, only return the leader replicatation
GetPeerReplica(id uint64, leader bool) *PeerReplica
// AddReplica add a replicatation
AddReplica(*PeerReplica)
RemoveReplica(id uint64)
MustStartTask(func(context.Context)) uint64
MustStopTask(uint64)
LocalStore() LocalStore
}
type defaultResourceStore struct {
sync.RWMutex
meta Container
transport ReplicaTransport
pd Prophet
localStore LocalStore
handler PeerReplicaHandler
replicas sync.Map // uint64 -> peer_replica
runner *task.Runner
hbInterval time.Duration
workerCount uint64
elector Elector
}
// NewResourceStore creates a resource store
func NewResourceStore(meta Container,
localStore LocalStore,
pd Prophet,
elector Elector,
handler PeerReplicaHandler,
factory func() Resource,
hbInterval time.Duration,
workerCount uint64) ResourceStore {
s := &defaultResourceStore{
meta: meta,
pd: pd,
hbInterval: hbInterval,
handler: handler,
workerCount: workerCount,
localStore: localStore,
elector: elector,
runner: task.NewRunner(),
}
s.transport = NewReplicaTransport(meta.ShardAddr(), s, factory)
return s
}
func (s *defaultResourceStore) Start() {
s.transport.Start()
log.Infof("sharding transport start at %s", s.meta.ShardAddr())
_, err := s.runner.RunCancelableTask(s.runHBTask)
if err != nil {
log.Fatalf("run hb task failed with %+v", err)
}
for i := uint64(0); i < s.workerCount; i++ {
idx := uint64(i)
_, err = s.runner.RunCancelableTask(func(ctx context.Context) {
s.runPRTask(ctx, idx)
})
if err != nil {
log.Fatalf("run pr event loop task failed with %+v", err)
}
}
}
func (s *defaultResourceStore) LocalStore() LocalStore {
return s.localStore
}
func (s *defaultResourceStore) Meta() Container {
return s.meta
}
func (s *defaultResourceStore) GetTransport() ReplicaTransport {
return s.transport
}
func (s *defaultResourceStore) GetContainerAddr(id uint64) (string, error) {
c, err := s.pd.GetStore().GetContainer(id)
if err != nil {
return "", err
}
if c == nil {
return "", nil
}
return c.ShardAddr(), nil
}
func (s *defaultResourceStore) ForeachReplica(fn func(*PeerReplica) bool) {
s.replicas.Range(func(key, value interface{}) bool {
return fn(value.(*PeerReplica))
})
}
func (s *defaultResourceStore) GetPeerReplica(id uint64, leader bool) *PeerReplica {
if pr, ok := s.replicas.Load(id); ok {
p := pr.(*PeerReplica)
if !leader ||
(leader && p.IsLeader()) {
return p
}
return nil
}
return nil
}
func (s *defaultResourceStore) AddReplica(pr *PeerReplica) {
pr.workerID = uint64(s.workerCount-1) & pr.meta.ID()
s.replicas.Store(pr.meta.ID(), pr)
log.Infof("%s added to local resource store", pr.tag)
}
func (s *defaultResourceStore) HandleReplicaMsg(data interface{}) interface{} {
if msg, ok := data.(*hbMsg); ok {
return s.handleHB(msg)
} else if msg, ok := data.(*hbACKMsg); ok {
return s.handleHBACK(msg)
} else if msg, ok := data.(*removeMsg); ok {
return s.handleRemovePR(msg)
}
log.Fatalf("not support msg %T: %+v",
data,
data)
return nil
}
func (s *defaultResourceStore) MustStartTask(task func(context.Context)) uint64 {
id, err := s.runner.RunCancelableTask(task)
if err != nil {
log.Fatalf("run task failed with %+v", err)
}
return id
}
func (s *defaultResourceStore) MustStopTask(task uint64) {
err := s.runner.StopCancelableTask(task)
if err != nil {
log.Fatalf("stop task %d failed with %+v", task, err)
}
}
func (s *defaultResourceStore) runPRTask(ctx context.Context, id uint64) {
log.Infof("pr event worker %d start", id)
hasEvent := true
for {
select {
case <-ctx.Done():
log.Infof("pr event worker %d exit", id)
return
default:
if !hasEvent {
time.Sleep(time.Millisecond * 10)
}
hasEvent = false
s.ForeachReplica(func(pr *PeerReplica) bool {
if pr.workerID == id && pr.handleEvent() {
hasEvent = true
}
return true
})
}
}
}
func (s *defaultResourceStore) runHBTask(ctx context.Context) {
log.Infof("resource store hb task started")
ticker := time.NewTicker(s.hbInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
log.Infof("resource store hb task exit")
return
case <-ticker.C:
s.doHB()
}
}
}
func (s *defaultResourceStore) doHB() {
s.ForeachReplica(func(pr *PeerReplica) bool {
if pr.IsLeader() {
pr.Heartbeat()
}
return true
})
}
func (s *defaultResourceStore) handleHB(msg *hbMsg) interface{} {
pr := s.GetPeerReplica(msg.res.ID(), false)
if pr == nil {
pr, err := CreatePeerReplica(s, msg.res, s.handler, s.elector)
if err != nil {
log.Fatalf("create peer replica %+v failed with %+v",
msg.res,
err)
return nil
}
s.AddReplica(pr)
return nil
}
return pr.onHB(msg)
}
func (s *defaultResourceStore) handleHBACK(msg *hbACKMsg) interface{} {
pr := s.GetPeerReplica(msg.res.ID(), true)
if pr == nil {
return nil
}
rsp := pr.onHBACK(msg)
if rsp != nil {
s.transport.Send(msg.peer.ContainerID, rsp)
}
return nil
}
func (s *defaultResourceStore) handleRemovePR(msg *removeMsg) interface{} {
pr := s.GetPeerReplica(msg.id, false)
if pr == nil {
return nil
}
pr.onRemove(msg)
return nil
}
func (s *defaultResourceStore) RemoveReplica(id uint64) {
s.replicas.Delete(id)
}