Skip to content

Commit

Permalink
#5039: add autotranslation service interface and NamespaceClient refa…
Browse files Browse the repository at this point in the history
…ctoring
  • Loading branch information
jaroslawmalekcodete committed Jun 13, 2018
1 parent 91db8b9 commit b0373eb
Show file tree
Hide file tree
Showing 59 changed files with 423 additions and 282 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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;

public interface AutotranslationService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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;

public class AutotranslationServiceImpl implements AutotranslationService {
}
36 changes: 36 additions & 0 deletions kernel/base/src/main/java/com/twosigma/beakerx/BeakerxClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.List;
import java.util.concurrent.SynchronousQueue;

public interface BeakerxClient {

void showProgressUpdate(String message, int progress);

void delBeaker();

Object set(String name, Object value);

Object get(final String name);

SynchronousQueue<Object> getMessageQueue(String channel);

List<CodeCell> getCodeCells(String tagFilter);

void runByTag(String tag);
}
115 changes: 53 additions & 62 deletions kernel/base/src/main/java/com/twosigma/beakerx/NamespaceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,79 +32,63 @@
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.SynchronousQueue;

import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_ENUMS_USING_TO_STRING;
import static com.twosigma.beakerx.kernel.msg.JupyterMessages.COMM_MSG;
import static com.twosigma.beakerx.util.Preconditions.checkNotNull;

public class NamespaceClient {
public class NamespaceClient implements BeakerxClient {

private static Map<String, NamespaceClient> nsClients = new ConcurrentHashMap<>();
private static String currentSession;
private static Map<String, SynchronousQueue<Object>> messagePool = new HashMap<>();
private ObjectMapper objectMapper;
private BeakerObjectConverter objectSerializer;
private SimpleEvaluationObject currentCeo = null;
private Comm autotranslationComm = null;
private Comm codeCellsComm = null;
private Comm tagRunComm = null;
private String session;
private AutotranslationService autotranslationService;

public NamespaceClient() {
public NamespaceClient(String session, AutotranslationService autotranslationService) {
this.session = checkNotNull(session);
this.autotranslationService = autotranslationService;
SimpleModule module = TableDisplayToJson.tableDisplayModule();
objectMapper = new ObjectMapper();
objectMapper.enable(WRITE_ENUMS_USING_TO_STRING);
objectMapper.registerModule(module);
objectSerializer = new BasicObjectSerializer();
}

@Override
public synchronized void showProgressUpdate(String message, int progress) {
SimpleEvaluationObject seo = InternalVariable.getSimpleEvaluationObject();
seo.structuredUpdate(message, progress);
}

public SimpleEvaluationObject getOutputObj() {
return currentCeo;
}

public synchronized void setOutputObj(SimpleEvaluationObject input) {
currentCeo = input;
}

public synchronized static NamespaceClient getBeaker() {
if (currentSession != null) {
return nsClients.get(currentSession);
}
return null;
}

public synchronized static NamespaceClient getBeaker(String session) {
currentSession = session;
if (!nsClients.containsKey(session)) {
nsClients.put(session, new NamespaceClient());
@Override
public synchronized void delBeaker() {
//clear autotranslation
}

@Override
public synchronized Object set(String name, Object value) {
try {
Comm c = getAutotranslationComm();
HashMap<String, Serializable> data = new HashMap<>();
HashMap<String, Serializable> state = new HashMap<>();
state.put("name", name);
state.put("value", getJson(value));
state.put("sync", true);
data.put("state", state);
data.put("buffer_paths", new HashMap<>());
c.send(COMM_MSG, Comm.Buffer.EMPTY, new Comm.Data(data));
return value;
} catch (Exception e) {
throw new RuntimeException(e);
}
return nsClients.get(currentSession);
}

public synchronized static void delBeaker(String sessionId) {
nsClients.remove(sessionId);
currentSession = null;
}

public synchronized Object set(String name, Object value) throws IOException {
Comm c = getAutotranslationComm();
HashMap<String, Serializable> data = new HashMap<>();
HashMap<String, Serializable> state = new HashMap<>();
state.put("name", name);
state.put("value", getJson(value));
state.put("sync", true);
data.put("state", state);
data.put("buffer_paths", new HashMap<>());
c.send(COMM_MSG, Comm.Buffer.EMPTY, new Comm.Data(data));
return value;
}

protected String getJson(Object value) throws IOException {
private String getJson(Object value) throws IOException {
StringWriter sw = new StringWriter();
JsonGenerator jgen = objectMapper.getFactory().createGenerator(sw);
objectSerializer.writeObject(value, jgen, true);
Expand All @@ -124,11 +108,13 @@ public Object unset(String name) {
}

//TODO : Not Implemented
@Override
public synchronized Object get(final String name) {
throw new RuntimeException("This option is not implemented now");
}

public static SynchronousQueue<Object> getMessageQueue(String channel) {
@Override
public SynchronousQueue<Object> getMessageQueue(String channel) {
SynchronousQueue<Object> result = messagePool.get(channel);
if (result == null) {
result = new SynchronousQueue<Object>();
Expand All @@ -137,46 +123,51 @@ public static SynchronousQueue<Object> getMessageQueue(String channel) {
return result;
}

protected Comm getAutotranslationComm() {
private Comm getAutotranslationComm() {
if (autotranslationComm == null) {
autotranslationComm = new Comm(TargetNamesEnum.BEAKER_AUTOTRANSLATION);
autotranslationComm.open();
}
return autotranslationComm;
}

protected Comm getCodeCellsComm() {
private Comm getCodeCellsComm() {
if (codeCellsComm == null) {
codeCellsComm = new Comm(TargetNamesEnum.BEAKER_GETCODECELLS);
codeCellsComm.open();
}
return codeCellsComm;
}

protected Comm getTagRunComm() {
private Comm getTagRunComm() {
if (tagRunComm == null) {
tagRunComm = new Comm(TargetNamesEnum.BEAKER_TAG_RUN);
tagRunComm.open();
}
return tagRunComm;
}


public List<CodeCell> getCodeCells(String tagFilter) throws IOException, InterruptedException {
@Override
public List<CodeCell> getCodeCells(String tagFilter) {
// first send message to get cells
Comm c = getCodeCellsComm();
HashMap<String, Serializable> data = new HashMap<>();
HashMap<String, Serializable> state = new HashMap<>();
state.put("name", "CodeCells");
state.put("value", getJson(tagFilter));
data.put("state", state);
data.put("buffer_paths", new HashMap<>());
c.send(COMM_MSG, Comm.Buffer.EMPTY, new Comm.Data(data));
// block
Object cells = getMessageQueue("CodeCells").take();
return (List<CodeCell>) cells;
try {
Comm c = getCodeCellsComm();
HashMap<String, Serializable> data = new HashMap<>();
HashMap<String, Serializable> state = new HashMap<>();
state.put("name", "CodeCells");
state.put("value", getJson(tagFilter));
data.put("state", state);
data.put("buffer_paths", new HashMap<>());
c.send(COMM_MSG, Comm.Buffer.EMPTY, new Comm.Data(data));
// block
Object cells = getMessageQueue("CodeCells").take();
return (List<CodeCell>) cells;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public synchronized void runByTag(String tag) {
Comm c = getTagRunComm();
HashMap<String, Serializable> data = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package com.twosigma.beakerx.chart;

import com.twosigma.beakerx.kernel.KernelManager;
import com.twosigma.beakerx.widget.CommActions;
import org.apache.commons.lang3.StringUtils;

import com.twosigma.beakerx.NamespaceClient;
import com.twosigma.beakerx.chart.actions.CategoryGraphicsActionObject;
import com.twosigma.beakerx.chart.actions.CombinedPlotActionObject;
import com.twosigma.beakerx.chart.actions.GraphicsActionObject;
Expand Down Expand Up @@ -90,9 +90,9 @@ protected void onActionDetails(HashMap content, Message message) {
info.setGraphics(g);
updateDetails(info);
if (CommActions.ONCLICK.equals(info.getActionType())) {
NamespaceClient.getBeaker().runByTag(info.getTag());
KernelManager.get().getBeakerx().runByTag(info.getTag());
} else if (CommActions.ONKEY.equals(info.getActionType())) {
NamespaceClient.getBeaker().runByTag(info.getTag());
KernelManager.get().getBeakerx().runByTag(info.getTag());
}
}

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

import com.twosigma.beakerx.BeakerxClient;
import com.twosigma.beakerx.DefaultJVMVariables;
import com.twosigma.beakerx.inspect.Inspect;
import com.twosigma.beakerx.inspect.InspectResult;
import com.twosigma.beakerx.NamespaceClient;
import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.jvm.threads.CellExecutor;
Expand Down Expand Up @@ -54,16 +54,23 @@ public abstract class BaseEvaluator implements Evaluator {
protected Imports imports;
private final CellExecutor executor;
private Path tempFolder;
private BeakerxClient beakerxClient;
protected EvaluatorParameters evaluatorParameters;
private EvaluatorHooks cancelHooks = new EvaluatorHooks();

protected ExecutorService executorService;

public BaseEvaluator(String id, String sId, CellExecutor cellExecutor, TempFolderFactory tempFolderFactory, EvaluatorParameters evaluatorParameters) {
public BaseEvaluator(String id,
String sId,
CellExecutor cellExecutor,
TempFolderFactory tempFolderFactory,
EvaluatorParameters evaluatorParameters,
BeakerxClient beakerxClient) {
shellId = id;
sessionId = sId;
executor = cellExecutor;
tempFolder = tempFolderFactory.createTempFolder();
this.beakerxClient = beakerxClient;
outDir = getOrCreateFile(tempFolder.toString() + File.separator + "outDir").getPath();
classPath = new Classpath();
classPath.add(new PathToJar(outDir));
Expand Down Expand Up @@ -100,6 +107,11 @@ public ClassLoader getClassLoaderForImport() {
return getClassLoader();
}

@Override
public BeakerxClient getBeakerx() {
return beakerxClient;
}

@Override
public List<Path> addJarsToClasspath(List<PathToJar> paths) {
LinkedList<Path> addedPaths = new LinkedList<>();
Expand Down Expand Up @@ -227,7 +239,7 @@ public Path getTempFolder() {

@Override
public void exit() {
NamespaceClient.delBeaker(getSessionId());
beakerxClient.delBeaker();
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.BeakerxClient;
import com.twosigma.beakerx.inspect.InspectResult;
import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.kernel.AddImportStatus;
Expand Down Expand Up @@ -74,4 +75,6 @@ public interface Evaluator {
String getOutDir();

void registerCancelHook(Hook hook);

BeakerxClient getBeakerx();
}
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.BeakerxClient;
import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.autocomplete.AutocompleteResult;
import com.twosigma.beakerx.inspect.InspectResult;
Expand Down Expand Up @@ -118,4 +119,8 @@ public String getOutDir() {
public void registerCancelHook(Hook hook) {
evaluator.registerCancelHook(hook);
}

public BeakerxClient getBeakerx() {
return evaluator.getBeakerx();
}
}
Loading

0 comments on commit b0373eb

Please sign in to comment.