forked from didapinchegit/go_rocket_mq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
138 lines (117 loc) · 3.24 KB
/
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
package rocketmq
import (
"errors"
"log"
"sync"
"sync/atomic"
)
const (
MEMORY_FIRST_THEN_STORE = 0
READ_FROM_MEMORY = 1
READ_FROM_STORE = 2
)
type OffsetStore interface {
//load() error
updateOffset(mq *MessageQueue, offset int64, increaseOnly bool)
readOffset(mq *MessageQueue, flag int) int64
//persistAll(mqs []MessageQueue)
//persist(mq MessageQueue)
//removeOffset(mq MessageQueue)
//cloneOffsetTable(topic string) map[MessageQueue]int64
}
type RemoteOffsetStore struct {
groupName string
mqClient *MqClient
offsetTable map[MessageQueue]int64
mutex sync.Mutex
}
func (self *RemoteOffsetStore) readOffset(mq *MessageQueue, readType int) int64 {
switch readType {
case MEMORY_FIRST_THEN_STORE:
case READ_FROM_MEMORY:
offset, ok := self.offsetTable[*mq]
if ok {
return offset
} else if readType == READ_FROM_MEMORY {
return -1
}
case READ_FROM_STORE:
offset, err := self.fetchConsumeOffsetFromBroker(mq)
if err != nil {
log.Print(err)
return -1
}
self.updateOffset(mq, offset, false)
return offset
}
return -1
}
func (self *RemoteOffsetStore) fetchConsumeOffsetFromBroker(mq *MessageQueue) (int64, error) {
brokerAddr, _, found := self.mqClient.findBrokerAddressInSubscribe(mq.brokerName, 0, false)
if !found {
if err := self.mqClient.updateTopicRouteInfoFromNameServerByTopic(mq.topic); err != nil {
return 0, err
}
brokerAddr, _, found = self.mqClient.findBrokerAddressInSubscribe(mq.brokerName, 0, false)
}
if found {
requestHeader := &QueryConsumerOffsetRequestHeader{}
requestHeader.Topic = mq.topic
requestHeader.QueueId = mq.queueId
requestHeader.ConsumerGroup = self.groupName
return self.mqClient.queryConsumerOffset(brokerAddr, requestHeader, 3000)
}
return 0, errors.New("fetch consumer offset error")
}
func (self *RemoteOffsetStore) persist(mq *MessageQueue) {
offset, ok := self.offsetTable[*mq]
if ok {
err := self.updateConsumeOffsetToBroker(mq, offset)
if err != nil {
log.Print(err)
}
}
}
type UpdateConsumerOffsetRequestHeader struct {
consumerGroup string
topic string
queueId int32
commitOffset int64
}
func (self *RemoteOffsetStore) updateConsumeOffsetToBroker(mq *MessageQueue, offset int64) error {
addr, found, _ := self.mqClient.findBrokerAddressInSubscribe(mq.brokerName, 0, false)
if !found {
if err := self.mqClient.updateTopicRouteInfoFromNameServerByTopic(mq.topic); err != nil {
return err
}
addr, found, _ = self.mqClient.findBrokerAddressInSubscribe(mq.brokerName, 0, false)
}
if found {
requestHeader := &UpdateConsumerOffsetRequestHeader{
consumerGroup: self.groupName,
topic: mq.topic,
queueId: mq.queueId,
commitOffset: offset,
}
self.mqClient.updateConsumerOffsetOneway(addr, requestHeader, 5*1000)
return nil
}
return errors.New("not found broker")
}
func (self *RemoteOffsetStore) updateOffset(mq *MessageQueue, offset int64, increaseOnly bool) {
self.mutex.Lock()
defer self.mutex.Unlock()
if mq != nil {
offsetOld, ok := self.offsetTable[*mq]
if !ok {
self.offsetTable[*mq] = offset
} else {
if increaseOnly {
atomic.AddInt64(&offsetOld, offset)
self.offsetTable[*mq] = offsetOld
} else {
self.offsetTable[*mq] = offset
}
}
}
}