-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathstore.go
145 lines (125 loc) · 3.54 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
139
140
141
142
143
144
145
package rocketmq
import (
"errors"
"github.com/golang/glog"
"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
offsetTableLock sync.RWMutex
}
func (self *RemoteOffsetStore) readOffset(mq *MessageQueue, readType int) int64 {
switch readType {
case MEMORY_FIRST_THEN_STORE:
case READ_FROM_MEMORY:
self.offsetTableLock.RLock()
offset, ok := self.offsetTable[*mq]
self.offsetTableLock.RUnlock()
if ok {
return offset
} else if readType == READ_FROM_MEMORY {
return -1
}
case READ_FROM_STORE:
offset, err := self.fetchConsumeOffsetFromBroker(mq)
if err != nil {
glog.Error(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 {
glog.Error(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) {
if mq != nil {
self.offsetTableLock.RLock()
offsetOld, ok := self.offsetTable[*mq]
self.offsetTableLock.RUnlock()
if !ok {
self.offsetTableLock.Lock()
self.offsetTable[*mq] = offset
self.offsetTableLock.Unlock()
} else {
if increaseOnly {
atomic.AddInt64(&offsetOld, offset)
self.offsetTableLock.Lock()
self.offsetTable[*mq] = offsetOld
self.offsetTableLock.Unlock()
} else {
self.offsetTableLock.Lock()
self.offsetTable[*mq] = offset
self.offsetTableLock.Unlock()
}
}
}
}