-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
251 lines (224 loc) · 6.89 KB
/
server.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
package main
import (
"context"
"errors"
pb "esdeath_go/esdeath_proto"
"esdeath_go/miku"
"fmt"
"github.com/hashicorp/raft"
cmap "github.com/orcaman/concurrent-map/v2"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
"math/rand"
rand2 "math/rand/v2"
"net"
"sync"
"time"
)
var (
rkv *miku.RMikuKv
// key 是topic,value是consumerGroups。用于处理一个topic下有多个consumerGroups的情况
topicToManyConsumerGroupsMap = cmap.New[*cmap.ConcurrentMap[string, struct{}]]()
)
func Start() error {
rkv = miku.Start()
hostAddr := Conf.ServerAddr
lis, err := net.Listen("tcp", hostAddr)
if err != nil {
return fmt.Errorf("failed to listen on %s: %v", hostAddr, err)
}
serv := grpc.NewServer()
esdeathSer := &esdeathServer{
pullHoldNotifyContainer: cmap.New[chan struct{}](),
pullLock: &PullServiceLock{groups: cmap.New[*ServiceLock]()},
}
pb.RegisterEsdeathServer(serv, esdeathSer)
if err := serv.Serve(lis); err != nil {
return fmt.Errorf("failed to serve on %s: %v", hostAddr, err)
}
log.Infoln("esdeathServer listening at", lis.Addr())
return nil
}
func GracefulStop() {
if err := rkv.Shutdown(); err != nil {
log.Errorln("Error shutting down kv: ", err)
}
log.Infoln("kv shutdown success")
if err := rkv.RaftShutdown(); err != nil {
log.Errorln("Error shutting down raft: ", err)
}
log.Infoln("raft shutdown success")
}
// 提供对client的交互grpc服务
type esdeathServer struct {
pb.UnimplementedEsdeathServer
// 确保一个消息不会被同时消费,不同topic消费是并发的
pullLock *PullServiceLock
// 长轮询通知容器:key 为服务名,value 为通知结束hold chan
// 在消息未达到延迟时间的时候,长轮询的请求会被hold住,直到消息到达延迟时间或者有新消息到达
pullHoldNotifyContainer cmap.ConcurrentMap[string, chan struct{}]
}
// AddDelayMsg 添加延迟消息
// 添加延迟消息首先会新增一条延迟消息,然后会尝试唤醒长轮询的请求
func (es *esdeathServer) AddDelayMsg(c context.Context, r *pb.DelayMsgAdd) (*pb.AddDelayMsgResult, error) {
log.Infoln("AddDelayMsg: ", r)
result, err := produceReqHandle(r)
if err != nil {
log.Errorln("Error handling AddDelayMsg: ", err)
return &pb.AddDelayMsgResult{BaseResult: errorRespHandle(err)}, nil
}
tryNotifyPullHold(es, r.Topic)
return result, nil
}
func tryNotifyPullHold(es *esdeathServer, topic string) {
if notifyCh, ok := es.pullHoldNotifyContainer.Pop(topic); ok && notifyCh != nil {
select {
case notifyCh <- struct{}{}:
default:
}
}
}
func (es *esdeathServer) PullDelayMsg(c context.Context, r *pb.DelayMsgPull) (*pb.PullDelayMsgResult, error) {
doTopicToManyConsumerGroup(r)
lock := es.pullLock.getOrAddLock(newServiceLock(r.Topic, r.ConsumerGroup))
lock.Lock()
defer lock.Unlock()
log.Debugln("PullDelayMsg into: ", r)
var (
delayTimestamp int64
msg *PulledMsg
err error
)
isHold := false
for {
if !isHold {
msg, err = pullReqHandle(r)
isHold = true
} else {
msg, err = es.holdProcess(r, delayTimestamp)
}
if err != nil {
return &pb.PullDelayMsgResult{BaseResult: errorRespHandle(err)}, nil
}
if msg == nil {
// 没有任何消息则设置默认的延迟时间
delayTimestamp = time.Now().UnixMilli() + defHoldTime.Milliseconds() + rand2.Int64N(5000)
} else {
delayTimestamp = msg.DelayTimestamp
}
if msg != nil && msg.MsgId != "" {
return &pb.PullDelayMsgResult{
BaseResult: successResult(),
Topic: msg.Topic,
Tag: msg.Tag,
MsgId: msg.MsgId,
Payload: string(msg.Payload),
}, nil
}
}
}
func doTopicToManyConsumerGroup(r *pb.DelayMsgPull) {
newConsumerGroupSet := cmap.New[struct{}]()
if topicToManyConsumerGroupsMap.SetIfAbsent(r.Topic, &newConsumerGroupSet) {
newConsumerGroupSet.Set(r.ConsumerGroup, struct{}{})
log.Debugln("add new topic: ", r.Topic)
return
}
oldConsumerGroupSet, _ := topicToManyConsumerGroupsMap.Get(r.Topic)
oldConsumerGroupSet.Set(r.ConsumerGroup, struct{}{})
log.Debugln("add group: ", r.ConsumerGroup)
}
var holdEndError = errors.New("hold end")
// 长轮询处理
func (es *esdeathServer) holdProcess(r *pb.DelayMsgPull, delayTimestamp int64) (*PulledMsg, error) {
notify := make(chan struct{}, 1)
es.pullHoldNotifyContainer.Set(r.Topic, notify)
holdTime := time.Duration(delayTimestamp-time.Now().UnixMilli()) * time.Millisecond
log.Infoln("hold time: ", holdTime)
select {
case <-time.After(holdTime):
es.pullHoldNotifyContainer.Pop(r.Topic)
return nil, holdEndError
case <-notify:
return pullReqHandle(r)
}
}
func (*esdeathServer) AckDelayMsg(c context.Context, r *pb.DelayMsgAck) (*pb.AckDelayMsgResult, error) {
log.Infoln("AckDelayMsg: ", r)
if err := reqConsumeResultHandle(r); err != nil {
return &pb.AckDelayMsgResult{BaseResult: errorRespHandle(err)}, nil
}
return &pb.AckDelayMsgResult{BaseResult: successResult()}, nil
}
func (*esdeathServer) CancelDelayMsg(c context.Context, r *pb.DelayMsgCancel) (*pb.CancelDelayMsgResult, error) {
log.Infoln("CancelDelayMsg: ", r)
result, err := cancelReqHandle(r)
if err != nil {
return &pb.CancelDelayMsgResult{BaseResult: errorRespHandle(err)}, nil
}
return result, nil
}
func (*esdeathServer) GetRole(c context.Context, r *emptypb.Empty) (*pb.RoleResult, error) {
log.Infoln("GetRole: ", r)
result := &pb.RoleResult{
BaseResult: successResult(),
Role: pb.Role_FOLLOWER,
}
if rkv.IsLeader() {
result.Role = pb.Role_LEADER
} else {
time.Sleep(time.Duration(1+rand.Intn(3)) * time.Second)
}
return result, nil
}
func errorRespHandle(err error) *pb.BaseResult {
if errors.Is(err, holdEndError) {
return &pb.BaseResult{
Status: pb.ResultStatus_NO_MESSAGE,
Desc: "no message",
}
}
if errors.Is(err, raft.ErrNotLeader) || errors.Is(err, raft.ErrLeadershipLost) {
return &pb.BaseResult{
Status: pb.ResultStatus_REFUSE_BY_FOLLOWER,
Desc: successDesc,
}
}
return &pb.BaseResult{
Status: pb.ResultStatus_FAIL,
Desc: err.Error(),
}
}
const successDesc = "success"
func successResult() *pb.BaseResult {
return &pb.BaseResult{
Status: pb.ResultStatus_SUCCESS,
Desc: successDesc,
}
}
type PullServiceLock struct {
groups cmap.ConcurrentMap[string, *ServiceLock]
}
func (p *PullServiceLock) getOrAddLock(newLock *ServiceLock) *ServiceLock {
if p.groups.SetIfAbsent(newLock.lockKey(), newLock) {
return newLock
}
lock, _ := p.groups.Get(newLock.lockKey())
return lock
}
// topic + consumerGroup 级别的锁,如果只有一个topic消费,则默认使用defConsumerGroup
type ServiceLock struct {
sync.Mutex
Topic string
ConsumerGroup string
}
func (s *ServiceLock) lockKey() string {
return s.Topic + s.ConsumerGroup
}
func newServiceLock(topic, consumeGroup string) *ServiceLock {
if consumeGroup == "" {
consumeGroup = defConsumerGroup
}
return &ServiceLock{Topic: topic, ConsumerGroup: consumeGroup}
}