You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "twosom (via GitHub)" <gi...@apache.org> on 2024/09/16 17:12:19 UTC

[PR] Add support for dynamic write in `MqttIO` [beam]

twosom opened a new pull request, #32470:
URL: https://github.com/apache/beam/pull/32470

   **Please** add a meaningful description for your change here
   
   fixes #19376
   
   This PR contains these changes
   - add support for dynamic write in `MqttIO`
   - add related test
   
   
   
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://github.com/apache/beam/blob/master/CONTRIBUTING.md#make-the-reviewers-job-easier).
   
   To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/workflows/Build%20python%20source%20distribution%20and%20wheels/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/workflows/Python%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/workflows/Java%20Tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go tests](https://github.com/apache/beam/workflows/Go%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI or the [workflows README](https://github.com/apache/beam/blob/master/.github/workflows/README.md) to see a list of phrases to trigger workflows.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for dynamic write in `MqttIO` [beam]

Posted by "twosom (via GitHub)" <gi...@apache.org>.
twosom commented on PR #32470:
URL: https://github.com/apache/beam/pull/32470#issuecomment-2370925463

   > Thanks for the contribution. The added PTransform and DoFn pretty much duplicated the existing one. I would suggest adjust the current ~Write transform~ and WriteFn for this feature. This avoids branches and more independent transform/DoFn to maintain. Should be straightforward to do so.
   > 
   > I see, there was concern of breaking change, Write originally is not a generic but WriteDynamics transform is a generic. We can make Write to return a WriteDynamics<byte[]> with fixed topicFn and identity payloadFn.
   
   @Abacn 
   Thanks for your comment. I'll make the improvements you suggested.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for dynamic write in `MqttIO` [beam]

Posted by "twosom (via GitHub)" <gi...@apache.org>.
twosom commented on PR #32470:
URL: https://github.com/apache/beam/pull/32470#issuecomment-2377408200

   > Hi, I do not see code update. Was the commit pushed to origin?
   
   Nope, I just merged conflicted ^_^
   
   and I'm doing working this `MqttIO`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for dynamic write in `MqttIO` [beam]

Posted by "Abacn (via GitHub)" <gi...@apache.org>.
Abacn commented on code in PR #32470:
URL: https://github.com/apache/beam/pull/32470#discussion_r1771942990


##########
sdks/java/io/mqtt/src/main/java/org/apache/beam/sdk/io/mqtt/MqttIO.java:
##########
@@ -502,6 +535,121 @@ public UnboundedMqttSource getCurrentSource() {
     }
   }
 
+  @AutoValue
+  public abstract static class DynamicWrite<InputT> extends PTransform<PCollection<InputT>, PDone> {
+    abstract @Nullable ConnectionConfiguration connectionConfiguration();
+
+    abstract @Nullable SerializableFunction<InputT, String> topicFn();

Review Comment:
   at pipeline expansion time, one can set topicFn to always return the topic, and payloadFn to identify function for normal writes



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for dynamic write in `MqttIO` [beam]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #32470:
URL: https://github.com/apache/beam/pull/32470#issuecomment-2353645513

   Assigning reviewers. If you would like to opt out of this review, comment `assign to next reviewer`:
   
   R: @kennknowles for label java.
   R: @damondouglas for label io.
   
   Available commands:
   - `stop reviewer notifications` - opt out of the automated review tooling
   - `remind me after tests pass` - tag the comment author after tests pass
   - `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
   
   The PR bot will only process comments in the main thread (not review comments).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for dynamic write in `MqttIO` [beam]

Posted by "twosom (via GitHub)" <gi...@apache.org>.
twosom commented on PR #32470:
URL: https://github.com/apache/beam/pull/32470#issuecomment-2377417386

   @Abacn 
   and I updated code just now.
   
   Thanks for your comment. ^_^


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for dynamic write in `MqttIO` [beam]

Posted by "twosom (via GitHub)" <gi...@apache.org>.
twosom commented on code in PR #32470:
URL: https://github.com/apache/beam/pull/32470#discussion_r1778724730


##########
sdks/java/io/mqtt/src/main/java/org/apache/beam/sdk/io/mqtt/MqttIO.java:
##########
@@ -538,54 +597,80 @@ public Write withConnectionConfiguration(ConnectionConfiguration configuration)
      * @param retained Whether or not the messaging engine should retain the message.
      * @return The {@link Write} {@link PTransform} with the corresponding retained configuration.
      */
-    public Write withRetained(boolean retained) {
+    public Write<InputT> withRetained(boolean retained) {
       return builder().setRetained(retained).build();
     }
 
-    @Override
-    public PDone expand(PCollection<byte[]> input) {
-      input.apply(ParDo.of(new WriteFn(this)));
-      return PDone.in(input.getPipeline());
-    }
-
     @Override
     public void populateDisplayData(DisplayData.Builder builder) {
       connectionConfiguration().populateDisplayData(builder);
       builder.add(DisplayData.item("retained", retained()));
     }
 
-    private static class WriteFn extends DoFn<byte[], Void> {
+    @Override
+    public PDone expand(PCollection<InputT> input) {
+      checkArgument(connectionConfiguration() != null, "connectionConfiguration can not be null");
+      final SerializableFunction<InputT, String> topicFn;
+      if (dynamic()) {

Review Comment:
   This would also allow calls like MqttIO.write().withTopicPayloadFn(...), since they use the same Write class.
   We don't know if the Write object was called from write() or dynamicWrite(). This is not enough to determine the scenario based on whether the topic is null.
   The topic value can be modified externally, but the dynamic value cannot.
   I would like to make sure that withTopicPayloadFn() and withPayloadFn() are only available when dynamic is true. What do you think? and please take a look a the following test code.
   
   
   ```java
   @Test
     public void testWriteWithTopicFn() {
       IllegalArgumentException exception =
           assertThrows(
               IllegalArgumentException.class, () -> MqttIO.write().withTopicFn(e -> "some topic"));
   
       assertEquals("withTopicFn can not use in non-dynamic write", exception.getMessage());
     }
   
     @Test
     public void testWriteWithPayloadFn() {
       final IllegalArgumentException exception =
           assertThrows(
               IllegalArgumentException.class, () -> MqttIO.write().withPayloadFn(e -> new byte[] {}));
   
       assertEquals("withPayloadFn can not use in non-dynamic write", exception.getMessage());
     }
   
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for dynamic write in `MqttIO` [beam]

Posted by "Abacn (via GitHub)" <gi...@apache.org>.
Abacn commented on code in PR #32470:
URL: https://github.com/apache/beam/pull/32470#discussion_r1777521944


##########
sdks/java/io/mqtt/src/main/java/org/apache/beam/sdk/io/mqtt/MqttIO.java:
##########
@@ -538,54 +597,80 @@ public Write withConnectionConfiguration(ConnectionConfiguration configuration)
      * @param retained Whether or not the messaging engine should retain the message.
      * @return The {@link Write} {@link PTransform} with the corresponding retained configuration.
      */
-    public Write withRetained(boolean retained) {
+    public Write<InputT> withRetained(boolean retained) {
       return builder().setRetained(retained).build();
     }
 
-    @Override
-    public PDone expand(PCollection<byte[]> input) {
-      input.apply(ParDo.of(new WriteFn(this)));
-      return PDone.in(input.getPipeline());
-    }
-
     @Override
     public void populateDisplayData(DisplayData.Builder builder) {
       connectionConfiguration().populateDisplayData(builder);
       builder.add(DisplayData.item("retained", retained()));
     }
 
-    private static class WriteFn extends DoFn<byte[], Void> {
+    @Override
+    public PDone expand(PCollection<InputT> input) {
+      checkArgument(connectionConfiguration() != null, "connectionConfiguration can not be null");
+      final SerializableFunction<InputT, String> topicFn;
+      if (dynamic()) {
+        checkArgument(
+            connectionConfiguration().getTopic() == null, "DynamicWrite can not have static topic");
+        topicFn = topicFn();
+      } else {
+        checkArgument(connectionConfiguration().getTopic() != null, "topic can not be null");
+        final String topic = connectionConfiguration().getTopic();
+        topicFn = ignore -> topic;
+      }
+
+      checkArgument(topicFn != null, "topicFn can not be null");
+      checkArgument(payloadFn() != null, "payloadFn can not be null");
+
+      input.apply(
+          ParDo.of(new WriteFn<>(connectionConfiguration(), topicFn, payloadFn(), retained())));
+      return PDone.in(input.getPipeline());
+    }
 
-      private final Write spec;

Review Comment:
   what is the purpose of this refactor? One can keep "spec" as well as constructor parameters for the convenience of future extension of configurations



##########
sdks/java/io/mqtt/src/main/java/org/apache/beam/sdk/io/mqtt/MqttIO.java:
##########
@@ -538,54 +597,80 @@ public Write withConnectionConfiguration(ConnectionConfiguration configuration)
      * @param retained Whether or not the messaging engine should retain the message.
      * @return The {@link Write} {@link PTransform} with the corresponding retained configuration.
      */
-    public Write withRetained(boolean retained) {
+    public Write<InputT> withRetained(boolean retained) {
       return builder().setRetained(retained).build();
     }
 
-    @Override
-    public PDone expand(PCollection<byte[]> input) {
-      input.apply(ParDo.of(new WriteFn(this)));
-      return PDone.in(input.getPipeline());
-    }
-
     @Override
     public void populateDisplayData(DisplayData.Builder builder) {
       connectionConfiguration().populateDisplayData(builder);
       builder.add(DisplayData.item("retained", retained()));
     }
 
-    private static class WriteFn extends DoFn<byte[], Void> {
+    @Override
+    public PDone expand(PCollection<InputT> input) {
+      checkArgument(connectionConfiguration() != null, "connectionConfiguration can not be null");
+      final SerializableFunction<InputT, String> topicFn;
+      if (dynamic()) {

Review Comment:
   since getTopic() is or not null corresponds to dynamic() being false and true, we can remove this property, and just use topic nullness to determine the scenario?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for dynamic write in `MqttIO` [beam]

Posted by "Abacn (via GitHub)" <gi...@apache.org>.
Abacn commented on code in PR #32470:
URL: https://github.com/apache/beam/pull/32470#discussion_r1771942990


##########
sdks/java/io/mqtt/src/main/java/org/apache/beam/sdk/io/mqtt/MqttIO.java:
##########
@@ -502,6 +535,121 @@ public UnboundedMqttSource getCurrentSource() {
     }
   }
 
+  @AutoValue
+  public abstract static class DynamicWrite<InputT> extends PTransform<PCollection<InputT>, PDone> {
+    abstract @Nullable ConnectionConfiguration connectionConfiguration();
+
+    abstract @Nullable SerializableFunction<InputT, String> topicFn();

Review Comment:
   at pipeline expansion time, one can set topicFn to always return the topic, and payloadFn to identify function for normal writes



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for dynamic write in `MqttIO` [beam]

Posted by "twosom (via GitHub)" <gi...@apache.org>.
twosom commented on code in PR #32470:
URL: https://github.com/apache/beam/pull/32470#discussion_r1778733342


##########
sdks/java/io/mqtt/src/main/java/org/apache/beam/sdk/io/mqtt/MqttIO.java:
##########
@@ -538,54 +597,80 @@ public Write withConnectionConfiguration(ConnectionConfiguration configuration)
      * @param retained Whether or not the messaging engine should retain the message.
      * @return The {@link Write} {@link PTransform} with the corresponding retained configuration.
      */
-    public Write withRetained(boolean retained) {
+    public Write<InputT> withRetained(boolean retained) {
       return builder().setRetained(retained).build();
     }
 
-    @Override
-    public PDone expand(PCollection<byte[]> input) {
-      input.apply(ParDo.of(new WriteFn(this)));
-      return PDone.in(input.getPipeline());
-    }
-
     @Override
     public void populateDisplayData(DisplayData.Builder builder) {
       connectionConfiguration().populateDisplayData(builder);
       builder.add(DisplayData.item("retained", retained()));
     }
 
-    private static class WriteFn extends DoFn<byte[], Void> {
+    @Override
+    public PDone expand(PCollection<InputT> input) {
+      checkArgument(connectionConfiguration() != null, "connectionConfiguration can not be null");
+      final SerializableFunction<InputT, String> topicFn;
+      if (dynamic()) {
+        checkArgument(
+            connectionConfiguration().getTopic() == null, "DynamicWrite can not have static topic");
+        topicFn = topicFn();
+      } else {
+        checkArgument(connectionConfiguration().getTopic() != null, "topic can not be null");
+        final String topic = connectionConfiguration().getTopic();
+        topicFn = ignore -> topic;
+      }
+
+      checkArgument(topicFn != null, "topicFn can not be null");
+      checkArgument(payloadFn() != null, "payloadFn can not be null");
+
+      input.apply(
+          ParDo.of(new WriteFn<>(connectionConfiguration(), topicFn, payloadFn(), retained())));
+      return PDone.in(input.getPipeline());
+    }
 
-      private final Write spec;

Review Comment:
   Oh, That's correct! Thanks for feedback! ^_^



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org