Multiple Gremlin Clusters in a single Java application instance

326 Views Asked by At

In a scenario when one java application instance can talk to multiple Gremlin servers (say multiple graph instances in CosmosDB). What is the recommendation for creating and caching Gremlin Clients in such a case. Looks like on my mac maximum number of Cluster instances I can create is less than 300. Beyond that I get "Too many files open exception".

caused by: java.io.IOException: Too many open files

java.lang.IllegalStateException: failed to create a child event loop

at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:88)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:58)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:47)
at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:59)
at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:86)
at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:81)
at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:68)
at org.apache.tinkerpop.gremlin.driver.Cluster$Factory.<init>(Cluster.java:1065)

Is there a way around it? This is what is being done for many different values of graphUserName .

final Cluster cluster = Cluster.build()
                .addContactPoint("host")
                .port(443)
                .credentials(graphUserName,
                        "graph-password")
                .serializer("serializer)
                .enableSsl(true)
                .create();
        cluster.connect());
1

There are 1 best solutions below

8
On

You should be creating one Cluster object for the life of your application. Assuming you are using sessionless requests, you would then also only create one Client instance from that Cluster and re-use it. There may of course be situations where a single Cluster object may not be sufficient. There are driver configuration options that can only apply to a Cluster object and not the Client instances it spawns. For example, if you have multiple authentication methods or are connecting to different servers, those settings are bound to the Cluster which would then require you to have several of those objects.

While you always want to take care in managing your settings for the driver, opening a large number of Cluster objects with default settings (which is where most people start) will likely trigger the error of caused by: java.io.IOException: Too many open files that you are seeing. Each Cluster object will open many network resources and unless you have made changes to your default settings in your OS you will likely exceed the file limit. This message is an operating system level issue and there are many resources on the internet for solving it.