Skip to content

Commit

Permalink
#6560: handling combination of code and magic, correct results
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslawmalekcodete committed Jan 25, 2018
1 parent 8e6237a commit 558a0ce
Show file tree
Hide file tree
Showing 77 changed files with 898 additions and 1,062 deletions.
43 changes: 37 additions & 6 deletions doc/groovy/LoadMagicCommand.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,61 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Added jar: [loadMagicJarDemo.jar]\n"
]
}
],
"source": [
"%classpath add jar ../resources/jar/loadMagicJarDemo.jar"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Magic command %showEnvs was successfully added.\n"
]
}
],
"source": [
"%load_magic com.twosigma.beakerx.custom.magic.command.ShowEnvsCustomMagicCommand"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{PATH=/home/codete/anaconda2/envs/beakerx2/bin:/home/codete/anaconda2/envs/beakerx2/bin:/home/codete/anaconda2/bin:/home/codete/.nvm/versions/node/v6.11.0/bin:/home/codete/anaconda2/bin:/home/codete/installs:/home/codete/bin:/home/codete/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/home/codete/devtools/java/java/bin:/home/codete/devtools/gradles/gradle/bin:/usr/lib/groovy/groovy-2.4.7/bin:/home/codete/devtools/apache-maven-3.3.9/bin:/home/codete/devtools/scaladk/scala/bin:/home/codete/devtools/sparkdk/spark/bin:/home/codete/devtools/kafka/kafka-0.10.2.0-src/bin, XMODIFIERS=@im=ibus, GDMSESSION=ubuntu, CONDA_DEFAULT_ENV=beakerx2, DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-oj9LsyokYo, DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.path, CONDA_PREFIX=/home/codete/anaconda2/envs/beakerx2, UPSTART_SESSION=unix:abstract=/com/ubuntu/upstart-session/1000/1592, LC_PAPER=pl_PL.UTF-8, PWD=/home/codete/workspaceBeakerx/beakerx, DERBY_HOME=/usr/lib/jvm/java-8-oracle/db, LANGUAGE=en_US, NVM_CD_FLAGS=, LESSOPEN=| /usr/bin/lesspipe %s, JAVA_HOME_CONDA_BACKUP=/home/codete/devtools/java/java, NVM_PATH=/home/codete/.nvm/versions/node/v6.11.0/lib/node, INSTANCE=, CONDA_PATH_BACKUP=/home/codete/anaconda2/bin:/home/codete/.nvm/versions/node/v6.11.0/bin:/home/codete/anaconda2/bin:/home/codete/installs:/home/codete/bin:/home/codete/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/home/codete/devtools/java/java/bin:/home/codete/devtools/gradles/gradle/bin:/usr/lib/groovy/groovy-2.4.7/bin:/home/codete/devtools/apache-maven-3.3.9/bin:/home/codete/devtools/scaladk/scala/bin:/home/codete/devtools/sparkdk/spark/bin:/home/codete/devtools/kafka/kafka-0.10.2.0-src/bin, UPSTART_INSTANCE=, GNOME_DESKTOP_SESSION_ID=this-is-deprecated, GTK_MODULES=gail:atk-bridge:unity-gtk-module, CLUTTER_IM_MODULE=xim, COMPIZ_BIN_PATH=/usr/bin/, SHLVL=1, JAVA_HOME=/home/codete/anaconda2/envs/beakerx2, KAFKA_HOME=/home/codete/devtools/kafka/kafka-0.10.2.0-src, TERM=xterm, XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg, XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0, CONDA_PS1_BACKUP=${debian_chroot:+($debian_chroot)}\\[\\033[00;32m\\]\\u@\\h\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00;31m\\]$(parse_git_branch)\\[\\033[00m\\]\\$ , XDG_SESSION_ID=c2, GRADLE_HOME=/home/codete/devtools/gradles/gradle, GROOVY_HOME=/usr/lib/groovy/groovy-2.4.7, MESOS_NATIVE_JAVA_LIBRARY=/home/codete/devtools/mesosdk/mesos1.1.0.bin/lib/libmesos.so, JPY_PARENT_PID=20562, LC_NAME=pl_PL.UTF-8, _=/home/codete/anaconda2/envs/beakerx2/bin/jupyter, XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/codete, UPSTART_EVENTS=xsession started, DESKTOP_SESSION=ubuntu, SESSION=ubuntu, QT_ACCESSIBILITY=1, NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat, NVM_BIN=/home/codete/.nvm/versions/node/v6.11.0/bin, XAUTHORITY=/home/codete/.Xauthority, LC_MEASUREMENT=pl_PL.UTF-8, LC_TELEPHONE=pl_PL.UTF-8, XDG_DATA_DIRS=/usr/share/ubuntu:/usr/share/gnome:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop, MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path, LC_TIME=pl_PL.UTF-8, GTK_IM_MODULE=ibus, PS1=groovy>, XDG_CURRENT_DESKTOP=Unity, QT4_IM_MODULE=xim, QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1, LOGNAME=codete, JOB=unity-settings-daemon, IM_CONFIG_PHASE=1, PYTHONPATH=/home/codete/workspaceNewBeaker/beaker-notebook-private/jupyter/beaker, SHELL=/bin/bash, LC_ADDRESS=pl_PL.UTF-8, GIO_LAUNCHED_DESKTOP_FILE=/usr/share/applications/guake.desktop, J2SDKDIR=/usr/lib/jvm/java-8-oracle, GTK2_MODULES=overlay-scrollbar, OLDPWD=/home/codete, J2REDIR=/usr/lib/jvm/java-8-oracle/jre, XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0, NVM_IOJS_ORG_MIRROR=https://iojs.org/dist, M2_HOME=/home/codete/devtools/apache-maven-3.3.9, SESSIONTYPE=gnome-session, XDG_SESSION_DESKTOP=ubuntu, LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:, LC_IDENTIFICATION=pl_PL.UTF-8, LESSCLOSE=/usr/bin/lesspipe %s %s, LC_MONETARY=pl_PL.UTF-8, MANPATH=/home/codete/.nvm/versions/node/v6.11.0/share/man:/home/codete/anaconda2/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man:/home/codete/devtools/java/java/man:/home/codete/devtools/scaladk/scala/man, COMPIZ_CONFIG_PROFILE=ubuntu, UPSTART_JOB=unity7, QT_IM_MODULE=ibus, XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt, LANG=en_US.UTF-8, GNOME_KEYRING_CONTROL=, XDG_SESSION_TYPE=x11, DISPLAY=:0, NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist, NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript, GDM_LANG=en_US, NVM_DIR=/home/codete/.nvm, GPG_AGENT_INFO=/home/codete/.gnupg/S.gpg-agent:0:1, USER=codete, CLASSPATH=:/home/codete/javaJarLibs/*, GIO_LAUNCHED_DESKTOP_FILE_PID=8050, LC_NUMERIC=pl_PL.UTF-8, XDG_SEAT=seat0, SSH_AUTH_SOCK=/run/user/1000/keyring/ssh, QT_QPA_PLATFORMTHEME=appmenu-qt5, XDG_RUNTIME_DIR=/run/user/1000, XDG_VTNR=7, HOME=/home/codete, GNOME_KEYRING_PID=}\n"
]
}
],
"source": [
"%showEnvs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
97 changes: 97 additions & 0 deletions kernel/base/src/main/java/com/twosigma/beakerx/TryResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.twosigma.beakerx;

import java.util.NoSuchElementException;

public interface TryResult {

boolean isResult();

boolean isError();

Object result();

String error();

static CellResult createResult(Object value) {
return new TryResult.CellResult(value);
}

static CellError createError(String value) {
return new TryResult.CellError(value);
}


final class CellResult implements TryResult {

private final Object value;

private CellResult(Object value) {
this.value = value;
}

@Override
public boolean isResult() {
return true;
}

@Override
public boolean isError() {
return false;
}

@Override
public Object result() {
return value;
}

@Override
public String error() {
throw new NoSuchElementException("error() on CellResult");
}
}

final class CellError implements TryResult {

private final String value;

private CellError(String value) {
this.value = value;
}

@Override
public boolean isResult() {
return false;
}

@Override
public boolean isError() {
return true;
}

@Override
public Object result() {
throw new NoSuchElementException("result() on CellError");
}

@Override
public String error() {
return value;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.common.collect.Lists;
import com.twosigma.beakerx.DefaultJVMVariables;
import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.jvm.threads.CellExecutor;
import com.twosigma.beakerx.kernel.AddImportStatus;
import com.twosigma.beakerx.kernel.Classpath;
Expand All @@ -35,6 +36,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public abstract class BaseEvaluator implements Evaluator {

Expand Down Expand Up @@ -171,7 +173,7 @@ public void setShellOptions(final EvaluatorParameters kernelParameters) {
resetEnvironment();
}

public boolean executeTask(Runnable codeRunner) {
public TryResult executeTask(Callable<TryResult> codeRunner) {
return executor.executeTask(codeRunner);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.twosigma.beakerx.evaluator;

import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.kernel.AddImportStatus;
import com.twosigma.beakerx.kernel.Repos;
import java.io.IOException;
Expand Down Expand Up @@ -48,7 +49,7 @@ public interface Evaluator {

void cancelExecution();

void evaluate(SimpleEvaluationObject seo, String code);
TryResult evaluate(SimpleEvaluationObject seo, String code);

void exit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package com.twosigma.beakerx.evaluator;

import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.autocomplete.AutocompleteResult;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObjectWithTime;
import com.twosigma.beakerx.kernel.AddImportStatus;
import com.twosigma.beakerx.kernel.Classpath;
import com.twosigma.beakerx.kernel.ImportPath;
Expand All @@ -26,7 +26,6 @@
import com.twosigma.beakerx.kernel.EvaluatorParameters;
import com.twosigma.beakerx.kernel.PathToJar;
import com.twosigma.beakerx.kernel.Repos;
import com.twosigma.beakerx.message.Message;

import java.io.IOException;

Expand Down Expand Up @@ -68,39 +67,16 @@ public synchronized void killAllThreads() {
evaluator.killAllThreads();
}

public synchronized SimpleEvaluationObject executeCode(String code, SimpleEvaluationObject seo) {
public synchronized TryResult executeCode(String code, SimpleEvaluationObject seo) {
return execute(code, seo);
}

public synchronized SimpleEvaluationObjectWithTime executeCodeWithTimeMeasurement(String code, Message message,
int executionCount, KernelFunctionality.ExecuteCodeCallbackWithTime executeCodeCallbackWithTime) {
return executeWithTimeMeasurement(code, message, executionCount, executeCodeCallbackWithTime);
}

public void exit() {
evaluator.exit();
}

private SimpleEvaluationObject execute(String code, SimpleEvaluationObject seo) {
evaluator.evaluate(seo, code);
return seo;
}

private SimpleEvaluationObjectWithTime executeWithTimeMeasurement(String code, Message message, int executionCount,
KernelFunctionality.ExecuteCodeCallbackWithTime executeCodeCallbackWithTime) {
SimpleEvaluationObjectWithTime seowt = createSimpleEvaluationObjectWithTime(code, message, executionCount,
executeCodeCallbackWithTime);
evaluator.evaluate(seowt, code);
return seowt;
}


private SimpleEvaluationObjectWithTime createSimpleEvaluationObjectWithTime(String code, Message message,
int executionCount, KernelFunctionality.ExecuteCodeCallbackWithTime executeCodeCallbackWithTime) {
SimpleEvaluationObjectWithTime seowt = new SimpleEvaluationObjectWithTime(code, executeCodeCallbackWithTime);
seowt.setJupyterMessage(message);
seowt.setExecutionCount(executionCount);
return seowt;
private TryResult execute(String code, SimpleEvaluationObject seo) {
return evaluator.evaluate(seo, code);
}

public List<Path> addJarsToClasspath(List<PathToJar> paths) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,16 @@
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.twosigma.beakerx.kernel.KernelFunctionality;
import com.twosigma.beakerx.message.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Abstraction around an evaluation, for communication of the state over REST to the plugin.
*/
public class SimpleEvaluationObject extends Observable {

private final static Logger logger = LoggerFactory.getLogger(SimpleEvaluationObject.class.getName());
private final KernelFunctionality.ExecuteCodeCallback executeCodeCallback;

private Message jupyterMessage;
private int executionCount;
Expand All @@ -54,17 +50,6 @@ public class SimpleEvaluationObject extends Observable {
public SimpleEvaluationObject(String e) {
expression = e;
status = EvaluationStatus.QUEUED;
this.executeCodeCallback = null;
}

public SimpleEvaluationObject(String e, final KernelFunctionality.ExecuteCodeCallback executeCodeCallback) {
expression = e;
status = EvaluationStatus.QUEUED;
this.executeCodeCallback = checkNotNull(executeCodeCallback);
}

public void executeCodeCallback() {
this.executeCodeCallback.execute(this);
}

public boolean isShowResult() {
Expand Down

This file was deleted.

Loading

0 comments on commit 558a0ce

Please sign in to comment.