generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
queue.go
477 lines (382 loc) · 12.7 KB
/
queue.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package coherence
import (
"context"
"errors"
"github.com/google/uuid"
"github.com/oracle/coherence-go-client/coherence/extractors"
"github.com/oracle/coherence-go-client/coherence/processors"
"sync"
"time"
)
const (
longMaxValue int64 = 9223372036854775807
longMinValue int64 = -9223372036854775808
offerResultSuccess = 1
offerResultFailedCapacity = 2
)
var (
_ NamedQueue[string] = &namedQueue[string]{}
_ NamedBlockingQueue[string] = &namedBlockingQueue[string]{}
ErrQueueFailedCapacity = errors.New("the queue has reached capacity, unable to offer")
ErrQueueFailedOffer = errors.New("did not return success for offer")
ErrQueueTimedOut = errors.New("operation timed out for Poll() or Peek()")
)
// NamedQueue defines a non-blocking Queue implementation.
type NamedQueue[V any] interface {
// Offer inserts the specified value to the end of this queue if it is possible to do
// so immediately without violating capacity restrictions. If queue is full then
// [ErrQueueFailedCapacity] is returned, if error is nil the element was added to the queue.
Offer(value V) error
// Poll retrieves and removes the head of this queue. If error is nil and the returned
// value and error is nil this means that there was no entry on the head of the queue.
Poll() (*V, error)
// Peek retrieves, but does not remove, the head of this queue. If error is nil and nil value
// return then there is no entry on the head of the queue.
Peek() (*V, error)
// GetName returns the cache name used for the queue.
GetName() string
// Size returns the current size of the queue.
Size() (int, error)
// Close closes a queue and removes any resources associated with it on the client side.
Close() error
}
// NamedBlockingQueue defines a blocking Queue implementation.
type NamedBlockingQueue[V any] interface {
// Offer inserts the specified value to the end of the queue if it is possible to do
// so immediately without violating capacity restrictions. If queue is full then
// [ErrQueueFailedCapacity] is returned, if error is nil the element was added to the queue.
Offer(value V) error
// Poll retrieves and removes the head of this queue within the specified timeout.
// If error is [ErrQueueTimedOut] this means the operation failed to get a value within the timeout.
// If error is nil, then the value on the head of the queue is returned.
Poll(timeout time.Duration) (*V, error)
// Peek retrieves, but does not remove, the head of this queue within the specified timeout.
// If error is [ErrQueueTimedOut] this means the operation failed to get a value within the timeout.
// If error is nil, then the value on the head of the queue is returned.
Peek(timeout time.Duration) (*V, error)
// GetName returns the cache name used for the queue.
GetName() string
// Size returns the current size of the queue.
Size() (int, error)
// Close closes a queue and removes any resources associated with it on the client side.
Close() error
}
// QueueKey defines the key of a queue entry. Exported only for serialization.
type QueueKey struct {
Class string `json:"@class"`
Hash int `json:"hash"`
ID int64 `json:"id"`
}
// QueueOfferResult defines the result of a queue Offer(). Exported only for serialization.
type QueueOfferResult struct {
Class string `json:"@class"`
ID int64 `json:"id"`
Result int `json:"result"`
}
// QueuePollResult defines the result of a queue Poll(). Exported only for serialization.
type QueuePollResult[V any] struct {
Class string `json:"@class"`
ID int64 `json:"id"`
Element V `json:"element"`
Present bool `json:"present"`
}
type baseQueueClient[V any] struct {
cache NamedMap[QueueKey, V]
queueNameHash int
ctx context.Context
}
type namedQueue[V any] struct {
*baseQueueClient[V]
}
type namedBlockingQueue[V any] struct {
*baseQueueClient[V]
queueCacheListener *queueCacheListener[V]
notifyMutex sync.Mutex
notifier *queueNotifier
}
// GetNamedQueue returns a new [NamedQueue].
func GetNamedQueue[V any](ctx context.Context, session *Session, queueName string) (NamedQueue[V], error) {
var (
existingQueue interface{}
ok bool
)
// protect updates to maps
session.mapMutex.Lock()
// check to see if we already have an entry for the queue
if existingQueue, ok = session.queues[queueName]; ok {
defer session.mapMutex.Unlock()
existing, ok2 := existingQueue.(*NamedQueue[V])
if !ok2 {
// the casting failed so return an error indicating the queue exists with different type mappings
return nil, getExistingError("NamedQueue", queueName)
}
session.debug("using existing NamedQueue", existing)
return *existing, nil
}
// put a place-holder incase second go routine gets here
session.queues[queueName] = nil
session.mapMutex.Unlock()
bq, err := newBaseQueueClient[V](ctx, session, queueName)
if err != nil {
return nil, err
}
queue := &namedQueue[V]{
baseQueueClient: bq,
}
session.queues[queueName] = queue
return queue, nil
}
// GetBlockingNamedQueue returns a new [NamedBlockingQueue].
func GetBlockingNamedQueue[V any](ctx context.Context, session *Session, queueName string) (NamedBlockingQueue[V], error) {
var (
existingQueue interface{}
ok bool
)
// protect updates to maps
session.mapMutex.Lock()
// check to see if we already have an entry for the queue
if existingQueue, ok = session.queues[queueName]; ok {
defer session.mapMutex.Unlock()
existing, ok2 := existingQueue.(*NamedBlockingQueue[V])
if !ok2 {
// the casting failed so return an error indicating the queue exists with different type mappings
return nil, getExistingError("NamedBlockingQueue", queueName)
}
session.debug("using existing NamedBlockingQueue", existing)
return *existing, nil
}
// put a place-holder incase second go routine gets here
session.queues[queueName] = nil
session.mapMutex.Unlock()
bq, err := newBaseQueueClient[V](ctx, session, queueName)
if err != nil {
return nil, err
}
session.mapMutex.Lock()
defer session.mapMutex.Unlock()
queue := &namedBlockingQueue[V]{
baseQueueClient: bq,
notifier: newQueueNotifier(),
}
listener := newQueueCacheListener[V](queue)
queue.queueCacheListener = listener
err = bq.cache.AddListener(ctx, listener.listener)
if err != nil {
return nil, err
}
session.queues[queueName] = queue
return queue, nil
}
func newBaseQueueClient[V any](ctx context.Context, session *Session, queueName string) (*baseQueueClient[V], error) {
if session.closed {
return nil, ErrClosed
}
bq := baseQueueClient[V]{}
namedMap, hash, err := setupNamedMap[V](ctx, session, queueName)
if err != nil {
return nil, err
}
bq.cache = namedMap
bq.ctx = ctx
bq.queueNameHash = hash
return &bq, nil
}
func setupNamedMap[V any](ctx context.Context, session *Session, queueName string) (NamedMap[QueueKey, V], int, error) {
namedMap, err := GetNamedMap[QueueKey, V](session, queueName)
if err != nil {
return nil, 0, err
}
// add QueueKeyExtractor index
err = AddIndex[QueueKey, V](ctx, namedMap, extractors.QueueKeyExtractor[V](), true)
if err != nil {
return nil, 0, err
}
// get the queue name hash
var (
hash *int
key = createQueueKey(1, 1) // mock
)
hash, err = Invoke[QueueKey, V, int](ctx, namedMap, key, processors.QueueNameHashProcessor(queueName))
if err != nil {
return nil, 0, err
}
return namedMap, *hash, nil
}
// NamedQueue
func (nq *namedQueue[V]) Offer(value V) error {
return offer(nq.baseQueueClient, value, longMaxValue)
}
func (nq *namedQueue[V]) Poll() (*V, error) {
return poll(nq.baseQueueClient, longMaxValue)
}
func (nq *namedQueue[V]) Peek() (*V, error) {
return peek(nq.baseQueueClient, longMaxValue)
}
func (nq *namedQueue[V]) Close() error {
return nil
}
func (nq *namedQueue[V]) GetName() string {
return nq.cache.Name()
}
func (nq *namedQueue[V]) Size() (int, error) {
return nq.cache.Size(nq.ctx)
}
// NamedBlockingQueue
func (bq *namedBlockingQueue[V]) Offer(value V) error {
return offer(bq.baseQueueClient, value, longMaxValue)
}
// Poll attempts to call Poll() with the specified timeout. If err == [ErrQueueTimedOut] this means
// the operation timed out. If error is non nil, this indicates an error, otherwise
// the Poll() was successful and the pointer to the value is returned.
func (bq *namedBlockingQueue[V]) Poll(timeout time.Duration) (*V, error) {
return bq.peekOrPoll(true, timeout)
}
func (bq *namedBlockingQueue[V]) Peek(timeout time.Duration) (*V, error) {
return bq.peekOrPoll(false, timeout)
}
func (bq *namedBlockingQueue[V]) peekOrPoll(isPoll bool, timeout time.Duration) (*V, error) {
for {
var (
err error
value *V
)
// lock while we are polling, specifically don't defer unlock
bq.notifyMutex.Lock()
if isPoll {
value, err = poll(bq.baseQueueClient, longMaxValue)
} else {
value, err = peek(bq.baseQueueClient, longMaxValue)
}
bq.notifyMutex.Unlock()
if err != nil {
return nil, err
}
if value != nil {
return value, nil
}
// no value, so wait on either event or timeout, subscribe for notification if any new entries arrive
// mutex is placed around the subscribe(), unsubscribe() and notifyAll() to ensure we don't try and read
// from a closed channel
id, ch := bq.notifier.subscribe()
select {
case <-ch:
// new item added, unsubscribe and attempt to poll() or peek() again,
bq.notifier.unsubscribe(id)
case <-time.After(timeout):
// timeout
bq.notifier.unsubscribe(id)
return nil, ErrQueueTimedOut
}
}
}
func (bq *namedBlockingQueue[V]) Close() error {
err := bq.cache.RemoveListener(bq.ctx, bq.queueCacheListener.listener)
if err != nil {
return err
}
return nil
}
func (bq *namedBlockingQueue[V]) GetName() string {
return bq.cache.Name()
}
func (bq *namedBlockingQueue[V]) Size() (int, error) {
return bq.cache.Size(bq.ctx)
}
// offer offers a value to the head or tail. id = longMaxValue for tail and
// id = longMinValue.
func offer[V any](nq *baseQueueClient[V], value V, id int64) error {
key := createQueueKey(nq.queueNameHash, id)
result, err := Invoke[QueueKey, V, QueueOfferResult](nq.ctx, nq.cache, key, processors.QueueOfferProcessor[V](value))
if err != nil {
return err
}
if result.Result == offerResultFailedCapacity {
return ErrQueueFailedCapacity
}
if result.Result == offerResultSuccess {
return nil
}
return ErrQueueFailedOffer
}
func poll[V any](nq *baseQueueClient[V], id int64) (*V, error) {
key := createQueueKey(nq.queueNameHash, id)
result, err := Invoke[QueueKey, V, QueuePollResult[V]](nq.ctx, nq.cache, key, processors.QueuePollProcessor())
if err != nil {
return nil, err
}
if result.Present {
return &result.Element, nil
}
return nil, nil
}
func peek[V any](nq *baseQueueClient[V], id int64) (*V, error) {
key := createQueueKey(nq.queueNameHash, id)
result, err := Invoke[QueueKey, V, QueuePollResult[V]](nq.ctx, nq.cache, key, processors.QueuePeekProcessor())
if err != nil {
return nil, err
}
if result.Present {
return &result.Element, nil
}
return nil, nil
}
// queueCacheListener is a [MapListener] to be called when any updates are done to the queue.
// this is for blocking clients.
type queueCacheListener[V any] struct {
listener MapListener[QueueKey, V]
namedQueue *namedBlockingQueue[V]
}
func newQueueCacheListener[V any](namedQueue *namedBlockingQueue[V]) *queueCacheListener[V] {
listener := queueCacheListener[V]{
listener: NewMapListener[QueueKey, V](),
namedQueue: namedQueue,
}
listener.listener.OnInserted(func(e MapEvent[QueueKey, V]) {
// notify all registered listeners that an entry has been added to the Queue
namedQueue.notifier.notifyAll()
})
return &listener
}
func createQueueKey(hash int, id int64) QueueKey {
return QueueKey{Class: "internal.net.queue.model.QueueKey", ID: id, Hash: hash}
}
type queueNotifier struct {
sync.Mutex
listeners map[string]chan struct{}
}
func newQueueNotifier() *queueNotifier {
return &queueNotifier{
listeners: make(map[string]chan struct{}, 0),
}
}
// subscribe subscribes to receive notifications about new queue messages.
func (qn *queueNotifier) subscribe() (string, chan struct{}) {
qn.Lock()
defer qn.Unlock()
id := uuid.New().String()
ch := make(chan struct{})
qn.listeners[id] = ch
return id, ch
}
// notifyAll() notifies all listeners registered.
func (qn *queueNotifier) notifyAll() {
qn.Lock()
defer qn.Unlock()
for _, v := range qn.listeners {
v <- struct{}{}
}
}
// unsubscribe unsubscribes from receiving notifications for a uuid.
func (qn *queueNotifier) unsubscribe(uuid string) {
qn.Lock()
defer qn.Unlock()
if v, ok := qn.listeners[uuid]; ok {
close(v)
delete(qn.listeners, uuid)
}
}