2020-08-31 17:19:28 +00:00
|
|
|
package stream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"sync/atomic"
|
2020-10-04 19:12:35 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2020-08-31 17:19:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// subscriptionStateOpen is the default state of a subscription. An open
|
|
|
|
// subscription may receive new events.
|
|
|
|
subscriptionStateOpen uint32 = 0
|
|
|
|
|
|
|
|
// subscriptionStateClosed indicates that the subscription was closed, possibly
|
|
|
|
// as a result of a change to an ACL token, and will not receive new events.
|
|
|
|
// The subscriber must issue a new Subscribe request.
|
|
|
|
subscriptionStateClosed uint32 = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrSubscriptionClosed is a error signalling the subscription has been
|
|
|
|
// closed. The client should Unsubscribe, then re-Subscribe.
|
|
|
|
var ErrSubscriptionClosed = errors.New("subscription closed by server, client should resubscribe")
|
|
|
|
|
|
|
|
type Subscription struct {
|
2020-10-08 18:27:52 +00:00
|
|
|
// state must be accessed atomically 0 means open, 1 means closed with reload
|
2020-08-31 17:19:28 +00:00
|
|
|
state uint32
|
|
|
|
|
|
|
|
req *SubscribeRequest
|
|
|
|
|
|
|
|
// currentItem stores the current buffer item we are on. It
|
|
|
|
// is mutated by calls to Next.
|
|
|
|
currentItem *bufferItem
|
|
|
|
|
|
|
|
// forceClosed is closed when forceClose is called. It is used by
|
2020-10-08 18:27:52 +00:00
|
|
|
// EventBroker to cancel Next().
|
2020-08-31 17:19:28 +00:00
|
|
|
forceClosed chan struct{}
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
// unsub is a function set by EventBroker that is called to free resources
|
2020-08-31 17:19:28 +00:00
|
|
|
// when the subscription is no longer needed.
|
|
|
|
// It must be safe to call the function from multiple goroutines and the function
|
|
|
|
// must be idempotent.
|
|
|
|
unsub func()
|
|
|
|
}
|
|
|
|
|
|
|
|
type SubscribeRequest struct {
|
2020-10-08 15:57:21 +00:00
|
|
|
Token string
|
|
|
|
Index uint64
|
|
|
|
Namespace string
|
2020-08-31 17:19:28 +00:00
|
|
|
|
2020-10-04 19:12:35 +00:00
|
|
|
Topics map[structs.Topic][]string
|
2020-10-08 18:27:52 +00:00
|
|
|
|
|
|
|
// StartExactlyAtIndex specifies if a subscription needs to
|
|
|
|
// start exactly at the requested Index. If set to false,
|
|
|
|
// the closest index in the buffer will be returned if there is not
|
|
|
|
// an exact match
|
|
|
|
StartExactlyAtIndex bool
|
2020-08-31 17:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSubscription(req *SubscribeRequest, item *bufferItem, unsub func()) *Subscription {
|
|
|
|
return &Subscription{
|
|
|
|
forceClosed: make(chan struct{}),
|
|
|
|
req: req,
|
|
|
|
currentItem: item,
|
|
|
|
unsub: unsub,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 19:12:35 +00:00
|
|
|
func (s *Subscription) Next(ctx context.Context) (structs.Events, error) {
|
2020-08-31 17:19:28 +00:00
|
|
|
if atomic.LoadUint32(&s.state) == subscriptionStateClosed {
|
2020-10-04 19:12:35 +00:00
|
|
|
return structs.Events{}, ErrSubscriptionClosed
|
2020-08-31 17:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
next, err := s.currentItem.Next(ctx, s.forceClosed)
|
|
|
|
switch {
|
|
|
|
case err != nil && atomic.LoadUint32(&s.state) == subscriptionStateClosed:
|
2020-10-04 19:12:35 +00:00
|
|
|
return structs.Events{}, ErrSubscriptionClosed
|
2020-08-31 17:19:28 +00:00
|
|
|
case err != nil:
|
2020-10-04 19:12:35 +00:00
|
|
|
return structs.Events{}, err
|
2020-10-01 18:43:28 +00:00
|
|
|
}
|
|
|
|
s.currentItem = next
|
|
|
|
|
2020-10-06 20:21:58 +00:00
|
|
|
events := filter(s.req, next.Events.Events)
|
2020-10-01 18:43:28 +00:00
|
|
|
if len(events) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-06 20:21:58 +00:00
|
|
|
return structs.Events{Index: next.Events.Index, Events: events}, nil
|
2020-10-01 18:43:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 19:12:35 +00:00
|
|
|
func (s *Subscription) NextNoBlock() ([]structs.Event, error) {
|
2020-10-01 18:43:28 +00:00
|
|
|
if atomic.LoadUint32(&s.state) == subscriptionStateClosed {
|
|
|
|
return nil, ErrSubscriptionClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
next := s.currentItem.NextNoBlock()
|
|
|
|
if next == nil {
|
|
|
|
return nil, nil
|
2020-08-31 17:19:28 +00:00
|
|
|
}
|
|
|
|
s.currentItem = next
|
|
|
|
|
2020-10-06 20:21:58 +00:00
|
|
|
events := filter(s.req, next.Events.Events)
|
2020-08-31 17:19:28 +00:00
|
|
|
if len(events) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscription) forceClose() {
|
|
|
|
swapped := atomic.CompareAndSwapUint32(&s.state, subscriptionStateOpen, subscriptionStateClosed)
|
|
|
|
if swapped {
|
|
|
|
close(s.forceClosed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscription) Unsubscribe() {
|
|
|
|
s.unsub()
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:57:21 +00:00
|
|
|
// filter events to only those that match a subscriptions topic/keys/namespace
|
2020-10-04 19:12:35 +00:00
|
|
|
func filter(req *SubscribeRequest, events []structs.Event) []structs.Event {
|
2020-08-31 17:19:28 +00:00
|
|
|
if len(events) == 0 {
|
2020-10-30 17:07:38 +00:00
|
|
|
return nil
|
2020-08-31 17:19:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 17:07:38 +00:00
|
|
|
allTopicKeys := req.Topics[structs.TopicAll]
|
2020-08-31 17:19:28 +00:00
|
|
|
|
2020-10-30 17:07:38 +00:00
|
|
|
if req.Namespace == "" && len(allTopicKeys) == 1 && allTopicKeys[0] == string(structs.TopicAll) {
|
2020-08-31 17:19:28 +00:00
|
|
|
return events
|
|
|
|
}
|
|
|
|
|
2020-10-30 17:07:38 +00:00
|
|
|
var result []structs.Event
|
|
|
|
|
|
|
|
for _, event := range events {
|
|
|
|
if req.Namespace != "" && event.Namespace != "" && event.Namespace != req.Namespace {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// *[*] always matches
|
|
|
|
if len(allTopicKeys) == 1 && allTopicKeys[0] == string(structs.TopicAll) {
|
|
|
|
result = append(result, event)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := allTopicKeys
|
|
|
|
|
|
|
|
if topicKeys, ok := req.Topics[event.Topic]; ok {
|
|
|
|
keys = append(keys, topicKeys...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(keys) == 1 && keys[0] == string(structs.TopicAll) {
|
|
|
|
result = append(result, event)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
if eventMatchesKey(event, key) {
|
|
|
|
result = append(result, event)
|
2020-10-08 15:57:21 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-08-31 17:19:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-30 17:07:38 +00:00
|
|
|
|
2020-08-31 17:19:28 +00:00
|
|
|
return result
|
|
|
|
}
|
2020-10-08 18:27:52 +00:00
|
|
|
|
2020-10-30 17:07:38 +00:00
|
|
|
func eventMatchesKey(event structs.Event, key string) bool {
|
|
|
|
if event.Key == key {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fk := range event.FilterKeys {
|
2020-10-08 18:27:52 +00:00
|
|
|
if fk == key {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-10-30 17:07:38 +00:00
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
return false
|
|
|
|
}
|