Przeglądaj źródła

fix(host): stop allocating global event IDs for subscriber gaps

Keep Event IDs strictly monotonic by attaching delivery_gap to the next real event and by using Hub ID allocation only for unpublished EventId 0.
root 3 miesięcy temu
rodzic
commit
6353d4bf5a

+ 9 - 3
internal/app/app.go

@@ -266,9 +266,15 @@ func (a *App) SubscribeEvents(ctx context.Context, id string, resume uint64, emi
 		return status.Error(codes.ResourceExhausted, err.Error())
 	}
 	defer sub.Cancel()
-	if gap != nil {
-		if err := emit(gap); err != nil {
-			return err
+	if gap != nil && gap.EventId != 0 {
+		return status.Error(codes.Internal, "event stream gap must not carry a global event_id")
+	}
+	if gap != nil && len(replay) > 0 && replay[0].GetDeliveryGap() == nil {
+		replay[0].DeliveryGap = &hthv1.DeliveryGap{
+			DroppedCount:                gap.DroppedCount,
+			LastContiguousEventId:       gap.LastContiguousEventId,
+			RequestedResumeAfterEventId: gap.ResumeAfterEventId,
+			OldestAvailableEventId:      0,
 		}
 	}
 	for _, e := range replay {

+ 3 - 13
internal/device/device.go

@@ -1372,19 +1372,9 @@ func (d *Device) SubscribeTelemetry(resume uint64, peer string) (*telemetry.Snap
 }
 
 func (d *Device) SubscribeEvents(resume uint64, peer string) (*telemetry.EventSub, []*hthv1.Event, *hthv1.Event, error) {
-	sub, replay, gap, err := d.hub.SubscribeEventsPeer(resume, peer)
-	if gap != nil && gap.EventId == 0 {
-		gap.EventId = d.store.NextEventID()
-		if gap.EventId == 0 {
-			gap.EventId = 1
-		}
-		now := d.clk.Now()
-		gap.WallTime = timestamppb.New(now)
-		gap.MonotonicNs = d.clk.MonotonicNS()
-		gap.DeviceId = d.id
-		gap.LastContiguousEventId = resume
-	}
-	return sub, replay, gap, err
+	// Gap descriptors must not consume a global event_id. SubscribeEventsPeer
+	// attaches delivery_gap onto the next real event (or first replay event).
+	return d.hub.SubscribeEventsPeer(resume, peer)
 }
 
 func (d *Device) AcquireLease(ctx context.Context, holder string, ttl time.Duration) (*hthv1.ControlLease, error) {

+ 3 - 0
internal/telemetry/telemetry.go

@@ -118,6 +118,9 @@ func (h *Hub) PublishEvent(ev *hthv1.Event) {
 func (h *Hub) publishEvent(ev *hthv1.Event, asSnap bool) {
 	h.mu.Lock()
 	defer h.mu.Unlock()
+	if ev.EventId == 0 {
+		ev.EventId = h.allocEventID()
+	}
 	h.ring = append(h.ring, ringItem{ev: ev})
 	if len(h.ring) > h.ringCap {
 		h.ring = h.ring[len(h.ring)-h.ringCap:]

+ 2 - 2
internal/telemetry/telemetry_test.go

@@ -237,9 +237,9 @@ func TestRepeatedGapsThenRecover(t *testing.T) {
 	for i := uint64(2); i <= 8; i++ {
 		h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
 	}
-	e := <-sub.C
+	<-sub.C
 	h.PublishEvent(&hthv1.Event{EventId: 9, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
-	e = <-sub.C
+	e := <-sub.C
 	if e.EventId <= 1 {
 		t.Fatalf("recovered id must be > 1, got %d", e.EventId)
 	}