2020-06-02 22:37:10 +00:00
|
|
|
package stream
|
|
|
|
|
2020-07-06 21:29:45 +00:00
|
|
|
// eventSnapshot represents the state of memdb for a given topic and key at some
|
2020-06-02 22:37:10 +00:00
|
|
|
// point in time. It is modelled as a buffer of events so that snapshots can be
|
|
|
|
// streamed to possibly multiple subscribers concurrently, and can be trivially
|
2020-07-06 21:29:45 +00:00
|
|
|
// cached by retaining a reference to a Snapshot. Once the reference to eventSnapshot
|
2020-06-10 23:07:58 +00:00
|
|
|
// is dropped from memory, any subscribers still reading from it may do so by following
|
2020-07-08 18:45:18 +00:00
|
|
|
// their pointers. When the last subscriber unsubscribes, the snapshot is garbage
|
2020-06-10 23:07:58 +00:00
|
|
|
// collected automatically by Go's runtime. This simplifies snapshot and buffer
|
|
|
|
// management dramatically.
|
2020-07-06 21:29:45 +00:00
|
|
|
type eventSnapshot struct {
|
2020-07-08 04:31:22 +00:00
|
|
|
// Head is the first item in the buffer containing the snapshot. Once the
|
2020-06-10 23:07:58 +00:00
|
|
|
// snapshot is complete, subsequent BufferItems are appended to snapBuffer,
|
|
|
|
// so that subscribers receive all the events from the same buffer.
|
2020-07-08 04:31:22 +00:00
|
|
|
Head *bufferItem
|
2020-06-02 22:37:10 +00:00
|
|
|
|
|
|
|
// snapBuffer is the Head of the snapshot buffer the fn should write to.
|
2020-07-06 21:29:45 +00:00
|
|
|
snapBuffer *eventBuffer
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:29:45 +00:00
|
|
|
type snapFunc func(req *SubscribeRequest, buf SnapshotAppender) (uint64, error)
|
2020-06-02 22:37:10 +00:00
|
|
|
|
2020-07-06 21:29:45 +00:00
|
|
|
// newEventSnapshot creates a snapshot buffer based on the subscription request.
|
2020-07-08 18:45:18 +00:00
|
|
|
// The current buffer head for the topic requested is passed so that once the
|
2020-06-02 22:37:10 +00:00
|
|
|
// snapshot is complete and has been delivered into the buffer, any events
|
|
|
|
// published during snapshotting can be immediately appended and won't be
|
|
|
|
// missed. Once the snapshot is delivered the topic buffer is spliced onto the
|
|
|
|
// snapshot buffer so that subscribers will naturally follow from the snapshot
|
|
|
|
// to wait for any subsequent updates.
|
2020-07-06 21:29:45 +00:00
|
|
|
func newEventSnapshot(req *SubscribeRequest, topicBufferHead *bufferItem, fn snapFunc) *eventSnapshot {
|
|
|
|
buf := newEventBuffer()
|
|
|
|
s := &eventSnapshot{
|
2020-07-08 04:31:22 +00:00
|
|
|
Head: buf.Head(),
|
2020-06-10 23:07:58 +00:00
|
|
|
snapBuffer: buf,
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
2020-06-10 23:07:58 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
idx, err := fn(req, s.snapBuffer)
|
|
|
|
if err != nil {
|
2020-07-08 04:31:22 +00:00
|
|
|
s.snapBuffer.AppendItem(&bufferItem{Err: err})
|
2020-06-10 23:07:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// We wrote the snapshot events to the buffer, send the "end of snapshot" event
|
|
|
|
s.snapBuffer.Append([]Event{{
|
|
|
|
Topic: req.Topic,
|
|
|
|
Key: req.Key,
|
|
|
|
Index: idx,
|
|
|
|
Payload: endOfSnapshot{},
|
|
|
|
}})
|
|
|
|
s.spliceFromTopicBuffer(topicBufferHead, idx)
|
|
|
|
}()
|
2020-06-02 22:37:10 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:29:45 +00:00
|
|
|
func (s *eventSnapshot) spliceFromTopicBuffer(topicBufferHead *bufferItem, idx uint64) {
|
2020-06-02 22:37:10 +00:00
|
|
|
// Now splice on the topic buffer. We need to iterate through the buffer to
|
|
|
|
// find the first event after the current snapshot.
|
2020-06-10 23:07:58 +00:00
|
|
|
item := topicBufferHead
|
2020-06-02 22:37:10 +00:00
|
|
|
for {
|
2020-07-08 04:31:22 +00:00
|
|
|
next := item.NextNoBlock()
|
|
|
|
switch {
|
|
|
|
case next == nil:
|
2020-06-02 22:37:10 +00:00
|
|
|
// This is the head of the topic buffer (or was just now which is after
|
|
|
|
// the snapshot completed). We don't want any of the events (if any) in
|
|
|
|
// the snapshot buffer as they came before the snapshot but we do need to
|
|
|
|
// wait for the next update.
|
2020-07-08 04:31:22 +00:00
|
|
|
s.snapBuffer.AppendItem(item.NextLink())
|
2020-06-02 22:37:10 +00:00
|
|
|
return
|
|
|
|
|
2020-07-08 04:31:22 +00:00
|
|
|
case next.Err != nil:
|
|
|
|
// This case is not currently possible because errors can only come
|
|
|
|
// from a snapshot func, and this is consuming events from a topic
|
|
|
|
// buffer which does not contain a snapshot.
|
|
|
|
// Handle this case anyway in case errors can come from other places
|
|
|
|
// in the future.
|
|
|
|
s.snapBuffer.AppendItem(next)
|
2020-06-02 22:37:10 +00:00
|
|
|
return
|
|
|
|
|
2020-07-08 04:31:22 +00:00
|
|
|
case len(next.Events) > 0 && next.Events[0].Index > idx:
|
2020-06-10 23:07:58 +00:00
|
|
|
// We've found an update in the topic buffer that happened after our
|
|
|
|
// snapshot was taken, splice it into the snapshot buffer so subscribers
|
|
|
|
// can continue to read this and others after it.
|
2020-07-08 04:31:22 +00:00
|
|
|
s.snapBuffer.AppendItem(next)
|
2020-06-10 23:07:58 +00:00
|
|
|
return
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
2020-07-08 04:31:22 +00:00
|
|
|
|
2020-06-02 22:37:10 +00:00
|
|
|
// We don't need this item, continue to next
|
|
|
|
item = next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:29:45 +00:00
|
|
|
// err returns an error if the snapshot func has failed with an error or nil
|
2020-06-02 22:37:10 +00:00
|
|
|
// otherwise. Nil doesn't necessarily mean there won't be an error but there
|
|
|
|
// hasn't been one yet.
|
2020-07-06 21:29:45 +00:00
|
|
|
func (s *eventSnapshot) err() error {
|
2020-06-02 22:37:10 +00:00
|
|
|
// Fetch the head of the buffer, this is atomic. If the snapshot func errored
|
|
|
|
// then the last event will be an error.
|
|
|
|
head := s.snapBuffer.Head()
|
|
|
|
return head.Err
|
|
|
|
}
|