Search in sources :

Example 61 with Interpreter

use of org.apache.zeppelin.interpreter.Interpreter in project zeppelin by apache.

the class SparkIntegrationTest method testInterpreterBasics.

private void testInterpreterBasics() throws IOException, InterpreterException, XmlPullParserException {
    // add jars & packages for testing
    InterpreterSetting sparkInterpreterSetting = interpreterSettingManager.getInterpreterSettingByName("spark");
    sparkInterpreterSetting.setProperty("spark.jars.packages", "com.maxmind.geoip2:geoip2:2.5.0");
    sparkInterpreterSetting.setProperty("SPARK_PRINT_LAUNCH_COMMAND", "true");
    sparkInterpreterSetting.setProperty("zeppelin.python.gatewayserver_address", "127.0.0.1");
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model = reader.read(new FileReader("pom.xml"));
    sparkInterpreterSetting.setProperty("spark.jars", new File("target/zeppelin-interpreter-integration-" + model.getVersion() + ".jar").getAbsolutePath());
    // test SparkInterpreter
    Interpreter sparkInterpreter = interpreterFactory.getInterpreter("spark.spark", new ExecutionContext("user1", "note1", "test"));
    InterpreterContext context = new InterpreterContext.Builder().setNoteId("note1").setParagraphId("paragraph_1").build();
    InterpreterResult interpreterResult = sparkInterpreter.interpret("sc.version", context);
    assertEquals(interpreterResult.toString(), InterpreterResult.Code.SUCCESS, interpreterResult.code());
    String detectedSparkVersion = interpreterResult.message().get(0).getData();
    assertTrue(detectedSparkVersion + " doesn't contain " + this.sparkVersion, detectedSparkVersion.contains(this.sparkVersion));
    interpreterResult = sparkInterpreter.interpret("sc.range(1,10).sum()", context);
    assertEquals(interpreterResult.toString(), InterpreterResult.Code.SUCCESS, interpreterResult.code());
    assertTrue(interpreterResult.toString(), interpreterResult.message().get(0).getData().contains("45"));
    interpreterResult = sparkInterpreter.interpret("sc.getConf.get(\"spark.user.name\")", context);
    assertEquals(interpreterResult.toString(), InterpreterResult.Code.SUCCESS, interpreterResult.code());
    assertTrue(interpreterResult.toString(), interpreterResult.message().get(0).getData().contains("user1"));
    // test jars & packages can be loaded correctly
    interpreterResult = sparkInterpreter.interpret("import org.apache.zeppelin.interpreter.integration.DummyClass\n" + "import com.maxmind.geoip2._", context);
    assertEquals(interpreterResult.toString(), InterpreterResult.Code.SUCCESS, interpreterResult.code());
    // test PySparkInterpreter
    Interpreter pySparkInterpreter = interpreterFactory.getInterpreter("spark.pyspark", new ExecutionContext("user1", "note1", "test"));
    interpreterResult = pySparkInterpreter.interpret("sqlContext.createDataFrame([(1,'a'),(2,'b')], ['id','name']).registerTempTable('test')", context);
    assertEquals(interpreterResult.toString(), InterpreterResult.Code.SUCCESS, interpreterResult.code());
    // test IPySparkInterpreter
    Interpreter ipySparkInterpreter = interpreterFactory.getInterpreter("spark.ipyspark", new ExecutionContext("user1", "note1", "test"));
    interpreterResult = ipySparkInterpreter.interpret("sqlContext.table('test').show()", context);
    assertEquals(interpreterResult.toString(), InterpreterResult.Code.SUCCESS, interpreterResult.code());
    // test SparkSQLInterpreter
    Interpreter sqlInterpreter = interpreterFactory.getInterpreter("spark.sql", new ExecutionContext("user1", "note1", "test"));
    interpreterResult = sqlInterpreter.interpret("select count(1) as c from test", context);
    assertEquals(interpreterResult.toString(), InterpreterResult.Code.SUCCESS, interpreterResult.code());
    assertEquals(interpreterResult.toString(), InterpreterResult.Type.TABLE, interpreterResult.message().get(0).getType());
    assertEquals(interpreterResult.toString(), "c\n2\n", interpreterResult.message().get(0).getData());
    // test SparkRInterpreter
    Interpreter sparkrInterpreter = interpreterFactory.getInterpreter("spark.r", new ExecutionContext("user1", "note1", "test"));
    if (isSpark2() || isSpark3()) {
        interpreterResult = sparkrInterpreter.interpret("df <- as.DataFrame(faithful)\nhead(df)", context);
    } else {
        interpreterResult = sparkrInterpreter.interpret("df <- createDataFrame(sqlContext, faithful)\nhead(df)", context);
    }
    assertEquals(interpreterResult.toString(), InterpreterResult.Code.SUCCESS, interpreterResult.code());
    assertEquals(interpreterResult.toString(), InterpreterResult.Type.TEXT, interpreterResult.message().get(0).getType());
    assertTrue(interpreterResult.toString(), interpreterResult.message().get(0).getData().contains("eruptions waiting"));
}
Also used : Interpreter(org.apache.zeppelin.interpreter.Interpreter) ExecutionContext(org.apache.zeppelin.interpreter.ExecutionContext) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) Model(org.apache.maven.model.Model) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) FileReader(java.io.FileReader) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) File(java.io.File)

Example 62 with Interpreter

use of org.apache.zeppelin.interpreter.Interpreter in project zeppelin by apache.

the class SparkSubmitIntegrationTest method testYarnMode.

@Test
public void testYarnMode() throws InterpreterException, YarnException {
    try {
        // test SparkSubmitInterpreterSetting
        Interpreter sparkSubmitInterpreter = interpreterFactory.getInterpreter("spark-submit", new ExecutionContext("user1", "note1", "test"));
        InterpreterContext context = new InterpreterContext.Builder().setNoteId("note1").setParagraphId("paragraph_1").build();
        String yarnAppName = "yarn_example";
        InterpreterResult interpreterResult = sparkSubmitInterpreter.interpret("--master yarn-cluster --class org.apache.spark.examples.SparkPi " + "--conf spark.app.name=" + yarnAppName + " --conf spark.driver.memory=512m " + "--conf spark.executor.memory=512m " + sparkHome + "/examples/jars/spark-examples_2.11-2.4.7.jar", context);
        assertEquals(interpreterResult.toString(), InterpreterResult.Code.SUCCESS, interpreterResult.code());
        GetApplicationsRequest request = GetApplicationsRequest.newInstance(EnumSet.of(YarnApplicationState.FINISHED));
        GetApplicationsResponse response = hadoopCluster.getYarnCluster().getResourceManager().getClientRMService().getApplications(request);
        assertTrue(response.getApplicationList().size() >= 1);
        List<ApplicationReport> apps = response.getApplicationList().stream().filter(app -> app.getName().equals(yarnAppName)).collect(Collectors.toList());
        assertEquals(1, apps.size());
    } finally {
        interpreterSettingManager.close();
    }
}
Also used : ApplicationReport(org.apache.hadoop.yarn.api.records.ApplicationReport) GetApplicationsRequest(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) BeforeClass(org.junit.BeforeClass) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) Waiter(net.jodah.concurrentunit.Waiter) YarnException(org.apache.hadoop.yarn.exceptions.YarnException) ApplicationReport(org.apache.hadoop.yarn.api.records.ApplicationReport) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) InterpreterFactory(org.apache.zeppelin.interpreter.InterpreterFactory) ExecutionContext(org.apache.zeppelin.interpreter.ExecutionContext) DownloadUtils(org.apache.zeppelin.interpreter.integration.DownloadUtils) EnumSet(java.util.EnumSet) Interpreter(org.apache.zeppelin.interpreter.Interpreter) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InterpreterSettingManager(org.apache.zeppelin.interpreter.InterpreterSettingManager) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) List(java.util.List) GetApplicationsResponse(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsResponse) YarnApplicationState(org.apache.hadoop.yarn.api.records.YarnApplicationState) Assert.assertEquals(org.junit.Assert.assertEquals) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) Interpreter(org.apache.zeppelin.interpreter.Interpreter) ExecutionContext(org.apache.zeppelin.interpreter.ExecutionContext) GetApplicationsResponse(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsResponse) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) GetApplicationsRequest(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest) Test(org.junit.Test)

Example 63 with Interpreter

use of org.apache.zeppelin.interpreter.Interpreter in project zeppelin by apache.

the class SparkSubmitIntegrationTest method testLocalMode.

@Test
public void testLocalMode() throws InterpreterException, YarnException {
    try {
        // test SparkSubmitInterpreterSetting
        Interpreter sparkSubmitInterpreter = interpreterFactory.getInterpreter("spark-submit", new ExecutionContext("user1", "note1", "test"));
        InterpreterContext context = new InterpreterContext.Builder().setNoteId("note1").setParagraphId("paragraph_1").build();
        InterpreterResult interpreterResult = sparkSubmitInterpreter.interpret("--master yarn-cluster --class org.apache.spark.examples.SparkPi " + sparkHome + "/examples/jars/spark-examples_2.11-2.4.7.jar", context);
        assertEquals(interpreterResult.toString(), InterpreterResult.Code.SUCCESS, interpreterResult.code());
        // no yarn application launched
        GetApplicationsRequest request = GetApplicationsRequest.newInstance(EnumSet.of(YarnApplicationState.RUNNING));
        GetApplicationsResponse response = hadoopCluster.getYarnCluster().getResourceManager().getClientRMService().getApplications(request);
        assertEquals(0, response.getApplicationList().size());
    } finally {
        interpreterSettingManager.close();
    }
}
Also used : Interpreter(org.apache.zeppelin.interpreter.Interpreter) ExecutionContext(org.apache.zeppelin.interpreter.ExecutionContext) GetApplicationsResponse(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsResponse) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) GetApplicationsRequest(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest) Test(org.junit.Test)

Example 64 with Interpreter

use of org.apache.zeppelin.interpreter.Interpreter in project zeppelin by apache.

the class SparkSubmitIntegrationTest method testCancelSparkYarnApp.

@Test
public void testCancelSparkYarnApp() throws InterpreterException, YarnException, TimeoutException, InterruptedException {
    try {
        // test SparkSubmitInterpreterSetting
        Interpreter sparkSubmitInterpreter = interpreterFactory.getInterpreter("spark-submit", new ExecutionContext("user1", "note1", "test"));
        InterpreterContext context = new InterpreterContext.Builder().setNoteId("note1").setParagraphId("paragraph_1").build();
        final Waiter waiter = new Waiter();
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    String yarnAppName = "yarn_cancel_example";
                    InterpreterResult interpreterResult = sparkSubmitInterpreter.interpret("--master yarn-cluster --class org.apache.spark.examples.SparkPi " + "--conf spark.app.name=" + yarnAppName + " --conf spark.driver.memory=512m " + "--conf spark.executor.memory=512m " + sparkHome + "/examples/jars/spark-examples_2.11-2.4.7.jar", context);
                    assertEquals(interpreterResult.toString(), InterpreterResult.Code.INCOMPLETE, interpreterResult.code());
                    assertTrue(interpreterResult.toString(), interpreterResult.toString().contains("Paragraph received a SIGTERM"));
                } catch (InterpreterException e) {
                    waiter.fail("Should not throw exception\n" + ExceptionUtils.getStackTrace(e));
                }
                waiter.resume();
            }
        };
        thread.start();
        long start = System.currentTimeMillis();
        long threshold = 120 * 1000;
        while ((System.currentTimeMillis() - start) < threshold) {
            GetApplicationsRequest request = GetApplicationsRequest.newInstance(EnumSet.of(YarnApplicationState.RUNNING));
            GetApplicationsResponse response = hadoopCluster.getYarnCluster().getResourceManager().getClientRMService().getApplications(request);
            if (response.getApplicationList().size() >= 1) {
                break;
            }
            Thread.sleep(5 * 1000);
        }
        sparkSubmitInterpreter.cancel(context);
        waiter.await(10000);
    } finally {
        interpreterSettingManager.close();
    }
}
Also used : Interpreter(org.apache.zeppelin.interpreter.Interpreter) ExecutionContext(org.apache.zeppelin.interpreter.ExecutionContext) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) GetApplicationsResponse(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsResponse) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) Waiter(net.jodah.concurrentunit.Waiter) GetApplicationsRequest(org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest) Test(org.junit.Test)

Example 65 with Interpreter

use of org.apache.zeppelin.interpreter.Interpreter in project zeppelin by apache.

the class RemoteInterpreterTest method testSharedMode.

@Test
public void testSharedMode() throws InterpreterException, IOException {
    interpreterSetting.getOption().setPerUser(InterpreterOption.SHARED);
    Interpreter interpreter1 = interpreterSetting.getDefaultInterpreter("user1", note1Id);
    Interpreter interpreter2 = interpreterSetting.getDefaultInterpreter("user2", note1Id);
    assertTrue(interpreter1 instanceof RemoteInterpreter);
    RemoteInterpreter remoteInterpreter1 = (RemoteInterpreter) interpreter1;
    assertTrue(interpreter2 instanceof RemoteInterpreter);
    RemoteInterpreter remoteInterpreter2 = (RemoteInterpreter) interpreter2;
    assertEquals(remoteInterpreter1.getScheduler(), remoteInterpreter2.getScheduler());
    InterpreterContext context1 = createDummyInterpreterContext();
    assertEquals("hello", remoteInterpreter1.interpret("hello", context1).message().get(0).getData());
    assertEquals(Interpreter.FormType.NATIVE, interpreter1.getFormType());
    assertEquals(0, remoteInterpreter1.getProgress(context1));
    assertNotNull(remoteInterpreter1.getOrCreateInterpreterProcess());
    assertTrue(remoteInterpreter1.getInterpreterGroup().getRemoteInterpreterProcess().isRunning());
    assertEquals("hello", remoteInterpreter2.interpret("hello", context1).message().get(0).getData());
    assertEquals(remoteInterpreter1.getInterpreterGroup().getRemoteInterpreterProcess(), remoteInterpreter2.getInterpreterGroup().getRemoteInterpreterProcess());
    // Call InterpreterGroup.close instead of Interpreter.close, otherwise we will have the
    // RemoteInterpreterProcess leakage.
    remoteInterpreter1.getInterpreterGroup().close(remoteInterpreter1.getSessionId());
    assertNull(remoteInterpreter1.getInterpreterGroup().getRemoteInterpreterProcess());
    InterpreterResult result = remoteInterpreter1.interpret("hello", context1);
    assertEquals(Code.ERROR, result.code());
    assertEquals("Interpreter process is not running\n", result.message().get(0).getData());
}
Also used : Interpreter(org.apache.zeppelin.interpreter.Interpreter) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) AbstractInterpreterTest(org.apache.zeppelin.interpreter.AbstractInterpreterTest) Test(org.junit.Test)

Aggregations

Interpreter (org.apache.zeppelin.interpreter.Interpreter)85 Test (org.junit.Test)46 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)42 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)30 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)27 LazyOpenInterpreter (org.apache.zeppelin.interpreter.LazyOpenInterpreter)26 AbstractInterpreterTest (org.apache.zeppelin.interpreter.AbstractInterpreterTest)21 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)21 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)20 Properties (java.util.Properties)19 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)11 ExecutionContext (org.apache.zeppelin.interpreter.ExecutionContext)11 IOException (java.io.IOException)10 InterpreterOutput (org.apache.zeppelin.interpreter.InterpreterOutput)10 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)9 WrappedInterpreter (org.apache.zeppelin.interpreter.WrappedInterpreter)7 ArrayList (java.util.ArrayList)6 InterpreterNotFoundException (org.apache.zeppelin.interpreter.InterpreterNotFoundException)6 ManagedInterpreterGroup (org.apache.zeppelin.interpreter.ManagedInterpreterGroup)6 Before (org.junit.Before)6