From c0436a248ed67dab38630a5b6d437e723c9557dc Mon Sep 17 00:00:00 2001 From: Tinkoff DWH Date: Tue, 4 Apr 2017 11:42:54 +0500 Subject: [PATCH 1/4] [ZEPPELIN-2216] general solution to precode. refactoring jdbc precode --- .../img/screenshots/interpreter_precode.png | Bin 0 -> 8327 bytes docs/interpreter/jdbc.md | 2 +- docs/manual/interpreters.md | 8 +++++++ .../apache/zeppelin/jdbc/JDBCInterpreter.java | 22 ++++++++---------- .../zeppelin/jdbc/JDBCInterpreterTest.java | 14 +++++++---- .../zeppelin/interpreter/Interpreter.java | 13 +++++++++++ .../interpreter/LazyOpenInterpreter.java | 8 +++++-- .../remote/RemoteInterpreterServer.java | 15 ++++++++---- 8 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 docs/assets/themes/zeppelin/img/screenshots/interpreter_precode.png diff --git a/docs/assets/themes/zeppelin/img/screenshots/interpreter_precode.png b/docs/assets/themes/zeppelin/img/screenshots/interpreter_precode.png new file mode 100644 index 0000000000000000000000000000000000000000..61b79c3da07141f7dc71f4c799534bbf74382dcb GIT binary patch literal 8327 zcmaKScUV))w{~n4L8J&sRXPF&Y0}|H??~^VgET{LfuM&bUAlCU-g|FKkq!!kK&S>V zLLl_s?&h4|ANRT6ckh!YvuDqmS$p=(%B*+2D-o|X6d&BDy$=F`9w;lxX@fv_IDtBp z;5JZxVDZ=nE_ha|igKXqo6qNtqC}vD&{fIE0|X+XxcS}!rDr|{8u2}q)#dSj-Mx!P za1TkU#RdXB1u4tD()C4cEl8W9P}kBs9@25=FLVTw*X@)aeZCQw|+rr6$08Z>$=?Ro2_Le6_rvO;dkcfCY6mk8>c@?XJU=zBo@ zKZ1|GH$$H8-e~w&08(m?%ui}E6DLc|)-CAm9W#FM+>#jR^)*VRJ)8B{@R`cf(0{c4 zJN5!EA~lJmHEgbdzTctkdpqBIQs&*UBQR9(4?%P-@v8)He&}KbtOFhXuplodSgjlFEh7Db_tW46eAXgJb1u?kJ)Zv2~+Ppq8bKoQ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Hn|I+nX;=TriM@^;*5L*?DixFSKh9`SP<>Gvv)L zvd3?=3&9CwqX`{Wx*kyIO#-e1IE1l5fBwms|C=J`9b#E>ybB0U0+%EITb}&?n?V2H zGVIHPXZwrW65`^r$H3_e((QGd@5i&C1gZo#{{`5JxyD!S=V$TMVJQaMK+5tOa+NX` GfBzSnT1LSD literal 0 HcmV?d00001 diff --git a/docs/interpreter/jdbc.md b/docs/interpreter/jdbc.md index 28403fc92fd..ac32173ba67 100644 --- a/docs/interpreter/jdbc.md +++ b/docs/interpreter/jdbc.md @@ -121,7 +121,7 @@ The JDBC interpreter properties are defined by default like below. default.precode - Some SQL which executes while opening connection + Some SQL which executes every time after initialization of the interpreter (see [Binding mode](../manual/interpreters.md#interpreter-binding-mode)) diff --git a/docs/manual/interpreters.md b/docs/manual/interpreters.md index 916d5911a39..51b49910a77 100644 --- a/docs/manual/interpreters.md +++ b/docs/manual/interpreters.md @@ -113,3 +113,11 @@ interpreter.start() The above code will start interpreter thread inside your process. Once the interpreter is started you can configure zeppelin to connect to RemoteInterpreter by checking **Connect to existing process** checkbox and then provide **Host** and **Port** on which interpreter process is listening as shown in the image below: + +## Precode + +Snippet of code (language of interpreter) that executes after initialization of the interpreter depends on [Binding mode](#interpreter-binding-mode). To configure add parameter with class of interpreter (`zeppelin..precode`) except JDBCInterpreter ([JDBC precode](../interpreter/jdbc.md#usage-precode)). + + + + diff --git a/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java b/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java index 1080c2bb7cd..23b47439bf0 100644 --- a/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java +++ b/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java @@ -343,9 +343,6 @@ private Connection getConnectionFromPool(String url, String user, String propert if (!getJDBCConfiguration(user).isConnectionInDBDriverPool(propertyKey)) { createConnectionPool(url, user, propertyKey, properties); - try (Connection connection = DriverManager.getConnection(jdbcDriver)) { - executePrecode(connection, propertyKey); - } } return DriverManager.getConnection(jdbcDriver); } @@ -550,18 +547,19 @@ protected ArrayList splitSqlQueries(String sql) { return queries; } - private void executePrecode(Connection connection, String propertyKey) throws SQLException { - String precode = getProperty(String.format(PRECODE_KEY_TEMPLATE, propertyKey)); - if (StringUtils.isNotBlank(precode)) { - precode = StringUtils.trim(precode); - logger.info("Run SQL precode '{}'", precode); - try (Statement statement = connection.createStatement()) { - statement.execute(precode); - if (!connection.getAutoCommit()) { - connection.commit(); + public InterpreterResult executePrecode(InterpreterContext interpreterContext) { + InterpreterResult interpreterResult = null; + for (String propertyKey : basePropretiesMap.keySet()) { + String precode = getProperty(String.format("%s.precode", propertyKey)); + if (StringUtils.isNotBlank(precode)) { + interpreterResult = executeSql(propertyKey, precode, interpreterContext); + if (interpreterResult.code() != Code.SUCCESS) { + break; } } } + + return interpreterResult; } private InterpreterResult executeSql(String propertyKey, String sql, diff --git a/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java b/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java index 2e7e1a5116e..09e0ad4b257 100644 --- a/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java +++ b/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java @@ -400,6 +400,7 @@ public void testPrecode() throws SQLException, IOException { properties.setProperty(DEFAULT_PRECODE, "SET @testVariable=1"); JDBCInterpreter jdbcInterpreter = new JDBCInterpreter(properties); jdbcInterpreter.open(); + jdbcInterpreter.executePrecode(interpreterContext); String sqlQuery = "select @testVariable"; @@ -417,13 +418,15 @@ public void testIncorrectPrecode() throws SQLException, IOException { properties.setProperty("default.url", getJdbcConnection()); properties.setProperty("default.user", ""); properties.setProperty("default.password", ""); - properties.setProperty(DEFAULT_PRECODE, "incorrect command"); + properties.setProperty(DEFAULT_PRECODE, "select 1"); + properties.setProperty("incorrect.driver", "org.h2.Driver"); + properties.setProperty("incorrect.url", getJdbcConnection()); + properties.setProperty("incorrect.user", ""); + properties.setProperty("incorrect.password", ""); + properties.setProperty(String.format(PRECODE_KEY_TEMPLATE, "incorrect"), "incorrect command"); JDBCInterpreter jdbcInterpreter = new JDBCInterpreter(properties); jdbcInterpreter.open(); - - String sqlQuery = "select 1"; - - InterpreterResult interpreterResult = jdbcInterpreter.interpret(sqlQuery, interpreterContext); + InterpreterResult interpreterResult = jdbcInterpreter.executePrecode(interpreterContext); assertEquals(InterpreterResult.Code.ERROR, interpreterResult.code()); assertEquals(InterpreterResult.Type.TEXT, interpreterResult.message().get(0).getType()); @@ -439,6 +442,7 @@ public void testPrecodeWithAnotherPrefix() throws SQLException, IOException { properties.setProperty(String.format(PRECODE_KEY_TEMPLATE, "anotherPrefix"), "SET @testVariable=2"); JDBCInterpreter jdbcInterpreter = new JDBCInterpreter(properties); jdbcInterpreter.open(); + jdbcInterpreter.executePrecode(interpreterContext); String sqlQuery = "(anotherPrefix) select @testVariable"; diff --git a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/Interpreter.java b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/Interpreter.java index b64530a1af8..d6f024d272e 100644 --- a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/Interpreter.java +++ b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/Interpreter.java @@ -64,6 +64,19 @@ public abstract class Interpreter { @ZeppelinApi public abstract void close(); + /** + * Run precode if exists. + */ + @ZeppelinApi + public InterpreterResult executePrecode(InterpreterContext interpreterContext) { + String simpleName = this.getClass().getSimpleName(); + String precode = getProperty(String.format("zeppelin.%s.precode", simpleName)); + if (StringUtils.isNotBlank(precode)) { + return interpret(precode, interpreterContext); + } + return null; + } + /** * Run code and return result, in synchronous way. * diff --git a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/LazyOpenInterpreter.java b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/LazyOpenInterpreter.java index ad85ded1bb3..4159746b450 100644 --- a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/LazyOpenInterpreter.java +++ b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/LazyOpenInterpreter.java @@ -21,7 +21,6 @@ import java.util.List; import java.util.Properties; -import org.apache.zeppelin.interpreter.remote.RemoteInterpreter; import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion; import org.apache.zeppelin.scheduler.Scheduler; @@ -73,6 +72,11 @@ public synchronized void open() { } } + @Override + public InterpreterResult executePrecode(InterpreterContext interpreterContext) { + return intp.executePrecode(interpreterContext); + } + @Override public void close() { synchronized (intp) { @@ -151,7 +155,7 @@ public void setInterpreterGroup(InterpreterGroup interpreterGroup) { public void setClassloaderUrls(URL [] urls) { intp.setClassloaderUrls(urls); } - + @Override public void registerHook(String noteId, String event, String cmd) { intp.registerHook(noteId, event, cmd); diff --git a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterServer.java b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterServer.java index effaf85ee86..040573696cd 100644 --- a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterServer.java +++ b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterServer.java @@ -481,19 +481,24 @@ protected Object jobRun() throws Throwable { try { InterpreterContext.set(context); + InterpreterResult result = null; + // Open the interpreter instance prior to calling interpret(). // This is necessary because the earliest we can register a hook // is from within the open() method. LazyOpenInterpreter lazy = (LazyOpenInterpreter) interpreter; if (!lazy.isOpen()) { lazy.open(); + result = lazy.executePrecode(context); } - // Add hooks to script from registry. - // Global scope first, followed by notebook scope - processInterpreterHooks(null); - processInterpreterHooks(context.getNoteId()); - InterpreterResult result = interpreter.interpret(script, context); + if (result == null || result.code() == Code.SUCCESS) { + // Add hooks to script from registry. + // Global scope first, followed by notebook scope + processInterpreterHooks(null); + processInterpreterHooks(context.getNoteId()); + result = interpreter.interpret(script, context); + } // data from context.out is prepended to InterpreterResult if both defined context.out.flush(); From 5a482a0221d32fec2b671e1a0f197a35735a3a12 Mon Sep 17 00:00:00 2001 From: Tinkoff DWH Date: Mon, 24 Apr 2017 11:49:51 +0500 Subject: [PATCH 2/4] [ZEPPELIN-2216] fix tests --- .../apache/zeppelin/jdbc/JDBCInterpreterTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java b/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java index 1500243c4e6..7c2eef39b07 100644 --- a/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java +++ b/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java @@ -400,18 +400,18 @@ public void testPrecode() throws SQLException, IOException { properties.setProperty("default.url", getJdbcConnection()); properties.setProperty("default.user", ""); properties.setProperty("default.password", ""); - properties.setProperty(DEFAULT_PRECODE, "SET @testVariable=1"); + properties.setProperty(DEFAULT_PRECODE, "create table test_precode (id int); insert into test_precode values (1);"); JDBCInterpreter jdbcInterpreter = new JDBCInterpreter(properties); jdbcInterpreter.open(); jdbcInterpreter.executePrecode(interpreterContext); - String sqlQuery = "select @testVariable"; + String sqlQuery = "select *from test_precode"; InterpreterResult interpreterResult = jdbcInterpreter.interpret(sqlQuery, interpreterContext); assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code()); assertEquals(InterpreterResult.Type.TABLE, interpreterResult.message().get(0).getType()); - assertEquals("@TESTVARIABLE\n1\n", interpreterResult.message().get(0).getData()); + assertEquals("ID\n1\n", interpreterResult.message().get(0).getData()); } @Test @@ -442,18 +442,18 @@ public void testPrecodeWithAnotherPrefix() throws SQLException, IOException { properties.setProperty("anotherPrefix.url", getJdbcConnection()); properties.setProperty("anotherPrefix.user", ""); properties.setProperty("anotherPrefix.password", ""); - properties.setProperty(String.format(PRECODE_KEY_TEMPLATE, "anotherPrefix"), "SET @testVariable=2"); + properties.setProperty(String.format(PRECODE_KEY_TEMPLATE, "anotherPrefix"), "create table test_precode_2 (id int); insert into test_precode_2 values (2);"); JDBCInterpreter jdbcInterpreter = new JDBCInterpreter(properties); jdbcInterpreter.open(); jdbcInterpreter.executePrecode(interpreterContext); - String sqlQuery = "(anotherPrefix) select @testVariable"; + String sqlQuery = "(anotherPrefix) select *from test_precode_2"; InterpreterResult interpreterResult = jdbcInterpreter.interpret(sqlQuery, interpreterContext); assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code()); assertEquals(InterpreterResult.Type.TABLE, interpreterResult.message().get(0).getType()); - assertEquals("@TESTVARIABLE\n2\n", interpreterResult.message().get(0).getData()); + assertEquals("ID\n2\n", interpreterResult.message().get(0).getData()); } @Test From e4cf72f135b130f7d22a16d8876de48b1f19da74 Mon Sep 17 00:00:00 2001 From: Tinkoff DWH Date: Mon, 24 Apr 2017 12:05:45 +0500 Subject: [PATCH 3/4] [ZEPPELIN-2216] added tests --- .../remote/RemoteInterpreterTest.java | 75 ++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/zeppelin-interpreter/src/test/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterTest.java b/zeppelin-interpreter/src/test/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterTest.java index 2914bb4945d..e7d472b854e 100644 --- a/zeppelin-interpreter/src/test/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterTest.java +++ b/zeppelin-interpreter/src/test/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterTest.java @@ -58,7 +58,7 @@ public class RemoteInterpreterTest { private static final String INTERPRETER_SCRIPT = System.getProperty("os.name").startsWith("Windows") ? "../bin/interpreter.cmd" : - "../bin/interpreter.sh"; + "bin/interpreter.sh"; private InterpreterGroup intpGroup; private HashMap env; @@ -170,6 +170,79 @@ public void testRemoteInterperterCall() throws TTransportException, IOException } + @Test + public void testExecuteIncorrectPrecode() throws TTransportException, IOException { + Properties p = new Properties(); + p.put("zeppelin.MockInterpreterA.precode", "fail test"); + intpGroup.put("note", new LinkedList()); + + RemoteInterpreter intpA = createMockInterpreterA(p); + + intpGroup.get("note").add(intpA); + + intpA.setInterpreterGroup(intpGroup); + + RemoteInterpreterProcess process = intpA.getInterpreterProcess(); + + intpA.open(); + + InterpreterResult result = intpA.interpret("1", + new InterpreterContext( + "note", + "id", + null, + "title", + "text", + new AuthenticationInfo(), + new HashMap(), + new GUI(), + new AngularObjectRegistry(intpGroup.getId(), null), + new LocalResourcePool("pool1"), + new LinkedList(), null)); + + + + intpA.close(); + assertEquals(Code.ERROR, result.code()); + } + + @Test + public void testExecuteCorrectPrecode() throws TTransportException, IOException { + Properties p = new Properties(); + p.put("zeppelin.MockInterpreterA.precode", "2"); + intpGroup.put("note", new LinkedList()); + + RemoteInterpreter intpA = createMockInterpreterA(p); + + intpGroup.get("note").add(intpA); + + intpA.setInterpreterGroup(intpGroup); + + RemoteInterpreterProcess process = intpA.getInterpreterProcess(); + + intpA.open(); + + InterpreterResult result = intpA.interpret("1", + new InterpreterContext( + "note", + "id", + null, + "title", + "text", + new AuthenticationInfo(), + new HashMap(), + new GUI(), + new AngularObjectRegistry(intpGroup.getId(), null), + new LocalResourcePool("pool1"), + new LinkedList(), null)); + + + + intpA.close(); + assertEquals(Code.SUCCESS, result.code()); + assertEquals("1", result.message().get(0).getData()); + } + @Test public void testRemoteInterperterErrorStatus() throws TTransportException, IOException { Properties p = new Properties(); From 1e3f3f717c9838ecb5abac56f252b60201d197b5 Mon Sep 17 00:00:00 2001 From: Tinkoff DWH Date: Mon, 24 Apr 2017 12:18:55 +0500 Subject: [PATCH 4/4] [ZEPPELIN-2216] fix path --- .../zeppelin/interpreter/remote/RemoteInterpreterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zeppelin-interpreter/src/test/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterTest.java b/zeppelin-interpreter/src/test/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterTest.java index e7d472b854e..ffcb8d5175c 100644 --- a/zeppelin-interpreter/src/test/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterTest.java +++ b/zeppelin-interpreter/src/test/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterTest.java @@ -58,7 +58,7 @@ public class RemoteInterpreterTest { private static final String INTERPRETER_SCRIPT = System.getProperty("os.name").startsWith("Windows") ? "../bin/interpreter.cmd" : - "bin/interpreter.sh"; + "../bin/interpreter.sh"; private InterpreterGroup intpGroup; private HashMap env;