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

[ZEPPELIN-1069]Ignore implicit interpreter when user enter wrong interpreter name #1248

Closed
wants to merge 10 commits into from
Expand Up @@ -30,7 +30,6 @@
import java.util.concurrent.atomic.AtomicReference;

import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -56,6 +55,11 @@
import org.apache.zeppelin.user.AuthenticationInfo;
import org.apache.zeppelin.user.Credentials;

import static org.apache.commons.lang.StringUtils.EMPTY;
import static org.apache.commons.lang.StringUtils.isEmpty;
import static org.apache.commons.lang.StringUtils.isNotEmpty;
import static org.apache.commons.lang.StringUtils.isBlank;

/**
* Binded interpreters for a note
*/
Expand All @@ -76,7 +80,7 @@ public class Note implements Serializable, ParagraphJobListener {
private String name = "";
private String id;

private AtomicReference<String> lastReplName = new AtomicReference<>(StringUtils.EMPTY);
private AtomicReference<String> lastReplName = new AtomicReference<>(EMPTY);
private transient ZeppelinConfiguration conf = ZeppelinConfiguration.create();

private Map<String, List<AngularObject>> angularObjects = new HashMap<>();
Expand Down Expand Up @@ -122,7 +126,7 @@ private void generateId() {

private String getDefaultInterpreterName() {
InterpreterSetting setting = factory.getDefaultInterpreterSetting(getId());
return null != setting ? setting.getName() : StringUtils.EMPTY;
return null != setting ? setting.getName() : EMPTY;
}

void putDefaultReplName() {
Expand Down Expand Up @@ -275,13 +279,17 @@ public Paragraph insertParagraph(int index) {
*/
private void addLastReplNameIfEmptyText(Paragraph p) {
String replName = lastReplName.get();
if (StringUtils.isEmpty(p.getText()) && StringUtils.isNotEmpty(replName)) {
if (isEmpty(p.getText()) && isNotEmpty(replName) && isBinding(replName)) {
p.setText(getInterpreterName(replName) + " ");
}
}

public boolean isBinding(String replName) {
return factory.getInterpreter(this.getId(), replName) != null;
}

private String getInterpreterName(String replName) {
return StringUtils.isBlank(replName) ? StringUtils.EMPTY : "%" + replName;
return isBlank(replName) ? EMPTY : "%" + replName;
}

/**
Expand Down Expand Up @@ -569,7 +577,7 @@ public void persist(AuthenticationInfo subject) throws IOException {
}

private void setLastReplName(Paragraph lastParagraphStarted) {
if (StringUtils.isNotEmpty(lastParagraphStarted.getRequiredReplName())) {
if (isNotEmpty(lastParagraphStarted.getRequiredReplName())) {
lastReplName.set(lastParagraphStarted.getRequiredReplName());
}
}
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.apache.zeppelin.interpreter.Interpreter;
import org.apache.zeppelin.interpreter.InterpreterFactory;
import org.apache.zeppelin.interpreter.InterpreterSetting;
import org.apache.zeppelin.interpreter.mock.MockInterpreter2;
import org.apache.zeppelin.notebook.repo.NotebookRepo;
import org.apache.zeppelin.scheduler.Scheduler;
import org.apache.zeppelin.search.SearchService;
Expand Down Expand Up @@ -138,6 +139,7 @@ public void addParagraphWithLastReplName() {

Note note = new Note(repo, interpreterFactory, jobListenerFactory, index, credentials, noteEventListener);
note.putDefaultReplName(); //set lastReplName
when(interpreterFactory.getInterpreter(note.getId(), "spark")).thenReturn(new MockInterpreter2(null));

Paragraph p = note.addParagraph();

Expand All @@ -153,6 +155,7 @@ public void insertParagraphWithLastReplName() {

Note note = new Note(repo, interpreterFactory, jobListenerFactory, index, credentials, noteEventListener);
note.putDefaultReplName(); //set lastReplName
when(interpreterFactory.getInterpreter(note.getId(), "spark")).thenReturn(new MockInterpreter2(null));

Paragraph p = note.insertParagraph(note.getParagraphs().size());

Expand All @@ -171,4 +174,20 @@ public void setLastReplName() {

assertEquals("spark", note.getLastReplName());
}

@Test
public void isBindingTest() {
Note note = spy(new Note());
when(note.getId()).thenReturn("test1");
InterpreterFactory mockInterpreterFactory = mock(InterpreterFactory.class);
note.setInterpreterFactory(mockInterpreterFactory);

//when is not binding
assertFalse(note.isBinding("spark"));

//when is binding
when(mockInterpreterFactory.getInterpreter("test1", "spark")).
thenReturn(new MockInterpreter2(null));
assertTrue(note.isBinding("spark"));
}
}