From 2df9531ca2ec10bab4ebb3c67850216b14e337ef Mon Sep 17 00:00:00 2001 From: Tim Ross Date: Thu, 10 Aug 2023 10:57:38 -0400 Subject: [PATCH] Fix resources being deleted from Firestore on update The change to use `Update` in #28473 caused a 0 value to be written to the expires field of the document. When using `Create` or `Set` a zero expiry value would result in an empty expires field for that document. Now that the expires column was containing a 0 value the purgeExpiredDocuments loop was detecting the document as needing to be removed. To prevent removing resources that have no expiry the purge loop now explicitly ignores any resources that have a 0 expiry value. Fixes #30236 --- lib/backend/firestore/firestorebk.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/backend/firestore/firestorebk.go b/lib/backend/firestore/firestorebk.go index 84777f40d016d..b4ab8da36dd71 100644 --- a/lib/backend/firestore/firestorebk.go +++ b/lib/backend/firestore/firestorebk.go @@ -764,12 +764,23 @@ func (b *Backend) purgeExpiredDocuments() error { return b.clientContext.Err() case <-t.C: expiryTime := b.clock.Now().UTC().Unix() - docs, err := b.svc.Collection(b.CollectionName).Where(expiresDocProperty, "<=", expiryTime).Documents(b.clientContext).GetAll() + // Find all documents that have expired, but EXCLUDE + // any documents that do not have an expiry as indicated + // by a value of 0. + docs, err := b.svc.Collection(b.CollectionName). + Where(expiresDocProperty, "<=", expiryTime). + Where(expiresDocProperty, ">", 0). + Documents(b.clientContext). + GetAll() if err != nil { b.Logger.WithError(trail.FromGRPC(err)).Warn("Failed to get expired documents") continue } + if len(docs) == 0 { + continue + } + if err := b.deleteDocuments(docs); err != nil { return trace.Wrap(err) }