2020-07-06 21:29:45 +00:00
|
|
|
/*
|
|
|
|
Package stream provides a publish/subscribe system for events produced by changes
|
|
|
|
to the state store.
|
|
|
|
*/
|
2020-06-05 23:36:31 +00:00
|
|
|
package stream
|
|
|
|
|
2020-11-05 22:57:25 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
"github.com/hashicorp/consul/proto/pbsubscribe"
|
2020-11-05 22:57:25 +00:00
|
|
|
)
|
2020-06-05 23:36:31 +00:00
|
|
|
|
2020-07-07 00:04:24 +00:00
|
|
|
// Topic is an identifier that partitions events. A subscription will only receive
|
|
|
|
// events which match the Topic.
|
|
|
|
type Topic fmt.Stringer
|
2020-06-05 23:36:31 +00:00
|
|
|
|
2022-01-28 12:27:00 +00:00
|
|
|
// Subject identifies a portion of a topic for which a subscriber wishes to
|
|
|
|
// receive events (e.g. health events for a particular service) usually the
|
|
|
|
// normalized resource name (including partition and namespace if applicable).
|
2022-04-05 14:26:14 +00:00
|
|
|
type Subject fmt.Stringer
|
2022-01-28 12:27:00 +00:00
|
|
|
|
2022-03-22 19:13:59 +00:00
|
|
|
// SubjectNone is used when all events on a given topic are "global" and not
|
|
|
|
// further partitioned by subject. For example: the "CA Roots" topic which is
|
|
|
|
// used to notify subscribers when the global set CA root certificates changes.
|
2022-04-19 17:03:03 +00:00
|
|
|
const SubjectNone StringSubject = "none"
|
2022-03-22 19:13:59 +00:00
|
|
|
|
2020-07-07 00:04:24 +00:00
|
|
|
// Event is a structure with identifiers and a payload. Events are Published to
|
|
|
|
// EventPublisher and returned to Subscribers.
|
2020-06-05 23:36:31 +00:00
|
|
|
type Event struct {
|
|
|
|
Topic Topic
|
|
|
|
Index uint64
|
2020-10-27 18:40:06 +00:00
|
|
|
Payload Payload
|
|
|
|
}
|
|
|
|
|
2020-11-06 18:00:33 +00:00
|
|
|
// A Payload contains the topic-specific data in an event. The payload methods
|
|
|
|
// should not modify the state of the payload if the Event is being submitted to
|
|
|
|
// EventPublisher.Publish.
|
2020-10-27 18:40:06 +00:00
|
|
|
type Payload interface {
|
2020-11-05 22:57:25 +00:00
|
|
|
// HasReadPermission uses the acl.Authorizer to determine if the items in the
|
|
|
|
// Payload are visible to the request. It returns true if the payload is
|
|
|
|
// authorized for Read, otherwise returns false.
|
|
|
|
HasReadPermission(authz acl.Authorizer) bool
|
2022-01-28 12:27:00 +00:00
|
|
|
|
|
|
|
// Subject is used to identify which subscribers should be notified of this
|
|
|
|
// event - e.g. those subscribing to health events for a particular service.
|
|
|
|
// it is usually the normalized resource name (including the partition and
|
|
|
|
// namespace if applicable).
|
|
|
|
Subject() Subject
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
|
|
|
|
// ToSubscriptionEvent is used to convert streaming events to their
|
|
|
|
// serializable equivalent.
|
|
|
|
ToSubscriptionEvent(idx uint64) *pbsubscribe.Event
|
2020-06-05 23:36:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 18:00:33 +00:00
|
|
|
// PayloadEvents is a Payload that may be returned by Subscription.Next when
|
|
|
|
// there are multiple events at an index.
|
|
|
|
//
|
|
|
|
// Note that unlike most other Payload, PayloadEvents is mutable and it is NOT
|
|
|
|
// safe to send to EventPublisher.Publish.
|
2020-11-05 22:50:17 +00:00
|
|
|
type PayloadEvents struct {
|
|
|
|
Items []Event
|
2020-10-05 16:38:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 18:00:33 +00:00
|
|
|
func newPayloadEvents(items ...Event) *PayloadEvents {
|
2020-11-05 22:50:17 +00:00
|
|
|
return &PayloadEvents{Items: items}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PayloadEvents) filter(f func(Event) bool) bool {
|
|
|
|
items := p.Items
|
2020-10-05 16:38:38 +00:00
|
|
|
|
|
|
|
// To avoid extra allocations, iterate over the list of events first and
|
|
|
|
// get a count of the total desired size. This trades off some extra cpu
|
|
|
|
// time in the worse case (when not all items match the filter), for
|
|
|
|
// fewer memory allocations.
|
|
|
|
var size int
|
2020-11-05 22:50:17 +00:00
|
|
|
for idx := range items {
|
|
|
|
if f(items[idx]) {
|
2020-10-05 16:38:38 +00:00
|
|
|
size++
|
|
|
|
}
|
|
|
|
}
|
2020-11-05 22:50:17 +00:00
|
|
|
if len(items) == size || size == 0 {
|
|
|
|
return size != 0
|
2020-10-05 16:38:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 22:50:17 +00:00
|
|
|
filtered := make([]Event, 0, size)
|
|
|
|
for idx := range items {
|
|
|
|
event := items[idx]
|
2020-10-05 16:38:38 +00:00
|
|
|
if f(event) {
|
|
|
|
filtered = append(filtered, event)
|
|
|
|
}
|
|
|
|
}
|
2020-11-05 22:50:17 +00:00
|
|
|
p.Items = filtered
|
|
|
|
return true
|
2020-10-05 16:38:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 22:50:17 +00:00
|
|
|
func (p *PayloadEvents) Len() int {
|
|
|
|
return len(p.Items)
|
2020-10-27 18:40:06 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 18:00:33 +00:00
|
|
|
// HasReadPermission filters the PayloadEvents to those which are authorized
|
|
|
|
// for reading by authz.
|
2020-11-05 22:57:25 +00:00
|
|
|
func (p *PayloadEvents) HasReadPermission(authz acl.Authorizer) bool {
|
|
|
|
return p.filter(func(event Event) bool {
|
|
|
|
return event.Payload.HasReadPermission(authz)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-28 12:27:00 +00:00
|
|
|
// Subject is required to satisfy the Payload interface but is not implemented
|
|
|
|
// by PayloadEvents. PayloadEvents structs are constructed by Subscription.Next
|
|
|
|
// *after* Subject has been used to dispatch the enclosed events to the correct
|
|
|
|
// buffer.
|
|
|
|
func (PayloadEvents) Subject() Subject {
|
|
|
|
panic("PayloadEvents does not implement Subject")
|
|
|
|
}
|
|
|
|
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
func (p PayloadEvents) ToSubscriptionEvent(idx uint64) *pbsubscribe.Event {
|
|
|
|
return &pbsubscribe.Event{
|
|
|
|
Index: idx,
|
|
|
|
Payload: &pbsubscribe.Event_EventBatch{
|
|
|
|
EventBatch: &pbsubscribe.EventBatch{
|
|
|
|
Events: batchEventsFromEventSlice(p.Items),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func batchEventsFromEventSlice(events []Event) []*pbsubscribe.Event {
|
|
|
|
result := make([]*pbsubscribe.Event, len(events))
|
|
|
|
for i := range events {
|
|
|
|
event := events[i]
|
|
|
|
result[i] = event.Payload.ToSubscriptionEvent(event.Index)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-07-08 18:45:18 +00:00
|
|
|
// IsEndOfSnapshot returns true if this is a framing event that indicates the
|
2020-10-01 17:51:55 +00:00
|
|
|
// snapshot has completed. Subsequent events from Subscription.Next will be
|
|
|
|
// streamed as they occur.
|
2020-06-15 22:49:00 +00:00
|
|
|
func (e Event) IsEndOfSnapshot() bool {
|
2020-06-05 23:36:31 +00:00
|
|
|
return e.Payload == endOfSnapshot{}
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:51:55 +00:00
|
|
|
// IsNewSnapshotToFollow returns true if this is a framing event that indicates
|
|
|
|
// that the clients view is stale, and must be reset. Subsequent events from
|
|
|
|
// Subscription.Next will be a new snapshot, followed by an EndOfSnapshot event.
|
|
|
|
func (e Event) IsNewSnapshotToFollow() bool {
|
|
|
|
return e.Payload == newSnapshotToFollow{}
|
2020-06-15 22:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 14:26:14 +00:00
|
|
|
// IsFramingEvent returns true if this is a framing event (e.g. EndOfSnapshot
|
|
|
|
// or NewSnapshotToFollow).
|
|
|
|
func (e Event) IsFramingEvent() bool {
|
|
|
|
return e.IsEndOfSnapshot() || e.IsNewSnapshotToFollow()
|
|
|
|
}
|
|
|
|
|
2020-11-05 22:57:25 +00:00
|
|
|
type framingEvent struct{}
|
2020-06-15 22:49:00 +00:00
|
|
|
|
2022-01-28 12:27:00 +00:00
|
|
|
func (framingEvent) HasReadPermission(acl.Authorizer) bool {
|
2020-10-27 18:40:06 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-01-28 12:27:00 +00:00
|
|
|
// Subject is required by the Payload interface but is not implemented by
|
|
|
|
// framing events, as they are typically *manually* appended to the correct
|
|
|
|
// buffer and do not need to be routed using a Subject.
|
|
|
|
func (framingEvent) Subject() Subject {
|
|
|
|
panic("framing events do not implement Subject")
|
2020-10-27 18:40:06 +00:00
|
|
|
}
|
|
|
|
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
func (framingEvent) ToSubscriptionEvent(idx uint64) *pbsubscribe.Event {
|
|
|
|
panic("framingEvent does not implement ToSubscriptionEvent")
|
|
|
|
}
|
|
|
|
|
2020-11-05 22:57:25 +00:00
|
|
|
type endOfSnapshot struct {
|
|
|
|
framingEvent
|
|
|
|
}
|
|
|
|
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
func (s endOfSnapshot) ToSubscriptionEvent(idx uint64) *pbsubscribe.Event {
|
|
|
|
return &pbsubscribe.Event{
|
|
|
|
Index: idx,
|
|
|
|
Payload: &pbsubscribe.Event_EndOfSnapshot{EndOfSnapshot: true},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 22:57:25 +00:00
|
|
|
type newSnapshotToFollow struct {
|
|
|
|
framingEvent
|
|
|
|
}
|
|
|
|
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
func (s newSnapshotToFollow) ToSubscriptionEvent(idx uint64) *pbsubscribe.Event {
|
|
|
|
return &pbsubscribe.Event{
|
|
|
|
Index: idx,
|
|
|
|
Payload: &pbsubscribe.Event_NewSnapshotToFollow{NewSnapshotToFollow: true},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:29:45 +00:00
|
|
|
type closeSubscriptionPayload struct {
|
|
|
|
tokensSecretIDs []string
|
2020-07-06 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
// closeSubscriptionPayload is only used internally and does not correspond to
|
|
|
|
// a subscription event that would be sent to clients.
|
|
|
|
func (s closeSubscriptionPayload) ToSubscriptionEvent(idx uint64) *pbsubscribe.Event {
|
|
|
|
panic("closeSubscriptionPayload does not implement ToSubscriptionEvent")
|
|
|
|
}
|
|
|
|
|
2022-01-28 12:27:00 +00:00
|
|
|
func (closeSubscriptionPayload) HasReadPermission(acl.Authorizer) bool {
|
2020-11-05 22:57:25 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-28 12:27:00 +00:00
|
|
|
// Subject is required by the Payload interface but it is not implemented by
|
|
|
|
// closeSubscriptionPayload, as this event type is handled separately and not
|
|
|
|
// actually appended to the buffer.
|
|
|
|
func (closeSubscriptionPayload) Subject() Subject {
|
|
|
|
panic("closeSubscriptionPayload does not implement Subject")
|
2020-10-27 18:40:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:29:45 +00:00
|
|
|
// NewCloseSubscriptionEvent returns a special Event that is handled by the
|
|
|
|
// stream package, and is never sent to subscribers. EventProcessor handles
|
|
|
|
// these events, and closes any subscriptions which were created using a token
|
|
|
|
// which matches any of the tokenSecretIDs.
|
2020-07-08 18:45:18 +00:00
|
|
|
//
|
|
|
|
// tokenSecretIDs may contain duplicate IDs.
|
2020-07-06 21:29:45 +00:00
|
|
|
func NewCloseSubscriptionEvent(tokenSecretIDs []string) Event {
|
|
|
|
return Event{Payload: closeSubscriptionPayload{tokensSecretIDs: tokenSecretIDs}}
|
2020-07-06 18:34:58 +00:00
|
|
|
}
|