From 79999e9769529ccdea7b22577dfb4be5d9bbe069 Mon Sep 17 00:00:00 2001 From: Stephen Mallette Date: Mon, 18 Sep 2017 14:54:31 -0400 Subject: [PATCH] TINKERPOP-1787 Add syntax for :remote to accept a Cluster object --- CHANGELOG.asciidoc | 1 + .../src/reference/gremlin-applications.asciidoc | 9 +++++++++ .../upgrade/release-3.2.x-incubating.asciidoc | 15 +++++++++++++++ .../groovy/plugin/DriverRemoteAcceptor.java | 17 +++++++++++++++-- .../console/jsr223/DriverRemoteAcceptor.java | 16 ++++++++++++++-- .../DriverRemoteAcceptorIntegrateTest.java | 9 ++++++++- .../DriverRemoteAcceptorIntegrateTest.java | 9 ++++++++- 7 files changed, 70 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 70d6134c793..8258a41e7aa 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -29,6 +29,7 @@ TinkerPop 3.2.7 (Release Date: NOT OFFICIALLY RELEASED YET) * Bump to Jackson 2.8.10. * Added an `EmbeddedRemoteConnection` so that it's possible to mimic a remote connection within the same JVM. * Supported interruption for remote traversals. +* Allow the `:remote` command to accept a `Cluster` object defined in the console itself. * The Console's `plugin.txt` file is only updated if there were manually uninstalled plugins. * Fixed a bug in `MatchStep` where mid-traversal `where()` variables were not being considered in start-scope. * Generalized `MatchStep` to locally compute all clauses with barriers (not just reducing barriers). diff --git a/docs/src/reference/gremlin-applications.asciidoc b/docs/src/reference/gremlin-applications.asciidoc index 7e72d338cca..75591f1119c 100644 --- a/docs/src/reference/gremlin-applications.asciidoc +++ b/docs/src/reference/gremlin-applications.asciidoc @@ -538,6 +538,15 @@ submitting `:> graph` will return a `Graph` instance and in most cases those are and will return a serialization error. It should be noted that `TinkerGraph`, as a convenience for shipping around small sub-graphs, is serializable from Gremlin Server. +The alternative syntax to connecting allows for the `Cluster` to be user constructed directly in the console as +opposed to simply providing a static YAML file. + +[gremlin-groovy] +---- +cluster = Cluster.open() +:remote connect tinkerpop.server cluster +---- + The Gremlin Server `:remote config` command for the driver has the following configuration options: [width="100%",cols="3,10a",options="header"] diff --git a/docs/src/upgrade/release-3.2.x-incubating.asciidoc b/docs/src/upgrade/release-3.2.x-incubating.asciidoc index ae504d49691..078bce6620c 100644 --- a/docs/src/upgrade/release-3.2.x-incubating.asciidoc +++ b/docs/src/upgrade/release-3.2.x-incubating.asciidoc @@ -41,6 +41,21 @@ simple way to provide a "remote" that is actually local to the same JVM. See: link:https://issues.apache.org/jira/browse/TINKERPOP-1756[TINKERPOP-1756] +Specify a Cluster Object +^^^^^^^^^^^^^^^^^^^^^^^^ + +The `:remote connect` command can now take a pre-defined `Cluster` object as its argument as opposed to a YAML +configuration file. + +[source,text] +---- +gremlin> cluster = Cluster.open() +==>localhost/127.0.0.1:8182 +gremlin> :remote connect tinkerpop.server cluster +==>Configured localhost/127.0.0.1:8182 +---- + +See: link:https://issues.apache.org/jira/browse/TINKERPOP-1787[TINKERPOP-1787] Remote Traversal Timeout ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptor.java b/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptor.java index a6a2ffc0c38..e1dbcf3c7e8 100644 --- a/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptor.java +++ b/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptor.java @@ -31,6 +31,7 @@ import org.codehaus.groovy.tools.shell.Groovysh; import javax.security.sasl.SaslException; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; @@ -86,10 +87,22 @@ public DriverRemoteAcceptor(final Groovysh shell) { @Override public Object connect(final List args) throws RemoteException { - if (args.size() < 1) throw new RemoteException("Expects the location of a configuration file as an argument"); + if (args.size() < 1) throw new RemoteException("Expects the location of a configuration file or variable name for a Cluster object as an argument"); try { - this.currentCluster = Cluster.open(args.get(0)); + final String fileOrVar = args.get(0); + if (new File(fileOrVar).isFile()) + this.currentCluster = Cluster.open(fileOrVar); + else if (shell.getInterp().getContext().getVariables().containsKey(fileOrVar)) { + final Object o = shell.getInterp().getContext().getVariable(fileOrVar); + if (o instanceof Cluster) { + this.currentCluster = (Cluster) o; + } + } + + if (null == currentCluster) + throw new RemoteException("Expects the location of a configuration file or variable name for a Cluster object as an argument"); + final boolean useSession = args.size() >= 2 && (args.get(1).equals(TOKEN_SESSION) || args.get(1).equals(TOKEN_SESSION_MANAGED)); if (useSession) { final String sessionName = args.size() == 3 ? args.get(2) : UUID.randomUUID().toString(); diff --git a/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/jsr223/DriverRemoteAcceptor.java b/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/jsr223/DriverRemoteAcceptor.java index 51272ba5913..dd7e3a69c2c 100644 --- a/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/jsr223/DriverRemoteAcceptor.java +++ b/gremlin-console/src/main/java/org/apache/tinkerpop/gremlin/console/jsr223/DriverRemoteAcceptor.java @@ -31,6 +31,7 @@ import org.apache.tinkerpop.gremlin.structure.util.ElementHelper; import javax.security.sasl.SaslException; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; @@ -84,10 +85,21 @@ public DriverRemoteAcceptor(final GremlinShellEnvironment shellEnvironment) { @Override public Object connect(final List args) throws RemoteException { - if (args.size() < 1) throw new RemoteException("Expects the location of a configuration file as an argument"); + if (args.size() < 1) throw new RemoteException("Expects the location of a configuration file or variable name for a Cluster object as an argument"); try { - this.currentCluster = Cluster.open(args.get(0)); + final String fileOrVar = args.get(0); + if (new File(fileOrVar).isFile()) + this.currentCluster = Cluster.open(fileOrVar); + else if (shellEnvironment.getVariable(fileOrVar) != null) { + final Object o = shellEnvironment.getVariable(fileOrVar); + if (o instanceof Cluster) { + this.currentCluster = (Cluster) o; + } + } + + if (null == currentCluster) + throw new RemoteException("Expects the location of a configuration file or variable name for a Cluster object as an argument"); final boolean useSession = args.size() >= 2 && (args.get(1).equals(TOKEN_SESSION) || args.get(1).equals(TOKEN_SESSION_MANAGED)); if (useSession) { final String sessionName = args.size() == 3 ? args.get(2) : UUID.randomUUID().toString(); diff --git a/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptorIntegrateTest.java b/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptorIntegrateTest.java index 1363c265f96..8788b838077 100644 --- a/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptorIntegrateTest.java +++ b/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/DriverRemoteAcceptorIntegrateTest.java @@ -19,6 +19,7 @@ package org.apache.tinkerpop.gremlin.console.groovy.plugin; import org.apache.tinkerpop.gremlin.TestHelper; +import org.apache.tinkerpop.gremlin.driver.Cluster; import org.apache.tinkerpop.gremlin.driver.Result; import org.apache.tinkerpop.gremlin.server.Settings; import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils; @@ -80,10 +81,16 @@ public void after() { } @Test - public void shouldConnect() throws Exception { + public void shouldConnectWithRemoteYaml() throws Exception { assertThat(acceptor.connect(Collections.singletonList(TestHelper.generateTempFileFromResource(this.getClass(), "remote.yaml", ".tmp").getAbsolutePath())).toString(), startsWith("Configured ")); } + @Test + public void shouldConnectWithRemoteVariable() throws Exception { + groovysh.getInterp().evaluate(Collections.singletonList("cluster = " + Cluster.class.getName() + ".open(\"" + TestHelper.generateTempFileFromResource(this.getClass(), "remote.yaml", ".tmp").getAbsolutePath() + "\")")); + assertThat(acceptor.connect(Collections.singletonList("cluster")).toString(), startsWith("Configured ")); + } + @Test public void shouldConnectAndSubmitSession() throws Exception { assertThat(acceptor.connect(Arrays.asList(TestHelper.generateTempFileFromResource(this.getClass(), "remote.yaml", ".tmp").getAbsolutePath(), "session")).toString(), startsWith("Configured ")); diff --git a/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/jsr223/DriverRemoteAcceptorIntegrateTest.java b/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/jsr223/DriverRemoteAcceptorIntegrateTest.java index f794c610a87..160a5e9c842 100644 --- a/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/jsr223/DriverRemoteAcceptorIntegrateTest.java +++ b/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/jsr223/DriverRemoteAcceptorIntegrateTest.java @@ -19,6 +19,7 @@ package org.apache.tinkerpop.gremlin.console.jsr223; import org.apache.tinkerpop.gremlin.TestHelper; +import org.apache.tinkerpop.gremlin.driver.Cluster; import org.apache.tinkerpop.gremlin.driver.Result; import org.apache.tinkerpop.gremlin.jsr223.console.GremlinShellEnvironment; import org.apache.tinkerpop.gremlin.server.Settings; @@ -82,10 +83,16 @@ public void after() { } @Test - public void shouldConnect() throws Exception { + public void shouldConnectWithRemoteYaml() throws Exception { assertThat(acceptor.connect(Collections.singletonList(TestHelper.generateTempFileFromResource(this.getClass(), "remote.yaml", ".tmp").getAbsolutePath())).toString(), startsWith("Configured ")); } + @Test + public void shouldConnectWithRemoteVariable() throws Exception { + groovysh.getInterp().evaluate(Collections.singletonList("cluster = " + Cluster.class.getName() + ".open(\"" + TestHelper.generateTempFileFromResource(this.getClass(), "remote.yaml", ".tmp").getAbsolutePath() + "\")")); + assertThat(acceptor.connect(Collections.singletonList("cluster")).toString(), startsWith("Configured ")); + } + @Test public void shouldConnectAndSubmitSession() throws Exception { assertThat(acceptor.connect(Arrays.asList(TestHelper.generateTempFileFromResource(this.getClass(), "remote.yaml", ".tmp").getAbsolutePath(), "session")).toString(), startsWith("Configured "));