You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by sa...@apache.org on 2018/11/01 19:01:09 UTC
[pulsar] branch master updated: Consolidate all inferMissing logic
inside ConfigUtils. (#2904)
This is an automated email from the ASF dual-hosted git repository.
sanjeevrk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/master by this push:
new 42c2b0a Consolidate all inferMissing logic inside ConfigUtils. (#2904)
42c2b0a is described below
commit 42c2b0affa32dec1632e7b03e7dd52565f86692b
Author: Sanjeev Kulkarni <sa...@gmail.com>
AuthorDate: Thu Nov 1 12:01:02 2018 -0700
Consolidate all inferMissing logic inside ConfigUtils. (#2904)
* Consolidate all inferMissing logic inside ConfigUtils.
* Removed tests that are already covered in windowconfigutils
* Reverted unneeded change
---
.../org/apache/pulsar/admin/cli/CmdFunctions.java | 53 +--
.../java/org/apache/pulsar/admin/cli/CmdSinks.java | 10 +-
.../org/apache/pulsar/admin/cli/CmdSources.java | 10 +-
.../windowing/WindowFunctionExecutor.java | 5 -
.../pulsar/functions/windowing/WindowUtils.java | 21 --
.../windowing/WindowFunctionExecutorTest.java | 375 +--------------------
.../functions/utils/FunctionConfigUtils.java | 48 +++
.../pulsar/functions/utils/SinkConfigUtils.java | 11 +
.../pulsar/functions/utils/SourceConfigUtils.java | 11 +
.../pulsar/functions/utils/WindowConfigUtils.java | 23 ++
10 files changed, 97 insertions(+), 470 deletions(-)
diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.java
index 8f30f2f..2ec5aff 100644
--- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.java
+++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.java
@@ -20,7 +20,6 @@ package org.apache.pulsar.admin.cli;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.charset.StandardCharsets.UTF_8;
-import static java.util.Objects.isNull;
import static org.apache.bookkeeper.common.concurrent.FutureUtils.result;
import static org.apache.commons.lang.StringUtils.isBlank;
import static org.apache.commons.lang.StringUtils.isNotBlank;
@@ -67,7 +66,6 @@ import org.apache.pulsar.common.functions.Resources;
import org.apache.pulsar.common.functions.WindowConfig;
import org.apache.pulsar.functions.utils.Utils;
import org.apache.pulsar.functions.utils.FunctionConfigUtils;
-import org.apache.pulsar.functions.windowing.WindowUtils;
@Slf4j
@Parameters(commandDescription = "Interface for managing Pulsar Functions (lightweight, Lambda-style compute processes that work with Pulsar)")
@@ -479,7 +477,7 @@ public class CmdFunctions extends CmdBase {
}
// infer default vaues
- inferMissingArguments(functionConfig);
+ FunctionConfigUtils.inferMissingArguments(functionConfig);
// check if configs are valid
validateFunctionConfigs(functionConfig);
@@ -514,55 +512,6 @@ public class CmdFunctions extends CmdBase {
throw new IllegalArgumentException(e.getMessage());
}
}
-
- private void inferMissingArguments(FunctionConfig functionConfig) {
- if (StringUtils.isEmpty(functionConfig.getName())) {
- inferMissingFunctionName(functionConfig);
- }
- if (StringUtils.isEmpty(functionConfig.getTenant())) {
- inferMissingTenant(functionConfig);
- }
- if (StringUtils.isEmpty(functionConfig.getNamespace())) {
- inferMissingNamespace(functionConfig);
- }
-
- if (functionConfig.getParallelism() == 0) {
- functionConfig.setParallelism(1);
- }
-
- if (functionConfig.getJar() != null) {
- functionConfig.setRuntime(FunctionConfig.Runtime.JAVA);
- } else if (functionConfig.getPy() != null) {
- functionConfig.setRuntime(FunctionConfig.Runtime.PYTHON);
- }
-
- WindowConfig windowConfig = functionConfig.getWindowConfig();
- if (windowConfig != null) {
- WindowUtils.inferDefaultConfigs(windowConfig);
- functionConfig.setAutoAck(false);
- }
- }
-
- private void inferMissingFunctionName(FunctionConfig functionConfig) {
- if (isNull(functionConfig.getClassName())) {
- throw new ParameterException("You must specify a class name for the function");
- }
-
- String[] domains = functionConfig.getClassName().split("\\.");
- if (domains.length == 0) {
- functionConfig.setName(functionConfig.getClassName());
- } else {
- functionConfig.setName(domains[domains.length - 1]);
- }
- }
-
- private void inferMissingTenant(FunctionConfig functionConfig) {
- functionConfig.setTenant(PUBLIC_TENANT);
- }
-
- private void inferMissingNamespace(FunctionConfig functionConfig) {
- functionConfig.setNamespace(DEFAULT_NAMESPACE);
- }
}
@Parameters(commandDescription = "Run the Pulsar Function locally (rather than deploying it to the Pulsar cluster)")
diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSinks.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSinks.java
index cd57b9a..48601b2 100644
--- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSinks.java
+++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSinks.java
@@ -22,6 +22,7 @@ import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.pulsar.common.naming.TopicName.DEFAULT_NAMESPACE;
import static org.apache.pulsar.common.naming.TopicName.PUBLIC_TENANT;
+import static org.apache.pulsar.functions.utils.SinkConfigUtils.inferMissingArguments;
import static org.apache.pulsar.functions.utils.Utils.BUILTIN;
import com.beust.jcommander.Parameter;
@@ -432,15 +433,6 @@ public class CmdSinks extends CmdBase {
return new Gson().fromJson(str, type);
}
- protected void inferMissingArguments(SinkConfig sinkConfig) {
- if (sinkConfig.getTenant() == null) {
- sinkConfig.setTenant(PUBLIC_TENANT);
- }
- if (sinkConfig.getNamespace() == null) {
- sinkConfig.setNamespace(DEFAULT_NAMESPACE);
- }
- }
-
protected void validateSinkConfigs(SinkConfig sinkConfig) {
if (isBlank(sinkConfig.getArchive())) {
diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSources.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSources.java
index ff46252..a2271e5 100644
--- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSources.java
+++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdSources.java
@@ -22,6 +22,7 @@ import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.pulsar.common.naming.TopicName.DEFAULT_NAMESPACE;
import static org.apache.pulsar.common.naming.TopicName.PUBLIC_TENANT;
+import static org.apache.pulsar.functions.utils.SourceConfigUtils.inferMissingArguments;
import static org.apache.pulsar.functions.utils.Utils.BUILTIN;
import com.beust.jcommander.Parameter;
@@ -391,15 +392,6 @@ public class CmdSources extends CmdBase {
return new Gson().fromJson(str, type);
}
- private void inferMissingArguments(SourceConfig sourceConfig) {
- if (sourceConfig.getTenant() == null) {
- sourceConfig.setTenant(PUBLIC_TENANT);
- }
- if (sourceConfig.getNamespace() == null) {
- sourceConfig.setNamespace(DEFAULT_NAMESPACE);
- }
- }
-
protected void validateSourceConfigs(SourceConfig sourceConfig) {
if (isBlank(sourceConfig.getArchive())) {
throw new ParameterException("Source archive not specfied");
diff --git a/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/windowing/WindowFunctionExecutor.java b/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/windowing/WindowFunctionExecutor.java
index 79e0891..e288261 100644
--- a/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/windowing/WindowFunctionExecutor.java
+++ b/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/windowing/WindowFunctionExecutor.java
@@ -55,9 +55,6 @@ public class WindowFunctionExecutor<I, O> implements Function<I, O> {
private TimestampExtractor<I> timestampExtractor;
protected transient WaterMarkEventGenerator<I> waterMarkEventGenerator;
- protected static final long DEFAULT_MAX_LAG_MS = 0; // no lag
- protected static final long DEFAULT_WATERMARK_EVENT_INTERVAL_MS = 1000; // 1s
-
protected java.util.function.Function<Collection<I>, O> windowFunction;
public void initialize(Context context) {
@@ -97,8 +94,6 @@ public class WindowFunctionExecutor<I, O> implements Function<I, O> {
(new Gson().toJson(context.getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY).get())),
WindowConfig.class);
-
- WindowUtils.inferDefaultConfigs(windowConfig);
return windowConfig;
}
diff --git a/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/windowing/WindowUtils.java b/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/windowing/WindowUtils.java
index 0c98847..b8ed23b 100644
--- a/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/windowing/WindowUtils.java
+++ b/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/windowing/WindowUtils.java
@@ -18,29 +18,8 @@
*/
package org.apache.pulsar.functions.windowing;
-import org.apache.pulsar.common.functions.WindowConfig;
-
public class WindowUtils {
public static String getFullyQualifiedName(String tenant, String namespace, String name) {
return String.format("%s/%s/%s", tenant, namespace, name);
}
-
- public static void inferDefaultConfigs(WindowConfig windowConfig) {
- if (windowConfig.getWindowLengthDurationMs() != null && windowConfig.getSlidingIntervalDurationMs() == null) {
- windowConfig.setSlidingIntervalDurationMs(windowConfig.getWindowLengthDurationMs());
- }
-
- if (windowConfig.getWindowLengthCount() != null && windowConfig.getSlidingIntervalCount() == null) {
- windowConfig.setSlidingIntervalCount(windowConfig.getWindowLengthCount());
- }
-
- if (windowConfig.getTimestampExtractorClassName() != null) {
- if (windowConfig.getMaxLagMs() == null) {
- windowConfig.setMaxLagMs(WindowFunctionExecutor.DEFAULT_MAX_LAG_MS);
- }
- if (windowConfig.getWatermarkEmitIntervalMs() == null) {
- windowConfig.setWatermarkEmitIntervalMs(WindowFunctionExecutor.DEFAULT_WATERMARK_EVENT_INTERVAL_MS);
- }
- }
- }
}
diff --git a/pulsar-functions/instance/src/test/java/org/apache/pulsar/functions/windowing/WindowFunctionExecutorTest.java b/pulsar-functions/instance/src/test/java/org/apache/pulsar/functions/windowing/WindowFunctionExecutorTest.java
index 0a52b74..21a78a2 100644
--- a/pulsar-functions/instance/src/test/java/org/apache/pulsar/functions/windowing/WindowFunctionExecutorTest.java
+++ b/pulsar-functions/instance/src/test/java/org/apache/pulsar/functions/windowing/WindowFunctionExecutorTest.java
@@ -24,6 +24,7 @@ import org.apache.pulsar.functions.api.Context;
import org.apache.pulsar.functions.api.Record;
import org.apache.pulsar.common.functions.WindowConfig;
+import org.apache.pulsar.functions.utils.WindowConfigUtils;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
@@ -214,378 +215,4 @@ public class WindowFunctionExecutorTest {
System.out.println(testWindowedPulsarFunction.windows);
long event = events.get(events.size() - 1);
}
-
- @Test
- public void testSettingSlidingCountWindow() throws Exception {
- final Object[][] args = new Object[][]{
- {5, 10},
- {1, 1},
- {10, 5},
- {100, 10},
- {100, 100},
- {200, 100},
- {500, 100},
- {1, null},
- };
-
- for (Object[] arg : args) {
- Object arg0 = arg[0];
- Object arg1 = arg[1];
- try {
-
- Integer windowLengthCount = null;
- if (arg0 != null) {
- windowLengthCount = (Integer) arg0;
- }
- Integer slidingIntervalCount = null;
-
- if (arg1 != null) {
- slidingIntervalCount = (Integer) arg1;
- }
- context = Mockito.mock(Context.class);
- Mockito.doReturn("test-function").when(context).getFunctionName();
- Mockito.doReturn("test-namespace").when(context).getNamespace();
- Mockito.doReturn("test-tenant").when(context).getTenant();
- Mockito.doReturn(Collections.singleton("test-source-topic")).when(context).getInputTopics();
- Mockito.doReturn("test-sink-topic").when(context).getOutputTopic();
- Record<?> record = Mockito.mock(Record.class);
- Mockito.doReturn(Optional.of("test-topic")).when(record).getTopicName();
- Mockito.doReturn(record).when(context).getCurrentRecord();
-
- WindowConfig windowConfig = new WindowConfig();
- windowConfig.setTimestampExtractorClassName(TestTimestampExtractor.class.getName());
- windowConfig.setWindowLengthCount(windowLengthCount);
- windowConfig.setSlidingIntervalCount(slidingIntervalCount);
- windowConfig.setActualWindowFunctionClassName(TestFunction.class.getName());
- Mockito.doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class)))
- .when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
-
- testWindowedPulsarFunction = new TestWindowFunctionExecutor();
- testWindowedPulsarFunction.process(10L, context);
-
- if (arg0 == null) {
- fail(String.format("Window length cannot be null -- "
- + "windowLengthCount: %s slidingIntervalCount: %s", arg0, arg1));
- }
- if ((Integer) arg0 <= 0) {
- fail(String.format("Window length cannot be zero or less -- "
- + "windowLengthCount: %s slidingIntervalCount: %s", arg0, arg1));
- }
- if (arg1 != null && (Integer) arg1 <= 0) {
- fail(String.format("Sliding interval length cannot be zero or less -- "
- + "windowLengthCount: %s slidingIntervalCount: %s", arg0, arg1));
- }
-
- Assert.assertEquals(testWindowedPulsarFunction.windowConfig.getWindowLengthCount().intValue(),
- windowLengthCount.intValue());
- // if slidingIntervalCount is null then its a tumbling windowing and slidingIntervalCount will be
- // set to window length count
- if (slidingIntervalCount == null) {
- Assert.assertEquals(
- testWindowedPulsarFunction.windowConfig.getSlidingIntervalCount().intValue(),
- windowLengthCount.intValue());
- } else {
- Assert.assertEquals(
- testWindowedPulsarFunction.windowConfig.getSlidingIntervalCount().intValue(),
- slidingIntervalCount.intValue());
- }
- } catch (IllegalArgumentException e) {
- if (arg0 != null && arg1 != null && (Integer) arg0 > 0 && (Integer) arg1 > 0) {
- fail(String.format("Exception: %s thrown on valid input -- windowLengthCount: %s "
- + "slidingIntervalCount: %s", e.getMessage(), arg0, arg1));
- }
- }
- }
- }
-
- @Test
- public void testSettingSlidingTimeWindow() throws Exception {
- final Object[][] args = new Object[][]{
- {5L, 10L},
- {1L, 1L},
- {10L, 5L},
- {100L, 10L},
- {100L, 100L},
- {200L, 100L},
- {500L, 100L},
- {1L, null},
- };
-
- for (Object[] arg : args) {
- Object arg0 = arg[0];
- Object arg1 = arg[1];
- try {
- Long windowLengthDuration = null;
- if (arg0 != null) {
- windowLengthDuration = (Long) arg0;
- }
- Long slidingIntervalDuration = null;
-
- if (arg1 != null) {
- slidingIntervalDuration = (Long) arg1;
- }
- context = Mockito.mock(Context.class);
- Mockito.doReturn("test-function").when(context).getFunctionName();
- Mockito.doReturn("test-namespace").when(context).getNamespace();
- Mockito.doReturn("test-tenant").when(context).getTenant();
- Mockito.doReturn(Collections.singleton("test-source-topic")).when(context).getInputTopics();
- Mockito.doReturn("test-sink-topic").when(context).getOutputTopic();
- Record<?> record = Mockito.mock(Record.class);
- Mockito.doReturn(Optional.of("test-topic")).when(record).getTopicName();
- Mockito.doReturn(record).when(context).getCurrentRecord();
-
- WindowConfig windowConfig = new WindowConfig();
- windowConfig.setTimestampExtractorClassName(TestTimestampExtractor.class.getName());
- windowConfig.setWindowLengthDurationMs(windowLengthDuration);
- windowConfig.setSlidingIntervalDurationMs(slidingIntervalDuration);
- windowConfig.setActualWindowFunctionClassName(TestFunction.class.getName());
- Mockito.doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class)))
- .when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
-
- testWindowedPulsarFunction = new TestWindowFunctionExecutor();
- testWindowedPulsarFunction.process(10L, context);
-
- if (arg0 == null) {
- fail(String.format("Window length cannot be null -- "
- + "windowLengthCount: %s slidingIntervalCount: %s", arg0, arg1));
- }
- if ((Long) arg0 <= 0) {
- fail(String.format("Window length cannot be zero or less -- "
- + "windowLengthCount: %s slidingIntervalCount: %s", arg0, arg1));
- }
- if (arg1 != null && (Long) arg1 <= 0) {
- fail(String.format("Sliding interval length cannot be zero or less -- "
- + "windowLengthCount: %s slidingIntervalCount: %s", arg0, arg1));
- }
-
- Assert.assertEquals(testWindowedPulsarFunction.windowConfig.getWindowLengthDurationMs().longValue(),
- windowLengthDuration.longValue());
- // if slidingIntervalDuration is null then its a tumbling windowing and slidingIntervalDuration will be
- // set to window length duration
- if (slidingIntervalDuration == null) {
- Assert.assertEquals(testWindowedPulsarFunction.windowConfig.getSlidingIntervalDurationMs().longValue(),
- windowLengthDuration.longValue());
- } else {
- Assert.assertEquals(testWindowedPulsarFunction.windowConfig.getSlidingIntervalDurationMs().longValue(),
- slidingIntervalDuration.longValue());
- }
- } catch (IllegalArgumentException e) {
- if (arg0 != null && arg1 != null && (Long) arg0 > 0 && (Long) arg1 > 0) {
- fail(String.format("Exception: %s thrown on valid input -- windowLengthDuration: %s "
- + "slidingIntervalDuration: %s", e.getMessage(), arg0, arg1));
- }
- }
- }
- }
-
- @Test
- public void testSettingTumblingCountWindow() throws Exception {
- final Object[] args = new Object[]{1, 2, 5, 10};
-
- for (Object arg : args) {
- Object arg0 = arg;
- try {
-
- Integer windowLengthCount = null;
- if (arg0 != null) {
- windowLengthCount = (Integer) arg0;
- }
-
- context = Mockito.mock(Context.class);
- Mockito.doReturn("test-function").when(context).getFunctionName();
- Mockito.doReturn("test-namespace").when(context).getNamespace();
- Mockito.doReturn("test-tenant").when(context).getTenant();
- Mockito.doReturn(Collections.singleton("test-source-topic")).when(context).getInputTopics();
- Mockito.doReturn("test-sink-topic").when(context).getOutputTopic();
- Record<?> record = Mockito.mock(Record.class);
- Mockito.doReturn(Optional.of("test-topic")).when(record).getTopicName();
- Mockito.doReturn(record).when(context).getCurrentRecord();
-
- WindowConfig windowConfig = new WindowConfig();
- windowConfig.setTimestampExtractorClassName(TestTimestampExtractor.class.getName());
- windowConfig.setWindowLengthCount(windowLengthCount);
- windowConfig.setActualWindowFunctionClassName(TestFunction.class.getName());
- Mockito.doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class)))
- .when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
-
- testWindowedPulsarFunction = new TestWindowFunctionExecutor();
- testWindowedPulsarFunction.process(10L, context);
-
- if (arg0 == null) {
- fail(String.format("Window length cannot be null -- windowLengthCount: %s", arg0));
- }
- if ((Integer) arg0 <= 0) {
- fail(String.format("Window length cannot be zero or less -- windowLengthCount: %s",
- arg0));
- }
-
- Assert.assertEquals(testWindowedPulsarFunction.windowConfig.getWindowLengthCount().intValue(),
- windowLengthCount.intValue());
- Assert.assertEquals(testWindowedPulsarFunction.windowConfig.getWindowLengthCount().intValue(),
- testWindowedPulsarFunction.windowConfig.getSlidingIntervalCount().intValue());
- } catch (IllegalArgumentException e) {
- if (arg0 != null && (Integer) arg0 > 0) {
- fail(String.format("Exception: %s thrown on valid input -- windowLengthCount: %s", e
- .getMessage(), arg0));
- }
- }
- }
- }
-
- @Test
- public void testSettingTumblingTimeWindow() throws Exception {
- final Object[] args = new Object[]{1L, 2L, 5L, 10L};
- for (Object arg : args) {
- Object arg0 = arg;
- try {
-
- Long windowLengthDuration = null;
- if (arg0 != null) {
- windowLengthDuration = (Long) arg0;
- }
-
- context = Mockito.mock(Context.class);
- Mockito.doReturn("test-function").when(context).getFunctionName();
- Mockito.doReturn("test-namespace").when(context).getNamespace();
- Mockito.doReturn("test-tenant").when(context).getTenant();
- Mockito.doReturn(Collections.singleton("test-source-topic")).when(context).getInputTopics();
- Mockito.doReturn("test-sink-topic").when(context).getOutputTopic();
- Record<?> record = Mockito.mock(Record.class);
- Mockito.doReturn(Optional.of("test-topic")).when(record).getTopicName();
- Mockito.doReturn(record).when(context).getCurrentRecord();
-
- WindowConfig windowConfig = new WindowConfig();
- windowConfig.setTimestampExtractorClassName(TestTimestampExtractor.class.getName());
- windowConfig.setWindowLengthDurationMs(windowLengthDuration);
- windowConfig.setActualWindowFunctionClassName(TestFunction.class.getName());
- Mockito.doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class)))
- .when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
-
- testWindowedPulsarFunction = new TestWindowFunctionExecutor();
- testWindowedPulsarFunction.process(10L, context);
-
- if (arg0 == null) {
- fail(String.format("Window count duration cannot be null -- windowLengthDuration: %s",
- arg0));
- }
- if ((Long) arg0 <= 0) {
- fail(String.format("Window length cannot be zero or less -- windowLengthDuration: %s",
- arg0));
- }
- Assert.assertEquals(testWindowedPulsarFunction.windowConfig.getWindowLengthDurationMs().longValue(),
- windowLengthDuration.longValue());
- Assert.assertEquals(testWindowedPulsarFunction.windowConfig.getWindowLengthDurationMs().longValue(),
- testWindowedPulsarFunction.windowConfig.getSlidingIntervalDurationMs().longValue());
- } catch (IllegalArgumentException e) {
- if (arg0 != null && (Long) arg0 > 0) {
- fail(String.format("Exception: %s thrown on valid input -- windowLengthDuration: %s", e
- .getMessage(), arg0));
- }
- }
- }
- }
-
- @Test
- public void testSettingLagTime() throws Exception {
- final Object[] args = new Object[]{0L, 1L, 2L, 5L, 10L, null};
- for (Object arg : args) {
- Object arg0 = arg;
- try {
-
- Long maxLagMs = null;
- if (arg0 != null) {
- maxLagMs = (Long) arg0;
- }
-
- context = Mockito.mock(Context.class);
- Mockito.doReturn("test-function").when(context).getFunctionName();
- Mockito.doReturn("test-namespace").when(context).getNamespace();
- Mockito.doReturn("test-tenant").when(context).getTenant();
- Mockito.doReturn(Collections.singleton("test-source-topic")).when(context).getInputTopics();
- Mockito.doReturn("test-sink-topic").when(context).getOutputTopic();
- Record<?> record = Mockito.mock(Record.class);
- Mockito.doReturn(Optional.of("test-topic")).when(record).getTopicName();
- Mockito.doReturn(record).when(context).getCurrentRecord();
-
- WindowConfig windowConfig = new WindowConfig();
- windowConfig.setTimestampExtractorClassName(TestTimestampExtractor.class.getName());
- windowConfig.setWindowLengthCount(1);
- windowConfig.setSlidingIntervalCount(1);
- windowConfig.setMaxLagMs(maxLagMs);
- windowConfig.setActualWindowFunctionClassName(TestFunction.class.getName());
- Mockito.doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class)))
- .when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
-
- testWindowedPulsarFunction = new TestWindowFunctionExecutor();
- testWindowedPulsarFunction.process(10L, context);
-
- if (arg0 == null) {
- Assert.assertEquals(testWindowedPulsarFunction.windowConfig.getMaxLagMs(),
- new Long(testWindowedPulsarFunction.DEFAULT_MAX_LAG_MS));
- } else if((Long) arg0 < 0) {
- fail(String.format("Window lag cannot be less than zero -- lagTime: %s", arg0));
- } else {
- Assert.assertEquals(testWindowedPulsarFunction.windowConfig.getMaxLagMs().longValue(),
- maxLagMs.longValue());
- }
- } catch (IllegalArgumentException e) {
- if (arg0 != null && (Long) arg0 > 0) {
- fail(String.format("Exception: %s thrown on valid input -- lagTime: %s",
- e.getMessage(), arg0));
- }
- }
- }
- }
-
- @Test
- public void testSettingWaterMarkInterval() throws Exception {
- final Object[] args = new Object[]{-1L, 0L, 1L, 2L, 5L, 10L, null};
- for (Object arg : args) {
- Object arg0 = arg;
- try {
- Long watermarkEmitInterval = null;
- if (arg0 != null) {
- watermarkEmitInterval = (Long) arg0;
- }
-
- context = Mockito.mock(Context.class);
- Mockito.doReturn("test-function").when(context).getFunctionName();
- Mockito.doReturn("test-namespace").when(context).getNamespace();
- Mockito.doReturn("test-tenant").when(context).getTenant();
- Mockito.doReturn(Collections.singleton("test-source-topic")).when(context).getInputTopics();
- Mockito.doReturn("test-sink-topic").when(context).getOutputTopic();
- Record<?> record = Mockito.mock(Record.class);
- Mockito.doReturn(Optional.of("test-topic")).when(record).getTopicName();
- Mockito.doReturn(record).when(context).getCurrentRecord();
-
- WindowConfig windowConfig = new WindowConfig();
- windowConfig.setTimestampExtractorClassName(TestTimestampExtractor.class.getName());
- windowConfig.setWindowLengthCount(1);
- windowConfig.setSlidingIntervalCount(1);
- windowConfig.setWatermarkEmitIntervalMs(watermarkEmitInterval);
- windowConfig.setActualWindowFunctionClassName(TestFunction.class.getName());
- Mockito.doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class)))
- .when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
-
- testWindowedPulsarFunction = new TestWindowFunctionExecutor();
- testWindowedPulsarFunction.process(10L, context);
-
- if (arg0 == null) {
- Assert.assertEquals(testWindowedPulsarFunction.windowConfig.getWatermarkEmitIntervalMs(),
- new Long(testWindowedPulsarFunction.DEFAULT_WATERMARK_EVENT_INTERVAL_MS));
- } else if ((Long) arg0 <= 0) {
- fail(String.format("Watermark interval cannot be zero or less -- watermarkInterval: "
- + "%s", arg0));
- } else {
- Assert.assertEquals(testWindowedPulsarFunction.windowConfig.getWatermarkEmitIntervalMs().longValue(),
- watermarkEmitInterval.longValue());
- }
- } catch (IllegalArgumentException e) {
- if (arg0 != null && (Long) arg0 > 0) {
- fail(String.format("Exception: %s thrown on valid input -- watermarkInterval: %s", e
- .getMessage(), arg0));
- }
- }
- }
- }
}
diff --git a/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/FunctionConfigUtils.java b/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/FunctionConfigUtils.java
index a7e45cc..968e63e 100644
--- a/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/FunctionConfigUtils.java
+++ b/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/FunctionConfigUtils.java
@@ -35,9 +35,12 @@ import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.util.*;
+import static java.util.Objects.isNull;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.apache.commons.lang.StringUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.isEmpty;
+import static org.apache.pulsar.common.naming.TopicName.DEFAULT_NAMESPACE;
+import static org.apache.pulsar.common.naming.TopicName.PUBLIC_TENANT;
import static org.apache.pulsar.functions.utils.Utils.BUILTIN;
import static org.apache.pulsar.functions.utils.Utils.loadJar;
@@ -295,6 +298,51 @@ public class FunctionConfigUtils {
return functionConfig;
}
+ public static void inferMissingArguments(FunctionConfig functionConfig) {
+ if (StringUtils.isEmpty(functionConfig.getName())) {
+ inferMissingFunctionName(functionConfig);
+ }
+ if (StringUtils.isEmpty(functionConfig.getTenant())) {
+ inferMissingTenant(functionConfig);
+ }
+ if (StringUtils.isEmpty(functionConfig.getNamespace())) {
+ inferMissingNamespace(functionConfig);
+ }
+
+ if (functionConfig.getParallelism() == 0) {
+ functionConfig.setParallelism(1);
+ }
+
+ if (functionConfig.getJar() != null) {
+ functionConfig.setRuntime(FunctionConfig.Runtime.JAVA);
+ } else if (functionConfig.getPy() != null) {
+ functionConfig.setRuntime(FunctionConfig.Runtime.PYTHON);
+ }
+
+ WindowConfig windowConfig = functionConfig.getWindowConfig();
+ if (windowConfig != null) {
+ WindowConfigUtils.inferMissingArguments(windowConfig);
+ functionConfig.setAutoAck(false);
+ }
+ }
+
+ private static void inferMissingFunctionName(FunctionConfig functionConfig) {
+ String[] domains = functionConfig.getClassName().split("\\.");
+ if (domains.length == 0) {
+ functionConfig.setName(functionConfig.getClassName());
+ } else {
+ functionConfig.setName(domains[domains.length - 1]);
+ }
+ }
+
+ private static void inferMissingTenant(FunctionConfig functionConfig) {
+ functionConfig.setTenant(PUBLIC_TENANT);
+ }
+
+ private static void inferMissingNamespace(FunctionConfig functionConfig) {
+ functionConfig.setNamespace(DEFAULT_NAMESPACE);
+ }
+
private static void doJavaChecks(FunctionConfig functionConfig, ClassLoader clsLoader) {
Class<?>[] typeArgs = Utils.getFunctionTypes(functionConfig, clsLoader);
// inputs use default schema, so there is no check needed there
diff --git a/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/SinkConfigUtils.java b/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/SinkConfigUtils.java
index 4834db0..558f33b 100644
--- a/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/SinkConfigUtils.java
+++ b/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/SinkConfigUtils.java
@@ -42,6 +42,8 @@ import java.util.*;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
+import static org.apache.pulsar.common.naming.TopicName.DEFAULT_NAMESPACE;
+import static org.apache.pulsar.common.naming.TopicName.PUBLIC_TENANT;
import static org.apache.pulsar.functions.utils.Utils.convertProcessingGuarantee;
import static org.apache.pulsar.functions.utils.Utils.getSinkType;
@@ -328,6 +330,15 @@ public class SinkConfigUtils {
return classLoader;
}
+ public static void inferMissingArguments(SinkConfig sinkConfig) {
+ if (sinkConfig.getTenant() == null) {
+ sinkConfig.setTenant(PUBLIC_TENANT);
+ }
+ if (sinkConfig.getNamespace() == null) {
+ sinkConfig.setNamespace(DEFAULT_NAMESPACE);
+ }
+ }
+
private static Collection<String> collectAllInputTopics(SinkConfig sinkConfig) {
List<String> retval = new LinkedList<>();
if (sinkConfig.getInputs() != null) {
diff --git a/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/SourceConfigUtils.java b/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/SourceConfigUtils.java
index 8d7c1d0..07d593d 100644
--- a/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/SourceConfigUtils.java
+++ b/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/SourceConfigUtils.java
@@ -38,6 +38,8 @@ import java.nio.file.Path;
import java.util.Map;
import static org.apache.commons.lang3.StringUtils.isEmpty;
+import static org.apache.pulsar.common.naming.TopicName.DEFAULT_NAMESPACE;
+import static org.apache.pulsar.common.naming.TopicName.PUBLIC_TENANT;
import static org.apache.pulsar.functions.utils.Utils.convertProcessingGuarantee;
import static org.apache.pulsar.functions.utils.Utils.getSourceType;
@@ -235,4 +237,13 @@ public class SourceConfigUtils {
}
return classLoader;
}
+
+ public static void inferMissingArguments(SourceConfig sourceConfig) {
+ if (sourceConfig.getTenant() == null) {
+ sourceConfig.setTenant(PUBLIC_TENANT);
+ }
+ if (sourceConfig.getNamespace() == null) {
+ sourceConfig.setNamespace(DEFAULT_NAMESPACE);
+ }
+ }
}
diff --git a/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/WindowConfigUtils.java b/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/WindowConfigUtils.java
index 42a6b03..a386472 100644
--- a/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/WindowConfigUtils.java
+++ b/pulsar-functions/utils/src/main/java/org/apache/pulsar/functions/utils/WindowConfigUtils.java
@@ -22,6 +22,10 @@ package org.apache.pulsar.functions.utils;
import org.apache.pulsar.common.functions.WindowConfig;
public class WindowConfigUtils {
+
+ public static final long DEFAULT_MAX_LAG_MS = 0; // no lag
+ public static final long DEFAULT_WATERMARK_EVENT_INTERVAL_MS = 1000; // 1s
+
public static void validate(WindowConfig windowConfig) {
if (windowConfig.getWindowLengthDurationMs() == null && windowConfig.getWindowLengthCount() == null) {
throw new IllegalArgumentException("Window length is not specified");
@@ -75,4 +79,23 @@ public class WindowConfigUtils {
}
}
}
+
+ public static void inferMissingArguments(WindowConfig windowConfig) {
+ if (windowConfig.getWindowLengthDurationMs() != null && windowConfig.getSlidingIntervalDurationMs() == null) {
+ windowConfig.setSlidingIntervalDurationMs(windowConfig.getWindowLengthDurationMs());
+ }
+
+ if (windowConfig.getWindowLengthCount() != null && windowConfig.getSlidingIntervalCount() == null) {
+ windowConfig.setSlidingIntervalCount(windowConfig.getWindowLengthCount());
+ }
+
+ if (windowConfig.getTimestampExtractorClassName() != null) {
+ if (windowConfig.getMaxLagMs() == null) {
+ windowConfig.setMaxLagMs(DEFAULT_MAX_LAG_MS);
+ }
+ if (windowConfig.getWatermarkEmitIntervalMs() == null) {
+ windowConfig.setWatermarkEmitIntervalMs(DEFAULT_WATERMARK_EVENT_INTERVAL_MS);
+ }
+ }
+ }
}
\ No newline at end of file