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
f334995865
commit
fbae7d712c
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user