fix(nodes): scope pending client delete supersession to target node

Keep pending deletes for old nodes when a client is recreated elsewhere. Preserve same-node attachment and generation safeguards and cover cross-node recreation in SQLite and PostgreSQL ordering regressions.
This commit is contained in:
Hamed Sanaei
2026-12-02 14:04:57 +03:30
parent 780de50b8b
commit 5459316fbd
2 changed files with 43 additions and 6 deletions
+38 -1
View File
@@ -579,7 +579,7 @@ func TestNodeWideDeleteSkipsReattachmentSentByQueuedReconcile(t *testing.T) {
}
func TestNodeWideDeleteRetriesAfterQueueExpiry(t *testing.T) {
for _, mode := range []string{"expired queue", "lost acknowledgment", "recreated client"} {
for _, mode := range []string{"expired queue", "lost acknowledgment", "recreated client", "recreated on another node"} {
t.Run(mode, func(t *testing.T) {
setupClientNodeOrderDB(t)
mgr := useTestRuntimeManager(t)
@@ -659,6 +659,31 @@ func TestNodeWideDeleteRetriesAfterQueueExpiry(t *testing.T) {
t.Fatal("fixture deleted the node client before delivery")
}
wantCalls := int32(1)
var otherNode *model.Node
var otherInbound *model.Inbound
var otherRuntime *fakeNodeRuntime
if mode == "recreated on another node" {
otherNode = &model.Node{Name: "other-delete-node", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true, Status: "online"}
if err := database.GetDB().Create(otherNode).Error; err != nil {
t.Fatal(err)
}
otherRuntime = &fakeNodeRuntime{}
mgr.SetRuntimeOverride(otherNode.Id, otherRuntime)
otherInbound = nodeInbound(t, otherNode.Id, 46020, nil)
client.ID = uuid.NewString()
if _, err := clientSvc.AddInboundClient(inboundSvc, &model.Inbound{
Id: otherInbound.Id, Protocol: otherInbound.Protocol, Settings: clientsSettings(t, []model.Client{client}),
}); err != nil {
t.Fatal(err)
}
if got := lookupClientRecord(t, email); got.Id == record.Id {
t.Fatal("test did not create a new client incarnation on the other node")
}
var pending int64
if err := database.GetDB().Model(&model.NodePendingClientDelete{}).Where("node_id = ? AND email = ?", node.Id, email).Count(&pending).Error; err != nil || pending != 1 {
t.Fatalf("recreation on another node must keep the original pending delete: %d (%v)", pending, err)
}
}
if mode == "recreated client" {
client.ID = uuid.NewString()
if _, err := clientSvc.AddInboundClient(inboundSvc, &model.Inbound{
@@ -688,6 +713,18 @@ func TestNodeWideDeleteRetriesAfterQueueExpiry(t *testing.T) {
if deletes.Load() != wantCalls || remoteRecord.Load() != wantPresent || remoteLink.Load() != wantPresent {
t.Fatalf("node after replay: RPCs=%d record=%v membership=%v", deletes.Load(), remoteRecord.Load(), remoteLink.Load())
}
if otherNode != nil {
current, err := inboundSvc.GetInbound(otherInbound.Id)
if err != nil || !strings.Contains(current.Settings, email) {
t.Fatalf("reconcile changed the other node's desired client: %v", err)
}
if otherRuntime.addClient.Load() != 1 || otherRuntime.deleteClient.Load() != 0 {
t.Fatalf("other node RPCs: adds=%d deletes=%d", otherRuntime.addClient.Load(), otherRuntime.deleteClient.Load())
}
if got := lookupClientRecord(t, email); got.Id == record.Id {
t.Fatal("reconcile did not preserve the new client record")
}
}
var pending int64
if err := database.GetDB().Model(&model.NodePendingClientDelete{}).Where("node_id = ?", node.Id).Count(&pending).Error; err != nil || pending != 0 {
t.Fatalf("pending deletes after reconciliation: %d (%v)", pending, err)
@@ -54,11 +54,10 @@ func sameNodeClientDelete(db *gorm.DB, row model.NodePendingClientDelete) *gorm.
func applyNodeClientDelete(ctx context.Context, rt runtime.Runtime, row model.NodePendingClientDelete) error {
db := database.GetDB().WithContext(ctx)
var target struct {
ClientId int
Desired bool
Desired bool
}
result := sameNodeClientDelete(db, row).
Select("COALESCE(clients.id, 0) AS client_id, EXISTS (" +
Select("EXISTS (" +
"SELECT 1 FROM client_inbounds JOIN inbounds ON inbounds.id = client_inbounds.inbound_id " +
"WHERE client_inbounds.client_id = clients.id AND inbounds.node_id = node_pending_client_deletes.node_id) AS desired").
Joins("LEFT JOIN clients ON clients.email = node_pending_client_deletes.email").
@@ -66,8 +65,9 @@ func applyNodeClientDelete(ctx context.Context, rt runtime.Runtime, row model.No
if result.Error != nil || result.RowsAffected == 0 {
return result.Error
}
superseded := target.Desired || (target.ClientId != 0 && target.ClientId != row.ClientId)
if !superseded {
// Only a desired attachment on this node supersedes its pending delete.
// Recreating the email elsewhere must not leave the old node record behind.
if !target.Desired {
if err := rt.DeleteClient(ctx, row.Email); err != nil {
return err
}