firebase-admin-node icon indicating copy to clipboard operation
firebase-admin-node copied to clipboard

Deadline Exceeded error!

Open warlockdn opened this issue 8 years ago • 86 comments

[REQUIRED] Step 2: Describe your environment

  • Operating System version: Ubuntu 16
  • Firebase SDK version: v6
  • Firebase Product: Firestore

[REQUIRED] Step 3: Describe the problem

I am doing a basic Add to Firestore through Node Admin SDK. I just add a object to a document and then wait for the document ID to return after that I send it to the front end to be subscribed for realtime updates. But sometimes I receive the error while adding a document and also updating as to Deadline Exceeded

Steps to reproduce:

The code that I have added to my backend.

let ref = db.collection("orders");

return new Promise((resolve, reject) => {
            ref.add(order).then((order) => {
                logger.info(order);
                resolve(order);
            }).catch((err) => {
                logger.info(err);
                reject(err);
            })
})

There you go I receive errors at catch.

Also checked my account I don't see any high usage since it is in development phase so hardly adding 10-20 docs on a normal day.

Can you please help here. Thanks

warlockdn avatar Sep 04 '18 06:09 warlockdn

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Sep 04 '18 06:09 google-oss-bot

Hi @warlockdn! Sorry to hear that you are having connection issues to Firestore. Note that the initial connection attempt can sometimes be slow, as there is a lot of overhead (Channel establishment, SSL handshakes, authentication). Once you have a connection to Cloud Firestore, the underlying channel will get re-used for further operations. You may be able to work around your issue if you start by issuing a DocumentReference.get() call. We retry those automatically if the connection establishment takes a bit longer than it should.

schmidt-sebastian avatar Sep 04 '18 17:09 schmidt-sebastian

Hi @schmidt-sebastian sorry I don't quite understand. On application restart I already created a connection to firebase. But anyways can you guide me to a resource where I can see some demo or sample code as to why and how this can be used. ?

warlockdn avatar Sep 04 '18 17:09 warlockdn

As said, this is not a solution, but rather a workaround:

const docRef = db.collection("orders").doc();
await docRef.get(); // This will retry on DEADLINE_EXCEEDED
// Now the channel is established.
docRef.create({...}):

schmidt-sebastian avatar Sep 04 '18 23:09 schmidt-sebastian

Do u have a permanent solution to this ? I don't know if with this I can go into production ? If I have to wait to retry on things. Coz the point of being realtime will be of no use if on a load i receive these errors. Or may be i need to follow some other process ?

warlockdn avatar Sep 05 '18 07:09 warlockdn

If you continue to run into these issues, then we should look at your network stack and how you connect to our data centers. If we do need to go down that route, we can let you know some environment variables that will help us debug.

schmidt-sebastian avatar Sep 05 '18 17:09 schmidt-sebastian

Yea you can help me then lemme know what needs to be done so that I don't end up embarrassing my product haha. Thanks

warlockdn avatar Sep 05 '18 17:09 warlockdn

Please set the following environment variables:

GRPC_TRACE=all GRPC_VERBOSITY=DEBUG

schmidt-sebastian avatar Sep 05 '18 20:09 schmidt-sebastian

so this is the error I finally received

I0916 13:25:14.586417074   10852 completion_queue.cc:951]    RETURN_EVENT[0x23f6660]: QUEUE_TIMEOUT
I0916 13:25:14.591104089   10852 completion_queue.cc:851]    grpc_completion_queue_next(cq=0x23f6660, deadline=gpr_timespec { tv_sec: -9223372036854775808, tv_nsec: 0, clock_type: 0 }, reserved=(nil))
I0916 13:25:14.591189393   10852 completion_queue.cc:951]    RETURN_EVENT[0x23f6660]: QUEUE_TIMEOUT
I0916 13:25:14.591880859   10852 completion_queue.cc:851]    grpc_completion_queue_next(cq=0x23f6660, deadline=gpr_timespec { tv_sec: -9223372036854775808, tv_nsec: 0, clock_type: 0 }, reserved=(nil))

warlockdn avatar Sep 16 '18 08:09 warlockdn

so do I need to initialise firebase connection on each function call or does it work once on initialising the server ?

warlockdn avatar Sep 16 '18 08:09 warlockdn

??

warlockdn avatar Sep 18 '18 17:09 warlockdn

Sorry, I am just catching up on my emails. You only need to initialize the client connection only once, and the network channel should stay open.

schmidt-sebastian avatar Sep 18 '18 18:09 schmidt-sebastian

So what's the issue with the time out then ? My internet connectivity is pretty good

On Wed, Sep 19, 2018, 12:23 AM Sebastian Schmidt [email protected] wrote:

Sorry, I am just catching up on my emails. You only need to initialize the client connection only once, and the network channel should stay open.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/firebase/firebase-admin-node/issues/349#issuecomment-422506974, or mute the thread https://github.com/notifications/unsubscribe-auth/ADR8XkgdpookRGwgcuh3JlynA0OnFEDmks5ucUEygaJpZM4WYWqy .

warlockdn avatar Sep 18 '18 18:09 warlockdn

Just to confirm, you are getting most of these errors on the initial document write and before you are using any other Firestore operations? I am suggesting that your initial operation should be a document read, which we will retry. The "time to first byte" from Firestore is not necessarily related to your network connection.

schmidt-sebastian avatar Sep 18 '18 19:09 schmidt-sebastian

I get this as well. I have a collection with about 300k documents on it and simply calling .get() on that collection will result in the timeout. Its a firebase problem and not your local system.

I've seen this happen in hosted cloud functions, and the fix is to increase the timeout limit in the function details (max time is 540 seconds, default is 60 seconds). However in the node sdk I can't get figure out how to extends the default past the 60 second limit.

CapitanRedBeard avatar Jan 04 '19 23:01 CapitanRedBeard

I'm dealing with the same issue. I reported it here: https://github.com/steadyequipment/node-firestore-backup/issues/48, due to it's actually this library who is throwing the error.

BrodaNoel avatar Jan 28 '19 01:01 BrodaNoel

I am having the same issue, when try to read my collection which has 100k docs, it gives me DEADLINE ERROR message. How to solve this, even a simple read is so trouble some. Is there a way to export the data from the DB without hitting this Error

farhankhwaja avatar Jan 29 '19 00:01 farhankhwaja

My workaround is to find a size that doesn't hit the DEADLINE ERROR, like 10k, then just loop by 10k increments and read for the database until you have read all your docs.

You can do something like this:

  const FOO_READ_LIMIT = 10000
  const foosRef = firestore.collection('foo');

  try {
    let isCalculating = true;
    let lastDoc = null;

    async.whilst(() => {
      return isCalculating;
    },
    async (next) => {
      const fooQuery = foosRef.limit(FOO_READ_LIMIT)
      const pagedFooQuery = 
        lastDoc 
          ? pagedFooQuery.startAfter(lastDoc)
          : pagedFooQuery

      const fooSnapshots = await pagedFooQuery.get();
      const size = fooSnapshots && fooSnapshots.size;

      if (size < FOO_READ_LIMIT) {
        isCalculating = false;
        next()
      }
      
      await doStuff(fooSnapshots);

      lastDoc = fooSnapshots.docs[FOO_READ_LIMIT - 1]
      next()
    },
    (error) => {
      console.log('All data is read');
    });
  } catch(e) {
	// Something wen wrong
  }

CapitanRedBeard avatar Jan 31 '19 16:01 CapitanRedBeard

@CapitanRedBeard I ended doing something similar. But, thanks for the reply.

If the Firebase team can add an export option to the Firestore DB like RTDB has, it would help some folks here

farhankhwaja avatar Jan 31 '19 22:01 farhankhwaja

@CapitanRedBeard & @farhankhwaja while I don't know if this will help you, still I was running into the same Deadline exceeded errors with only less than 2k of records! I have implemented this into all of my cloud functions, hopefully, this will help you too:

/**
* Set timeout and memory allocation
* In some cases, your functions may have special requirements for a long timeout value or a large * allocation of memory.
*You can set these values either in the Google Cloud Console or in the function source code (Firebase only).
*/
const runtimeOpts = {
  timeoutSeconds: 540, // 9 minutes
  memory: '2GB'
}
/**
 * When a csv file is uploaded in the Storage bucket it is parsed automatically using
 * PapaParse.
 */
exports.covertToJSON = functions
  .runWith(runtimeOpts) // <== apply timeout and memory limit here!
  .storage
  .object()
  .onFinalize( object => {
[...]

Sadly I can't remember where I found that snippet but as I said, every little bit helps!

Jon

jonrandahl avatar Feb 01 '19 00:02 jonrandahl