neo4j / neo4j-javascript-driver

Neo4j Bolt driver for JavaScript
https://neo4j.com/docs/javascript-manual/current/
Apache License 2.0
847 stars 148 forks source link

Neo.ClientError.Cluster.NotALeader using Neo4j aura #1207

Open nicholasoxford opened 1 month ago

nicholasoxford commented 1 month ago

Bug Report

After upgrading to a paid tier and when calling

    const graphDbResponse = await graphDb
      .session({ defaultAccessMode: neo4j.session.WRITE })
      .executeWrite((q) => q.run(query, params))
      .catch((err) => {
        console.error("Error adding follow to graph db", JSON.stringify(err));
      });

      // or

      const session = graphDb.session({
      defaultAccessMode: neo4j.session.WRITE,
    });

    session
      .run(query, params)
      .then(async (res) => {
        console.log("result", res);
        graphDbResponse = res;
        await session.close();
      })
      .catch((err) => {
        console.error("Error adding follow to graph db", JSON.stringify(err));
      });

with graphDB coming from

const graphDb = driver(
  GRAPH_DB_URL,
  auth.basic(GRAPH_DB_USERNAME, GRAPH_DB_PASSWORD)
);

I am getting the error: Neo.ClientError.Cluster.NotALeader about 50% of the time

I am currently using the bolt connection. Reading online it seems like I should be using neo4j+s connections, which I have tried both locally and in production with no success. I get this info from the get go

INFO Routing table is stale for database: "neo4j" and access mode: "WRITE": RoutingTable[database=neo4j, expirationTime=0, currentTime=1721796877254, routers=[], readers=[], writers=[]]

then when I try to query the database I get

ConnectionHolder got an error while releasing the connection. Error Neo4jError: Connection acquisition timed out in 60000 ms. Pool status: Active conn count = 0, Idle conn count = 0.. Stacktrace: Neo4jError:

My url is in this format GRAPH_DB_URL="neo4j+s://<DID>.databases.neo4j.io"

Here is where I am acquiring the session, I am assuming the name of the database is neo4j, I understand if I dont pass anything it just makes a call to look it up

const session = graphDb.session({
            database: "neo4j",
            defaultAccessMode: "WRITE",
          });
          console.log("ABOUIT TO CALL");
          const result = await session.run(query, { id }).catch((e) => {
            console.log({ e });
            throw e;
          });

How can I force it to write to the leader? Is there something wrong with how I am trying to connect?

My Environment

Javascript Runtime Version: Bun 1.20 Driver Version: 5.22.0 Neo4j Version and Edition: Neo4J Aura

nicholasoxford commented 1 month ago

I think this is more of an issue of not being able to connect with neo4j+s no matter what I try, neo4j+s login works on workspace preview with the same url, username, and password

Any ideas?

nicholasoxford commented 1 month ago

After reserach it looks to only happen when using Bun!

bigmontz commented 1 month ago

@nicholasoxford, when you move to the paid version, you can start to run use a cluster environment which only one of the nodes can performs write.

So, for this scenarios:

nicholasoxford commented 1 month ago

@bigmontz when using executeWrite, with bolt+s, I am still getting the issue with both Node and Bun

To make it more clear: When using Bun, I am unable to use neo4j+s connections at all. The connection simply hangs, if I use node I am able to write using neo4j+s url

https://github.com/oven-sh/bun/issues/12772

bigmontz commented 1 month ago

In cluster environments, you should use neo4j+s, otherwise the queries will not be routed to the correct cluster member.

nicholasoxford commented 2 weeks ago

@bigmontz https://github.com/oven-sh/bun/issues/12772#issuecomment-2284124618

I pulled this library and started debugging where Bun was getting hung up compared to node, was def an interesting rabbit hole

I am using neo4j+s, the problem is it doesn't work with Bun when using an aura cluster. You can check my scripts, maybe this library could make changes to accommodate Bun, for example the "net" library hasn't been updated in 11 years, but I think that is asking too much of y'all

I am not the only person to run into this issue: https://github.com/oven-sh/bun/issues/9914