2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
package submatview
|
|
|
|
|
2020-10-03 00:04:45 +00:00
|
|
|
import (
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/pbsubscribe"
|
2020-10-03 00:04:45 +00:00
|
|
|
)
|
2020-10-01 06:36:36 +00:00
|
|
|
|
2020-10-03 00:04:45 +00:00
|
|
|
// eventHandler is a function which performs some operation on the received
|
|
|
|
// events, then returns the eventHandler that should be used for the next set
|
|
|
|
// of events.
|
|
|
|
// If eventHandler fails to handle the events it may return an error. If an
|
|
|
|
// error is returned the next eventHandler will be ignored.
|
|
|
|
// eventHandler is used to implement a very simple finite-state machine.
|
2020-10-05 19:08:03 +00:00
|
|
|
type eventHandler func(state viewState, events *pbsubscribe.Event) (next eventHandler, err error)
|
2020-10-01 06:36:36 +00:00
|
|
|
|
2020-10-05 19:08:03 +00:00
|
|
|
type viewState interface {
|
|
|
|
updateView(events []*pbsubscribe.Event, index uint64) error
|
|
|
|
reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initialHandler(index uint64) eventHandler {
|
2020-10-01 06:36:36 +00:00
|
|
|
if index == 0 {
|
2020-10-05 19:08:03 +00:00
|
|
|
return newSnapshotHandler()
|
2020-10-01 06:36:36 +00:00
|
|
|
}
|
2020-10-05 19:08:03 +00:00
|
|
|
return resumeStreamHandler
|
2020-10-01 06:36:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 19:08:03 +00:00
|
|
|
// snapshotHandler accumulates events. When it receives an EndOfSnapshot event
|
|
|
|
// it updates the view, and then returns eventStreamHandler to handle new events.
|
2020-10-01 06:36:36 +00:00
|
|
|
type snapshotHandler struct {
|
2020-10-05 19:08:03 +00:00
|
|
|
events []*pbsubscribe.Event
|
2020-10-01 06:36:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 19:08:03 +00:00
|
|
|
func newSnapshotHandler() eventHandler {
|
|
|
|
return (&snapshotHandler{}).handle
|
2020-10-01 06:36:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 19:08:03 +00:00
|
|
|
func (h *snapshotHandler) handle(state viewState, event *pbsubscribe.Event) (eventHandler, error) {
|
2020-10-01 06:36:36 +00:00
|
|
|
if event.GetEndOfSnapshot() {
|
2020-10-05 19:08:03 +00:00
|
|
|
err := state.updateView(h.events, event.Index)
|
|
|
|
return eventStreamHandler, err
|
2020-10-01 06:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.events = append(h.events, eventsFromEvent(event)...)
|
|
|
|
return h.handle, nil
|
|
|
|
}
|
|
|
|
|
2020-10-05 19:08:03 +00:00
|
|
|
// eventStreamHandler handles events by updating the view. It always returns
|
|
|
|
// itself as the next handler.
|
|
|
|
func eventStreamHandler(state viewState, event *pbsubscribe.Event) (eventHandler, error) {
|
|
|
|
err := state.updateView(eventsFromEvent(event), event.Index)
|
|
|
|
return eventStreamHandler, err
|
2020-10-01 06:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func eventsFromEvent(event *pbsubscribe.Event) []*pbsubscribe.Event {
|
|
|
|
if batch := event.GetEventBatch(); batch != nil {
|
|
|
|
return batch.Events
|
|
|
|
}
|
|
|
|
return []*pbsubscribe.Event{event}
|
|
|
|
}
|
|
|
|
|
2020-10-05 19:08:03 +00:00
|
|
|
// resumeStreamHandler checks if the event is a NewSnapshotToFollow event. If it
|
|
|
|
// is it resets the view and returns a snapshotHandler to handle the next event.
|
|
|
|
// Otherwise it uses eventStreamHandler to handle events.
|
|
|
|
func resumeStreamHandler(state viewState, event *pbsubscribe.Event) (eventHandler, error) {
|
2020-10-01 06:36:36 +00:00
|
|
|
if event.GetNewSnapshotToFollow() {
|
2020-10-05 19:08:03 +00:00
|
|
|
state.reset()
|
|
|
|
return newSnapshotHandler(), nil
|
2020-10-01 06:36:36 +00:00
|
|
|
}
|
2020-10-05 19:08:03 +00:00
|
|
|
return eventStreamHandler(state, event)
|
2020-10-01 06:36:36 +00:00
|
|
|
}
|