Firestore transaction is taking 2 minutes consistently.
So my 1 cloud function is taking several minutes to execute. I just changed it all without transactions, and it is executed within seconds.
I am handling promises by reading first and then write correctly. But it seems like the issue is that Firestore only allows running transaction from the root and RTDB transaction starting from close to root is causing this even though I don't have many users. I feel pretty confident that this is Firebase's bug. In the mean time the only solution seems to be not to use transactions when you are doing a lot of things. I am running just some data manipulations like moving them around and rejigging them. I am using second generation of cloud functions.
What I am doing is creating a new chat room after a user adds a new member. I tried to use Transaction, but if I do that the function takes several minutes, so I had no choice but to not use Transaction.
Firestore codes:
await admin.firestore().runTransaction(async (t) => {
const document = await t.get(truthRef);
if (document.data() !== undefined) {
const firestorePromises: any = []
// change data of this first room to contain more data blah blah blah
firestorePromises.push(admin.firestore().collection("a").doc(b).set(c).then(() => {
const firestoreInnerPromises = [];
firestoreInnerPromises.push(admin.firestore().collection("a").doc(b).update({
messages: allMessagesHistory
}))
firestoreInnerPromises.push(oldTruthHistoryRef.delete());
return Promise.all(firestoreInnerPromises)
}));
return Promise.all(firestorePromises); // also tried to do await here same results
}
});
You're not performing the transaction correctly. In a transaction handler, all reads and writes for documents database absolutely must use the Transaction object t
you were provided in the callback. It's not going to work to call the set()
method on a document reference directly, nor is it going to work to call get(),
update()
, or delete()
without using the Transaction. This is necessary so that the transaction can keep track of which documents your transaction needs to work, so that it can be retried if there is contention on any of them.
I suggest reviewing the examples in the documentation to see how the Transaction object is to be used correctly. Note that the first example uses the transaction
variable to get and update data. If you need to build a path to a new document, you can certainly do that, but you must use the Transaction with it to perform the read or write.