You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@samza.apache.org by ca...@apache.org on 2021/04/28 18:22:16 UTC
[samza] branch master updated: SAMZA-2650: [Scala cleanup] Convert
ShellCommandBuilder from scala to java (#1496)
This is an automated email from the ASF dual-hosted git repository.
cameronlee pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/samza.git
The following commit(s) were added to refs/heads/master by this push:
new 33bc5be SAMZA-2650: [Scala cleanup] Convert ShellCommandBuilder from scala to java (#1496)
33bc5be is described below
commit 33bc5be6a5c339d2953a9053eaca3dc178a1be40
Author: Cameron Lee <ca...@linkedin.com>
AuthorDate: Wed Apr 28 11:22:08 2021 -0700
SAMZA-2650: [Scala cleanup] Convert ShellCommandBuilder from scala to java (#1496)
Issues: For Samza on Kubernetes, we might need to update/extend this class, so converting to Java in order to reduce the Scala touchpoints.
Changes:
1. Converted ShellCommandBuilder to Java
2. Updated unit tests for ShellCommandBuilder to add some more code coverage
API changes and usage/upgrade instructions: N/A
---
.../org/apache/samza/job/ShellCommandBuilder.java | 52 +++++++++++++
.../org/apache/samza/job/ShellCommandBuilder.scala | 54 -------------
.../apache/samza/job/TestShellCommandBuilder.java | 90 ++++++++++++++++++++++
.../apache/samza/job/TestShellCommandBuilder.scala | 61 ---------------
4 files changed, 142 insertions(+), 115 deletions(-)
diff --git a/samza-core/src/main/java/org/apache/samza/job/ShellCommandBuilder.java b/samza-core/src/main/java/org/apache/samza/job/ShellCommandBuilder.java
new file mode 100644
index 0000000..3725344
--- /dev/null
+++ b/samza-core/src/main/java/org/apache/samza/job/ShellCommandBuilder.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.samza.job;
+
+import java.io.File;
+import java.util.Map;
+import com.google.common.collect.ImmutableMap;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.samza.config.ShellCommandConfig;
+
+
+public class ShellCommandBuilder extends CommandBuilder {
+ @Override
+ public String buildCommand() {
+ ShellCommandConfig shellCommandConfig = new ShellCommandConfig(config);
+ if (StringUtils.isEmpty(this.commandPath)) {
+ return shellCommandConfig.getCommand();
+ } else {
+ return this.commandPath + File.separator + shellCommandConfig.getCommand();
+ }
+ }
+
+ @Override
+ public Map<String, String> buildEnvironment() {
+ ShellCommandConfig shellCommandConfig = new ShellCommandConfig(config);
+ ImmutableMap.Builder<String, String> envBuilder = new ImmutableMap.Builder<>();
+ envBuilder.put(ShellCommandConfig.ENV_CONTAINER_ID, this.id);
+ envBuilder.put(ShellCommandConfig.ENV_COORDINATOR_URL, this.url.toString());
+ envBuilder.put(ShellCommandConfig.ENV_JAVA_OPTS, shellCommandConfig.getTaskOpts().orElse(""));
+ envBuilder.put(ShellCommandConfig.ENV_ADDITIONAL_CLASSPATH_DIR,
+ shellCommandConfig.getAdditionalClasspathDir().orElse(""));
+ shellCommandConfig.getJavaHome().ifPresent(javaHome -> envBuilder.put(ShellCommandConfig.ENV_JAVA_HOME, javaHome));
+ return envBuilder.build();
+ }
+}
diff --git a/samza-core/src/main/scala/org/apache/samza/job/ShellCommandBuilder.scala b/samza-core/src/main/scala/org/apache/samza/job/ShellCommandBuilder.scala
deleted file mode 100644
index 9b95648..0000000
--- a/samza-core/src/main/scala/org/apache/samza/job/ShellCommandBuilder.scala
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 org.apache.samza.job
-
-
-import java.io.File
-
-import org.apache.samza.config.ShellCommandConfig
-import org.apache.samza.util.ScalaJavaUtil.JavaOptionals
-
-import scala.collection.JavaConverters._
-
-class ShellCommandBuilder extends CommandBuilder {
- def buildCommand() = {
- val shellCommandConfig = new ShellCommandConfig(config)
- if(commandPath == null || commandPath.isEmpty())
- shellCommandConfig.getCommand
- else
- commandPath + File.separator + shellCommandConfig.getCommand
- }
-
- def buildEnvironment(): java.util.Map[String, String] = {
- val shellCommandConfig = new ShellCommandConfig(config)
- val envMap = Map(
- ShellCommandConfig.ENV_CONTAINER_ID -> id.toString,
- ShellCommandConfig.ENV_COORDINATOR_URL -> url.toString,
- ShellCommandConfig.ENV_JAVA_OPTS -> shellCommandConfig.getTaskOpts.orElse(""),
- ShellCommandConfig.ENV_ADDITIONAL_CLASSPATH_DIR -> shellCommandConfig.getAdditionalClasspathDir.orElse(""))
-
- val envMapWithJavaHome = JavaOptionals.toRichOptional(shellCommandConfig.getJavaHome).toOption match {
- case Some(javaHome) => envMap + (ShellCommandConfig.ENV_JAVA_HOME -> javaHome)
- case None => envMap
- }
-
- envMapWithJavaHome.asJava
- }
-}
diff --git a/samza-core/src/test/java/org/apache/samza/job/TestShellCommandBuilder.java b/samza-core/src/test/java/org/apache/samza/job/TestShellCommandBuilder.java
new file mode 100644
index 0000000..ca7be0e
--- /dev/null
+++ b/samza-core/src/test/java/org/apache/samza/job/TestShellCommandBuilder.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.samza.job;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Map;
+import com.google.common.collect.ImmutableMap;
+import org.apache.samza.config.Config;
+import org.apache.samza.config.MapConfig;
+import org.apache.samza.config.ShellCommandConfig;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+
+public class TestShellCommandBuilder {
+ private static final String URL_STRING = "http://www.google.com";
+
+ @Test
+ public void testBasicBuild() throws MalformedURLException {
+ Config config = new MapConfig(ImmutableMap.of(ShellCommandConfig.COMMAND_SHELL_EXECUTE, "foo"));
+ ShellCommandBuilder shellCommandBuilder = new ShellCommandBuilder();
+ shellCommandBuilder.setConfig(config);
+ shellCommandBuilder.setId("1");
+ shellCommandBuilder.setUrl(new URL(URL_STRING));
+ Map<String, String> expectedEnvironment = ImmutableMap.of(
+ ShellCommandConfig.ENV_CONTAINER_ID, "1",
+ ShellCommandConfig.ENV_COORDINATOR_URL, URL_STRING,
+ ShellCommandConfig.ENV_JAVA_OPTS, "",
+ ShellCommandConfig.ENV_ADDITIONAL_CLASSPATH_DIR, "");
+ // assertions when command path is not set
+ assertEquals("foo", shellCommandBuilder.buildCommand());
+ assertEquals(expectedEnvironment, shellCommandBuilder.buildEnvironment());
+ // assertions when command path is set to empty string
+ shellCommandBuilder.setCommandPath("");
+ assertEquals("foo", shellCommandBuilder.buildCommand());
+ assertEquals(expectedEnvironment, shellCommandBuilder.buildEnvironment());
+ }
+
+ @Test
+ public void testBuildEnvironment() throws MalformedURLException {
+ Config config = new MapConfig(new ImmutableMap.Builder<String, String>()
+ .put(ShellCommandConfig.COMMAND_SHELL_EXECUTE, "foo")
+ .put(ShellCommandConfig.TASK_JVM_OPTS, "-Xmx4g")
+ .put(ShellCommandConfig.ADDITIONAL_CLASSPATH_DIR, "/path/to/additional/classpath")
+ .put(ShellCommandConfig.TASK_JAVA_HOME, "/path/to/java/home")
+ .build());
+ ShellCommandBuilder shellCommandBuilder = new ShellCommandBuilder();
+ shellCommandBuilder.setConfig(config);
+ shellCommandBuilder.setId("1");
+ shellCommandBuilder.setUrl(new URL(URL_STRING));
+ Map<String, String> expectedEnvironment = new ImmutableMap.Builder<String, String>()
+ .put(ShellCommandConfig.ENV_CONTAINER_ID, "1")
+ .put(ShellCommandConfig.ENV_COORDINATOR_URL, URL_STRING)
+ .put(ShellCommandConfig.ENV_JAVA_OPTS, "-Xmx4g")
+ .put(ShellCommandConfig.ENV_ADDITIONAL_CLASSPATH_DIR, "/path/to/additional/classpath")
+ .put(ShellCommandConfig.ENV_JAVA_HOME, "/path/to/java/home")
+ .build();
+ assertEquals(expectedEnvironment, shellCommandBuilder.buildEnvironment());
+ }
+
+ @Test
+ public void testBuildCommandWithCommandPath() throws MalformedURLException {
+ Config config = new MapConfig(ImmutableMap.of(ShellCommandConfig.COMMAND_SHELL_EXECUTE, "foo"));
+ ShellCommandBuilder shellCommandBuilder = new ShellCommandBuilder();
+ shellCommandBuilder.setConfig(config);
+ shellCommandBuilder.setId("1");
+ shellCommandBuilder.setUrl(new URL(URL_STRING));
+ shellCommandBuilder.setCommandPath("/package/path");
+ assertEquals("/package/path/foo", shellCommandBuilder.buildCommand());
+ }
+}
\ No newline at end of file
diff --git a/samza-core/src/test/scala/org/apache/samza/job/TestShellCommandBuilder.scala b/samza-core/src/test/scala/org/apache/samza/job/TestShellCommandBuilder.scala
deleted file mode 100644
index c70af8d..0000000
--- a/samza-core/src/test/scala/org/apache/samza/job/TestShellCommandBuilder.scala
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 org.apache.samza.job
-
-import org.junit.Assert._
-import org.junit.Test
-import scala.collection.JavaConverters._
-import org.apache.samza.config.MapConfig
-import org.apache.samza.config.ShellCommandConfig
-import java.net.URL
-
-class TestShellCommandBuilder {
- @Test
- def testEnvironmentVariables {
- val urlStr = "http://www.google.com"
- val config = new MapConfig(Map(ShellCommandConfig.COMMAND_SHELL_EXECUTE -> "foo").asJava)
- val scb = new ShellCommandBuilder
- scb.setConfig(config)
- scb.setId("1")
- scb.setUrl(new URL(urlStr))
- val command = scb.buildCommand
- val environment = scb.buildEnvironment
- assertEquals("foo", command)
- assertEquals("1", environment.get(ShellCommandConfig.ENV_CONTAINER_ID))
- assertEquals(urlStr, environment.get(ShellCommandConfig.ENV_COORDINATOR_URL))
- }
-
- // if cmdPath is specified, the full path to the command should be adjusted
- @Test
- def testBuildCommandWithCommandPath {
- val urlStr = "http://www.linkedin.com"
- val config = new MapConfig(Map(ShellCommandConfig.COMMAND_SHELL_EXECUTE -> "foo").asJava)
- val scb = new ShellCommandBuilder
- scb.setConfig(config)
- scb.setId("1")
- scb.setUrl(new URL(urlStr))
- val command = scb.buildCommand
- assertEquals("foo", command)
-
- scb.setCommandPath("/package/path")
- val command1 = scb.buildCommand
- assertEquals("/package/path/foo", command1)
- }
-}
\ No newline at end of file