(可选)在每个查询级别设置超时
Neptune 向您提供了使用参数组选项 neptune_query_timeout 为查询设置超时的功能(请参阅参数)。您也可以用如下代码覆盖全局超时:
final Cluster cluster = Cluster.build("localhost") .port(8182) .maxInProcessPerConnection(32) .maxSimultaneousUsagePerConnection(32) .serializer(Serializers.GRAPHBINARY_V1D0) .create(); try { final GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster)); List<Object> verticesWithNamePumba = g.with(ARGS_EVAL_TIMEOUT, 500L).V().has("name", "pumba").out("friendOf").id().toList(); System.out.println(verticesWithNamePumba); } finally { cluster.close(); }
或者,对于基于字符串的查询提交,代码如下所示:
RequestOptions options = RequestOptions.build().timeout(500).create(); List<Result> result = client.submit("g.V()", options).all().get();
注意
如果您将查询超时值设置得过高,可能会产生意想不到的成本,尤其是在无服务器实例上。如果没有合理的超时设置,查询保持运行的时间可能会比预期长得多,从而产生您意想不到的成本。在运行查询时可以纵向扩展到昂贵的大型实例类型的无服务器实例上尤其如此。
您可以通过使用查询超时值来避免此类意外开支,该值可以适应您预期的运行时间,并且只会导致异常长的运行超时。
从 Neptune 引擎版本 1.3.2.0 开始,Neptune 支持一个新的 neptune_lab_mode 参数作为 StrictTimeoutValidation。当该参数的值设置为 Enabled 时,通过请求选项或查询提示指定的单查询超时时间,不得超过参数组中设置的全局超时值。若超出限制,Neptune 会抛出 InvalidParameterException。
当该参数值为 Disabled 时,可通过 '/status' 端点的响应确认此设置;在 1.3.2.0 版本中,该参数的默认值为 Disabled。从 1.4.0.0 版本开始,默认情况下,StrictTimeoutValidation 已更改为 Enabled。