fix(udps): stop packets being dated from an earlier time base
Reported as samples sporadically carrying a previous packet's timestamp: holes on one side of the stream and collisions on the other, in both the Go and the MARTe2 receiver. That it appeared in both is what located it -- the shared cause is upstream of either client. Four independent defects, all of which end in a packet's values being placed at a time that is not theirs. Reassembly slot exhaustion (the "Reassembly slots full; evicting oldest" flood). Chunk size was learnt only from fragment 0, so an out-of-order burst destroyed a packet whose bytes had all arrived and left the slot occupied until the 2 s GC. Slots were keyed on the counter alone, but DATA and CONFIG number independently, so equal counters merged the two streams. The 32-byte received-mask covered 256 of the 512 fragments the client accepts, so a duplicate above 255 was counted as new and the packet was delivered with a hole of stale bytes in it. And one datagram was read per Execute(), which cannot drain a fast producer. Fixed with a pendingTail deferral, (counter, type) keying, a 64-byte mask, a 256-datagram drain, counter-age slot reclamation, and a 1 Hz aggregated warning in place of the per-eviction flood. UDPStreamer dropping whole Accumulate batches. EventSem::ResetWait is Reset-then-Wait, so a Post() landing while the sender thread was inside ServiceClients()/SendData() was destroyed by the next Reset. The batch was then skipped with dataReady false, readyFill was never cleared, and the following flush overwrote it: an entire run of RT cycles never reached the wire. The record of pending work now lives in the buffers rather than in the semaphore edge, which also removes up to UDPS_DATA_WAIT_MS of latency; genuine backpressure overwrites are counted and reported. Against the unfixed code the new test sees 2999/3000 batches never consumed. Period inflation after loss. Accumulated scalars carry no SamplingRate, so the receiver derives dt from the sender-clock gap -- but dividing it by the previous packet's sample count is only right while nothing is lost. One loss doubles the reported period, which spreads a batch a full batch past its own end and into the range the next packet claims. That is the hole and the collision, exactly. Inferring the cycle count from the estimate's own period is not a way out: it has a stable fixed point wherever gap/dt is an integer, so a real rate change locks it at the old one for good (AccumDtGTest.FollowsSustainedRateChange). The packet counter removes the ambiguity, so all three receivers now order on it: a DATA packet that does not advance the counter is dropped rather than delivered, because its values are older than data already handed over. Ordering is on the signed difference so it survives the uint32 wrap, and the sequence resets on reconnect, where the producer's counter restarts independently of ours. The loss count that falls out of the same delta feeds the period estimate as cycles = prevN * (1 + lost), which reduces exactly to gap/prevN when nothing is lost and therefore still tracks a genuine rate change. UDPSClient::AcceptDataCounter (C++), udpsprotocol.SequenceGate (Go), decode_data (C). The C client's existing gap counter was wrap-unsafe and let a stale packet rewind last_counter, which made every subsequent gap wrong; it uses the same code now. Docs/Protocol.md gains an Ordering DATA section stating the requirement for any receiver, including ones outside this repository. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
13fac79400
commit
deabd257e5
@@ -482,11 +482,36 @@ static int decode_data(udps_client_t *c, const uint8_t *pl, size_t len,
|
||||
size_t total = 0u;
|
||||
size_t written = 0u;
|
||||
uint32_t i;
|
||||
uint32_t lost = 0u;
|
||||
udps_frame_t frame;
|
||||
|
||||
if (c->num_sigs == 0u) {
|
||||
return 0; /* DATA before CONFIG: nothing to decode against. */
|
||||
}
|
||||
|
||||
/* Order the sequence before spending anything on the payload.
|
||||
*
|
||||
* Reassembly completes in arrival order, not counter order, so a packet
|
||||
* delayed or duplicated on the wire surfaces after a newer one has already
|
||||
* been delivered. Its samples carry an older time base: they land on top
|
||||
* of data the consumer already has and leave the span they should have
|
||||
* filled empty. Nothing in the payload distinguishes such a packet from a
|
||||
* good one, only the counter does.
|
||||
*
|
||||
* The counter is a wrapping uint32, so it is ordered by the signed
|
||||
* difference; comparing the values directly would call the first packet
|
||||
* after the wrap stale and reject the stream from then on. */
|
||||
if (c->have_counter) {
|
||||
int32_t delta = (int32_t)(counter - c->last_counter);
|
||||
if (delta <= 0) {
|
||||
c->stats.stale_packets++;
|
||||
return 0;
|
||||
}
|
||||
lost = (uint32_t)delta - 1u;
|
||||
c->stats.counter_gaps += lost;
|
||||
}
|
||||
c->last_counter = counter;
|
||||
c->have_counter = 1;
|
||||
if (len < 8u) {
|
||||
return fail(c, "DATA payload too short (%lu bytes)", (unsigned long)len);
|
||||
}
|
||||
@@ -528,15 +553,11 @@ static int decode_data(udps_client_t *c, const uint8_t *pl, size_t len,
|
||||
written += count;
|
||||
}
|
||||
|
||||
if (c->have_counter && counter > c->last_counter + 1u) {
|
||||
c->stats.counter_gaps += counter - c->last_counter - 1u;
|
||||
}
|
||||
c->last_counter = counter;
|
||||
c->have_counter = 1;
|
||||
c->stats.frames_delivered++;
|
||||
|
||||
if (c->on_data != NULL) {
|
||||
frame.counter = counter;
|
||||
frame.lost = lost;
|
||||
frame.hrt = rd_u64(pl);
|
||||
frame.recv_time = recv_time;
|
||||
frame.publish_mode = c->publish_mode;
|
||||
|
||||
@@ -140,6 +140,16 @@ typedef struct {
|
||||
/** One fully decoded DATA packet. */
|
||||
typedef struct {
|
||||
uint32_t counter; /**< Packet counter; gaps mean lost datagrams. */
|
||||
/**
|
||||
* DATA packets missing immediately before this one, from the counter.
|
||||
*
|
||||
* Needed to space samples correctly: the elapsed time since the previous
|
||||
* frame covers the lost packets' cycles too, so dividing it by this
|
||||
* frame's sample count alone gives a period too long by exactly
|
||||
* @c lost + 1, which walks the samples past their own end and into the
|
||||
* range the next frame claims.
|
||||
*/
|
||||
uint32_t lost;
|
||||
uint64_t hrt; /**< Producer's high-resolution timer at send. */
|
||||
double recv_time; /**< Wall-clock seconds (CLOCK_REALTIME) at arrival. */
|
||||
uint8_t publish_mode; /**< UDPS_PUBLISH_*. */
|
||||
@@ -164,6 +174,12 @@ typedef struct {
|
||||
uint64_t config_updates;
|
||||
uint64_t fragments_dropped; /**< Duplicate, stale or unplaceable fragments. */
|
||||
uint64_t counter_gaps; /**< DATA packets missing from the sequence. */
|
||||
/**
|
||||
* DATA packets dropped for not advancing the counter: reordered or
|
||||
* duplicated on the wire. Delivering one would stamp its values with a
|
||||
* time base older than data already handed over.
|
||||
*/
|
||||
uint64_t stale_packets;
|
||||
uint64_t reconnects;
|
||||
} udps_stats_t;
|
||||
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
package udpsprotocol
|
||||
|
||||
// Accumulate mode ships one full snapshot of EVERY signal per RT cycle —
|
||||
// arrays included. See UDPStreamer.cpp pass 5 ("ALL signals (scalars and
|
||||
// arrays alike) are tagged accumulated = true") and SerializeAccumulated,
|
||||
// which writes, for each signal in CONFIG order, numSamples consecutive
|
||||
// snapshots of that signal's full element set.
|
||||
//
|
||||
// The tests below build a payload byte-for-byte the way the C++ producer
|
||||
// does, so a decoding regression shows up here rather than as a mangled
|
||||
// waveform three components downstream.
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// buildAccumulatePayload lays out an Accumulate DATA payload exactly as
|
||||
// UDPStreamer::SerializeAccumulated does:
|
||||
//
|
||||
// [8 HRT][4 numSamples] then, per signal, numSamples × NumElements float64.
|
||||
//
|
||||
// slots[i][k] holds signal i's element set for cycle k.
|
||||
func buildAccumulatePayload(hrt uint64, slots [][][]float64) []byte {
|
||||
numSamples := 0
|
||||
if len(slots) > 0 {
|
||||
numSamples = len(slots[0])
|
||||
}
|
||||
out := make([]byte, 12)
|
||||
binary.LittleEndian.PutUint64(out[0:8], hrt)
|
||||
binary.LittleEndian.PutUint32(out[8:12], uint32(numSamples))
|
||||
for _, sig := range slots {
|
||||
for _, elems := range sig {
|
||||
for _, v := range elems {
|
||||
var b [8]byte
|
||||
binary.LittleEndian.PutUint64(b[:], math.Float64bits(v))
|
||||
out = append(out, b[:]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// TestParseDataAccumulateGivesEachSlotItsOwnArray pins the array case: with an
|
||||
// accumulated batch, slot k's array signal must decode to the values the
|
||||
// producer captured on cycle k, not to some other cycle's. Handing every slot
|
||||
// slot 0's array would stamp one cycle's data with every slot's timestamp —
|
||||
// the same samples drawn repeatedly at advancing times, with the cycles they
|
||||
// displaced missing entirely.
|
||||
func TestParseDataAccumulateGivesEachSlotItsOwnArray(t *testing.T) {
|
||||
sigs := []SignalInfo{
|
||||
{Name: "Time", TypeCode: 9, NumRows: 1, NumCols: 1, QuantType: QuantNone},
|
||||
{Name: "Wave", TypeCode: 9, NumRows: 4, NumCols: 1, QuantType: QuantNone},
|
||||
}
|
||||
// Three RT cycles. "Wave" carries a different ramp each cycle so a
|
||||
// mix-up is unambiguous.
|
||||
timeSlots := [][]float64{{10}, {20}, {30}}
|
||||
waveSlots := [][]float64{
|
||||
{1, 2, 3, 4},
|
||||
{5, 6, 7, 8},
|
||||
{9, 10, 11, 12},
|
||||
}
|
||||
payload := buildAccumulatePayload(777, [][][]float64{timeSlots, waveSlots})
|
||||
|
||||
samples, err := ParseData(payload, sigs, PublishModeAccumulate, time.Now())
|
||||
if err != nil {
|
||||
t.Fatalf("ParseData: %v", err)
|
||||
}
|
||||
if len(samples) != 3 {
|
||||
t.Fatalf("expected 3 slots, got %d", len(samples))
|
||||
}
|
||||
for k, s := range samples {
|
||||
got := s.Values["Wave"]
|
||||
want := waveSlots[k]
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("slot %d: Wave has %d elements, want %d", k, len(got), len(want))
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("slot %d: Wave = %v, want %v (slot %d's data has been "+
|
||||
"served for this slot's timestamp)", k, got, want,
|
||||
indexOfSlot(waveSlots, got))
|
||||
}
|
||||
}
|
||||
if tv := s.Values["Time"]; len(tv) != 1 || tv[0] != timeSlots[k][0] {
|
||||
t.Fatalf("slot %d: Time = %v, want %v", k, tv, timeSlots[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseDataAccumulateConsumesTheWholeArrayBlock catches the same defect
|
||||
// from the other side: a signal following an array must be read at the right
|
||||
// offset. Under-reading the array block slides every later signal backwards
|
||||
// into the array's tail, which decodes as plausible-looking but wrong values
|
||||
// rather than as an error.
|
||||
func TestParseDataAccumulateConsumesTheWholeArrayBlock(t *testing.T) {
|
||||
sigs := []SignalInfo{
|
||||
{Name: "Wave", TypeCode: 9, NumRows: 4, NumCols: 1, QuantType: QuantNone},
|
||||
{Name: "Tail", TypeCode: 9, NumRows: 1, NumCols: 1, QuantType: QuantNone},
|
||||
}
|
||||
waveSlots := [][]float64{
|
||||
{1, 2, 3, 4},
|
||||
{5, 6, 7, 8},
|
||||
{9, 10, 11, 12},
|
||||
}
|
||||
tailSlots := [][]float64{{100}, {200}, {300}}
|
||||
payload := buildAccumulatePayload(0, [][][]float64{waveSlots, tailSlots})
|
||||
|
||||
samples, err := ParseData(payload, sigs, PublishModeAccumulate, time.Now())
|
||||
if err != nil {
|
||||
t.Fatalf("ParseData: %v", err)
|
||||
}
|
||||
if len(samples) != 3 {
|
||||
t.Fatalf("expected 3 slots, got %d", len(samples))
|
||||
}
|
||||
for k, s := range samples {
|
||||
tv := s.Values["Tail"]
|
||||
if len(tv) != 1 || tv[0] != tailSlots[k][0] {
|
||||
t.Fatalf("slot %d: Tail = %v, want %v — the array block before it "+
|
||||
"was not fully consumed", k, tv, tailSlots[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// indexOfSlot reports which slot's data a decoded array actually matches, so a
|
||||
// failure message can name the culprit instead of just showing numbers.
|
||||
func indexOfSlot(slots [][]float64, got []float64) int {
|
||||
for k, want := range slots {
|
||||
if len(want) != len(got) {
|
||||
continue
|
||||
}
|
||||
same := true
|
||||
for i := range want {
|
||||
if want[i] != got[i] {
|
||||
same = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if same {
|
||||
return k
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@@ -290,28 +290,37 @@ type DataSample struct {
|
||||
HRTTimestamp uint64
|
||||
WallTime time.Time // wall-clock time at UDP arrival; used as x-axis
|
||||
Values map[string][]float64 // key = signal name, value = []float64 with NumElements entries
|
||||
// Lost is the number of DATA packets missing between the previous sample
|
||||
// and this one, taken from the producer's packet counter (see
|
||||
// SequenceGate). Consumers that derive a per-element period from the
|
||||
// inter-packet gap need it: the gap widens with every lost packet, and
|
||||
// dividing it by this packet's element count alone reports a period too
|
||||
// long by exactly that factor — which walks the packet's elements past
|
||||
// their own end and into the range the next packet claims.
|
||||
Lost uint32
|
||||
}
|
||||
|
||||
// parseElems reads n elements for sig from payload at offset, advancing offset.
|
||||
// Returns the slice of float64 values and the new offset.
|
||||
func parseElems(payload []byte, offset, n int, sig SignalInfo) ([]float64, int, error) {
|
||||
sz := rawTypeSize(sig.TypeCode)
|
||||
if sig.QuantType != QuantNone {
|
||||
sz = quantSize(sig.QuantType)
|
||||
}
|
||||
// Bounds-check before allocating. In Accumulate mode n is numSamples ×
|
||||
// NumElements, so a malformed packet could otherwise ask for an allocation
|
||||
// far larger than its own payload could ever justify.
|
||||
if n < 0 || n > (len(payload)-offset)/sz {
|
||||
return nil, offset, fmt.Errorf("data payload truncated for signal %q", sig.Name)
|
||||
}
|
||||
elems := make([]float64, n)
|
||||
needed := n * sz
|
||||
if sig.QuantType == QuantNone {
|
||||
sz := rawTypeSize(sig.TypeCode)
|
||||
needed := n * sz
|
||||
if offset+needed > len(payload) {
|
||||
return nil, offset, fmt.Errorf("data payload truncated for signal %q", sig.Name)
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
elems[i] = readRawElement(payload, offset+i*sz, sig.TypeCode)
|
||||
}
|
||||
offset += needed
|
||||
} else {
|
||||
sz := quantSize(sig.QuantType)
|
||||
needed := n * sz
|
||||
if offset+needed > len(payload) {
|
||||
return nil, offset, fmt.Errorf("data payload truncated (quant) for signal %q", sig.Name)
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
var raw uint16
|
||||
if sz == 1 {
|
||||
@@ -331,7 +340,13 @@ func parseElems(payload []byte, offset, n int, sig SignalInfo) ([]float64, int,
|
||||
//
|
||||
// For PublishModeAccumulate the payload format is:
|
||||
//
|
||||
// [8 HRT][4 numSamples][for each signal: accumulated scalars → numSamples elems; arrays → NumElements elems]
|
||||
// [8 HRT][4 numSamples][for each signal: numSamples × NumElements elems]
|
||||
//
|
||||
// Every signal is accumulated, arrays included: the producer captures one full
|
||||
// snapshot of the whole signal set per RT cycle and lays the cycles out
|
||||
// contiguously per signal (UDPStreamer::SerializeAccumulated). Reading only
|
||||
// NumElements for an array would hand every slot the first cycle's data and
|
||||
// slide all later signals into that array's tail.
|
||||
//
|
||||
// The function returns one DataSample per accumulated snapshot so the hub can
|
||||
// process each slot independently with its own timestamp.
|
||||
@@ -357,28 +372,18 @@ func ParseData(payload []byte, sigs []SignalInfo, publishMode uint8, arrivalTime
|
||||
}
|
||||
|
||||
// Parse per-signal data blocks (all slots for a signal are contiguous).
|
||||
accumVals := make(map[string][]float64, len(sigs)) // scalars: numSamples values
|
||||
fixedVals := make(map[string][]float64, len(sigs)) // arrays: NumElements values
|
||||
accumVals := make(map[string][]float64, len(sigs)) // numSamples × NumElements
|
||||
accumElems := make(map[string]int, len(sigs))
|
||||
|
||||
for _, sig := range sigs {
|
||||
n := sig.NumElements()
|
||||
if n == 1 {
|
||||
// Accumulated scalar: read numSamples back-to-back elements.
|
||||
elems, newOff, err := parseElems(payload, offset, numSamples, sig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
offset = newOff
|
||||
accumVals[sig.Name] = elems
|
||||
} else {
|
||||
// Fixed array (non-accumulated): one set of NumElements values.
|
||||
elems, newOff, err := parseElems(payload, offset, n, sig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
offset = newOff
|
||||
fixedVals[sig.Name] = elems
|
||||
elems, newOff, err := parseElems(payload, offset, numSamples*n, sig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
offset = newOff
|
||||
accumVals[sig.Name] = elems
|
||||
accumElems[sig.Name] = n
|
||||
}
|
||||
|
||||
// Build one DataSample per slot.
|
||||
@@ -386,10 +391,10 @@ func ParseData(payload []byte, sigs []SignalInfo, publishMode uint8, arrivalTime
|
||||
for k := 0; k < numSamples; k++ {
|
||||
vals := make(map[string][]float64, len(sigs))
|
||||
for sigName, av := range accumVals {
|
||||
vals[sigName] = []float64{av[k]}
|
||||
}
|
||||
for sigName, fv := range fixedVals {
|
||||
vals[sigName] = fv // shared read-only reference; hub does not modify
|
||||
n := accumElems[sigName]
|
||||
// Sub-slice of the decoded block; the hub treats values as
|
||||
// read-only, so no copy is needed.
|
||||
vals[sigName] = av[k*n : (k+1)*n : (k+1)*n]
|
||||
}
|
||||
samples[k] = DataSample{HRTTimestamp: hrt, WallTime: arrivalTime, Values: vals}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package udpsprotocol
|
||||
|
||||
// SequenceGate orders DATA packets by the producer's packet counter.
|
||||
//
|
||||
// Reassembly completes in arrival order, not counter order, so a packet that
|
||||
// was delayed or duplicated on the wire is handed up after a newer one has
|
||||
// already been consumed. Its samples then carry an older time base than the
|
||||
// data already in the ring: they land on top of samples that are already
|
||||
// there, and the span they should have filled stays empty. That is a hole on
|
||||
// one side and a collision on the other, from a packet that is entirely
|
||||
// well-formed — the counter is the only thing that distinguishes it.
|
||||
//
|
||||
// A SequenceGate is not safe for concurrent use; each receive loop owns one.
|
||||
type SequenceGate struct {
|
||||
last uint32
|
||||
valid bool
|
||||
// Stale counts packets rejected for not advancing the counter (reordered
|
||||
// or duplicated), for diagnostics.
|
||||
Stale uint64
|
||||
}
|
||||
|
||||
// Reset forgets the sequence. Call it on (re)connect: the producer's counter
|
||||
// restarts independently of ours, so a counter carried over from the previous
|
||||
// connection would reject the whole new stream.
|
||||
func (g *SequenceGate) Reset() {
|
||||
g.last = 0
|
||||
g.valid = false
|
||||
}
|
||||
|
||||
// Accept reports whether a DATA packet with this counter should be delivered,
|
||||
// and how many packets went missing immediately before it.
|
||||
//
|
||||
// The counter is a wrapping uint32, so ordering is done on the signed
|
||||
// difference: a plain comparison would call the first packet after the wrap
|
||||
// stale and reject everything from then on.
|
||||
func (g *SequenceGate) Accept(counter uint32) (ok bool, lost uint32) {
|
||||
if !g.valid {
|
||||
g.valid = true
|
||||
g.last = counter
|
||||
return true, 0
|
||||
}
|
||||
delta := int32(counter - g.last)
|
||||
if delta <= 0 {
|
||||
g.Stale++
|
||||
return false, 0
|
||||
}
|
||||
g.last = counter
|
||||
return true, uint32(delta) - 1
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package udpsprotocol
|
||||
|
||||
import "testing"
|
||||
|
||||
// A packet older than one already delivered carries an older time base. Its
|
||||
// samples land on top of data that is already in the ring and leave the span
|
||||
// they should have filled empty, so it must not get through.
|
||||
func TestSequenceGateRejectsStaleAndDuplicate(t *testing.T) {
|
||||
var g SequenceGate
|
||||
|
||||
if ok, lost := g.Accept(10); !ok || lost != 0 {
|
||||
t.Fatalf("first packet: got (%v, %d), want (true, 0)", ok, lost)
|
||||
}
|
||||
if ok, _ := g.Accept(11); !ok {
|
||||
t.Fatal("counter 11 advances past 10 and must be accepted")
|
||||
}
|
||||
if ok, _ := g.Accept(9); ok {
|
||||
t.Error("counter 9 is older than the delivered 11 and must be dropped")
|
||||
}
|
||||
if ok, _ := g.Accept(11); ok {
|
||||
t.Error("a repeat of the delivered counter must be dropped")
|
||||
}
|
||||
if g.Stale != 2 {
|
||||
t.Errorf("Stale = %d, want 2", g.Stale)
|
||||
}
|
||||
// The rejections must not have moved the sequence on.
|
||||
if ok, lost := g.Accept(12); !ok || lost != 0 {
|
||||
t.Errorf("after rejections: got (%v, %d), want (true, 0)", ok, lost)
|
||||
}
|
||||
}
|
||||
|
||||
// The loss count is what lets a consumer tell a widened gap from a slowed
|
||||
// producer, so it must exclude the packet being delivered and must not persist
|
||||
// into the next one.
|
||||
func TestSequenceGateReportsLoss(t *testing.T) {
|
||||
var g SequenceGate
|
||||
|
||||
g.Accept(100)
|
||||
if _, lost := g.Accept(104); lost != 3 {
|
||||
t.Errorf("101..103 missing: lost = %d, want 3", lost)
|
||||
}
|
||||
if _, lost := g.Accept(105); lost != 0 {
|
||||
t.Errorf("consecutive packet: lost = %d, want 0", lost)
|
||||
}
|
||||
if g.Stale != 0 {
|
||||
t.Errorf("Stale = %d, want 0", g.Stale)
|
||||
}
|
||||
}
|
||||
|
||||
// The counter is a wrapping uint32. Ordering it by plain comparison would call
|
||||
// every packet after the wrap older than 0xFFFFFFFF and kill the stream.
|
||||
func TestSequenceGateSurvivesWraparound(t *testing.T) {
|
||||
var g SequenceGate
|
||||
|
||||
for _, c := range []uint32{0xFFFFFFFD, 0xFFFFFFFE, 0xFFFFFFFF, 0, 1, 2} {
|
||||
ok, lost := g.Accept(c)
|
||||
if !ok {
|
||||
t.Fatalf("counter %#x rejected across the wrap", c)
|
||||
}
|
||||
if lost != 0 {
|
||||
t.Errorf("counter %#x: lost = %d, want 0", c, lost)
|
||||
}
|
||||
}
|
||||
// Loss must still be measured correctly across the wrap.
|
||||
var h SequenceGate
|
||||
h.Accept(0xFFFFFFFE)
|
||||
if _, lost := h.Accept(1); lost != 2 {
|
||||
t.Errorf("0xFFFFFFFF and 0 missing: lost = %d, want 2", lost)
|
||||
}
|
||||
}
|
||||
|
||||
// A reconnect restarts the producer's counter independently of ours; a carried
|
||||
// over counter would reject the entire new stream.
|
||||
func TestSequenceGateResetAcceptsLowerCounter(t *testing.T) {
|
||||
var g SequenceGate
|
||||
|
||||
g.Accept(5000)
|
||||
g.Reset()
|
||||
if ok, lost := g.Accept(3); !ok || lost != 0 {
|
||||
t.Errorf("after Reset: got (%v, %d), want (true, 0)", ok, lost)
|
||||
}
|
||||
}
|
||||
@@ -1216,15 +1216,24 @@ func (h *Hub) buildBinaryDataMessageForSource(src *sourceHubState, batch []udpsp
|
||||
wallNs := s.WallTime.UnixNano()
|
||||
wallSec := float64(wallNs) / 1e9
|
||||
var dtSec float64
|
||||
// A gap spans the elements of every packet that went missing
|
||||
// inside it as well as this packet's own, so the divisor has
|
||||
// to widen with it. Without this a single loss halves the
|
||||
// apparent rate and the elements overrun into the next
|
||||
// packet's range. The loss count belongs to the packet the
|
||||
// gap ends at.
|
||||
if bi+1 < len(batch) {
|
||||
// Two consecutive packets in this tick → exact dt.
|
||||
dtSec = (float64(batch[bi+1].WallTime.UnixNano()) - float64(wallNs)) / 1e9 / float64(n)
|
||||
span := float64(n) * float64(1+batch[bi+1].Lost)
|
||||
dtSec = (float64(batch[bi+1].WallTime.UnixNano()) - float64(wallNs)) / 1e9 / span
|
||||
} else if bi > 0 {
|
||||
// Last of multiple packets → use diff from previous.
|
||||
dtSec = (float64(wallNs) - float64(batch[bi-1].WallTime.UnixNano())) / 1e9 / float64(n)
|
||||
span := float64(n) * float64(1+s.Lost)
|
||||
dtSec = (float64(wallNs) - float64(batch[bi-1].WallTime.UnixNano())) / 1e9 / span
|
||||
} else if prevNs, ok2 := src.lastPktNs[sig.Name]; ok2 && prevNs > 0 && wallNs > prevNs {
|
||||
// Single packet this tick → gap from the previous tick's packet.
|
||||
dtSec = (float64(wallNs) - float64(prevNs)) / 1e9 / float64(n)
|
||||
span := float64(n) * float64(1+s.Lost)
|
||||
dtSec = (float64(wallNs) - float64(prevNs)) / 1e9 / span
|
||||
} else {
|
||||
// Truly first packet ever — inter-packet timing unknown.
|
||||
// Skip to avoid poisoning the ring with wrongly-spaced timestamps;
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
package wshub
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"marte2/common/udpsprotocol"
|
||||
)
|
||||
|
||||
// pktSignal is a 4-element array with no time signal and no declared sampling
|
||||
// rate, i.e. the TimeModePacket path where dt has to be inferred from the gap
|
||||
// between packets.
|
||||
func pktSignal(name string) udpsprotocol.SignalInfo {
|
||||
return udpsprotocol.SignalInfo{
|
||||
Name: name,
|
||||
TypeCode: 8, // float64
|
||||
NumDimensions: 1,
|
||||
NumRows: 4,
|
||||
NumCols: 1,
|
||||
TimeMode: udpsprotocol.TimeModePacket,
|
||||
TimeSignalIdx: udpsprotocol.NoTimeSignal,
|
||||
}
|
||||
}
|
||||
|
||||
// newPacketDtHub builds a Hub with a ring for one packet-timed array signal and
|
||||
// returns both. It does not start Run(): buildBinaryDataMessageForSource is
|
||||
// called directly so the timestamps it produces can be read back verbatim.
|
||||
func newPacketDtHub(t *testing.T, sigName string) (*Hub, *sourceHubState, *sigRing) {
|
||||
t.Helper()
|
||||
h := NewHub()
|
||||
src := &sourceHubState{
|
||||
id: "s1",
|
||||
signals: []udpsprotocol.SignalInfo{pktSignal(sigName)},
|
||||
timeSigCalib: map[string]float64{},
|
||||
lastPktNs: map[string]int64{},
|
||||
lastFrameMeasured: map[string]float64{},
|
||||
lastFrameEndT: map[string]float64{},
|
||||
gapEMA: map[string]float64{},
|
||||
}
|
||||
rb := newSigRing(4096)
|
||||
h.rings["s1:"+sigName] = rb
|
||||
return h, src, rb
|
||||
}
|
||||
|
||||
// packet builds a one-signal batch entry arriving at t0 with the given loss
|
||||
// count; the values are irrelevant, only the timestamps are under test.
|
||||
func packet(sigName string, at time.Time, lost uint32, n int) udpsprotocol.DataSample {
|
||||
vals := make([]float64, n)
|
||||
return udpsprotocol.DataSample{WallTime: at, Values: map[string][]float64{sigName: vals}, Lost: lost}
|
||||
}
|
||||
|
||||
// ringTimes returns the timestamps written to the ring, in order.
|
||||
func ringTimes(rb *sigRing) []float64 {
|
||||
rb.mu.RLock()
|
||||
defer rb.mu.RUnlock()
|
||||
out := make([]float64, 0, rb.size)
|
||||
start := (rb.head - rb.size + rb.cap) % rb.cap
|
||||
for i := 0; i < rb.size; i++ {
|
||||
out = append(out, rb.t[(start+i)%rb.cap])
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// A lost packet widens the inter-packet gap without adding elements to the
|
||||
// packet that follows it. Dividing the gap by that packet's element count
|
||||
// alone reports a period too long by exactly the number of packets missing,
|
||||
// which walks the elements past their own end and into the range the next
|
||||
// packet claims: they collide there, and the span they vacated stays empty.
|
||||
func TestPacketDtIgnoresLostPacketWidening(t *testing.T) {
|
||||
const sig = "Wave"
|
||||
const n = 4
|
||||
const dt = 1 * time.Millisecond
|
||||
base := time.Unix(1700000000, 0)
|
||||
|
||||
h, src, rb := newPacketDtHub(t, sig)
|
||||
|
||||
// One clean packet establishes lastPktNs.
|
||||
h.buildBinaryDataMessageForSource(src, []udpsprotocol.DataSample{
|
||||
packet(sig, base, 0, n)})
|
||||
|
||||
// The next producer packet is lost, so the one after it arrives a full
|
||||
// extra batch later and reports Lost=1.
|
||||
arrival := base.Add(2 * n * dt)
|
||||
h.buildBinaryDataMessageForSource(src, []udpsprotocol.DataSample{
|
||||
packet(sig, arrival, 1, n)})
|
||||
|
||||
ts := ringTimes(rb)
|
||||
if len(ts) != n {
|
||||
t.Fatalf("ring holds %d points, want %d (the first packet is skipped: no gap yet)", len(ts), n)
|
||||
}
|
||||
got := ts[1] - ts[0]
|
||||
if !nearSec(got, dt.Seconds()) {
|
||||
t.Errorf("dt = %v s, want %v s (the gap spans two batches, not one)", got, dt.Seconds())
|
||||
}
|
||||
// Elements run forward from the packet's own arrival, so a doubled dt
|
||||
// would stretch this batch across two batch periods and into the range the
|
||||
// next packet claims.
|
||||
span := ts[len(ts)-1] - ts[0]
|
||||
if !nearSec(span, float64(n-1)*dt.Seconds()) {
|
||||
t.Errorf("batch spans %v s, want %v s: it overruns into the next packet's range",
|
||||
span, float64(n-1)*dt.Seconds())
|
||||
}
|
||||
}
|
||||
|
||||
// nearSec compares two intervals in seconds. The hub carries timestamps as
|
||||
// float64 seconds derived from UnixNano, whose spacing near the current epoch
|
||||
// is a couple of hundred nanoseconds, so exact equality is not available. The
|
||||
// defect under test moves the period by a factor of two, three orders of
|
||||
// magnitude outside this tolerance.
|
||||
func nearSec(got, want float64) bool { return math.Abs(got-want) <= 1e-6 }
|
||||
|
||||
// The correction must be driven by the reported loss and nothing else: with no
|
||||
// packet missing the period still comes straight from the gap, so a producer
|
||||
// that genuinely slows down is followed rather than second-guessed.
|
||||
func TestPacketDtFollowsGapWhenNothingIsLost(t *testing.T) {
|
||||
const sig = "Wave"
|
||||
const n = 4
|
||||
base := time.Unix(1700000000, 0)
|
||||
|
||||
h, src, rb := newPacketDtHub(t, sig)
|
||||
|
||||
h.buildBinaryDataMessageForSource(src, []udpsprotocol.DataSample{
|
||||
packet(sig, base, 0, n)})
|
||||
|
||||
// Same widened gap as the test above, but reported as no loss: the
|
||||
// producer really is running at half the rate.
|
||||
slowDt := 2 * time.Millisecond
|
||||
h.buildBinaryDataMessageForSource(src, []udpsprotocol.DataSample{
|
||||
packet(sig, base.Add(n*slowDt), 0, n)})
|
||||
|
||||
ts := ringTimes(rb)
|
||||
if len(ts) != n {
|
||||
t.Fatalf("ring holds %d points, want %d", len(ts), n)
|
||||
}
|
||||
if got := ts[1] - ts[0]; !nearSec(got, slowDt.Seconds()) {
|
||||
t.Errorf("dt = %v s, want %v s: a real rate change must be followed", got, slowDt.Seconds())
|
||||
}
|
||||
}
|
||||
@@ -341,6 +341,9 @@ func (u *UDPClient) runSession() error {
|
||||
}
|
||||
|
||||
reassembler := udpsprotocol.NewReassembler(2 * time.Second)
|
||||
// Per-session: the producer's counter restarts independently of ours, so
|
||||
// the gate must not carry a counter over from the previous connection.
|
||||
var gate udpsprotocol.SequenceGate
|
||||
buf := make([]byte, readBufSize)
|
||||
var currentSigs []udpsprotocol.SignalInfo
|
||||
var currentPublishMode uint8
|
||||
@@ -414,11 +417,20 @@ func (u *UDPClient) runSession() error {
|
||||
if len(currentSigs) == 0 {
|
||||
continue
|
||||
}
|
||||
fresh, lost := gate.Accept(hdr.Counter)
|
||||
if !fresh {
|
||||
continue
|
||||
}
|
||||
samples, err := udpsprotocol.ParseData(complete, currentSigs, currentPublishMode, arrivalTime)
|
||||
if err != nil {
|
||||
log.Printf("[%s] udp: parse data: %v", u.sourceID, err)
|
||||
continue
|
||||
}
|
||||
// The gap precedes the packet, so it belongs to its first slot only;
|
||||
// the slots after it are consecutive cycles of the same batch.
|
||||
if len(samples) > 0 {
|
||||
samples[0].Lost = lost
|
||||
}
|
||||
for _, s := range samples {
|
||||
u.hub.PushDataForSource(u.sourceID, s)
|
||||
}
|
||||
@@ -589,6 +601,9 @@ func (u *UDPClient) runMulticastSession() error {
|
||||
}()
|
||||
|
||||
reassembler := udpsprotocol.NewReassembler(2 * time.Second)
|
||||
// Per-session, as in runSession(): a counter from the previous connection
|
||||
// would reject the whole new stream.
|
||||
var gate udpsprotocol.SequenceGate
|
||||
buf := make([]byte, readBufSize)
|
||||
|
||||
for {
|
||||
@@ -629,11 +644,18 @@ func (u *UDPClient) runMulticastSession() error {
|
||||
if len(currentSigs) == 0 {
|
||||
continue
|
||||
}
|
||||
fresh, lost := gate.Accept(hdr.Counter)
|
||||
if !fresh {
|
||||
continue
|
||||
}
|
||||
samples, parseErr := udpsprotocol.ParseData(complete, currentSigs, currentPublishMode, arrivalTime)
|
||||
if parseErr != nil {
|
||||
log.Printf("[%s] multicast: parse data: %v", u.sourceID, parseErr)
|
||||
continue
|
||||
}
|
||||
if len(samples) > 0 {
|
||||
samples[0].Lost = lost
|
||||
}
|
||||
for _, s := range samples {
|
||||
u.hub.PushDataForSource(u.sourceID, s)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user