|
| 1 | +using EcencyApi.Infrastructure; |
| 2 | +using Xunit; |
| 3 | + |
| 4 | +namespace EcencyApi.Tests; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// The health tracker's per-call-class latency, driven directly with an |
| 8 | +/// injected clock: latency is the only thing that splits by class, everything |
| 9 | +/// that decides whether a node is answering at all stays node-wide. |
| 10 | +/// </summary> |
| 11 | +public class NodeCallClassTests |
| 12 | +{ |
| 13 | + private static (NodeHealthTracker Tracker, Action<long> Advance) Build(int nodes) |
| 14 | + { |
| 15 | + long now = 0; |
| 16 | + return (new NodeHealthTracker(nodes, () => now), ms => now += ms); |
| 17 | + } |
| 18 | + |
| 19 | + private static double? Ewma(NodeHealthTracker t, int node, CallClass cls) => |
| 20 | + t.Snapshot()[node].Latency.First(l => l.Class == cls).EwmaMs; |
| 21 | + |
| 22 | + private static int Samples(NodeHealthTracker t, int node, CallClass cls) => |
| 23 | + t.Snapshot()[node].Latency.First(l => l.Class == cls).Samples; |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void EachClassKeepsItsOwnLatencyProfile() |
| 27 | + { |
| 28 | + var (t, _) = Build(1); |
| 29 | + for (var i = 0; i < 3; i++) t.RecordSuccess(0, 100, CallClass.Cheap); |
| 30 | + for (var i = 0; i < 3; i++) t.RecordSuccess(0, 1500, CallClass.Heavy); |
| 31 | + |
| 32 | + Assert.Equal(100, Ewma(t, 0, CallClass.Cheap)); |
| 33 | + Assert.Equal(1500, Ewma(t, 0, CallClass.Heavy)); |
| 34 | + Assert.Equal(3, Samples(t, 0, CallClass.Cheap)); |
| 35 | + Assert.Equal(3, Samples(t, 0, CallClass.Heavy)); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void ANodeQuickOnPointReadsAndSlowOnFeedQueries_LeadsOnlyTheCheapOrdering() |
| 40 | + { |
| 41 | + // The whole point of the split: node 0 wins the cheap ranking on its own |
| 42 | + // measurements and must NOT carry that win into the heavy ranking, where |
| 43 | + // it is slower than a node nothing is known about. |
| 44 | + var (t, _) = Build(2); |
| 45 | + for (var i = 0; i < 3; i++) |
| 46 | + { |
| 47 | + t.RecordSuccess(0, 100, CallClass.Cheap); |
| 48 | + t.RecordSuccess(0, 1500, CallClass.Heavy); |
| 49 | + } |
| 50 | + |
| 51 | + Assert.Equal(new[] { 0, 1 }, t.OrderedNodeIndices(CallClass.Cheap)); |
| 52 | + Assert.Equal(new[] { 1, 0 }, t.OrderedNodeIndices(CallClass.Heavy)); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public void OneClassGoingStale_LeavesTheOtherProfileAlone() |
| 57 | + { |
| 58 | + // Staleness is per class. A class the traffic has moved away from |
| 59 | + // becoming unproven again is exploration, not a penalty: the node keeps |
| 60 | + // its other profile and all of its health. |
| 61 | + var (t, advance) = Build(2); |
| 62 | + advance(1_000); // a profile stamped at tick 0 reads as never stamped |
| 63 | + for (var i = 0; i < 3; i++) |
| 64 | + { |
| 65 | + t.RecordSuccess(0, 100, CallClass.Cheap); |
| 66 | + t.RecordSuccess(0, 1500, CallClass.Heavy); |
| 67 | + } |
| 68 | + |
| 69 | + advance(6 * 60_000); |
| 70 | + t.RecordSuccess(0, 120, CallClass.Cheap); |
| 71 | + |
| 72 | + Assert.Equal(1, Samples(t, 0, CallClass.Cheap)); // reset and re-learning |
| 73 | + Assert.Equal(120, Ewma(t, 0, CallClass.Cheap)); |
| 74 | + Assert.Equal(3, Samples(t, 0, CallClass.Heavy)); // untouched |
| 75 | + Assert.Equal(1500, Ewma(t, 0, CallClass.Heavy)); |
| 76 | + // ...but stale, so it no longer orders anything: node 0 scores the prior |
| 77 | + // for heavy, so config order breaks the tie with the untried node. |
| 78 | + Assert.Equal(new[] { 0, 1 }, t.OrderedNodeIndices(CallClass.Heavy)); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void ATimeoutIsALatencySample_ForTheClassThatTimedOut() |
| 83 | + { |
| 84 | + // Floored above the unproven prior so a node that never answers a heavy |
| 85 | + // query cannot outrank nodes never tried for one. The cheap profile learns |
| 86 | + // nothing from it, because nothing cheap was measured. |
| 87 | + var (t, _) = Build(2); |
| 88 | + for (var i = 0; i < 3; i++) t.RecordFailure(0, 300, CallClass.Heavy, timedOut: true); |
| 89 | + |
| 90 | + Assert.True(Ewma(t, 0, CallClass.Heavy) > 1000); |
| 91 | + Assert.Equal(3, Samples(t, 0, CallClass.Heavy)); |
| 92 | + Assert.Null(Ewma(t, 0, CallClass.Cheap)); |
| 93 | + Assert.Equal(0, Samples(t, 0, CallClass.Cheap)); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public void AFailureParkedNode_IsSkippedForEveryClass() |
| 98 | + { |
| 99 | + // "Not answering" is not a per-class property: a parked node is out of |
| 100 | + // both orderings while any other node can take the call. |
| 101 | + var (t, _) = Build(2); |
| 102 | + for (var i = 0; i < 3; i++) t.RecordFailure(0, 300, CallClass.Heavy, timedOut: true); |
| 103 | + |
| 104 | + Assert.Equal(new[] { 1 }, t.OrderedNodeIndices(CallClass.Heavy)); |
| 105 | + Assert.Equal(new[] { 1 }, t.OrderedNodeIndices(CallClass.Cheap)); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void ARateLimitedNode_SortsLastForEveryClass() |
| 110 | + { |
| 111 | + var (t, _) = Build(2); |
| 112 | + for (var i = 0; i < 3; i++) t.RecordSuccess(0, 10, CallClass.Cheap); |
| 113 | + for (var i = 0; i < 3; i++) t.RecordSuccess(0, 10, CallClass.Heavy); |
| 114 | + t.RecordRateLimited(0, 5_000); |
| 115 | + |
| 116 | + Assert.Equal(new[] { 1, 0 }, t.OrderedNodeIndices(CallClass.Cheap)); |
| 117 | + Assert.Equal(new[] { 1, 0 }, t.OrderedNodeIndices(CallClass.Heavy)); |
| 118 | + } |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public void ARecentFailureOnOneClass_DemotesTheNodeForBoth() |
| 122 | + { |
| 123 | + // Deliberate. It is also the narrow scope of the split: only the latency |
| 124 | + // score is per class. A node that just failed is a node that just failed, |
| 125 | + // whatever the call was, so it sorts behind clean nodes for everything. |
| 126 | + var (t, _) = Build(2); |
| 127 | + for (var i = 0; i < 3; i++) t.RecordSuccess(0, 10, CallClass.Cheap); |
| 128 | + t.RecordFailure(0, 50, CallClass.Heavy); |
| 129 | + |
| 130 | + Assert.Equal(new[] { 1, 0 }, t.OrderedNodeIndices(CallClass.Cheap)); |
| 131 | + Assert.Equal(new[] { 1, 0 }, t.OrderedNodeIndices(CallClass.Heavy)); |
| 132 | + } |
| 133 | + |
| 134 | + [Fact] |
| 135 | + public void ASuccessOnOneClass_ClearsNodeWideFailureState() |
| 136 | + { |
| 137 | + var (t, _) = Build(2); |
| 138 | + for (var i = 0; i < 3; i++) t.RecordFailure(0, 300, CallClass.Heavy, timedOut: true); |
| 139 | + Assert.Equal(new[] { 1 }, t.OrderedNodeIndices(CallClass.Cheap)); |
| 140 | + |
| 141 | + t.RecordSuccess(0, 20, CallClass.Cheap); |
| 142 | + |
| 143 | + Assert.Equal(2, t.OrderedNodeIndices(CallClass.Cheap).Count); |
| 144 | + Assert.Equal(0, t.Snapshot()[0].FailureParkedForMs); |
| 145 | + } |
| 146 | +} |
0 commit comments