Error: Deadline exceeded at Http2CallStream.call.on
Related issues
[REQUIRED] Version info
node: 8
firebase-functions: ^3.2.0
firebase-tools: 7.0.2
firebase-admin: ^8.5.0
[REQUIRED] Test case
We are working in a project with google cloud functions for Firebase. We have the blaze plan but we are facing some execution problems with background functions when it comes to deal with read/write and iterate a good deal of documents (+200) from/to Firebase.
The functions just throw some errors after some time of execution or do not complete the task at all.
I have tried to scale up the RAM assigned to the function (2 GB) and the wait time (540 seconds) but it didn´t seem to work out. The function is programmed to run on an onUpdate event.
[REQUIRED] Steps to reproduce
Read a large collections and iterate over the snapshot. Then perform set and updates on firestore based on the retrieved data.
The code:
Index.js:
exports.createRDTCode = functions.runWith({ timeoutSeconds: 540,
memory: '2GB'}).firestore.document("xxxxx/{project}").onUpdate((change, context, callback) => {
return createRDTCode.handler(change,context,callback)
})
createRDTCode.js:
const admin = require("firebase-admin");
const firestore = admin.firestore();
exports.handler = async (change, context) => {
const newValue = change.after.data();
const previousValue = change.before.data();
let barsConsecutivo = newValue.barras;
if (
newValue.writeCode != previousValue.writeCode &&
newValue.writeCode == true
) {
const drillingRDTRef = admin
.firestore()
.collection("xxxxxxx")
.doc(context.params.project);
drillingRDTRef.update({
writeCode: false
});
const project = context.params.project.split("_", 2)[0];
const company = context.params.project.split("_", 2)[1];
const projectRef = admin
.firestore()
.collection("xxxxxxx")
.doc(context.params.project);
const barsRef = admin
.firestore()
.collection("xxxxx")
.doc(context.params.project)
.collection("xxxxx")
.where("idRDT", "==", null);
barsRef.get().then(snapshot => {
snapshot.forEach(rdtDoc => {
let barCode = (
company.substr(0, 2) +
project.substr(0, 2) +
"BA" +
barsConsecutivo.toString()
).toUpperCase();
admin
.firestore()
.collection("xxxxxx")
.doc(context.params.project)
.collection("xxxxx")
.doc(rdtDoc.id)
.update({
idRDT: barCode
});
projectRef.update({
drillingRDT: admin.firestore.FieldValue.arrayUnion(barCode),
inventory: admin.firestore.FieldValue.arrayUnion(barCode)
});
admin
.firestore()
.collection("xxxxxxx")
.doc(context.params.project)
.collection(rdtDoc.data().typeInventory.toLowerCase())
.doc(barCode)
.set({
typeRDT: rdtDoc.data().tipoRDT,
codeRDT: barCode,
statusRDT: rdtDoc.data().statusRDT,
rigArray: rdtDoc.data().rigArray,
typeInventory: rdtDoc.data().typeInventory.toLowerCase(),
numberHoles: rdtDoc.data().numberHoles,
lengthHole: rdtDoc.data().lengthHole
});
barsConsecutivo += 1;
});
const drillingRDTRef = admin
.firestore()
.collection("xxxxxxx")
.doc(context.params.project);
drillingRDTRef.update({
barras: barsConsecutivo
});
});
}
};
[REQUIRED] Expected behavior
Do not get deadline error and perform the update and write tasks.
[REQUIRED] Actual behavior
Error: Deadline exceeded at Http2CallStream.call.on (/srv/node_modules/@grpc/grpc-js/build/src/client.js:96:45) at emitOne (events.js:121:20) at Http2CallStream.emit (events.js:211:7) at process.nextTick (/srv/node_modules/@grpc/grpc-js/build/src/call-stream.js:71:22) at _combinedTickCallback (internal/process/next_tick.js:132:7) at process._tickDomainCallback (internal/process/next_tick.js:219:9)
Were you able to successfully deploy your functions?
Yes
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
A slightly shorter repro - JSON upload to Firestore.
const firestoreImport = require('node-firestore-import-export').firestoreImport;
const firebase = require('firebase-admin');
const fs = require('fs');
var adminConfig = {};
const serviceAccount = require('./serviceAccount.json');
adminConfig.credential = firebase.credential.cert(serviceAccount);
firebase.initializeApp(adminConfig)
const collectionRef = firebase.firestore().collection(`/${name}`);
let data = JSON.parse(fs.readFileSync(`${name}-export.json`, 'utf-8'));
firestoreImport(data, collectionRef)
.then(() => console.log('Data was imported.'))
.catch(err => console.error(err));
"firebase-admin": "^8.3.0",
"firebase-functions": "^3.2.0",
"firebase-tools" : "7.3.2"
{ Error: Deadline exceeded
at Http2CallStream.call.on (D:\Blyng\cloud-functions\dal\functions\node_modules\@grpc\grpc-js\build\src\client.js:96:45)
at emitOne (events.js:121:20)
at Http2CallStream.emit (events.js:211:7)
at process.nextTick (D:\Blyng\cloud-functions\dal\functions\node_modules\@grpc\grpc-js\build\src\call-stream.js:71:22)
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickCallback (internal/process/next_tick.js:181:9)
code: 4,
details: 'Deadline exceeded',
metadata: Metadata { options: undefined, internalRepr: Map {} } }
I am having a similar issue with my Google Cloud function throwing the same error:
Something went wrong with the cloud function call. Error: deadline-exceeded at new HttpsErrorImpl
I also increased the time outs and the ram on the function, and just like @slinkstep I am doing a background function that I expect to use a lot of memory and take several minutes to finish.
The background function executes correctly, but Firebase throws the time out error before the background function completes and resolves the promise.
I have exactly the same problem, any solution?
@newmesiss I too had this problem. It was for a one-time run function that kept getting this error in my production database (it was fine in my test database). I eventually took this advise and added and await in front of my .get(). I also increased my timeout and ram for that function in my GCP console. It was able to complete with the error.
@gilcreque I do not have a get() I only have:
const ref = admin.firestore().collection(`/RBD/${rbd}/messages`);
await ref.doc(idMessage).set({ serverResponse: true, idDelivery: payloadMessage.id_delivery, idTargetsSms: idTargetsSms }, { merge: true });
is stuck in the await and for a while the function is executed again.
Not sure if this comment will be helpful at all, but I am getting similar issues when using firebase-admin from a CLI tool - i.e. this is possibly not limited to functions.
I see the timeouts when processing a lot of set() operations in parallel. E.g. collection('something').get() then map over the results calling snapshot.set(). If the collection has more than 5000 or so documents in it, we will start to see these errors. It seems like as the memory taken by node increases (processing 5000 sets in parallel causes quite a memory load), "something" slows down and we start seeing DEADLINE_EXCEEDED. Processing the set calls one at a time in a synchronous for..of loop rather than async using map does not cause the error (it just takes a long time to process).
I am facing this exact issue with CLI tool trying to add close to 250 documents into a collection in Firestore. The last I had performed this action successfully was in Nov 2019. Tried it again today but it keeps failing with DEADLINE_EXCEEDED error message on running my nodejs code.
Any suggestions?
My only suggestion would be to reduce the number of operations in parallel. I initially started doing that when our collection got over 3000 records in it. A "batch" size of 1000 was fine (we are not talking Firestore batches here, just literally my code only doing 1000 at a time). Then on a lower-powered machine (mac book air) the problem only went away with a batch of 500. But now, more than about 300 on my mac mini get the timeouts. This has definitely changed over time with firebase-admin upgrades.
The read operations in question individually take on the order of a second or so normally, but in a batch of 500 Node will take maybe 1G of memory and some of the requests (maybe 10 or so) will take more than the 60 second timeout (They will all start immediately, obviously, then some will come back in a second, but as processing continues, they will get slower and slower). I am thinking some kind of contention, maybe in GRPC? Maybe also useful information: the memory usage does not go up much more than 1G even with a larger batch size, i.e. I would have thought just doing all 5000 operations at once would take something like 5G but then be pretty fast to return the results (that is what we were wanting actually)
@thebeingapp
My use case was to migrate subtrees from realtime database.
It turns out that for db size of a merely a few GB this is far from trivial. Import/export on the web interface has a limitations of 250MB. Anything beyond that needs to be scripted. By scripts have got their limitations too. Was rather disappointment to learn from Firebase support team that the recommended way to perform data migrations is via support request to Firebase support team. Yes, they did that. Swiftly. In less than 24h. That is the official way to perform migrations or larger databases on Firebase Realtime database.
Oh, one other thing, I am able to get a "bit more" out of a batch by increasing the node memory size, there is a limit, but it's gotten me out of trouble on one machine that was not capable of doing batches of even 100 with default settings but can do 500 with 4GB available. (batches of 100 would take 50 minutes, but 500 at least "only" about 10)
here is the command to set the env variable for (say) 4Gb if you need it:
export NODE_OPTIONS=--max_old_space_size=4096
Limiting concurrency was an issue for me here too. I was coming very close to maxing out memory with ~10 concurrent processes (using p-limit) - I backed it down to 6 and it's running better.
Closing this out since this is expected behavior for large reads, please re-open the issue or submit a customer support ticket if you need further assistance for implementation.
Please re-open