On a soft reload, the new process starts with empty stick-tables and
serves immediately. As soon as its sessions to the remote peers are up,
peer_send_msgs() pushes its own values to them: it only checks the learn
state of the destination peer, never the resync state of the process
itself. The remote peers overwrite their entries with those values, then
count them and push them back, possibly after the old process's lesson
has arrived. Since updates carry absolute values and no ordering, the key
then continues from the value counted from scratch on every peer, and its
whole history is lost.
The same race is visible before the local lesson is even assigned: the
first push to a remote peer happens as soon as that session is up, which
is what makes the reset possible when both nodes write the same key.
Fix this by not pushing anything while the process has not finished its
resync (PEERS_RESYNC_FINISHED). Updates made meanwhile are not lost:
they are sent with their current value once the resync is over, which is
correct since a message carries a value and not an increment.
This is restricted to a process started from older ones, which is what
nb_oldpids accounts for: it is non-zero only when -sf or -st was used and
the signal could be delivered to the old processes. An instance started
on its own has no older process to learn from, and holding its pushes
back delays the replication of its entries by up to PEER_RESYNC_TIMEOUT
(5s): with no ancestor and two peers starting empty, neither can teach
the other, so the remote resync state is only left on timeout. This is
not theoretical: with the condition applied unconditionally, both
reg-tests/stickiness/{srvkey-addr,lb-services}.vtc fail. They start two
instances without ancestors, one side reports used:0, and the requests
routed there get a 504 or time out. Both pass with the nb_oldpids
restriction, which only affects processes that really are reloaded.
The condition was introduced with the peers code itself, so it should be
backported to all stable versions.
This fixes GitHub issue #3613. Thank you to @swenske for the detailed
report, the traces and the analysis of the race.
struct shared_table *st;
struct shared_table *last_local_table;
int updates = 0;
+ int resync_state;
last_local_table = peer->last_local_table;
if (!last_local_table)
peer->stop_local_table = last_local_table;
st = peer->stop_local_table->next;
+ resync_state = HA_ATOMIC_LOAD(&peers->flags) & PEERS_RESYNC_STATEMASK;
while (1) {
if (!st)
st = peer->tables;
repl = -1;
goto end;
}
- must_send = (peer->learnstate == PEER_LR_ST_NOTASSIGNED) && (st->last_pushed != st->table->localupdate);
+ /* A reloaded process must not push anything before
+ * it has learnt its tables, or the values it
+ * counted meanwhile overwrite the counters of the
+ * other peers, which then continue from them and
+ * lose their history. An instance started on its
+ * own (cold start, no old process to learn from)
+ * keeps pushing immediately.
+ */
+ must_send = (peer->learnstate == PEER_LR_ST_NOTASSIGNED) &&
+ (!nb_oldpids || resync_state == PEERS_RESYNC_FINISHED) &&
+ (st->last_pushed != st->table->localupdate);
HA_RWLOCK_RDUNLOCK(STK_TABLE_UPDT_LOCK, &st->table->updt_lock);
if (must_send) {