forked from nats-io/nats-streaming-server
/
common.go
395 lines (341 loc) · 9.74 KB
/
common.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright 2016 Apcera Inc. All rights reserved.
package stores
import (
"sort"
"sync"
"github.com/nats-io/go-nats-streaming/pb"
"github.com/nats-io/nats-streaming-server/spb"
)
// format string used to report that limit is reached when storing
// messages.
var droppingMsgsFmt = "WARNING: Reached limits for store %q (msgs=%v/%v bytes=%v/%v), " +
"dropping old messages to make room for new ones."
// commonStore contains everything that is common to any type of store
type commonStore struct {
sync.RWMutex
limits ChannelLimits
closed bool
}
// genericStore is the generic store implementation with a map of channels.
type genericStore struct {
commonStore
name string
channels map[string]*ChannelStore
clients map[string]*Client
}
// genericSubStore is the generic store implementation that manages subscriptions
// for a given channel.
type genericSubStore struct {
commonStore
subject string // Can't be wildcard
subsCount int
maxSubID uint64
}
// genericMsgStore is the generic store implementation that manages messages
// for a given channel.
type genericMsgStore struct {
commonStore
subject string // Can't be wildcard
first uint64
last uint64
msgs map[uint64]*pb.MsgProto
totalCount int
totalBytes uint64
hitLimit bool // indicates if store had to drop messages due to limit
}
////////////////////////////////////////////////////////////////////////////
// genericStore methods
////////////////////////////////////////////////////////////////////////////
// init initializes the structure of a generic store
func (gs *genericStore) init(name string, limits *ChannelLimits) {
gs.name = name
if limits == nil {
gs.limits = DefaultChannelLimits
} else {
gs.limits = *limits
}
// Do not use limits values to create the map.
gs.channels = make(map[string]*ChannelStore)
gs.clients = make(map[string]*Client)
}
// Init can be used to initialize the store with server's information.
func (gs *genericStore) Init(info *spb.ServerInfo) error {
return nil
}
// Name returns the type name of this store
func (gs *genericStore) Name() string {
return gs.name
}
// SetChannelLimits sets the limit for the messages and subscriptions stores.
func (gs *genericStore) SetChannelLimits(limits ChannelLimits) {
gs.Lock()
gs.limits = limits
gs.Unlock()
}
// LookupChannel returns a ChannelStore for the given channel.
func (gs *genericStore) LookupChannel(channel string) *ChannelStore {
gs.RLock()
cs := gs.channels[channel]
gs.RUnlock()
return cs
}
// HasChannel returns true if this store has any channel
func (gs *genericStore) HasChannel() bool {
gs.RLock()
l := len(gs.channels)
gs.RUnlock()
return l > 0
}
// State returns message store statistics for a given channel ('*' for all)
func (gs *genericStore) MsgsState(channel string) (numMessages int, byteSize uint64, err error) {
numMessages = 0
byteSize = 0
err = nil
if channel == AllChannels {
gs.RLock()
cs := gs.channels
gs.RUnlock()
for _, c := range cs {
n, b, lerr := c.Msgs.State()
if lerr != nil {
err = lerr
return
}
numMessages += n
byteSize += b
}
} else {
cs := gs.LookupChannel(channel)
if cs != nil {
numMessages, byteSize, err = cs.Msgs.State()
}
}
return
}
// canAddChannel returns true if the current number of channels is below the limit.
// Store lock is assumed to be locked.
func (gs *genericStore) canAddChannel() error {
if len(gs.channels) >= gs.limits.MaxChannels {
return ErrTooManyChannels
}
return nil
}
// AddClient stores information about the client identified by `clientID`.
func (gs *genericStore) AddClient(clientID, hbInbox string, userData interface{}) (*Client, bool, error) {
c := &Client{spb.ClientInfo{ID: clientID, HbInbox: hbInbox}, userData}
gs.Lock()
oldClient := gs.clients[clientID]
if oldClient != nil {
gs.Unlock()
return oldClient, false, nil
}
gs.clients[c.ID] = c
gs.Unlock()
return c, true, nil
}
// GetClient returns the stored Client, or nil if it does not exist.
func (gs *genericStore) GetClient(clientID string) *Client {
gs.RLock()
c := gs.clients[clientID]
gs.RUnlock()
return c
}
// GetClients returns all stored Client objects, as a map keyed by client IDs.
func (gs *genericStore) GetClients() map[string]*Client {
gs.RLock()
clients := make(map[string]*Client, len(gs.clients))
for k, v := range gs.clients {
clients[k] = v
}
gs.RUnlock()
return clients
}
// GetClientsCount returns the number of registered clients
func (gs *genericStore) GetClientsCount() int {
gs.RLock()
count := len(gs.clients)
gs.RUnlock()
return count
}
// DeleteClient deletes the client identified by `clientID`.
func (gs *genericStore) DeleteClient(clientID string) *Client {
gs.Lock()
c := gs.clients[clientID]
if c != nil {
delete(gs.clients, clientID)
}
gs.Unlock()
return c
}
// Close closes all stores
func (gs *genericStore) Close() error {
gs.Lock()
defer gs.Unlock()
if gs.closed {
return nil
}
gs.closed = true
return gs.close()
}
// close closes all stores. Store lock is assumed held on entry
func (gs *genericStore) close() error {
var err error
var lerr error
for _, cs := range gs.channels {
lerr = cs.Subs.Close()
if lerr != nil && err == nil {
err = lerr
}
lerr = cs.Msgs.Close()
if lerr != nil && err == nil {
err = lerr
}
}
return err
}
////////////////////////////////////////////////////////////////////////////
// genericMsgStore methods
////////////////////////////////////////////////////////////////////////////
// init initializes this generic message store
func (gms *genericMsgStore) init(subject string, limits ChannelLimits) {
gms.subject = subject
gms.limits = limits
// FIXME(ik) - Long term, msgs map should probably not be part of the
// generic store.
// We could use limits.MaxNumMsgs for the size of the map, but that
// may be too big if there is lots of channels with only few messages.
// The map will grow as needed.
gms.msgs = make(map[uint64]*pb.MsgProto, 64)
}
// State returns some statistics related to this store
func (gms *genericMsgStore) State() (numMessages int, byteSize uint64, err error) {
gms.RLock()
c, b := gms.totalCount, gms.totalBytes
gms.RUnlock()
return c, b, nil
}
// FirstSequence returns sequence for first message stored.
func (gms *genericMsgStore) FirstSequence() uint64 {
gms.RLock()
first := gms.first
gms.RUnlock()
return first
}
// LastSequence returns sequence for last message stored.
func (gms *genericMsgStore) LastSequence() uint64 {
gms.RLock()
last := gms.last
gms.RUnlock()
return last
}
// FirstAndLastSequence returns sequences for the first and last messages stored.
func (gms *genericMsgStore) FirstAndLastSequence() (uint64, uint64) {
gms.RLock()
first, last := gms.first, gms.last
gms.RUnlock()
return first, last
}
// Lookup returns the stored message with given sequence number.
func (gms *genericMsgStore) Lookup(seq uint64) *pb.MsgProto {
gms.RLock()
m := gms.msgs[seq]
gms.RUnlock()
return m
}
// FirstMsg returns the first message stored.
func (gms *genericMsgStore) FirstMsg() *pb.MsgProto {
gms.RLock()
m := gms.msgs[gms.first]
gms.RUnlock()
return m
}
// LastMsg returns the last message stored.
func (gms *genericMsgStore) LastMsg() *pb.MsgProto {
gms.RLock()
m := gms.msgs[gms.last]
gms.RUnlock()
return m
}
func (gms *genericMsgStore) Flush() error {
// no-op
return nil
}
// GetSequenceFromTimestamp returns the sequence of the first message whose
// timestamp is greater or equal to given timestamp.
func (gms *genericMsgStore) GetSequenceFromTimestamp(timestamp int64) uint64 {
gms.RLock()
defer gms.RUnlock()
index := sort.Search(len(gms.msgs), func(i int) bool {
m := gms.msgs[uint64(i)+gms.first]
if m.Timestamp >= timestamp {
return true
}
return false
})
return uint64(index) + gms.first
}
// Close closes this store.
func (gms *genericMsgStore) Close() error {
return nil
}
////////////////////////////////////////////////////////////////////////////
// genericSubStore methods
////////////////////////////////////////////////////////////////////////////
// init initializes the structure of a generic sub store
func (gss *genericSubStore) init(channel string, limits ChannelLimits) {
gss.subject = channel
gss.limits = limits
}
// CreateSub records a new subscription represented by SubState. On success,
// it records the subscription's ID in SubState.ID. This ID is to be used
// by the other SubStore methods.
func (gss *genericSubStore) CreateSub(sub *spb.SubState) error {
gss.Lock()
err := gss.createSub(sub)
gss.Unlock()
return err
}
// UpdateSub updates a given subscription represented by SubState.
func (gss *genericSubStore) UpdateSub(sub *spb.SubState) error {
return nil
}
// createSub is the unlocked version of CreateSub that can be used by
// non-generic implementations.
func (gss *genericSubStore) createSub(sub *spb.SubState) error {
if gss.subsCount >= gss.limits.MaxSubs {
return ErrTooManySubs
}
// Bump the max value before assigning it to the new subscription.
gss.maxSubID++
gss.subsCount++
// This new subscription has the max value.
sub.ID = gss.maxSubID
return nil
}
// DeleteSub invalidates this subscription.
func (gss *genericSubStore) DeleteSub(subid uint64) {
gss.Lock()
gss.subsCount--
gss.Unlock()
}
// AddSeqPending adds the given message seqno to the given subscription.
func (gss *genericSubStore) AddSeqPending(subid, seqno uint64) error {
// no-op
return nil
}
// AckSeqPending records that the given message seqno has been acknowledged
// by the given subscription.
func (gss *genericSubStore) AckSeqPending(subid, seqno uint64) error {
// no-op
return nil
}
// Flush is for stores that may buffer operations and need them to be persisted.
func (gss *genericSubStore) Flush() error {
// no-op
return nil
}
// Close closes this store
func (gss *genericSubStore) Close() error {
// no-op
return nil
}