From a5df5d17b4d46df4e43fa4f379ac80c574322476 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 1 Oct 2020 13:51:55 -0400 Subject: [PATCH 1/3] stream: refactor to support change in framing events Removing EndOfEmptySnapshot, add NewSnapshotToFollow --- agent/consul/stream/event.go | 16 +-- agent/consul/stream/event_buffer.go | 6 ++ agent/consul/stream/event_publisher.go | 113 ++++++++++----------- agent/consul/stream/event_snapshot.go | 77 +++++++------- agent/consul/stream/event_snapshot_test.go | 7 +- agent/consul/stream/event_test.go | 2 +- agent/consul/stream/subscription.go | 4 +- agent/consul/stream/subscription_test.go | 6 +- 8 files changed, 114 insertions(+), 117 deletions(-) diff --git a/agent/consul/stream/event.go b/agent/consul/stream/event.go index d45ab80182..65a2edaf9a 100644 --- a/agent/consul/stream/event.go +++ b/agent/consul/stream/event.go @@ -20,22 +20,22 @@ type Event struct { } // IsEndOfSnapshot returns true if this is a framing event that indicates the -// snapshot has completed. Future events from Subscription.Next will be -// change events. +// snapshot has completed. Subsequent events from Subscription.Next will be +// streamed as they occur. func (e Event) IsEndOfSnapshot() bool { return e.Payload == endOfSnapshot{} } -// IsEndOfEmptySnapshot returns true if this is a framing event that indicates -// there is no snapshot. Future events from Subscription.Next will be -// change events. -func (e Event) IsEndOfEmptySnapshot() bool { - return e.Payload == endOfEmptySnapshot{} +// 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{} } type endOfSnapshot struct{} -type endOfEmptySnapshot struct{} +type newSnapshotToFollow struct{} type closeSubscriptionPayload struct { tokensSecretIDs []string diff --git a/agent/consul/stream/event_buffer.go b/agent/consul/stream/event_buffer.go index 7701ab44b2..1208b07b14 100644 --- a/agent/consul/stream/event_buffer.go +++ b/agent/consul/stream/event_buffer.go @@ -216,3 +216,9 @@ func (i *bufferItem) NextLink() *bufferItem { } return next } + +// HasEventIndex returns true if index matches the Event.Index of this item. Returns +// false if there are no events stored in the item, or the index does not match. +func (i *bufferItem) HasEventIndex(index uint64) bool { + return len(i.Events) > 0 && i.Events[0].Index == index +} diff --git a/agent/consul/stream/event_publisher.go b/agent/consul/stream/event_publisher.go index 1de9e10580..2ff9b09e23 100644 --- a/agent/consul/stream/event_publisher.go +++ b/agent/consul/stream/event_publisher.go @@ -157,8 +157,7 @@ func (e *EventPublisher) getTopicBuffer(topic Topic) *eventBuffer { // When the caller is finished with the subscription for any reason, it must // call Subscription.Unsubscribe to free ACL tracking resources. func (e *EventPublisher) Subscribe(req *SubscribeRequest) (*Subscription, error) { - // Ensure we know how to make a snapshot for this topic - _, ok := e.snapshotHandlers[req.Topic] + handler, ok := e.snapshotHandlers[req.Topic] if !ok || req.Topic == nil { return nil, fmt.Errorf("unknown topic %v", req.Topic) } @@ -166,47 +165,48 @@ func (e *EventPublisher) Subscribe(req *SubscribeRequest) (*Subscription, error) e.lock.Lock() defer e.lock.Unlock() - // Ensure there is a topic buffer for that topic so we start capturing any - // future published events. - buf := e.getTopicBuffer(req.Topic) + topicHead := e.getTopicBuffer(req.Topic).Head() - // See if we need a snapshot - topicHead := buf.Head() - var sub *Subscription - if req.Index > 0 && len(topicHead.Events) > 0 && topicHead.Events[0].Index == req.Index { - // No need for a snapshot, send the "end of empty snapshot" message to signal to - // client its cache is still good, then follow along from here in the topic. + // If the client view is fresh, resume the stream. + if req.Index > 0 && topicHead.HasEventIndex(req.Index) { buf := newEventBuffer() - - // Store the head of that buffer before we append to it to give as the - // starting point for the subscription. - subHead := buf.Head() - - buf.Append([]Event{{ - Index: req.Index, - Topic: req.Topic, - Key: req.Key, - Payload: endOfEmptySnapshot{}, - }}) - - // Now splice the rest of the topic buffer on so the subscription will - // continue to see future updates in the topic buffer. + subscriptionHead := buf.Head() + // splice the rest of the topic buffer onto the subscription buffer so + // the subscription will receive new events. buf.AppendItem(topicHead.NextLink()) - - sub = newSubscription(req, subHead, e.subscriptions.unsubscribe(req)) - } else { - snap, err := e.getSnapshotLocked(req, topicHead) - if err != nil { - return nil, err - } - sub = newSubscription(req, snap.Head, e.subscriptions.unsubscribe(req)) + return e.subscriptions.add(req, subscriptionHead), nil } - e.subscriptions.add(req, sub) - return sub, nil + snapFromCache := e.getCachedSnapshotLocked(req) + if req.Index == 0 && snapFromCache != nil { + return e.subscriptions.add(req, snapFromCache.First), nil + } + snap := newEventSnapshot() + + // TODO: testcase for this case, especially the from-cache-splice case + // if the request has an Index the client view is stale and must be reset + // with a NewSnapshotToFollow event. + if req.Index > 0 { + snap.buffer.Append([]Event{{ + Topic: req.Topic, + Key: req.Key, + Payload: newSnapshotToFollow{}, + }}) + + if snapFromCache != nil { + snap.buffer.AppendItem(snapFromCache.First) + return e.subscriptions.add(req, snap.First), nil + } + } + + snap.appendAndSplice(*req, handler, topicHead) + e.setCachedSnapshotLocked(req, snap) + return e.subscriptions.add(req, snap.First), nil } -func (s *subscriptions) add(req *SubscribeRequest, sub *Subscription) { +func (s *subscriptions) add(req *SubscribeRequest, head *bufferItem) *Subscription { + sub := newSubscription(*req, head, s.unsubscribe(req)) + s.lock.Lock() defer s.lock.Unlock() @@ -216,6 +216,7 @@ func (s *subscriptions) add(req *SubscribeRequest, sub *Subscription) { s.byToken[req.Token] = subsByToken } subsByToken[req] = sub + return sub } func (s *subscriptions) closeSubscriptionsForTokens(tokenSecretIDs []string) { @@ -263,7 +264,8 @@ func (s *subscriptions) closeAll() { } } -func (e *EventPublisher) getSnapshotLocked(req *SubscribeRequest, topicHead *bufferItem) (*eventSnapshot, error) { +// EventPublisher.lock must be held to call this method. +func (e *EventPublisher) getCachedSnapshotLocked(req *SubscribeRequest) *eventSnapshot { topicSnaps, ok := e.snapCache[req.Topic] if !ok { topicSnaps = make(map[string]*eventSnapshot) @@ -272,25 +274,22 @@ func (e *EventPublisher) getSnapshotLocked(req *SubscribeRequest, topicHead *buf snap, ok := topicSnaps[req.Key] if ok && snap.err() == nil { - return snap, nil + return snap } - - handler, ok := e.snapshotHandlers[req.Topic] - if !ok { - return nil, fmt.Errorf("unknown topic %v", req.Topic) - } - - snap = newEventSnapshot(req, topicHead, handler) - if e.snapCacheTTL > 0 { - topicSnaps[req.Key] = snap - - // Trigger a clearout after TTL - time.AfterFunc(e.snapCacheTTL, func() { - e.lock.Lock() - defer e.lock.Unlock() - delete(topicSnaps, req.Key) - }) - } - - return snap, nil + return nil +} + +// EventPublisher.lock must be held to call this method. +func (e *EventPublisher) setCachedSnapshotLocked(req *SubscribeRequest, snap *eventSnapshot) { + if e.snapCacheTTL == 0 { + return + } + e.snapCache[req.Topic][req.Key] = snap + + // Setup a cache eviction + time.AfterFunc(e.snapCacheTTL, func() { + e.lock.Lock() + defer e.lock.Unlock() + delete(e.snapCache[req.Topic], req.Key) + }) } diff --git a/agent/consul/stream/event_snapshot.go b/agent/consul/stream/event_snapshot.go index 2f0d276f78..124cb32513 100644 --- a/agent/consul/stream/event_snapshot.go +++ b/agent/consul/stream/event_snapshot.go @@ -9,50 +9,45 @@ package stream // collected automatically by Go's runtime. This simplifies snapshot and buffer // management dramatically. type eventSnapshot struct { - // Head is the first item in the buffer containing the snapshot. Once the - // snapshot is complete, subsequent BufferItems are appended to snapBuffer, - // so that subscribers receive all the events from the same buffer. - Head *bufferItem + // First item in the buffer. Used as the Head of a subscription, or to + // splice this snapshot onto another one. + First *bufferItem - // snapBuffer is the Head of the snapshot buffer the fn should write to. - snapBuffer *eventBuffer + // buffer is the Head of the snapshot buffer the fn should write to. + buffer *eventBuffer } -// newEventSnapshot creates a snapshot buffer based on the subscription request. -// The current buffer head for the topic requested is passed so that once the -// 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. -func newEventSnapshot(req *SubscribeRequest, topicBufferHead *bufferItem, fn SnapshotFunc) *eventSnapshot { - buf := newEventBuffer() - s := &eventSnapshot{ - Head: buf.Head(), - snapBuffer: buf, +// newEventSnapshot creates an empty snapshot buffer. +func newEventSnapshot() *eventSnapshot { + snapBuffer := newEventBuffer() + return &eventSnapshot{ + First: snapBuffer.Head(), + buffer: snapBuffer, } - - go func() { - idx, err := fn(*req, s.snapBuffer) - if err != nil { - s.snapBuffer.AppendItem(&bufferItem{Err: err}) - 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) - }() - return s } +// appendAndSlice populates the snapshot buffer by calling the SnapshotFunc, +// then adding an endOfSnapshot framing event, and finally by splicing in +// events from the topicBuffer. +func (s *eventSnapshot) appendAndSplice(req SubscribeRequest, fn SnapshotFunc, topicBufferHead *bufferItem) { + idx, err := fn(req, s.buffer) + if err != nil { + s.buffer.AppendItem(&bufferItem{Err: err}) + return + } + s.buffer.Append([]Event{{ + Topic: req.Topic, + Key: req.Key, + Index: idx, + Payload: endOfSnapshot{}, + }}) + s.spliceFromTopicBuffer(topicBufferHead, idx) +} + +// spliceFromTopicBuffer traverses the topicBuffer looking for the last item +// in the buffer, or the first item where the index is greater than idx. Once +// the item is found it is appended to the snapshot buffer. func (s *eventSnapshot) spliceFromTopicBuffer(topicBufferHead *bufferItem, idx uint64) { - // Now splice on the topic buffer. We need to iterate through the buffer to - // find the first event after the current snapshot. item := topicBufferHead for { next := item.NextNoBlock() @@ -62,7 +57,7 @@ func (s *eventSnapshot) spliceFromTopicBuffer(topicBufferHead *bufferItem, idx u // 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. - s.snapBuffer.AppendItem(item.NextLink()) + s.buffer.AppendItem(item.NextLink()) return case next.Err != nil: @@ -71,14 +66,14 @@ func (s *eventSnapshot) spliceFromTopicBuffer(topicBufferHead *bufferItem, idx u // 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) + s.buffer.AppendItem(next) return case len(next.Events) > 0 && next.Events[0].Index > idx: // 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. - s.snapBuffer.AppendItem(next) + s.buffer.AppendItem(next) return } @@ -93,6 +88,6 @@ func (s *eventSnapshot) spliceFromTopicBuffer(topicBufferHead *bufferItem, idx u func (s *eventSnapshot) err() error { // 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() + head := s.buffer.Head() return head.Err } diff --git a/agent/consul/stream/event_snapshot_test.go b/agent/consul/stream/event_snapshot_test.go index c888e844ab..98e3c683bc 100644 --- a/agent/consul/stream/event_snapshot_test.go +++ b/agent/consul/stream/event_snapshot_test.go @@ -87,9 +87,8 @@ func TestEventSnapshot(t *testing.T) { tb.Append([]Event{newDefaultHealthEvent(index, 10000+i)}) } - // Create eventSnapshot, (will call snFn in another goroutine). The - // Request is ignored by the snapFunc so doesn't matter for now. - es := newEventSnapshot(&SubscribeRequest{}, tbHead, snFn) + es := newEventSnapshot() + es.appendAndSplice(SubscribeRequest{}, snFn, tbHead) // Deliver any post-snapshot events simulating updates that occur // logically after snapshot. It doesn't matter that these might actually @@ -112,7 +111,7 @@ func TestEventSnapshot(t *testing.T) { snapIDs := make([]string, 0, tc.snapshotSize) updateIDs := make([]string, 0, tc.updatesAfterSnap) snapDone := false - curItem := es.Head + curItem := es.First var err error RECV: for { diff --git a/agent/consul/stream/event_test.go b/agent/consul/stream/event_test.go index aff938a678..182f0d5122 100644 --- a/agent/consul/stream/event_test.go +++ b/agent/consul/stream/event_test.go @@ -11,7 +11,7 @@ func TestEvent_IsEndOfSnapshot(t *testing.T) { require.True(t, e.IsEndOfSnapshot()) t.Run("not EndOfSnapshot", func(t *testing.T) { - e := Event{Payload: endOfEmptySnapshot{}} + e := Event{Payload: newSnapshotToFollow{}} require.False(t, e.IsEndOfSnapshot()) }) } diff --git a/agent/consul/stream/subscription.go b/agent/consul/stream/subscription.go index e4a24cc1eb..5b86d48133 100644 --- a/agent/consul/stream/subscription.go +++ b/agent/consul/stream/subscription.go @@ -28,7 +28,7 @@ type Subscription struct { state uint32 // req is the requests that we are responding to - req *SubscribeRequest + req SubscribeRequest // currentItem stores the current snapshot or topic buffer item we are on. It // is mutated by calls to Next. @@ -56,7 +56,7 @@ type SubscribeRequest struct { // newSubscription return a new subscription. The caller is responsible for // calling Unsubscribe when it is done with the subscription, to free resources. -func newSubscription(req *SubscribeRequest, item *bufferItem, unsub func()) *Subscription { +func newSubscription(req SubscribeRequest, item *bufferItem, unsub func()) *Subscription { return &Subscription{ forceClosed: make(chan struct{}), req: req, diff --git a/agent/consul/stream/subscription_test.go b/agent/consul/stream/subscription_test.go index 84e941a3bf..29582c2bde 100644 --- a/agent/consul/stream/subscription_test.go +++ b/agent/consul/stream/subscription_test.go @@ -23,8 +23,7 @@ func TestSubscription(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - // Create a subscription - req := &SubscribeRequest{ + req := SubscribeRequest{ Topic: testTopic, Key: "test", } @@ -103,8 +102,7 @@ func TestSubscription_Close(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - // Create a subscription - req := &SubscribeRequest{ + req := SubscribeRequest{ Topic: testTopic, Key: "test", } From b88f817bc5e966fe27dff61502df6146640c67c2 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 1 Oct 2020 14:41:47 -0400 Subject: [PATCH 2/3] Update proto for change to stream framing events --- proto/pbsubscribe/subscribe.pb.go | 160 ++++++++++++++++-------------- proto/pbsubscribe/subscribe.proto | 34 ++++--- 2 files changed, 106 insertions(+), 88 deletions(-) diff --git a/proto/pbsubscribe/subscribe.pb.go b/proto/pbsubscribe/subscribe.pb.go index 435307f987..35122711ec 100644 --- a/proto/pbsubscribe/subscribe.pb.go +++ b/proto/pbsubscribe/subscribe.pb.go @@ -200,7 +200,7 @@ type Event struct { // // Types that are valid to be assigned to Payload: // *Event_EndOfSnapshot - // *Event_EndOfEmptySnapshot + // *Event_NewSnapshotToFollow // *Event_EventBatch // *Event_ServiceHealth Payload isEvent_Payload `protobuf_oneof:"Payload"` @@ -251,8 +251,8 @@ type isEvent_Payload interface { type Event_EndOfSnapshot struct { EndOfSnapshot bool `protobuf:"varint,5,opt,name=EndOfSnapshot,proto3,oneof"` } -type Event_EndOfEmptySnapshot struct { - EndOfEmptySnapshot bool `protobuf:"varint,6,opt,name=EndOfEmptySnapshot,proto3,oneof"` +type Event_NewSnapshotToFollow struct { + NewSnapshotToFollow bool `protobuf:"varint,6,opt,name=NewSnapshotToFollow,proto3,oneof"` } type Event_EventBatch struct { EventBatch *EventBatch `protobuf:"bytes,7,opt,name=EventBatch,proto3,oneof"` @@ -261,10 +261,10 @@ type Event_ServiceHealth struct { ServiceHealth *ServiceHealthUpdate `protobuf:"bytes,10,opt,name=ServiceHealth,proto3,oneof"` } -func (*Event_EndOfSnapshot) isEvent_Payload() {} -func (*Event_EndOfEmptySnapshot) isEvent_Payload() {} -func (*Event_EventBatch) isEvent_Payload() {} -func (*Event_ServiceHealth) isEvent_Payload() {} +func (*Event_EndOfSnapshot) isEvent_Payload() {} +func (*Event_NewSnapshotToFollow) isEvent_Payload() {} +func (*Event_EventBatch) isEvent_Payload() {} +func (*Event_ServiceHealth) isEvent_Payload() {} func (m *Event) GetPayload() isEvent_Payload { if m != nil { @@ -301,9 +301,9 @@ func (m *Event) GetEndOfSnapshot() bool { return false } -func (m *Event) GetEndOfEmptySnapshot() bool { - if x, ok := m.GetPayload().(*Event_EndOfEmptySnapshot); ok { - return x.EndOfEmptySnapshot +func (m *Event) GetNewSnapshotToFollow() bool { + if x, ok := m.GetPayload().(*Event_NewSnapshotToFollow); ok { + return x.NewSnapshotToFollow } return false } @@ -326,7 +326,7 @@ func (m *Event) GetServiceHealth() *ServiceHealthUpdate { func (*Event) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{}) { return _Event_OneofMarshaler, _Event_OneofUnmarshaler, _Event_OneofSizer, []interface{}{ (*Event_EndOfSnapshot)(nil), - (*Event_EndOfEmptySnapshot)(nil), + (*Event_NewSnapshotToFollow)(nil), (*Event_EventBatch)(nil), (*Event_ServiceHealth)(nil), } @@ -343,9 +343,9 @@ func _Event_OneofMarshaler(msg proto.Message, b *proto.Buffer) error { } _ = b.EncodeVarint(5<<3 | proto.WireVarint) _ = b.EncodeVarint(t) - case *Event_EndOfEmptySnapshot: + case *Event_NewSnapshotToFollow: t := uint64(0) - if x.EndOfEmptySnapshot { + if x.NewSnapshotToFollow { t = 1 } _ = b.EncodeVarint(6<<3 | proto.WireVarint) @@ -377,12 +377,12 @@ func _Event_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) x, err := b.DecodeVarint() m.Payload = &Event_EndOfSnapshot{x != 0} return true, err - case 6: // Payload.EndOfEmptySnapshot + case 6: // Payload.NewSnapshotToFollow if wire != proto.WireVarint { return true, proto.ErrInternalBadWireType } x, err := b.DecodeVarint() - m.Payload = &Event_EndOfEmptySnapshot{x != 0} + m.Payload = &Event_NewSnapshotToFollow{x != 0} return true, err case 7: // Payload.EventBatch if wire != proto.WireBytes { @@ -412,7 +412,7 @@ func _Event_OneofSizer(msg proto.Message) (n int) { case *Event_EndOfSnapshot: n += 1 // tag and wire n += 1 - case *Event_EndOfEmptySnapshot: + case *Event_NewSnapshotToFollow: n += 1 // tag and wire n += 1 case *Event_EventBatch: @@ -546,40 +546,40 @@ func init() { func init() { proto.RegisterFile("proto/pbsubscribe/subscribe.proto", fileDescriptor_ab3eb8c810e315fb) } var fileDescriptor_ab3eb8c810e315fb = []byte{ - // 521 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0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0x7b, 0x5b, 0xe2, 0xed, 0x98, 0x4e, 0x5e, 0xa5, 0x4b, 0xa7, 0x35, 0xa8, 0x9c, 0xf3, 0x29, 0x17, - 0xdf, 0xb8, 0x69, 0xd0, 0xbb, 0x5b, 0x73, 0x32, 0x09, 0xb5, 0xa0, 0xbe, 0x01, 0xf5, 0x04, 0xe7, - 0xe8, 0x2b, 0xb3, 0x70, 0xf2, 0x18, 0xaa, 0x59, 0x38, 0x7a, 0x07, 0xf6, 0x3d, 0x9c, 0x44, 0x89, - 0xc2, 0xd8, 0x34, 0xe8, 0x21, 0xc0, 0x19, 0xc6, 0xd7, 0x35, 0xe9, 0x7c, 0x82, 0x07, 0x23, 0xc5, - 0x14, 0xf6, 0x42, 0xc6, 0x27, 0x98, 0x5e, 0x84, 0x54, 0x91, 0xe0, 0xf4, 0x05, 0x54, 0xb3, 0x0b, - 0xa1, 0x47, 0xf9, 0x85, 0x6c, 0xdd, 0x4d, 0x63, 0x67, 0xa6, 0x2d, 0xe3, 0x94, 0x74, 0x5f, 0xfe, - 0x5a, 0xda, 0xe4, 0xf7, 0xd2, 0x26, 0x7f, 0x96, 0x36, 0xb9, 0xfa, 0x6b, 0x1b, 0x9f, 0x9f, 0x4c, - 0x22, 0x15, 0xce, 0xc7, 0x6d, 0x5f, 0x5c, 0xb8, 0x21, 0x4b, 0xc2, 0xc8, 0x17, 0xb1, 0x74, 0x7d, - 0xc1, 0x93, 0xf9, 0xcc, 0xdd, 0x39, 0xed, 0x71, 0x59, 0x43, 0x4f, 0xff, 0x05, 0x00, 0x00, 0xff, - 0xff, 0x7d, 0xf7, 0xca, 0x01, 0xf6, 0x03, 0x00, 0x00, + // 526 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x53, 0x5f, 0x6f, 0xd2, 0x50, + 0x14, 0xef, 0x85, 0x01, 0xe3, 0xe0, 0x96, 0x7a, 0x87, 0xb1,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, 0xd7, 0xe8, 0xc3, 0x8d, 0x39, 0xe9, 0x84, 0x1a, 0x50, + 0x5f, 0x83, 0x7a, 0x82, 0x73, 0x74, 0xa5, 0x5e, 0x38, 0x7e, 0x06, 0xd5, 0x2c, 0x1c, 0x7d, 0x00, + 0xbb, 0x0e, 0x8e, 0x83, 0x44, 0x62, 0xac, 0x6b, 0x74, 0x1f, 0xe0, 0x04, 0xe3, 0x55, 0x4d, 0x3a, + 0x9f, 0xe1, 0xf1, 0x50, 0x32, 0x89, 0x3d, 0x9f, 0xf1, 0x31, 0xa6, 0x37, 0x11, 0xc9, 0x40, 0x70, + 0xfa, 0x06, 0xaa, 0xd9, 0x8d, 0xd0, 0xc3, 0xfc, 0x42, 0x36, 0x2e, 0xa7, 0xb1, 0x35, 0xd3, 0x96, + 0xf6, 0x82, 0x74, 0xdf, 0xfe, 0x9e, 0x9b, 0xe4, 0xcf, 0xdc, 0x24, 0x7f, 0xe7, 0x26, 0xb9, 0xf9, + 0x67, 0x6a, 0x5f, 0x9e, 0x8f, 0x03, 0xe9, 0x4f, 0x47, 0x6d, 0x57, 0x7c, 0xb3, 0x7d, 0x96, 0xf8, + 0x81, 0x2b, 0xe2, 0xc8, 0x76, 0x05, 0x4f, 0xa6, 0xa1, 0xbd, 0x75, 0xdc, 0xa3, 0xb2, 0x82, 0x5e, + 0xfe, 0x0f, 0x00, 0x00, 0xff, 0xff, 0x44, 0xbc, 0x0a, 0xfb, 0xf8, 0x03, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -595,18 +595,24 @@ const _ = grpc.SupportPackageIsVersion4 // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. type StateChangeSubscriptionClient interface { // Subscribe to a topic to receive events when there are changes to the topic. - // TODO: document how to handle framing events // + // If SubscribeRequest.Index is 0 the event stream will start with one or + // more snapshot events, followed by an EndOfSnapshot event. Subsequent + // events will be a live stream of events as they happen. // - // Subscribe may return an ABORTED status error to indicate the client must - // re-start the Subscribe call. + // If SubscribeRequest.Index is > 0 it is assumed the client already has a + // snapshot, and is trying to resume a stream that was disconnected. The + // client will either receive a NewSnapshotToFollow event, indicating the + // client view is stale and it must reset its view and prepare for a new + // snapshot. Or, if no NewSnapshotToFollow event is received, the client + // view is still fresh, and all events will be the live stream. + // + // Subscribe may return a gRPC status error with codes.ABORTED to indicate + // the client view is now stale due to a change on the server. The client + // must reset its view and issue a new Subscribe call to restart the stream. // This error is used when the server can no longer correctly maintain the - // stream, for example because the ACL permissions for the token changed - // and the server doesn't know which previously delivered events should - // now not be visible. Clients when receiving this must reset their - // local copy of the state to empty and start over from index 0 to get a - // valid snapshot again. Servers may also send this if their state store - // is restored from a snapshot. + // stream, for example because the ACL permissions for the token changed, or + // because the server state was restored from a snapshot. Subscribe(ctx context.Context, in *SubscribeRequest, opts ...grpc.CallOption) (StateChangeSubscription_SubscribeClient, error) } @@ -653,18 +659,24 @@ func (x *stateChangeSubscriptionSubscribeClient) Recv() (*Event, error) { // StateChangeSubscriptionServer is the server API for StateChangeSubscription service. type StateChangeSubscriptionServer interface { // Subscribe to a topic to receive events when there are changes to the topic. - // TODO: document how to handle framing events // + // If SubscribeRequest.Index is 0 the event stream will start with one or + // more snapshot events, followed by an EndOfSnapshot event. Subsequent + // events will be a live stream of events as they happen. // - // Subscribe may return an ABORTED status error to indicate the client must - // re-start the Subscribe call. + // If SubscribeRequest.Index is > 0 it is assumed the client already has a + // snapshot, and is trying to resume a stream that was disconnected. The + // client will either receive a NewSnapshotToFollow event, indicating the + // client view is stale and it must reset its view and prepare for a new + // snapshot. Or, if no NewSnapshotToFollow event is received, the client + // view is still fresh, and all events will be the live stream. + // + // Subscribe may return a gRPC status error with codes.ABORTED to indicate + // the client view is now stale due to a change on the server. The client + // must reset its view and issue a new Subscribe call to restart the stream. // This error is used when the server can no longer correctly maintain the - // stream, for example because the ACL permissions for the token changed - // and the server doesn't know which previously delivered events should - // now not be visible. Clients when receiving this must reset their - // local copy of the state to empty and start over from index 0 to get a - // valid snapshot again. Servers may also send this if their state store - // is restored from a snapshot. + // stream, for example because the ACL permissions for the token changed, or + // because the server state was restored from a snapshot. Subscribe(*SubscribeRequest, StateChangeSubscription_SubscribeServer) error } @@ -842,14 +854,14 @@ func (m *Event_EndOfSnapshot) MarshalToSizedBuffer(dAtA []byte) (int, error) { dAtA[i] = 0x28 return len(dAtA) - i, nil } -func (m *Event_EndOfEmptySnapshot) MarshalTo(dAtA []byte) (int, error) { +func (m *Event_NewSnapshotToFollow) MarshalTo(dAtA []byte) (int, error) { return m.MarshalToSizedBuffer(dAtA[:m.Size()]) } -func (m *Event_EndOfEmptySnapshot) MarshalToSizedBuffer(dAtA []byte) (int, error) { +func (m *Event_NewSnapshotToFollow) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) i-- - if m.EndOfEmptySnapshot { + if m.NewSnapshotToFollow { dAtA[i] = 1 } else { dAtA[i] = 0 @@ -1058,7 +1070,7 @@ func (m *Event_EndOfSnapshot) Size() (n int) { n += 2 return n } -func (m *Event_EndOfEmptySnapshot) Size() (n int) { +func (m *Event_NewSnapshotToFollow) Size() (n int) { if m == nil { return 0 } @@ -1444,7 +1456,7 @@ func (m *Event) Unmarshal(dAtA []byte) error { m.Payload = &Event_EndOfSnapshot{b} case 6: if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field EndOfEmptySnapshot", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field NewSnapshotToFollow", wireType) } var v int for shift := uint(0); ; shift += 7 { @@ -1462,7 +1474,7 @@ func (m *Event) Unmarshal(dAtA []byte) error { } } b := bool(v != 0) - m.Payload = &Event_EndOfEmptySnapshot{b} + m.Payload = &Event_NewSnapshotToFollow{b} case 7: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field EventBatch", wireType) diff --git a/proto/pbsubscribe/subscribe.proto b/proto/pbsubscribe/subscribe.proto index 82e3a1d30f..c0efab87c3 100644 --- a/proto/pbsubscribe/subscribe.proto +++ b/proto/pbsubscribe/subscribe.proto @@ -13,18 +13,24 @@ import "proto/pbservice/node.proto"; // state change events. Events are streamed as they happen. service StateChangeSubscription { // Subscribe to a topic to receive events when there are changes to the topic. - // TODO: document how to handle framing events // + // If SubscribeRequest.Index is 0 the event stream will start with one or + // more snapshot events, followed by an EndOfSnapshot event. Subsequent + // events will be a live stream of events as they happen. // - // Subscribe may return an ABORTED status error to indicate the client must - // re-start the Subscribe call. + // If SubscribeRequest.Index is > 0 it is assumed the client already has a + // snapshot, and is trying to resume a stream that was disconnected. The + // client will either receive a NewSnapshotToFollow event, indicating the + // client view is stale and it must reset its view and prepare for a new + // snapshot. Or, if no NewSnapshotToFollow event is received, the client + // view is still fresh, and all events will be the live stream. + // + // Subscribe may return a gRPC status error with codes.ABORTED to indicate + // the client view is now stale due to a change on the server. The client + // must reset its view and issue a new Subscribe call to restart the stream. // This error is used when the server can no longer correctly maintain the - // stream, for example because the ACL permissions for the token changed - // and the server doesn't know which previously delivered events should - // now not be visible. Clients when receiving this must reset their - // local copy of the state to empty and start over from index 0 to get a - // valid snapshot again. Servers may also send this if their state store - // is restored from a snapshot. + // stream, for example because the ACL permissions for the token changed, or + // because the server state was restored from a snapshot. rpc Subscribe(SubscribeRequest) returns (stream Event) {} } @@ -92,11 +98,11 @@ message Event { // ended. Subsequent Events delivered will be mutations to that result. bool EndOfSnapshot = 5; - // EndOfEmptySnapshot indicates that the client is still up-to-date. - // The snapshot has ended, and was empty. The rest of the stream will be - // individual update events. It distinguishes between "up to date, no snapshot" - // and "snapshot contains zero events but you should reset any old state to be blank". - bool EndOfEmptySnapshot = 6; + // NewSnapshotToFollow indicates that the client view is stale. The client + // must reset its view before handing any more events. Subsequent events + // in the stream will be for a new snapshot until an EndOfSnapshot event + // is received. + bool NewSnapshotToFollow = 6; // EventBatch is a set of events. This is typically used as the payload // type where multiple events are emitted in a single topic and raft From 1c6be5ac75a943dd0374f3e1d955c882e29b4a0a Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 1 Oct 2020 16:34:00 -0400 Subject: [PATCH 3/3] stream: full test coverage for EventPublisher.Subscribe --- agent/consul/stream/event_publisher.go | 21 +- agent/consul/stream/event_publisher_test.go | 286 ++++++++++++++++++-- 2 files changed, 268 insertions(+), 39 deletions(-) diff --git a/agent/consul/stream/event_publisher.go b/agent/consul/stream/event_publisher.go index 2ff9b09e23..9512a04b5e 100644 --- a/agent/consul/stream/event_publisher.go +++ b/agent/consul/stream/event_publisher.go @@ -36,7 +36,7 @@ type EventPublisher struct { // publishCh is used to send messages from an active txn to a goroutine which // publishes events, so that publishing can happen asynchronously from // the Commit call in the FSM hot path. - publishCh chan changeEvents + publishCh chan []Event snapshotHandlers SnapshotHandlers } @@ -54,10 +54,6 @@ type subscriptions struct { byToken map[string]map[*SubscribeRequest]*Subscription } -type changeEvents struct { - events []Event -} - // SnapshotHandlers is a mapping of Topic to a function which produces a snapshot // of events for the SubscribeRequest. Events are appended to the snapshot using SnapshotAppender. // The nil Topic is reserved and should not be used. @@ -84,7 +80,7 @@ func NewEventPublisher(handlers SnapshotHandlers, snapCacheTTL time.Duration) *E snapCacheTTL: snapCacheTTL, topicBuffers: make(map[Topic]*eventBuffer), snapCache: make(map[Topic]map[string]*eventSnapshot), - publishCh: make(chan changeEvents, 64), + publishCh: make(chan []Event, 64), subscriptions: &subscriptions{ byToken: make(map[string]map[*SubscribeRequest]*Subscription), }, @@ -97,7 +93,7 @@ func NewEventPublisher(handlers SnapshotHandlers, snapCacheTTL time.Duration) *E // Publish events to all subscribers of the event Topic. func (e *EventPublisher) Publish(events []Event) { if len(events) > 0 { - e.publishCh <- changeEvents{events: events} + e.publishCh <- events } } @@ -110,16 +106,16 @@ func (e *EventPublisher) Run(ctx context.Context) { e.subscriptions.closeAll() return case update := <-e.publishCh: - e.sendEvents(update) + e.publishEvent(update) } } } -// sendEvents sends the given events to any applicable topic listeners, as well -// as any ACL update events to cause affected listeners to reset their stream. -func (e *EventPublisher) sendEvents(update changeEvents) { +// publishEvent appends the events to any applicable topic buffers. It handles +// any closeSubscriptionPayload events by closing associated subscriptions. +func (e *EventPublisher) publishEvent(events []Event) { eventsByTopic := make(map[Topic][]Event) - for _, event := range update.events { + for _, event := range events { if unsubEvent, ok := event.Payload.(closeSubscriptionPayload); ok { e.subscriptions.closeSubscriptionsForTokens(unsubEvent.tokensSecretIDs) continue @@ -183,7 +179,6 @@ func (e *EventPublisher) Subscribe(req *SubscribeRequest) (*Subscription, error) } snap := newEventSnapshot() - // TODO: testcase for this case, especially the from-cache-splice case // if the request has an Index the client view is stale and must be reset // with a NewSnapshotToFollow event. if req.Index > 0 { diff --git a/agent/consul/stream/event_publisher_test.go b/agent/consul/stream/event_publisher_test.go index 63f7d763d9..87f8729617 100644 --- a/agent/consul/stream/event_publisher_test.go +++ b/agent/consul/stream/event_publisher_test.go @@ -17,8 +17,8 @@ func (i intTopic) String() string { var testTopic Topic = intTopic(999) -func TestEventPublisher_PublishChangesAndSubscribe_WithSnapshot(t *testing.T) { - subscription := &SubscribeRequest{ +func TestEventPublisher_SubscribeWithIndex0(t *testing.T) { + req := &SubscribeRequest{ Topic: testTopic, Key: "sub-key", } @@ -28,20 +28,18 @@ func TestEventPublisher_PublishChangesAndSubscribe_WithSnapshot(t *testing.T) { publisher := NewEventPublisher(newTestSnapshotHandlers(), 0) go publisher.Run(ctx) - sub, err := publisher.Subscribe(subscription) + sub, err := publisher.Subscribe(req) require.NoError(t, err) - eventCh := consumeSubscription(ctx, sub) + eventCh := runSubscription(ctx, sub) - result := nextResult(t, eventCh) - require.NoError(t, result.Err) - expected := []Event{{Payload: "snapshot-event-payload", Key: "sub-key"}} - require.Equal(t, expected, result.Events) + next := getNextEvents(t, eventCh) + expected := []Event{testSnapshotEvent} + require.Equal(t, expected, next) - result = nextResult(t, eventCh) - require.Len(t, result.Events, 1) - require.True(t, result.Events[0].IsEndOfSnapshot()) + next = getNextEvents(t, eventCh) + require.Len(t, next, 1) + require.True(t, next[0].IsEndOfSnapshot()) - // Now subscriber should block waiting for updates assertNoResult(t, eventCh) events := []Event{{ @@ -52,10 +50,16 @@ func TestEventPublisher_PublishChangesAndSubscribe_WithSnapshot(t *testing.T) { publisher.Publish(events) // Subscriber should see the published event - result = nextResult(t, eventCh) - require.NoError(t, result.Err) + next = getNextEvents(t, eventCh) expected = []Event{{Payload: "the-published-event-payload", Key: "sub-key", Topic: testTopic}} - require.Equal(t, expected, result.Events) + require.Equal(t, expected, next) +} + +var testSnapshotEvent = Event{ + Topic: testTopic, + Payload: "snapshot-event-payload", + Key: "sub-key", + Index: 1, } func newTestSnapshotHandlers() SnapshotHandlers { @@ -64,18 +68,18 @@ func newTestSnapshotHandlers() SnapshotHandlers { if req.Topic != testTopic { return 0, fmt.Errorf("unexpected topic: %v", req.Topic) } - buf.Append([]Event{{Payload: "snapshot-event-payload", Key: "sub-key"}}) + buf.Append([]Event{testSnapshotEvent}) return 1, nil }, } } -func consumeSubscription(ctx context.Context, sub *Subscription) <-chan subNextResult { - eventCh := make(chan subNextResult, 1) +func runSubscription(ctx context.Context, sub *Subscription) <-chan eventOrErr { + eventCh := make(chan eventOrErr, 1) go func() { for { es, err := sub.Next(ctx) - eventCh <- subNextResult{ + eventCh <- eventOrErr{ Events: es, Err: err, } @@ -87,30 +91,31 @@ func consumeSubscription(ctx context.Context, sub *Subscription) <-chan subNextR return eventCh } -type subNextResult struct { +type eventOrErr struct { Events []Event Err error } -func nextResult(t *testing.T, eventCh <-chan subNextResult) subNextResult { +func getNextEvents(t *testing.T, eventCh <-chan eventOrErr) []Event { t.Helper() select { case next := <-eventCh: - return next + require.NoError(t, next.Err) + return next.Events case <-time.After(100 * time.Millisecond): - t.Fatalf("no event after 100ms") + t.Fatalf("timeout waiting for event from subscription") + return nil } - return subNextResult{} } -func assertNoResult(t *testing.T, eventCh <-chan subNextResult) { +func assertNoResult(t *testing.T, eventCh <-chan eventOrErr) { t.Helper() select { case next := <-eventCh: require.NoError(t, next.Err) require.Len(t, next.Events, 1) t.Fatalf("received unexpected event: %#v", next.Events[0].Payload) - case <-time.After(100 * time.Millisecond): + case <-time.After(25 * time.Millisecond): } } @@ -156,3 +161,232 @@ func consumeSub(ctx context.Context, sub *Subscription) error { } } } + +func TestEventPublisher_SubscribeWithIndex0_FromCache(t *testing.T) { + req := &SubscribeRequest{ + Topic: testTopic, + Key: "sub-key", + } + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + + publisher := NewEventPublisher(newTestSnapshotHandlers(), time.Second) + go publisher.Run(ctx) + _, err := publisher.Subscribe(req) + require.NoError(t, err) + + publisher.snapshotHandlers[testTopic] = func(_ SubscribeRequest, _ SnapshotAppender) (uint64, error) { + return 0, fmt.Errorf("error should not be seen, cache should have been used") + } + + sub, err := publisher.Subscribe(req) + require.NoError(t, err) + + eventCh := runSubscription(ctx, sub) + next := getNextEvents(t, eventCh) + expected := []Event{testSnapshotEvent} + require.Equal(t, expected, next) + + next = getNextEvents(t, eventCh) + require.Len(t, next, 1) + require.True(t, next[0].IsEndOfSnapshot()) + + // Now subscriber should block waiting for updates + assertNoResult(t, eventCh) + + events := []Event{{ + Topic: testTopic, + Key: "sub-key", + Payload: "the-published-event-payload", + Index: 3, + }} + publisher.Publish(events) + + // Subscriber should see the published event + next = getNextEvents(t, eventCh) + expected = []Event{events[0]} + require.Equal(t, expected, next) +} + +func TestEventPublisher_SubscribeWithIndexNotZero_CanResume(t *testing.T) { + req := &SubscribeRequest{ + Topic: testTopic, + Key: "sub-key", + } + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + + publisher := NewEventPublisher(newTestSnapshotHandlers(), time.Second) + go publisher.Run(ctx) + // Include the same event in the topicBuffer + publisher.publishEvent([]Event{testSnapshotEvent}) + + runStep(t, "start a subscription and unsub", func(t *testing.T) { + sub, err := publisher.Subscribe(req) + require.NoError(t, err) + defer sub.Unsubscribe() + + eventCh := runSubscription(ctx, sub) + + next := getNextEvents(t, eventCh) + expected := []Event{testSnapshotEvent} + require.Equal(t, expected, next) + + next = getNextEvents(t, eventCh) + require.Len(t, next, 1) + require.True(t, next[0].IsEndOfSnapshot()) + require.Equal(t, uint64(1), next[0].Index) + }) + + runStep(t, "resume the subscription", func(t *testing.T) { + newReq := *req + newReq.Index = 1 + sub, err := publisher.Subscribe(&newReq) + require.NoError(t, err) + + eventCh := runSubscription(ctx, sub) + assertNoResult(t, eventCh) + + expected := Event{ + Topic: testTopic, + Key: "sub-key", + Index: 3, + Payload: "event-3", + } + publisher.publishEvent([]Event{expected}) + + next := getNextEvents(t, eventCh) + require.Equal(t, []Event{expected}, next) + }) +} + +func TestEventPublisher_SubscribeWithIndexNotZero_NewSnapshot(t *testing.T) { + req := &SubscribeRequest{ + Topic: testTopic, + Key: "sub-key", + } + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + + publisher := NewEventPublisher(newTestSnapshotHandlers(), 0) + go publisher.Run(ctx) + // Include the same event in the topicBuffer + publisher.publishEvent([]Event{testSnapshotEvent}) + + runStep(t, "start a subscription and unsub", func(t *testing.T) { + sub, err := publisher.Subscribe(req) + require.NoError(t, err) + defer sub.Unsubscribe() + + eventCh := runSubscription(ctx, sub) + + next := getNextEvents(t, eventCh) + expected := []Event{testSnapshotEvent} + require.Equal(t, expected, next) + + next = getNextEvents(t, eventCh) + require.Len(t, next, 1) + require.True(t, next[0].IsEndOfSnapshot()) + require.Equal(t, uint64(1), next[0].Index) + }) + + nextEvent := Event{ + Topic: testTopic, + Key: "sub-key", + Index: 3, + Payload: "event-3", + } + + runStep(t, "publish an event while unsubed", func(t *testing.T) { + publisher.publishEvent([]Event{nextEvent}) + }) + + runStep(t, "resume the subscription", func(t *testing.T) { + newReq := *req + newReq.Index = 1 + sub, err := publisher.Subscribe(&newReq) + require.NoError(t, err) + + eventCh := runSubscription(ctx, sub) + next := getNextEvents(t, eventCh) + require.True(t, next[0].IsNewSnapshotToFollow(), next) + + next = getNextEvents(t, eventCh) + require.Equal(t, testSnapshotEvent, next[0]) + + next = getNextEvents(t, eventCh) + require.True(t, next[0].IsEndOfSnapshot()) + }) +} + +func TestEventPublisher_SubscribeWithIndexNotZero_NewSnapshotFromCache(t *testing.T) { + req := &SubscribeRequest{ + Topic: testTopic, + Key: "sub-key", + } + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + + publisher := NewEventPublisher(newTestSnapshotHandlers(), time.Second) + go publisher.Run(ctx) + // Include the same event in the topicBuffer + publisher.publishEvent([]Event{testSnapshotEvent}) + + runStep(t, "start a subscription and unsub", func(t *testing.T) { + sub, err := publisher.Subscribe(req) + require.NoError(t, err) + defer sub.Unsubscribe() + + eventCh := runSubscription(ctx, sub) + + next := getNextEvents(t, eventCh) + expected := []Event{testSnapshotEvent} + require.Equal(t, expected, next) + + next = getNextEvents(t, eventCh) + require.Len(t, next, 1) + require.True(t, next[0].IsEndOfSnapshot()) + require.Equal(t, uint64(1), next[0].Index) + }) + + nextEvent := Event{ + Topic: testTopic, + Key: "sub-key", + Index: 3, + Payload: "event-3", + } + + runStep(t, "publish an event while unsubed", func(t *testing.T) { + publisher.publishEvent([]Event{nextEvent}) + }) + + publisher.snapshotHandlers[testTopic] = func(_ SubscribeRequest, _ SnapshotAppender) (uint64, error) { + return 0, fmt.Errorf("error should not be seen, cache should have been used") + } + + runStep(t, "resume the subscription", func(t *testing.T) { + newReq := *req + newReq.Index = 1 + sub, err := publisher.Subscribe(&newReq) + require.NoError(t, err) + + eventCh := runSubscription(ctx, sub) + next := getNextEvents(t, eventCh) + require.True(t, next[0].IsNewSnapshotToFollow(), next) + + next = getNextEvents(t, eventCh) + require.Equal(t, testSnapshotEvent, next[0]) + + next = getNextEvents(t, eventCh) + require.True(t, next[0].IsEndOfSnapshot()) + + next = getNextEvents(t, eventCh) + require.Equal(t, nextEvent, next[0]) + }) +} + +func runStep(t *testing.T, name string, fn func(t *testing.T)) { + if !t.Run(name, fn) { + t.FailNow() + } +}