-
Notifications
You must be signed in to change notification settings - Fork 3
/
bulksetack.got
247 lines (229 loc) · 9.12 KB
/
bulksetack.got
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
package store
import (
"encoding/binary"
"io"
"sync"
"sync/atomic"
"go.uber.org/zap"
)
// bsam: entries:n
// bsam entry: keyA:8, keyB:8, timestampbits:8
{{if eq .t "value"}}
const _{{.TT}}_BULK_SET_ACK_MSG_TYPE = 0x39589f4746844e3b
const _{{.TT}}_BULK_SET_ACK_MSG_ENTRY_LENGTH = 24
{{else}}
const _{{.TT}}_BULK_SET_ACK_MSG_TYPE = 0xec3577cc6dbb75bb
const _{{.TT}}_BULK_SET_ACK_MSG_ENTRY_LENGTH = 40
{{end}}
type {{.t}}BulkSetAckState struct {
msgCap int
inWorkers int
inBulkSetAckMsgs int
outBulkSetAckMsgs int
startupShutdownLock sync.Mutex
inNotifyChan chan *bgNotification
inMsgChan chan *{{.t}}BulkSetAckMsg
inFreeMsgChan chan *{{.t}}BulkSetAckMsg
outFreeMsgChan chan *{{.t}}BulkSetAckMsg
}
type {{.t}}BulkSetAckMsg struct {
store *default{{.T}}Store
body []byte
}
func (store *default{{.T}}Store) bulkSetAckConfig(cfg *{{.T}}StoreConfig) {
store.bulkSetAckState.msgCap = cfg.BulkSetAckMsgCap
store.bulkSetAckState.inWorkers = cfg.InBulkSetAckWorkers
store.bulkSetAckState.inBulkSetAckMsgs = cfg.InBulkSetAckMsgs
store.bulkSetAckState.outBulkSetAckMsgs = cfg.OutBulkSetAckMsgs
if store.msgRing != nil {
store.msgRing.SetMsgHandler(_{{.TT}}_BULK_SET_ACK_MSG_TYPE, store.newInBulkSetAckMsg)
}
}
func (store *default{{.T}}Store) bulkSetAckStartup() {
store.bulkSetAckState.startupShutdownLock.Lock()
if store.bulkSetAckState.inNotifyChan == nil {
store.bulkSetAckState.inNotifyChan = make(chan *bgNotification, 1)
store.bulkSetAckState.inMsgChan = make(chan *{{.t}}BulkSetAckMsg, store.bulkSetAckState.inBulkSetAckMsgs)
store.bulkSetAckState.inFreeMsgChan = make(chan *{{.t}}BulkSetAckMsg, store.bulkSetAckState.inBulkSetAckMsgs)
for i := 0; i < cap(store.bulkSetAckState.inFreeMsgChan); i++ {
store.bulkSetAckState.inFreeMsgChan <- &{{.t}}BulkSetAckMsg{
store: store,
body: make([]byte, store.bulkSetAckState.msgCap),
}
}
store.bulkSetAckState.outFreeMsgChan = make(chan *{{.t}}BulkSetAckMsg, store.bulkSetAckState.outBulkSetAckMsgs)
for i := 0; i < cap(store.bulkSetAckState.outFreeMsgChan); i++ {
store.bulkSetAckState.outFreeMsgChan <- &{{.t}}BulkSetAckMsg{
store: store,
body: make([]byte, store.bulkSetAckState.msgCap),
}
}
go store.inBulkSetAckLauncher(store.bulkSetAckState.inNotifyChan)
}
store.bulkSetAckState.startupShutdownLock.Unlock()
}
func (store *default{{.T}}Store) bulkSetAckShutdown() {
store.bulkSetAckState.startupShutdownLock.Lock()
if store.bulkSetAckState.inNotifyChan != nil {
c := make(chan struct{}, 1)
store.bulkSetAckState.inNotifyChan <- &bgNotification{
action: _BG_DISABLE,
doneChan: c,
}
<-c
store.bulkSetAckState.inNotifyChan = nil
store.bulkSetAckState.inMsgChan = nil
store.bulkSetAckState.inFreeMsgChan = nil
store.bulkSetAckState.outFreeMsgChan = nil
}
store.bulkSetAckState.startupShutdownLock.Unlock()
}
func (store *default{{.T}}Store) inBulkSetAckLauncher(notifyChan chan *bgNotification) {
wg := &sync.WaitGroup{}
wg.Add(store.bulkSetAckState.inWorkers)
for i := 0; i < store.bulkSetAckState.inWorkers; i++ {
go store.inBulkSetAck(wg)
}
var notification *bgNotification
running := true
for running {
notification = <-notifyChan
if notification.action == _BG_DISABLE {
for i := 0; i < store.bulkSetAckState.inWorkers; i++ {
store.bulkSetAckState.inMsgChan <- nil
}
wg.Wait()
running = false
} else {
store.logger.Error("invalid action requested", zap.String("name", store.loggerPrefix + "inBulkSetAck"), zap.Int("action", int(notification.action)))
}
notification.doneChan <- struct{}{}
}
}
// newInBulkSetAckMsg reads bulk-set-ack messages from the MsgRing and puts
// them on the inMsgChan for the inBulkSetAck workers to work on.
func (store *default{{.T}}Store) newInBulkSetAckMsg(r io.Reader, l uint64) (uint64, error) {
var bsam *{{.t}}BulkSetAckMsg
select {
case bsam = <-store.bulkSetAckState.inFreeMsgChan:
default:
// If there isn't a free {{.t}}BulkSetAckMsg, just read and discard the
// incoming bulk-set-ack message.
left := l
var sn int
var err error
for left > 0 {
t := toss
if left < uint64(len(t)) {
t = t[:left]
}
sn, err = r.Read(t)
left -= uint64(sn)
if err != nil {
atomic.AddInt32(&store.inBulkSetAckInvalids, 1)
return l - left, err
}
}
atomic.AddInt32(&store.inBulkSetAckDrops, 1)
return l, nil
}
var n int
var sn int
var err error
// TODO: Need to read up the actual msg cap and toss rest.
if l > uint64(cap(bsam.body)) {
bsam.body = make([]byte, l)
}
bsam.body = bsam.body[:l]
n = 0
for n != len(bsam.body) {
sn, err = r.Read(bsam.body[n:])
n += sn
if err != nil {
store.bulkSetAckState.inFreeMsgChan <- bsam
atomic.AddInt32(&store.inBulkSetAckInvalids, 1)
return uint64(n), err
}
}
store.bulkSetAckState.inMsgChan <- bsam
atomic.AddInt32(&store.inBulkSetAcks, 1)
return l, nil
}
// inBulkSetAck actually processes incoming bulk-set-ack messages; there may be
// more than one of these workers.
func (store *default{{.T}}Store) inBulkSetAck(wg *sync.WaitGroup) {
for {
bsam := <-store.bulkSetAckState.inMsgChan
if bsam == nil {
break
}
ring := store.msgRing.Ring()
var rightwardPartitionShift uint64
if ring != nil {
rightwardPartitionShift = 64 - uint64(ring.PartitionBitCount())
}
b := bsam.body
// div mul just ensures any trailing bytes are dropped
l := len(b) / _{{.TT}}_BULK_SET_ACK_MSG_ENTRY_LENGTH * _{{.TT}}_BULK_SET_ACK_MSG_ENTRY_LENGTH
for o := 0; o < l; o += _{{.TT}}_BULK_SET_ACK_MSG_ENTRY_LENGTH {
keyA := binary.BigEndian.Uint64(b[o:])
if ring != nil && !ring.Responsible(uint32(keyA>>rightwardPartitionShift)) {
atomic.AddInt32(&store.inBulkSetAckWrites, 1)
timestampbits := binary.BigEndian.Uint64(b[o+{{if eq .t "value"}}16{{else}}32{{end}}:]) | _TSB_LOCAL_REMOVAL
ptimestampbits, err := store.write(keyA, binary.BigEndian.Uint64(b[o+8:]){{if eq .t "group"}}, binary.BigEndian.Uint64(b[o+16:]), binary.BigEndian.Uint64(b[o+24:]){{end}}, timestampbits, nil, true)
if err != nil {
atomic.AddInt32(&store.inBulkSetAckWriteErrors, 1)
} else if ptimestampbits >= timestampbits {
atomic.AddInt32(&store.inBulkSetAckWritesOverridden, 1)
}
}
}
store.bulkSetAckState.inFreeMsgChan <- bsam
}
wg.Done()
}
// newOutBulkSetAckMsg gives an initialized {{.t}}BulkSetAckMsg for filling out
// and eventually sending using the MsgRing. The MsgRing (or someone else if
// the message doesn't end up with the MsgRing) will call
// {{.t}}BulkSetAckMsg.Free eventually and the {{.t}}BulkSetAckMsg will be
// requeued for reuse later. There is a fixed number of outgoing
// {{.t}}BulkSetAckMsg instances that can exist at any given time, capping
// memory usage. Once the limit is reached, this method will block until a
// {{.t}}BulkSetAckMsg is available to return.
func (store *default{{.T}}Store) newOutBulkSetAckMsg() *{{.t}}BulkSetAckMsg {
bsam := <-store.bulkSetAckState.outFreeMsgChan
bsam.body = bsam.body[:0]
return bsam
}
func (bsam *{{.t}}BulkSetAckMsg) MsgType() uint64 {
return _{{.TT}}_BULK_SET_ACK_MSG_TYPE
}
func (bsam *{{.t}}BulkSetAckMsg) MsgLength() uint64 {
return uint64(len(bsam.body))
}
func (bsam *{{.t}}BulkSetAckMsg) WriteContent(w io.Writer) (uint64, error) {
n, err := w.Write(bsam.body)
return uint64(n), err
}
func (bsam *{{.t}}BulkSetAckMsg) Free(successes int, failures int) {
bsam.store.bulkSetAckState.outFreeMsgChan <- bsam
}
func (bsam *{{.t}}BulkSetAckMsg) add(keyA uint64, keyB uint64{{if eq .t "group"}}, childKeyA uint64, childKeyB uint64{{end}}, timestampbits uint64) bool {
o := len(bsam.body)
if o+_{{.TT}}_BULK_SET_ACK_MSG_ENTRY_LENGTH >= cap(bsam.body) {
return false
}
bsam.body = bsam.body[:o+_{{.TT}}_BULK_SET_ACK_MSG_ENTRY_LENGTH]
{{if eq .t "value"}}
binary.BigEndian.PutUint64(bsam.body[o:], keyA)
binary.BigEndian.PutUint64(bsam.body[o+8:], keyB)
binary.BigEndian.PutUint64(bsam.body[o+16:], timestampbits)
{{else}}
binary.BigEndian.PutUint64(bsam.body[o:], keyA)
binary.BigEndian.PutUint64(bsam.body[o+8:], keyB)
binary.BigEndian.PutUint64(bsam.body[o+16:], childKeyA)
binary.BigEndian.PutUint64(bsam.body[o+24:], childKeyB)
binary.BigEndian.PutUint64(bsam.body[o+32:], timestampbits)
{{end}}
return true
}