Commit 57ec06c
committed
fix(keys): avoid duplicate metrics from KeyIndex desync
KeyIndex could emit the same key more than once, surfacing as duplicate
metrics (apache/apisix#11934). Two issues combine to cause this:
1. KeyIndex:list() iterated the stored key values (self.keys), which can
transiently hold the same key value at two different slots when an
expired metric is re-added at a new slot before the old slot is
reclaimed. The duplicated value was then emitted twice. list() now
walks the slots but only emits a key at the slot the index points at
(self.index[key] == idx), so each key is listed exactly once while
preserving ordering.
2. When add() re-adds a key whose shared-dict entry had already expired,
it cleared the local index/keys but did not bump delete_count or clear
the dict slot. Other workers therefore never did a full sync and kept
the stale slot in their local self.keys, desynchronizing the index.
add() now mirrors remove(): it clears the dict slot, increments the
local deleted counter and bumps delete_count.
Adds a regression test (TestKeyIndex:testExpiredReAddNoDuplicate) that
reproduces the duplicate emission before the fix and passes after.1 parent f86aa3e commit 57ec06c
2 files changed
Lines changed: 57 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
95 | | - | |
96 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
97 | 106 | | |
98 | 107 | | |
99 | 108 | | |
| |||
125 | 134 | | |
126 | 135 | | |
127 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
128 | 144 | | |
129 | 145 | | |
130 | 146 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
804 | 804 | | |
805 | 805 | | |
806 | 806 | | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
807 | 845 | | |
808 | 846 | | |
809 | 847 | | |
| |||
0 commit comments