You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2019/03/23 01:22:06 UTC
[tinkerpop] branch TINKERPOP-2181 created (now 9914226)
This is an automated email from the ASF dual-hosted git repository.
spmallette pushed a change to branch TINKERPOP-2181
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git.
at 9914226 TINKERPOP-2181 Allow ctrl+c to break a command in Gremlin Console
This branch includes the following new commits:
new 9914226 TINKERPOP-2181 Allow ctrl+c to break a command in Gremlin Console
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[tinkerpop] 01/01: TINKERPOP-2181 Allow ctrl+c to break a command
in Gremlin Console
Posted by sp...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
spmallette pushed a commit to branch TINKERPOP-2181
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
commit 99142269b99b9e559856e8cd3d4fc1ce0b1446d0
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Fri Mar 22 21:21:36 2019 -0400
TINKERPOP-2181 Allow ctrl+c to break a command in Gremlin Console
---
CHANGELOG.asciidoc | 1 +
docs/src/reference/gremlin-applications.asciidoc | 25 +++++++
docs/src/upgrade/release-3.3.x.asciidoc | 17 +++++
.../tinkerpop/gremlin/console/Console.groovy | 67 ++++++++++++-----
.../gremlin/console/GremlinGroovysh.groovy | 84 ++++++++++++----------
.../tinkerpop/gremlin/console/Mediator.groovy | 7 ++
6 files changed, 146 insertions(+), 55 deletions(-)
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 620c047..2370379 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -23,6 +23,7 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
[[release-3-3-7]]
=== TinkerPop 3.3.7 (Release Date: NOT OFFICIALLY RELEASED YET)
+* Enabled `ctrl+c` to interrupt long running processes in Gremlin Console.
[[release-3-3-6]]
=== TinkerPop 3.3.6 (Release Date: March 18, 2019)
diff --git a/docs/src/reference/gremlin-applications.asciidoc b/docs/src/reference/gremlin-applications.asciidoc
index 0181efd..2c27d20 100644
--- a/docs/src/reference/gremlin-applications.asciidoc
+++ b/docs/src/reference/gremlin-applications.asciidoc
@@ -181,6 +181,31 @@ with local commands, but may record session outputs differently for `:remote` co
`:record` it may be best to manually create a `Cluster` object and issue commands that way so that they evaluate
locally in the shell.
+=== Interrupting Evaluations
+
+If there is some input that is taking too long to evaluate or to iterate through, use `ctrl+c` to attempt to interrupt
+that process. It is an "attempt" in the sense that the long running process is only informed of the interruption by
+the user and must respond to it (as with any call to `interrupt()` on a `Thread`). A `Traversal` will typically respond
+to such requests as do most commands, including `:remote` operations.
+
+[source,text]
+----
+gremlin> java.util.stream.IntStream.range(0, 1000).iterator()
+==>0
+==>1
+==>2
+==>3
+==>4
+...
+==>348
+==>349
+==>350
+==>351
+==>352
+Execution interrupted by ctrl+c
+gremlin>
+----
+
[[console-preferences]]
=== Console Preferences
diff --git a/docs/src/upgrade/release-3.3.x.asciidoc b/docs/src/upgrade/release-3.3.x.asciidoc
index c1911d3..57e6990 100644
--- a/docs/src/upgrade/release-3.3.x.asciidoc
+++ b/docs/src/upgrade/release-3.3.x.asciidoc
@@ -27,6 +27,23 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
Please see the link:https://github.com/apache/tinkerpop/blob/3.3.7/CHANGELOG.asciidoc#release-3-3-7[changelog] for a complete list of all the modifications that are part of this release.
+== Upgrading for Users
+
+==== Gremlin Console Interrupt
+
+There are occasions where a traversal (or other evaluation) takes longer than expected to complete. Common examples
+of these situations typically include:
+
+* Accidentally executing `g.V()` (i.e. full graph scan) on a very large graph as an OLTP style traversal.
+* Not terminating a `repeat()` properly which results in an infinite loop.
+* Bad data might create a cycle in the graph where one wasn't expected.
+* Failing to properly bound a traversal's path through one or more supernodes.
+
+In earlier versions, the only option was to kill the Gremlin Console (typically with `ctrl+c`), but as of this version
+`ctrl+c` will be intercepted and attempt to interrupt whatever process is currently executing.
+
+See link:https://issues.apache.org/jira/browse/TINKERPOP-2181[TINKERPOP-2181]
+
== TinkerPop 3.3.6
*Release Date: March 18, 2019*
diff --git a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy
index 89c37ad..fb9c9a4 100644
--- a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy
+++ b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy
@@ -47,6 +47,10 @@ import org.codehaus.groovy.tools.shell.InteractiveShellRunner
import org.codehaus.groovy.tools.shell.commands.SetCommand
import org.codehaus.groovy.tools.shell.util.HelpFormatter
import org.fusesource.jansi.Ansi
+import sun.misc.Signal
+import sun.misc.SignalHandler
+
+import java.util.concurrent.atomic.AtomicBoolean
/**
* @author Stephen Mallette (http://stephen.genoprime.com)
@@ -79,9 +83,21 @@ class Console {
final Mediator mediator = new Mediator(this)
- // make sure that remotes are closed if console takes a ctrl-c
+ // make sure that remotes are closed on jvm shutdown
addShutdownHook { mediator.close() }
+ // try to grab ctrl+c to interrupt an evaluation.
+ final Thread main = Thread.currentThread()
+ Signal.handle(new Signal("INT"), new SignalHandler() {
+ @Override
+ void handle(final Signal signal) {
+ if (mediator.evaluating.get()) {
+ io.out.println("Execution interrupted by ctrl+c")
+ main.interrupt()
+ }
+ }
+ })
+
groovy = new GremlinGroovysh(mediator)
def commandsToRemove = groovy.getRegistry().commands().findAll { it instanceof SetCommand }
@@ -184,6 +200,7 @@ class Console {
private def handleResultShowNothing = { args -> null }
private def handleResultIterate = { result ->
+
try {
// necessary to save persist history to file
groovy.getHistory().flush()
@@ -192,16 +209,34 @@ class Console {
}
while (true) {
+ // give ctrl+c a chance
+ Thread.yield()
+
+ // if this is true then ctrl+c was triggered
+ if (Thread.interrupted()) {
+ this.tempIterator = Collections.emptyIterator()
+ return null
+ }
+
if (this.tempIterator.hasNext()) {
- int counter = 0;
+ int counter = 0
while (this.tempIterator.hasNext() && (Preferences.maxIteration == -1 || counter < Preferences.maxIteration)) {
+ // give ctrl+c a chance
+ Thread.yield()
+
+ // if this is true then ctrl+c was triggered
+ if (Thread.interrupted()) {
+ this.tempIterator = Collections.emptyIterator()
+ return null
+ }
+
printResult(tempIterator.next())
- counter++;
+ counter++
}
if (this.tempIterator.hasNext())
- io.out.println(Colorizer.render(Preferences.resultPromptColor,ELLIPSIS));
- this.tempIterator = Collections.emptyIterator();
- return null
+ io.out.println(Colorizer.render(Preferences.resultPromptColor,ELLIPSIS))
+ this.tempIterator = Collections.emptyIterator()
+ break
} else {
try {
// if the result is an empty iterator then the tempIterator needs to be set to one, as a
@@ -217,29 +252,29 @@ class Console {
if (result instanceof Iterator) {
this.tempIterator = (Iterator) result
if (!this.tempIterator.hasNext()) {
- this.tempIterator = Collections.emptyIterator();
+ this.tempIterator = Collections.emptyIterator()
return null
}
} else if (result instanceof Iterable) {
this.tempIterator = ((Iterable) result).iterator()
if (!this.tempIterator.hasNext()) {
- this.tempIterator = Collections.emptyIterator();
+ this.tempIterator = Collections.emptyIterator()
return null
}
} else if (result instanceof Object[]) {
this.tempIterator = new ArrayIterator((Object[]) result)
if (!this.tempIterator.hasNext()) {
- this.tempIterator = Collections.emptyIterator();
+ this.tempIterator = Collections.emptyIterator()
return null
}
} else if (result instanceof Map) {
this.tempIterator = ((Map) result).entrySet().iterator()
if (!this.tempIterator.hasNext()) {
- this.tempIterator = Collections.emptyIterator();
+ this.tempIterator = Collections.emptyIterator()
return null
}
} else if (result instanceof TraversalExplanation) {
- final int width = TerminalFactory.get().getWidth();
+ final int width = TerminalFactory.get().getWidth()
io.out.println(Colorizer.render(Preferences.resultPromptColor,(buildResultPrompt() + result.prettyPrint(width < 20 ? 80 : width))))
return null
} else {
@@ -272,7 +307,7 @@ class Console {
} else if (object instanceof Edge) {
return Colorizer.render(Preferences.edgeColor, object.toString())
} else if (object instanceof Iterable) {
- List<String> buf = new ArrayList<>();
+ List<String> buf = new ArrayList<>()
def pathIter = object.iterator()
while (pathIter.hasNext()) {
Object n = pathIter.next()
@@ -280,7 +315,7 @@ class Console {
}
return ("[" + buf.join(",") + "]")
} else if (object instanceof Map) {
- List<String> buf = new ArrayList<>();
+ List<String> buf = new ArrayList<>()
object.each{k, v ->
buf.add(colorizeResult(k) + ":" + colorizeResult(v))
}
@@ -297,7 +332,7 @@ class Console {
}
private def handleError = { err ->
- this.tempIterator = Collections.emptyIterator();
+ this.tempIterator = Collections.emptyIterator()
if (err instanceof Throwable) {
try {
final Throwable e = (Throwable) err
@@ -368,9 +403,9 @@ class Console {
File file = new File(scriptFile)
if (!file.exists() && !file.isAbsolute()) {
- final String userWorkingDir = System.getProperty("user.working_dir");
+ final String userWorkingDir = System.getProperty("user.working_dir")
if (userWorkingDir != null) {
- file = new File(userWorkingDir, scriptFile);
+ file = new File(userWorkingDir, scriptFile)
}
}
int lineNumber = 0
diff --git a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovysh.groovy b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovysh.groovy
index 2da0100..f5fa16a 100644
--- a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovysh.groovy
+++ b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovysh.groovy
@@ -68,48 +68,54 @@ class GremlinGroovysh extends Groovysh {
@Override
Object execute(final String line) {
- if (mediator.localEvaluation)
- return super.execute(line)
- else {
- assert line != null
- if (line.trim().size() == 0) {
- return null
- }
-
- maybeRecordInput(line)
-
- Object result
+ mediator.evaluating.set(true)
+
+ try {
+ if (mediator.localEvaluation)
+ return super.execute(line)
+ else {
+ assert line != null
+ if (line.trim().size() == 0) {
+ return null
+ }
+
+ maybeRecordInput(line)
+
+ Object result
+
+ if (isExecutable(line)) {
+ result = executeCommand(line)
+ if (result != null) setLastResult(result)
+ return result
+ }
+
+ List<String> current = new ArrayList<String>(buffers.current())
+ current << line
+
+ // determine if this script is complete or not - if not it's a multiline script
+ def status = parser.parse(current)
+
+ switch (status.code) {
+ case ParseCode.COMPLETE:
+ // concat script here because commands don't support multi-line
+ def script = String.join(Parser.getNEWLINE(), current)
+ setLastResult(mediator.currentRemote().submit([script]))
+ buffers.clearSelected()
+ break
+ case ParseCode.INCOMPLETE:
+ buffers.updateSelected(current)
+ break
+ case ParseCode.ERROR:
+ throw status.cause
+ default:
+ // Should never happen
+ throw new Error("Invalid parse status: $status.code")
+ }
- if (isExecutable(line)) {
- result = executeCommand(line)
- if (result != null) setLastResult(result)
return result
}
-
- List<String> current = new ArrayList<String>(buffers.current())
- current << line
-
- // determine if this script is complete or not - if not it's a multiline script
- def status = parser.parse(current)
-
- switch (status.code) {
- case ParseCode.COMPLETE:
- // concat script here because commands don't support multi-line
- def script = String.join(Parser.getNEWLINE(), current)
- setLastResult(mediator.currentRemote().submit([script]))
- buffers.clearSelected()
- break
- case ParseCode.INCOMPLETE:
- buffers.updateSelected(current)
- break
- case ParseCode.ERROR:
- throw status.cause
- default:
- // Should never happen
- throw new Error("Invalid parse status: $status.code")
- }
-
- return result
+ } finally {
+ mediator.evaluating.set(false)
}
}
diff --git a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Mediator.groovy b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Mediator.groovy
index 679f269..4efeb9b 100644
--- a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Mediator.groovy
+++ b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Mediator.groovy
@@ -20,6 +20,8 @@ package org.apache.tinkerpop.gremlin.console
import org.apache.tinkerpop.gremlin.jsr223.console.RemoteAcceptor
+import java.util.concurrent.atomic.AtomicBoolean
+
/**
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
@@ -29,6 +31,11 @@ class Mediator {
public int position
public boolean localEvaluation = true
+ /**
+ * Determines when the Console is evaluating/iterating input/result.
+ */
+ public AtomicBoolean evaluating = new AtomicBoolean(false)
+
private final Console console
private static String LINE_SEP = System.getProperty("line.separator")