Send para runtimeinfos via websocker, but dont persist in json

This commit is contained in:
Karup 2017-01-01 22:15:54 +05:30
parent 09fc0e223c
commit 19513a6604
3 changed files with 89 additions and 2 deletions

View file

@ -0,0 +1,68 @@
package org.apache.zeppelin.json;
import java.io.IOException;
import org.apache.zeppelin.socket.NotebookServer;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
/**
* Custom adapter type factory
* Modify the jsonObject before serailaization/deserialization
* Check sample implementation at {@link NotebookServer}
* @param <C> the type whose json is to be customized for serialization/deserialization
*/
public class NotebookTypeAdapterFactory<C> implements TypeAdapterFactory {
private final Class<C> customizedClass;
public NotebookTypeAdapterFactory(Class<C> customizedClass) {
this.customizedClass = customizedClass;
}
@SuppressWarnings("unchecked")
// we use a runtime check to guarantee that 'C' and 'T' are equal
public final <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return type.getRawType() == customizedClass ? (TypeAdapter<T>) customizeTypeAdapter(gson,
(TypeToken<C>) type) : null;
}
private TypeAdapter<C> customizeTypeAdapter(Gson gson, TypeToken<C> type) {
final TypeAdapter<C> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<C>() {
@Override
public void write(JsonWriter out, C value) throws IOException {
JsonElement tree = delegate.toJsonTree(value);
beforeWrite(value, tree);
elementAdapter.write(out, tree);
}
@Override
public C read(JsonReader in) throws IOException {
JsonElement tree = elementAdapter.read(in);
afterRead(tree);
return delegate.fromJsonTree(tree);
}
};
}
/**
* Override this to change {@code toSerialize} before it is written to the
* outgoing JSON stream.
*/
protected void beforeWrite(C source, JsonElement toSerialize) {
}
/**
* Override this to change {@code deserialized} before it parsed into the
* application type.
*/
protected void afterRead(JsonElement deserialized) {
}
}

View file

@ -55,6 +55,7 @@ import org.apache.zeppelin.interpreter.InterpreterSetting;
import org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry;
import org.apache.zeppelin.interpreter.remote.RemoteInterpreterProcessListener;
import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion;
import org.apache.zeppelin.json.NotebookTypeAdapterFactory;
import org.apache.zeppelin.notebook.JobListenerFactory;
import org.apache.zeppelin.notebook.Folder;
import org.apache.zeppelin.notebook.Note;
@ -63,6 +64,7 @@ import org.apache.zeppelin.notebook.NotebookAuthorization;
import org.apache.zeppelin.notebook.NotebookEventListener;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.notebook.ParagraphJobListener;
import org.apache.zeppelin.notebook.ParagraphRuntimeInfo;
import org.apache.zeppelin.notebook.repo.NotebookRepo.Revision;
import org.apache.zeppelin.notebook.socket.Message;
import org.apache.zeppelin.notebook.socket.Message.OP;
@ -87,6 +89,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Queues;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
/**
@ -114,7 +120,20 @@ public class NotebookServer extends WebSocketServlet
private static final Logger LOG = LoggerFactory.getLogger(NotebookServer.class);
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new NotebookTypeAdapterFactory<Paragraph>(Paragraph.class) {
@Override
protected void beforeWrite(Paragraph source, JsonElement toSerialize) {
Map<String, ParagraphRuntimeInfo> runtimeInfos = source.getRuntimeInfos();
if (runtimeInfos != null) {
JsonElement jsonTree = gson.toJsonTree(runtimeInfos);
if (toSerialize instanceof JsonObject) {
JsonObject jsonObj = (JsonObject) toSerialize;
jsonObj.add("runtimeInfos", jsonTree);
}
}
}
}).setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
final Map<String, List<NotebookSocket>> noteSocketMap = new HashMap<>();
final Queue<NotebookSocket> connectedSockets = new ConcurrentLinkedQueue<>();
final Map<String, Queue<NotebookSocket>> userConnectedSockets = new ConcurrentHashMap<>();

View file

@ -74,7 +74,7 @@ public class Paragraph extends Job implements Serializable, Cloneable {
private Map<String, ParagraphRuntimeInfos> runtimeInfos;
/**
* Applicaiton states in this paragraph
* Application states in this paragraph
*/
private final List<ApplicationState> apps = new LinkedList<>();