memgraph / memgraph

Open-source graph database, tuned for dynamic analytics environments. Easy to adopt, scale and own.
https://memgraph.com
Other
2.32k stars 102 forks source link

Session closing slow on Memgraph 2.12.x with Neo4j Java driver #1450

Open antejavor opened 10 months ago

antejavor commented 10 months ago

Memgraph version Memgraph 2.12 and Memgraph 2.12.1

Environment Docker Memgraph and platform

Describe the bug Session closing is slow compared to execution of a query:

2.12 Memgraph Platform (it is similar for pure Memgraph):

Session open + execution time in seconds: 0.12970075
Session closed and executed after 6.402134709

Memgraph 2.12.1 Memgraph platform (it is similar for pure Memgraph):

Session open + execution time in seconds: 0.099575875
Session closed and executed after 1.92729075

To Reproduce Start Memgraph: docker run -it -p 7687:7687 -p 7444:7444 --name memgraph-stream-test memgraph/memgraph --also-log-to-stderr=TRUE --log-level=TRACE

Prepare Memgraph: UNWIND range(0, 500000) AS id CREATE (n:Node {compoundKey: id}); CREATE INDEX ON :Node(compoundKey);

Edit the manifest for driver:

<dependencies>
    <dependency>
        <groupId>org.neo4j.driver</groupId>
        <artifactId>neo4j-java-driver</artifactId>
        <version>5.13.0</version>
    </dependency>
</dependencies>

Run the following code sample:

package org.example;
import org.neo4j.driver.*;

import java.util.*;

public class HelloWorldExample implements AutoCloseable {
    private final Driver driver;

    public HelloWorldExample(String uri, String user, String password) {
        driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
    }

    @Override
    public void close() throws RuntimeException {
        driver.close();
    }

    public void getByKeys(Collection<String> keys) {
        long startTime = System.nanoTime();
        try (Session session = driver.session()) {

            Map<String, Object> params = new HashMap<>();
            params.put("ids", keys);
            final String query = """
                MATCH (o:Node)
                WHERE o.compoundKey IN $ids
                RETURN o
                """;
            Result result = session.run(query, params);
            long endTime = System.nanoTime();
            double duration = (endTime - startTime) / 1_000_000_000.0;
            System.out.println("Session open + execution time in seconds: " + duration);
        }
        long session_end = System.nanoTime();
        double duration_session = (session_end - startTime) / 1_000_000_000.0;
        System.out.println("Session closed and executed after " + duration_session);
    }

    public static void main(String... args) {
        try (var runner = new HelloWorldExample("bolt://localhost:7687", "", "")) {
            Collection<String> keys = new ArrayList<>();
            for (int i = 0; i < 180; i++) {
                double key = i / 180.0;
                keys.add(String.valueOf(key));
            }
            runner.getByKeys(keys);
        }
    }
}

Expected behavior Session close is slow, and it confuses users that query execution is slow

antejavor commented 9 months ago

I am currently not sure how big an issue this is across the available drivers, or if this is a specific edge case query + session issue, hence the given priorities.