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"
|
|
|
|
|
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.
|
2021-02-23 19:27:24 +00:00
|
|
|
Result(index uint64) interface{}
|
2020-09-18 22:25:56 +00:00
|
|
|
|
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-10-05 19:44:47 +00:00
|
|
|
updateCh: make(chan struct{}),
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
2021-04-27 21:01:12 +00:00
|
|
|
if v.retryWaiter == nil {
|
2021-02-22 21:27:18 +00:00
|
|
|
v.retryWaiter = &retry.Waiter{
|
|
|
|
MinFailures: 1,
|
|
|
|
// Start backing off with small increments (200-400ms) which will double
|
|
|
|
// each attempt. (200-400, 400-800, 800-1600, 1600-3200, 3200-6000, 6000
|
|
|
|
// after that). (retry.Wait applies Max limit after jitter right now).
|
|
|
|
Factor: 200 * time.Millisecond,
|
|
|
|
MinWait: 0,
|
|
|
|
MaxWait: 60 * time.Second,
|
|
|
|
Jitter: retry.NewJitter(100),
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 21:42:48 +00:00
|
|
|
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-05 19:44:47 +00:00
|
|
|
// isNonTemporaryOrConsecutiveFailure returns true if the error is not a
|
|
|
|
// temporary error or if failures > 0.
|
2020-10-03 00:04:45 +00:00
|
|
|
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.
|
2020-10-06 17:52:02 +00:00
|
|
|
temp, ok := err.(interface {
|
2020-10-03 00:04:45 +00:00
|
|
|
Temporary() bool
|
|
|
|
})
|
2020-10-06 17:52:02 +00:00
|
|
|
return !ok || !temp.Temporary() || 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-05 19:08:03 +00:00
|
|
|
m.handler = 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-05 19:08:03 +00:00
|
|
|
m.handler, err = m.handler(m, event)
|
2020-10-01 06:36:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-10-05 19:44:47 +00:00
|
|
|
close(m.updateCh)
|
2020-10-01 06:36:36 +00:00
|
|
|
m.updateCh = make(chan struct{})
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 20:02:28 +00:00
|
|
|
type Result struct {
|
2021-02-23 19:27:24 +00:00
|
|
|
Index uint64
|
|
|
|
Value interface{}
|
|
|
|
}
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2021-02-23 19:27:24 +00:00
|
|
|
// getFromView blocks until the index of the View is greater than opts.MinIndex,
|
|
|
|
//or the context is cancelled.
|
2021-02-23 20:02:28 +00:00
|
|
|
func (m *Materializer) getFromView(ctx context.Context, minIndex uint64) (Result, error) {
|
2020-10-01 06:36:36 +00:00
|
|
|
m.lock.Lock()
|
2020-09-18 22:25:56 +00:00
|
|
|
|
2021-02-23 20:02:28 +00:00
|
|
|
result := Result{
|
2021-02-23 19:27:24 +00:00
|
|
|
Index: m.index,
|
|
|
|
Value: m.view.Result(m.index),
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 19:27:24 +00:00
|
|
|
updateCh := m.updateCh
|
|
|
|
m.lock.Unlock()
|
2020-09-18 22:25:56 +00:00
|
|
|
|
|
|
|
// 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
|
2021-04-27 22:15:57 +00:00
|
|
|
// the update chan.
|
2021-02-23 19:27:24 +00:00
|
|
|
if result.Index > 0 && result.Index > minIndex {
|
2020-09-18 22:25:56 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-04-27 22:15:57 +00:00
|
|
|
switch {
|
|
|
|
case m.err != nil:
|
2021-04-26 16:20:33 +00:00
|
|
|
err := m.err
|
2021-02-23 19:27:24 +00:00
|
|
|
m.lock.Unlock()
|
2021-04-26 16:20:33 +00:00
|
|
|
return result, err
|
2021-04-27 22:15:57 +00:00
|
|
|
case result.Index <= minIndex:
|
|
|
|
// get a reference to the new updateCh, the previous one was closed
|
|
|
|
updateCh = m.updateCh
|
|
|
|
m.lock.Unlock()
|
|
|
|
continue
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 19:27:24 +00:00
|
|
|
result.Value = m.view.Result(m.index)
|
|
|
|
m.lock.Unlock()
|
2020-09-18 22:25:56 +00:00
|
|
|
return result, nil
|
|
|
|
|
2021-02-23 19:27:24 +00:00
|
|
|
case <-ctx.Done():
|
2021-04-27 22:15:57 +00:00
|
|
|
// Update the result value to the latest because callers may still
|
|
|
|
// use the value when the error is context.DeadlineExceeded
|
|
|
|
m.lock.Lock()
|
|
|
|
result.Value = m.view.Result(m.index)
|
|
|
|
m.lock.Unlock()
|
2021-02-23 19:27:24 +00:00
|
|
|
return result, ctx.Err()
|
2020-09-18 22:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|