Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jarek/6560: handle arbitrary combinantion of code and magic #6717

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*~
*.pyc
*.iml
.idea/
.metadata/
.recommenders/
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,8 @@

import com.google.common.collect.Lists;
import com.twosigma.beakerx.DefaultJVMVariables;
import com.twosigma.beakerx.NamespaceClient;
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 +37,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 +174,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 Expand Up @@ -206,6 +209,7 @@ public Path getTempFolder() {

@Override
public void exit() {
NamespaceClient.delBeaker(getSessionId());
removeTempFolder();
}

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,51 +67,16 @@ public synchronized void killAllThreads() {
evaluator.killAllThreads();
}

public synchronized SimpleEvaluationObject executeCode(String code, Message message,
int executionCount, KernelFunctionality.ExecuteCodeCallback executeCodeCallback) {
return execute(code, message, executionCount, executeCodeCallback);
}

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

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

private SimpleEvaluationObject execute(String code, Message message, int executionCount,
KernelFunctionality.ExecuteCodeCallback executeCodeCallback) {
SimpleEvaluationObject seo = createSimpleEvaluationObject(code, message, executionCount,
executeCodeCallback);
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 SimpleEvaluationObject createSimpleEvaluationObject(String code, Message message,
int executionCount, KernelFunctionality.ExecuteCodeCallback executeCodeCallback) {
SimpleEvaluationObject seo = new SimpleEvaluationObject(code, executeCodeCallback);
seo.setJupyterMessage(message);
seo.setExecutionCount(executionCount);
seo.addObserver(kernel.getExecutionResultSender());
return seo;
}

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
Loading