Configure the timeout period for Gremlin requests
Graph Database (GDB) lets you change the timeout period for client requests. You can adjust the timeout period based on your business scenario and data volume. This topic describes how to configure the timeout for Gremlin requests in different programming languages.
Timeout overview
When a client sends a request to Graph Database (GDB), the request times out if the processing time exceeds the configured timeout period, and no result is returned. The default timeout period is 1800 ms.
The timeout value is a Long data type and is specified in milliseconds (ms).
Do not set an excessively long timeout period.
If the connection between the client and GDB is disconnected before the request times out, any updates from that request will not take effect.
Configure the timeout period in different languages
The following sections describe how to configure the timeout period for Gremlin requests in different programming languages. The examples set the timeout period to 5000 ms.
Java
Install Java and Maven on the client.
Run the following command to install Java. This example uses version 1.8.0.
sudo yum install java-1.8.0-develRun the following command to add a repository that contains the Maven package.
wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repoRun the following command to set the version number for the repository. This example uses version 4.0.0.
sudo sed -i s/4.0.0/6/g /etc/yum.repos.d/epel-apache-maven.repoRun the following command to install Maven.
sudo yum install -y apache-mavenNoteIf an error message containing XXX is returned, run one of the following commands to install Maven.
sudo yum install -y apache-maven --skip-brokensudo yum install -y apache-maven --nobest
Create a configuration file to connect to the GDB instance. This example uses pom.xml.
Run the following commands to create and enter the directory for the configuration file.
mkdir gdb-gremlin-test cd gdb-gremlin-testRun the following command to create the pom.xml file.
touch pom.xmlRun the following command to edit the pom.xml file.
vi pom.xmlPress
i, and then paste the following content into the pom.xml file.<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gdb.alibaba</groupId> <artifactId>GdbGremlinExample</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>GdbGremlinExample</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.apache.tinkerpop</groupId> <artifactId>gremlin-driver</artifactId> <version>3.4.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.3</version> <configuration> <mainClass>com.gdb.alibaba.Test</mainClass> <complianceLevel>1.8</complianceLevel> </configuration> </plugin> </plugins> </build> </project>Press the Esc key, enter
:wq, and then press Enter to exit the vi editor.
Create a configuration file for the Java client. This example uses Test.java.
Run the following commands to create and enter the directory for the configuration file.
mkdir -p src/main/java/com/gdb/alibaba/ cd src/main/java/com/gdb/alibabaRun the following command to create the Test.java file.
touch Test.javaRun the following command to edit the Test.java file.
vi Test.javaPress
i. Modify the following content as needed, and then paste it into the Test.java file.package com.gdb.alibaba; import org.apache.tinkerpop.gremlin.driver.Cluster; import org.apache.tinkerpop.gremlin.driver.Client; import org.apache.tinkerpop.gremlin.driver.Result; import org.apache.tinkerpop.gremlin.driver.ResultSet; import org.apache.tinkerpop.gremlin.driver.RequestOptions; import java.util.List; import java.util.Map; import java.util.HashMap; import java.io.File; public class Test { public static void main( String[] args ) { try { if(args.length != 1) { System.out.println("gdb-remote.yaml path needed"); return; } String yaml = args[0]; // 1. Initialize the client. Cluster cluster = Cluster.build(new File(yaml)).create(); Client client = cluster.connect().init(); // 2. Set the timeout period to 5000 ms. RequestOptions.Builder options = RequestOptions.build().timeout(5000); options.addParameter("G___id", "sand131_id_5_99"); String dsl = "g.V(G___id)"; // 3. Send the Gremlin request to the GDB server. ResultSet results = client.submit(dsl, options.create()); List<Result> result = results.all().join(); result.forEach(p -> System.out.println(p.getObject())); // 4. Shut down the client. cluster.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } }Press the Esc key, enter
:wq, and then press Enter to exit the vi editor.
Create a configuration file to connect the Java client to the GDB instance. This example uses gdb-remote.yaml.
Run the following command to create the gdb-remote.yaml file.
touch gdb-remote.yamlRun the following command to edit the gdb-remote.yaml file.
vi gdb-remote.yamlPress
i. Modify the following content as needed, and then paste it into the gdb-remote.yaml file.hosts: <your_gdb_endpoint> // Replace <your_gdb_endpoint> with the endpoint of your GDB instance. port: <port> // Replace <port> with the port number of your GDB instance. username: <username> // Replace <username> with the username for your GDB instance. password: <password> // Replace <password> with the password for the username. serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1, config: { serializeResultToString: false } }Press the Esc key, enter
:wq, and then press Enter to exit the vi editor.
Compile and run the Java program.
Change to the directory where the pom.xml file is located.
cd /home/gdb-gremlin-testRun the following command to compile and run the Java program.
mvn compile exec:java -Dexec.args="/home/apache-tinkerpop-gmlin-console-3.4.0/conf/gdb-remote.yaml"
Python
Run the following command to initialize the client.
client = client.Client('ws://${your-gdb-endpoint}:8182/gremlin', 'g', username="${username}", password="${password}")The following code sets the timeout period to 5000 ms.
dsl = "g.V(G___id)" parameters = {} parameters["G___id"] = "sand131_id_5_99" message = RequestMessage('', 'eval', {'gremlin': dsl, 'scriptEvaluationTimeout': 5000, 'bindings': parameters})The following code sends the request to the GDB server.
results = client.submit(message).all().result()Run the following command to shut down the client.
client.close()
.Net
Run the following command to initialize the client.
var gremlinServer = new GremlinServer(endpoint, port, username: username, password: password); var gremlinClient = new GremlinClient(gremlinServer);The following code sets the timeout period to 5000 ms.
string dsl = "g.V(G___id)"; Dictionary<string, object> bindings = new Dictionary<string, object> {{"G___id", "sand131_id_5_99"}}; var msgBuilder = RequestMessage.Build(Tokens.OpsEval).AddArgument(Tokens.ArgsGremlin, dsl) .AddArgument(Tokens.ArgsBindings, bindings).AddArgument("scriptEvaluationTimeout", 5000);Run the following command to send a request to the Graph Database server.
await gremlinClient.SubmitAsync<T>(msgBuilder.Create()).ConfigureAwait(false);Shut down the client.
Go
Run the following command to initialize the client.
settings := &goClient.Settings{ Host: host, Port: port, Username: username, Password: password, } client := goClient.NewClient(settings)The following code sets the timeout period to 5000 ms.
parameters := make(map[string]interface{}) parameters["G___id"] = "sand131_id_5_99" options := graph.NewRequestOptionsWithBindings(bindings) options.SetTimeout(5000)The following code sends the request to the GDB server.
dsl := "g.V(G___id)" results, err := client.SubmitScriptOptions(dsl, options)The following code shuts down the client.
client.Close()
Other methods to configure the timeout period
Java bytecode
Run the following command to initialize the client.
Cluster cluster = Cluster.build(new File(yaml)).create(); Client client = cluster.connect().init();The following code sets the timeout period to 5000 ms.
g.with('scriptEvaluationTimeout', 5000).V().limit(1).toList();The following code sends the request to the GDB server.
List<Vertex> list = g.V().has("person","name","marko").out("knows").toList();You can run the following command to shut down the client.
cluster.close();
Configure the request timeout period for Java
// 1. Initialize the client.
// 2. Configure request parameters such as the timeout period. You can add parameters using bindings.
RequestOptions.Builder options = RequestOptions.build().timeout(5000);
options.addParameter("G___id", "sand131_id_5_99");
String dsl = "g.V(G___id)";
// 3. Send the request to the GDB server.
ResultSet results = client.submit(dsl, options.create());
// 4. Shut down the client after the task is complete.Replace sand131_id_5_99 with the actual request parameter.
Configure the timeout period for requests from the Gremlin console
:remote config timeout 5000Configure the request timeout period for Python
from gremlin_python.driver.request import RequestMessage
# 1. Initialize the client.
dsl = "g.V(G___id)";
parameters = {}
parameters["G___id"] = "sand131_id_5_99"
# 2. Configure request parameters such as the timeout period. You can add parameters using bindings.
message = RequestMessage('', 'eval', {'gremlin': dsl, 'scriptEvaluationTimeout': 5000, 'bindings': parameters})
# 3. Send the request to the GDB server.
results = client.submit(message).all().result()
# 4. Shut down the client after the task is complete.Replace sand131_id_5_99 with the actual request parameter.
Configure the request timeout period for .NET
// 1. Initialize the client.
string dsl = "g.V(G___id)";
Dictionary<string, object> bindings = new Dictionary<string, object> {{"G___id", "sand131_id_5_99"}};
// 2. Configure request parameters such as the timeout period. You can add parameters using bindings.
var msgBuilder = RequestMessage.Build(Tokens.OpsEval).AddArgument(Tokens.ArgsGremlin, dsl)
.AddArgument(Tokens.ArgsBindings, bindings).AddArgument("scriptEvaluationTimeout", 5000);
// 3. Send the request to the GDB server.
await gremlinClient.SubmitAsync<T>(msgBuilder.Create()).ConfigureAwait(false);
// 4. Shut down the client after the task is complete.Replace sand131_id_5_99 with the actual request parameter.
Configure the request timeout period for Go
import "github.com/aliyun/alibabacloud-gdb-go-sdk/gdbclient/graph"
// 1. Initialize the client.
parameters := make(map[string]interface{})
parameters["G___id"] = "sand131_id_5_99"
// 2. Configure request parameters such as the timeout period. You can add parameters using bindings.
options := graph.NewRequestOptionsWithBindings(bindings)
options.SetTimeout(5000)
// 3. Send the request to the GDB server.
dsl := "g.V(G___id)"
results, err := client.SubmitScriptOptions(dsl, options)
// 4. Shut down the client after the task is complete.Replace sand131_id_5_99 with the actual request parameter.
Configure the timeout period for bytecode requests
Graph Database supports configuring the timeout period for bytecode requests. The following example uses the Java environment. This method is not supported in other environments.
g.with('scriptEvaluationTimeout', 2000).V().limit(1).toList()