2020-10-01 06:21:50 +00:00
|
|
|
package submatview
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"google.golang.org/grpc"
|
2020-09-18 22:33:02 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
2020-09-29 21:42:48 +00:00
|
|
|
"github.com/hashicorp/consul/lib/retry"
|
2020-09-18 22:33:02 +00:00
|
|
|
"github.com/hashicorp/consul/proto/pbsubscribe"
|
2020-09-18 22:25:56 +00:00
|
|
|
)
|
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
// View receives events from, and return results to, Materializer. A view is
|
|
|
|
// responsible for converting the pbsubscribe.Event.Payload into the local
|
|
|
|
// type, and storing it so that it can be returned by Result().
|
2020-09-29 21:42:48 +00:00
|
|
|
type View interface {
|
2020-09-18 22:25:56 +00:00
|
|
|
// Update is called when one or more events are received. The first call will
|
|
|
|
// include _all_ events in the initial snapshot which may be an empty set.
|
|
|
|
// Subsequent calls will contain one or more update events in the order they
|
|
|
|
// are received.
|
2020-09-18 22:33:02 +00:00
|
|
|
Update(events []*pbsubscribe.Event) error
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
// Result returns the type-specific cache result based on the state. When no
|
|
|
|
// events have been delivered yet the result should be an empty value type
|
|
|
|
// suitable to return to clients in case there is an empty result on the
|
|
|
|
// servers. The index the materialized view represents is maintained
|
|
|
|
// separately and passed in in case the return type needs an Index field
|
|
|
|
// populating. This allows implementations to not worry about maintaining
|
|
|
|
// indexes seen during Update.
|
|
|
|
Result(index uint64) (interface{}, error)
|
|
|
|
|
2020-09-29 21:42:48 +00:00
|
|
|
// Reset the view to the zero state, done in preparation for receiving a new
|
|
|
|
// snapshot.
|
|
|
|
Reset()
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
// Materializer consumes the event stream, handling any framing events, and
|
|
|
|
// sends the events to View as they are received.
|
2020-09-18 22:25:56 +00:00
|
|
|
//
|
2020-10-01 06:36:36 +00:00
|
|
|
// Materializer is used as the cache.Result.State for a streaming
|
2020-09-18 22:25:56 +00:00
|
|
|
// cache type and manages the actual streaming RPC call to the servers behind
|
|
|
|
// the scenes until the cache result is discarded when TTL expires.
|
2020-09-29 21:42:48 +00:00
|
|
|
type Materializer struct {
|
2020-10-01 06:36:36 +00:00
|
|
|
deps Deps
|
|
|
|
retryWaiter *retry.Waiter
|
|
|
|
handler eventHandler
|
|
|
|
|
|
|
|
// lock protects the mutable state - all fields below it must only be accessed
|
|
|
|
// while holding lock.
|
|
|
|
lock sync.Mutex
|
|
|
|
index uint64
|
|
|
|
view View
|
|
|
|
updateCh chan struct{}
|
|
|
|
err error
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
type Deps struct {
|
|
|
|
View View
|
|
|
|
Client StreamClient
|
2020-09-29 21:42:48 +00:00
|
|
|
Logger hclog.Logger
|
|
|
|
Waiter *retry.Waiter
|
2020-10-01 06:36:36 +00:00
|
|
|
Request func(index uint64) pbsubscribe.SubscribeRequest
|
2020-09-29 21:42:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
// StreamClient provides a subscription to state change events.
|
|
|
|
type StreamClient interface {
|
2020-09-29 21:42:48 +00:00
|
|
|
Subscribe(ctx context.Context, in *pbsubscribe.SubscribeRequest, opts ...grpc.CallOption) (pbsubscribe.StateChangeSubscription_SubscribeClient, error)
|
|
|
|
}
|
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
// NewMaterializer returns a new Materializer. Run must be called to start it.
|
|
|
|
func NewMaterializer(deps Deps) *Materializer {
|
2020-09-29 21:42:48 +00:00
|
|
|
v := &Materializer{
|
|
|
|
deps: deps,
|
2020-10-01 06:36:36 +00:00
|
|
|
view: deps.View,
|
2020-09-29 21:42:48 +00:00
|
|
|
retryWaiter: deps.Waiter,
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
2020-09-29 21:42:48 +00:00
|
|
|
v.reset()
|
|
|
|
return v
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
// Run receives events from the StreamClient and sends them to the View. It runs
|
|
|
|
// until ctx is cancelled, so it is expected to be run in a goroutine.
|
|
|
|
func (m *Materializer) Run(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
req := m.deps.Request(m.index)
|
|
|
|
err := m.runSubscription(ctx, req)
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-10-03 00:04:45 +00:00
|
|
|
failures := m.retryWaiter.Failures()
|
|
|
|
if isNonTemporaryOrConsecutiveFailure(err, failures) {
|
|
|
|
m.lock.Lock()
|
2020-10-01 06:36:36 +00:00
|
|
|
m.notifyUpdateLocked(err)
|
2020-10-03 00:04:45 +00:00
|
|
|
m.lock.Unlock()
|
2020-10-01 06:36:36 +00:00
|
|
|
}
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
m.deps.Logger.Error("subscribe call failed",
|
|
|
|
"err", err,
|
|
|
|
"topic", req.Topic,
|
|
|
|
"key", req.Key,
|
2020-10-03 00:04:45 +00:00
|
|
|
"failure_count", failures+1)
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-10-03 00:04:45 +00:00
|
|
|
if err := m.retryWaiter.Wait(ctx); err != nil {
|
2020-10-01 06:36:36 +00:00
|
|
|
return
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-29 21:42:48 +00:00
|
|
|
}
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-10-03 00:04:45 +00:00
|
|
|
// isNonTemporaryOrConsecutiveFailure returns true if the error is a temporary error
|
|
|
|
// or if the failures > 0.
|
|
|
|
func isNonTemporaryOrConsecutiveFailure(err error, failures int) bool {
|
|
|
|
// temporary is an interface used by net and other std lib packages to
|
|
|
|
// show error types represent temporary/recoverable errors.
|
|
|
|
_, ok := err.(interface {
|
|
|
|
Temporary() bool
|
|
|
|
})
|
|
|
|
return !ok || failures > 0
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runSubscription opens a new subscribe streaming call to the servers and runs
|
|
|
|
// for it's lifetime or until the view is closed.
|
2020-10-01 06:36:36 +00:00
|
|
|
func (m *Materializer) runSubscription(ctx context.Context, req pbsubscribe.SubscribeRequest) error {
|
2020-09-29 21:42:48 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-09-18 22:25:56 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
m.handler = m.initialHandler(req.Index)
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
s, err := m.deps.Client.Subscribe(ctx, &req)
|
2020-09-18 22:25:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
event, err := s.Recv()
|
2020-09-18 22:33:02 +00:00
|
|
|
switch {
|
|
|
|
case isGrpcStatus(err, codes.Aborted):
|
2020-10-01 06:36:36 +00:00
|
|
|
m.reset()
|
2020-09-18 22:25:56 +00:00
|
|
|
return resetErr("stream reset requested")
|
2020-09-18 22:33:02 +00:00
|
|
|
case err != nil:
|
|
|
|
return err
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
m.handler, err = m.handler(event)
|
|
|
|
if err != nil {
|
|
|
|
m.reset()
|
|
|
|
return err
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:33:02 +00:00
|
|
|
func isGrpcStatus(err error, code codes.Code) bool {
|
|
|
|
s, ok := status.FromError(err)
|
|
|
|
return ok && s.Code() == code
|
|
|
|
}
|
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
// resetErr represents a server request to reset the subscription, it's typed so
|
|
|
|
// we can mark it as temporary and so attempt to retry first time without
|
|
|
|
// notifying clients.
|
|
|
|
type resetErr string
|
|
|
|
|
|
|
|
// Temporary Implements the internal Temporary interface
|
|
|
|
func (e resetErr) Temporary() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements error
|
|
|
|
func (e resetErr) Error() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:25:56 +00:00
|
|
|
// reset clears the state ready to start a new stream from scratch.
|
2020-10-01 06:36:36 +00:00
|
|
|
func (m *Materializer) reset() {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
m.view.Reset()
|
|
|
|
m.index = 0
|
|
|
|
m.notifyUpdateLocked(nil)
|
|
|
|
m.retryWaiter.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Materializer) updateView(events []*pbsubscribe.Event, index uint64) error {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
if err := m.view.Update(events); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.index = index
|
|
|
|
m.notifyUpdateLocked(nil)
|
|
|
|
m.retryWaiter.Reset()
|
|
|
|
return nil
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// notifyUpdateLocked closes the current update channel and recreates a new
|
2020-10-01 06:36:36 +00:00
|
|
|
// one. It must be called while holding the s.lock lock.
|
|
|
|
func (m *Materializer) notifyUpdateLocked(err error) {
|
|
|
|
m.err = err
|
|
|
|
if m.updateCh != nil {
|
|
|
|
close(m.updateCh)
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
2020-10-01 06:36:36 +00:00
|
|
|
m.updateCh = make(chan struct{})
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch implements the logic a StreamingCacheType will need during it's Fetch
|
|
|
|
// call. Cache types that use streaming should just be able to proxy to this
|
|
|
|
// once they have a subscription object and return it's results directly.
|
2020-10-01 06:36:36 +00:00
|
|
|
func (m *Materializer) Fetch(done <-chan struct{}, opts cache.FetchOptions) (cache.FetchResult, error) {
|
2020-09-18 22:25:56 +00:00
|
|
|
var result cache.FetchResult
|
|
|
|
|
|
|
|
// Get current view Result and index
|
2020-10-01 06:36:36 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
index := m.index
|
|
|
|
val, err := m.view.Result(m.index)
|
|
|
|
updateCh := m.updateCh
|
|
|
|
m.lock.Unlock()
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Index = index
|
|
|
|
result.Value = val
|
|
|
|
|
|
|
|
// If our index is > req.Index return right away. If index is zero then we
|
|
|
|
// haven't loaded a snapshot at all yet which means we should wait for one on
|
|
|
|
// the update chan. Note it's opts.MinIndex that the cache is using here the
|
|
|
|
// request min index might be different and from initial user request.
|
|
|
|
if index > 0 && index > opts.MinIndex {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch for timeout of the Fetch. Note it's opts.Timeout not req.Timeout
|
|
|
|
// since that is the timeout the client requested from the cache Get while the
|
|
|
|
// options one is the internal "background refresh" timeout which is what the
|
|
|
|
// Fetch call should be using.
|
|
|
|
timeoutCh := time.After(opts.Timeout)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-updateCh:
|
|
|
|
// View updated, return the new result
|
2020-10-01 06:36:36 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
result.Index = m.index
|
2020-09-18 22:25:56 +00:00
|
|
|
// Grab the new updateCh in case we need to keep waiting for the next
|
|
|
|
// update.
|
2020-10-01 06:36:36 +00:00
|
|
|
updateCh = m.updateCh
|
|
|
|
fetchErr := m.err
|
2020-09-18 22:25:56 +00:00
|
|
|
if fetchErr == nil {
|
|
|
|
// Only generate a new result if there was no error to avoid pointless
|
|
|
|
// work potentially shuffling the same data around.
|
2020-10-01 06:36:36 +00:00
|
|
|
result.Value, err = m.view.Result(m.index)
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
2020-10-01 06:36:36 +00:00
|
|
|
m.lock.Unlock()
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
// If there was a non-transient error return it
|
|
|
|
if fetchErr != nil {
|
|
|
|
return result, fetchErr
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check the update is actually later than the one the user
|
|
|
|
// requested.
|
|
|
|
if result.Index <= opts.MinIndex {
|
|
|
|
// The result is still older/same as the requested index, continue to
|
|
|
|
// wait for further updates.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the updated result
|
|
|
|
return result, nil
|
|
|
|
|
|
|
|
case <-timeoutCh:
|
|
|
|
// Just return whatever we got originally, might still be empty
|
|
|
|
return result, nil
|
|
|
|
|
2020-10-01 06:36:36 +00:00
|
|
|
case <-done:
|
2020-09-29 21:42:48 +00:00
|
|
|
return result, context.Canceled
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|