You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "shunping (via GitHub)" <gi...@apache.org> on 2025/06/24 03:07:48 UTC

[PR] Move PeriodicStream into periodicsequence. [beam]

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

   A follow-up PR of #35300 to address the concern at https://github.com/apache/beam/pull/35300#issuecomment-2997517699


-- 
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] Move PeriodicStream into periodicsequence. [beam]

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


##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -91,7 +94,39 @@ class ImpulseSeqGenDoFn(beam.DoFn):
   ImpulseSeqGenDoFn can't guarantee that each element is output at exact time.
   ImpulseSeqGenDoFn guarantees that elements would not be output prior to
   given runtime timestamp.
+
+  The output mode of the DoFn is based on the input `data`:
+
+    - **None**: If `data` is None (by default), the output element will be the

Review Comment:
   This is a good point. You could get around this by moving most of this logic into the `map` transform instead, but I don't think this is necessary if you don't think it is cleaner (I won't block on this).



##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -114,24 +149,31 @@ def process(
     assert isinstance(restriction_tracker, sdf_utils.RestrictionTrackerView)
 
     current_output_index = restriction_tracker.current_restriction().start
-    current_output_timestamp = start + interval * current_output_index
-    current_time = time.time()
-    watermark_estimator.set_watermark(
-        timestamp.Timestamp(current_output_timestamp))
-
-    while current_output_timestamp <= current_time:
-      if restriction_tracker.try_claim(current_output_index):
-        yield current_output_timestamp
-        current_output_index += 1
-        current_output_timestamp = start + interval * current_output_index
-        current_time = time.time()
-        watermark_estimator.set_watermark(
+
+    while True:
+      current_output_timestamp = start + interval * current_output_index
+
+      if current_output_timestamp > time.time():
+        # we are too ahead of time, let's wait.
+        restriction_tracker.defer_remainder(
             timestamp.Timestamp(current_output_timestamp))
-      else:
         return
 
-    restriction_tracker.defer_remainder(
-        timestamp.Timestamp(current_output_timestamp))
+      if not restriction_tracker.try_claim(current_output_index):
+        # nothing to claim, just stop
+        return
+
+      output, output_ts = self._get_output(current_output_index,
+                                           current_output_timestamp)
+
+      current_watermark = watermark_estimator.current_watermark()
+      if current_watermark is None or output_ts > current_watermark:
+        # ensure watermark is monotonic
+        watermark_estimator.set_watermark(output_ts)

Review Comment:
   I think this watermark estimation won't work if the data has provided event times.
   
   For example, imagine we have the following data:
   
   ```
   (data=foo, event_time=10)
   (data=foo, event_time=15)
   (data=foo, event_time=1)
   ```
   
   when we see the first element, we'd increment the watermark to 10. But this would immediately mean that the 3rd element is late.
   
   The problem is even more severe when you consider repeating data. If you have repeating data, the only valid watermark you can advance to is `min(all_event_times)`.
   
   I'd propose the following changes:
   
   1) If you repeat data, that data cannot have event times associated with it (otherwise watermark estimation is basically impossible). This would be a validation step at transform construction.
   2) If you don't repeat data, but there are associated watermarks, when emitting `data[i]`, set the watermark to `min(data[i+1:].map(lambda d: d. event_time))`. That will ensure a valid watermark.
   
   Note that this would allow you to move the logic into the `map` transform if you want to, resolving the problems described in https://github.com/apache/beam/pull/35412/files#r2164781118



-- 
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] Move PeriodicStream into periodicsequence. [beam]

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


##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -202,3 +244,79 @@ def expand(self, pbegin):
       result = result | 'ApplyWindowing' >> beam.WindowInto(
           window.FixedWindows(self.interval))
     return result
+
+
+class PeriodicStream(beam.PTransform):
+  """A PTransform that generates a periodic stream of elements from a sequence.
+
+  This transform creates a `PCollection` by emitting elements from a provided
+  Python sequence at a specified time interval. It is designed for use in
+  streaming pipelines to simulate a live, continuous source of data.
+
+  The transform can be configured to:
+  - Emit the sequence only once.
+  - Repeat the sequence indefinitely or for a maximum duration.
+  - Control the time interval between elements.
+
+  To ensure that the stream does not emit a burst of elements immediately at
+  pipeline startup, a fixed warmup period is added before the first element
+  is generated.
+
+  Args:
+      data: The sequence of elements to emit into the PCollection. The elements
+        can be raw values or pre-timestamped tuples in the format
+        `(apache_beam.utils.timestamp.Timestamp, value)`.
+      max_duration: The maximum total duration in seconds for the stream
+        generation. If `None` (the default) and `repeat` is `True`, the
+        stream is effectively infinite. If `repeat` is `False`, the stream's
+        duration is the shorter of this value and the time required to emit
+        the sequence once.
+      interval: The delay in seconds between consecutive elements.
+        Defaults to 0.1.
+      repeat: If `True`, the input `data` sequence is emitted repeatedly.
+        If `False` (the default), the sequence is emitted only once.
+      warmup_time: The extra wait time for the impulse element
+        (start, end, interval) to reach `ImpulseSeqGenDoFn`. It is used to
+        avoid the events clustering at the beginning.
+  """
+  def __init__(
+      self,
+      data: Sequence[Any],
+      max_duration: Optional[float] = None,
+      interval: float = 0.1,
+      repeat: bool = False,
+      warmup_time: float = 2.0):

Review Comment:
   I find that the time domain of `start_timestamp`, `stop_timestamp` and `fire_interval` is a bit confusing in `PeriodicImpulse`. 
   
   It seems that the `start_timestamp` and `stop_timestamp` are about event time. The current behavior when `start_timestamp < now` is to catch up event time to current time, which is very unintuitive. Assuming someone specifies `start_timestamp` to 0, then most cpu time will be spent on emitting billions of events. 
   
   After the event time counter catches up to `now`, then `fire_interval` takes into affect but now this interval sounds like an interval of processing time.
   
   Given such inconsistency, my thought is to hide `start_timestamp`/`stop_timstamp` from the useage. By setting `start_timestamp = Timestamp.now()` (and plus some warm up time), we can let it behave as close to processing time as possible. If users want to inject events with old timestamps, they can specify the event-time timestamps in the `data` array. For example, they can put `data = [(Timestamp(1), 1), (Timestamp(2), 2)]`, which then simulates two events coming from 1970s and it takes 55 years for them to arrive at and process at the current time.



-- 
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] Move PeriodicStream into periodicsequence. [beam]

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


##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -202,3 +244,79 @@ def expand(self, pbegin):
       result = result | 'ApplyWindowing' >> beam.WindowInto(
           window.FixedWindows(self.interval))
     return result
+
+
+class PeriodicStream(beam.PTransform):
+  """A PTransform that generates a periodic stream of elements from a sequence.
+
+  This transform creates a `PCollection` by emitting elements from a provided
+  Python sequence at a specified time interval. It is designed for use in
+  streaming pipelines to simulate a live, continuous source of data.
+
+  The transform can be configured to:
+  - Emit the sequence only once.
+  - Repeat the sequence indefinitely or for a maximum duration.
+  - Control the time interval between elements.
+
+  To ensure that the stream does not emit a burst of elements immediately at
+  pipeline startup, a fixed warmup period is added before the first element
+  is generated.
+
+  Args:
+      data: The sequence of elements to emit into the PCollection. The elements
+        can be raw values or pre-timestamped tuples in the format
+        `(apache_beam.utils.timestamp.Timestamp, value)`.
+      max_duration: The maximum total duration in seconds for the stream
+        generation. If `None` (the default) and `repeat` is `True`, the
+        stream is effectively infinite. If `repeat` is `False`, the stream's
+        duration is the shorter of this value and the time required to emit
+        the sequence once.
+      interval: The delay in seconds between consecutive elements.
+        Defaults to 0.1.
+      repeat: If `True`, the input `data` sequence is emitted repeatedly.
+        If `False` (the default), the sequence is emitted only once.
+      warmup_time: The extra wait time for the impulse element
+        (start, end, interval) to reach `ImpulseSeqGenDoFn`. It is used to
+        avoid the events clustering at the beginning.
+  """
+  def __init__(
+      self,
+      data: Sequence[Any],
+      max_duration: Optional[float] = None,
+      interval: float = 0.1,
+      repeat: bool = False,
+      warmup_time: float = 2.0):

Review Comment:
   I find that the time domain of `start_timestamp`, `stop_timestamp` and `fire_interval` is a bit confusing in `PeriodicImpulse`. 
   
   It seems that the `start_timestamp` and `stop_timestamp` are about event time. The current behavior when `start_timestamp < now` is to catch up event time to current time, which is very unintuitive. Assuming someone specifies `start_timestamp` to 0, then most cpu time will be spent on emitting billions of events. 
   
   After the event time counter catches up to `now`, then `fire_interval` takes into affect but now this interval sounds like an interval of processing time.
   
   Given such inconsistency, my thought is to hide `start_timestamp`/`stop_timstamp` from the useage. By setting `start_timestamp = Timestamp.now()` (and plus some warm up time), we can let it behave as close to processing time as possible. If users want to inject events with old timestamps, they can specify the event-time timestamps in the `data` array. For example, they can put data = [(Timestamp(1), 1), (Timestamp(2), 2)], which then simulates two events coming from 1970s and it takes 55 years for them to arrive at and process at the current time.



-- 
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] Move PeriodicStream into periodicsequence. [beam]

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


##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -91,7 +94,39 @@ class ImpulseSeqGenDoFn(beam.DoFn):
   ImpulseSeqGenDoFn can't guarantee that each element is output at exact time.
   ImpulseSeqGenDoFn guarantees that elements would not be output prior to
   given runtime timestamp.
+
+  The output mode of the DoFn is based on the input `data`:
+
+    - **None**: If `data` is None (by default), the output element will be the

Review Comment:
   Can we still output a TimestampedValue here for consistency? Then we could remove
   
   ```
   | 'MapToTimestamped' >> beam.Map(lambda tt: TimestampedValue(tt, tt)))
   ```
   
   from PeriodicImpulse



##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -202,3 +244,79 @@ def expand(self, pbegin):
       result = result | 'ApplyWindowing' >> beam.WindowInto(
           window.FixedWindows(self.interval))
     return result
+
+
+class PeriodicStream(beam.PTransform):
+  """A PTransform that generates a periodic stream of elements from a sequence.
+
+  This transform creates a `PCollection` by emitting elements from a provided
+  Python sequence at a specified time interval. It is designed for use in
+  streaming pipelines to simulate a live, continuous source of data.
+
+  The transform can be configured to:
+  - Emit the sequence only once.
+  - Repeat the sequence indefinitely or for a maximum duration.
+  - Control the time interval between elements.
+
+  To ensure that the stream does not emit a burst of elements immediately at
+  pipeline startup, a fixed warmup period is added before the first element
+  is generated.
+
+  Args:
+      data: The sequence of elements to emit into the PCollection. The elements
+        can be raw values or pre-timestamped tuples in the format
+        `(apache_beam.utils.timestamp.Timestamp, value)`.
+      max_duration: The maximum total duration in seconds for the stream
+        generation. If `None` (the default) and `repeat` is `True`, the
+        stream is effectively infinite. If `repeat` is `False`, the stream's
+        duration is the shorter of this value and the time required to emit
+        the sequence once.
+      interval: The delay in seconds between consecutive elements.
+        Defaults to 0.1.
+      repeat: If `True`, the input `data` sequence is emitted repeatedly.
+        If `False` (the default), the sequence is emitted only once.
+      warmup_time: The extra wait time for the impulse element
+        (start, end, interval) to reach `ImpulseSeqGenDoFn`. It is used to
+        avoid the events clustering at the beginning.
+  """
+  def __init__(
+      self,
+      data: Sequence[Any],
+      max_duration: Optional[float] = None,
+      interval: float = 0.1,
+      repeat: bool = False,

Review Comment:
   `repeat` seems like it is expressing a subset of what `max_duration` expresses, right?
   
   Isn't `repeat=False` more or less the same as saying `max_duration=interval*(len(data)-1)`? Instead of having a separate parameter, could we just doc this?



##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -202,3 +244,79 @@ def expand(self, pbegin):
       result = result | 'ApplyWindowing' >> beam.WindowInto(
           window.FixedWindows(self.interval))
     return result
+
+
+class PeriodicStream(beam.PTransform):
+  """A PTransform that generates a periodic stream of elements from a sequence.
+
+  This transform creates a `PCollection` by emitting elements from a provided
+  Python sequence at a specified time interval. It is designed for use in
+  streaming pipelines to simulate a live, continuous source of data.
+
+  The transform can be configured to:
+  - Emit the sequence only once.
+  - Repeat the sequence indefinitely or for a maximum duration.
+  - Control the time interval between elements.
+
+  To ensure that the stream does not emit a burst of elements immediately at
+  pipeline startup, a fixed warmup period is added before the first element
+  is generated.
+
+  Args:
+      data: The sequence of elements to emit into the PCollection. The elements
+        can be raw values or pre-timestamped tuples in the format
+        `(apache_beam.utils.timestamp.Timestamp, value)`.
+      max_duration: The maximum total duration in seconds for the stream
+        generation. If `None` (the default) and `repeat` is `True`, the
+        stream is effectively infinite. If `repeat` is `False`, the stream's
+        duration is the shorter of this value and the time required to emit
+        the sequence once.
+      interval: The delay in seconds between consecutive elements.
+        Defaults to 0.1.
+      repeat: If `True`, the input `data` sequence is emitted repeatedly.
+        If `False` (the default), the sequence is emitted only once.
+      warmup_time: The extra wait time for the impulse element
+        (start, end, interval) to reach `ImpulseSeqGenDoFn`. It is used to
+        avoid the events clustering at the beginning.
+  """
+  def __init__(
+      self,
+      data: Sequence[Any],
+      max_duration: Optional[float] = None,
+      interval: float = 0.1,
+      repeat: bool = False,
+      warmup_time: float = 2.0):

Review Comment:
   I think `warmup_time` ends up being a worse version of `start_timestamp`. Basically, `start_timestamp` allows me to pretty easily specify a `warmup_time` (`start_timestamp = timestamp.Timestamp.now() + my_custom_warmup_time`), but `warmup_time` does not give me the ability to specify a precise `start_timestamp` since the exact time of expansion is unknown. I'd also prefer we keep one consistent way of doing this in Beam.
   
   If we switch to `start_timestamp`, we could also switch `max_duration` to `end_timestamp` for consistency, though I feel less strongly about that. I actually like expressing it in terms of duration, but I think it is probably better to pattern match to existing work if we can.



##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -202,3 +244,79 @@ def expand(self, pbegin):
       result = result | 'ApplyWindowing' >> beam.WindowInto(
           window.FixedWindows(self.interval))
     return result
+
+
+class PeriodicStream(beam.PTransform):
+  """A PTransform that generates a periodic stream of elements from a sequence.
+
+  This transform creates a `PCollection` by emitting elements from a provided
+  Python sequence at a specified time interval. It is designed for use in
+  streaming pipelines to simulate a live, continuous source of data.
+
+  The transform can be configured to:
+  - Emit the sequence only once.
+  - Repeat the sequence indefinitely or for a maximum duration.
+  - Control the time interval between elements.
+
+  To ensure that the stream does not emit a burst of elements immediately at
+  pipeline startup, a fixed warmup period is added before the first element
+  is generated.
+
+  Args:
+      data: The sequence of elements to emit into the PCollection. The elements
+        can be raw values or pre-timestamped tuples in the format
+        `(apache_beam.utils.timestamp.Timestamp, value)`.
+      max_duration: The maximum total duration in seconds for the stream
+        generation. If `None` (the default) and `repeat` is `True`, the
+        stream is effectively infinite. If `repeat` is `False`, the stream's
+        duration is the shorter of this value and the time required to emit
+        the sequence once.
+      interval: The delay in seconds between consecutive elements.
+        Defaults to 0.1.
+      repeat: If `True`, the input `data` sequence is emitted repeatedly.
+        If `False` (the default), the sequence is emitted only once.
+      warmup_time: The extra wait time for the impulse element
+        (start, end, interval) to reach `ImpulseSeqGenDoFn`. It is used to
+        avoid the events clustering at the beginning.
+  """
+  def __init__(
+      self,
+      data: Sequence[Any],
+      max_duration: Optional[float] = None,
+      interval: float = 0.1,

Review Comment:
   Can we call this `fire_interval` to match the other transforms?



-- 
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] Move PeriodicStream into periodicsequence. [beam]

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


##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -202,3 +244,79 @@ def expand(self, pbegin):
       result = result | 'ApplyWindowing' >> beam.WindowInto(
           window.FixedWindows(self.interval))
     return result
+
+
+class PeriodicStream(beam.PTransform):
+  """A PTransform that generates a periodic stream of elements from a sequence.
+
+  This transform creates a `PCollection` by emitting elements from a provided
+  Python sequence at a specified time interval. It is designed for use in
+  streaming pipelines to simulate a live, continuous source of data.
+
+  The transform can be configured to:
+  - Emit the sequence only once.
+  - Repeat the sequence indefinitely or for a maximum duration.
+  - Control the time interval between elements.
+
+  To ensure that the stream does not emit a burst of elements immediately at
+  pipeline startup, a fixed warmup period is added before the first element
+  is generated.
+
+  Args:
+      data: The sequence of elements to emit into the PCollection. The elements
+        can be raw values or pre-timestamped tuples in the format
+        `(apache_beam.utils.timestamp.Timestamp, value)`.
+      max_duration: The maximum total duration in seconds for the stream
+        generation. If `None` (the default) and `repeat` is `True`, the
+        stream is effectively infinite. If `repeat` is `False`, the stream's
+        duration is the shorter of this value and the time required to emit
+        the sequence once.
+      interval: The delay in seconds between consecutive elements.
+        Defaults to 0.1.
+      repeat: If `True`, the input `data` sequence is emitted repeatedly.
+        If `False` (the default), the sequence is emitted only once.
+      warmup_time: The extra wait time for the impulse element
+        (start, end, interval) to reach `ImpulseSeqGenDoFn`. It is used to
+        avoid the events clustering at the beginning.
+  """
+  def __init__(
+      self,
+      data: Sequence[Any],
+      max_duration: Optional[float] = None,
+      interval: float = 0.1,
+      repeat: bool = False,

Review Comment:
   I think `max_duration` and `repeat` are controlling two different things. One is to control how long is the stream going to be, and the other is to control the behavior **if** the duration is longer than interval * (len(data) - 1).



##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -202,3 +244,79 @@ def expand(self, pbegin):
       result = result | 'ApplyWindowing' >> beam.WindowInto(
           window.FixedWindows(self.interval))
     return result
+
+
+class PeriodicStream(beam.PTransform):
+  """A PTransform that generates a periodic stream of elements from a sequence.
+
+  This transform creates a `PCollection` by emitting elements from a provided
+  Python sequence at a specified time interval. It is designed for use in
+  streaming pipelines to simulate a live, continuous source of data.
+
+  The transform can be configured to:
+  - Emit the sequence only once.
+  - Repeat the sequence indefinitely or for a maximum duration.
+  - Control the time interval between elements.
+
+  To ensure that the stream does not emit a burst of elements immediately at
+  pipeline startup, a fixed warmup period is added before the first element
+  is generated.
+
+  Args:
+      data: The sequence of elements to emit into the PCollection. The elements
+        can be raw values or pre-timestamped tuples in the format
+        `(apache_beam.utils.timestamp.Timestamp, value)`.
+      max_duration: The maximum total duration in seconds for the stream
+        generation. If `None` (the default) and `repeat` is `True`, the
+        stream is effectively infinite. If `repeat` is `False`, the stream's
+        duration is the shorter of this value and the time required to emit
+        the sequence once.
+      interval: The delay in seconds between consecutive elements.
+        Defaults to 0.1.
+      repeat: If `True`, the input `data` sequence is emitted repeatedly.
+        If `False` (the default), the sequence is emitted only once.
+      warmup_time: The extra wait time for the impulse element
+        (start, end, interval) to reach `ImpulseSeqGenDoFn`. It is used to
+        avoid the events clustering at the beginning.
+  """
+  def __init__(
+      self,
+      data: Sequence[Any],
+      max_duration: Optional[float] = None,
+      interval: float = 0.1,
+      repeat: bool = False,

Review Comment:
   I think `max_duration` and `repeat` are controlling two different things. One is to control how long is the stream going to be, and the other is to control the behavior **if** the duration is longer than `interval * (len(data) - 1)`.



-- 
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] Move PeriodicStream into periodicsequence. [beam]

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


##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -91,7 +94,39 @@ class ImpulseSeqGenDoFn(beam.DoFn):
   ImpulseSeqGenDoFn can't guarantee that each element is output at exact time.
   ImpulseSeqGenDoFn guarantees that elements would not be output prior to
   given runtime timestamp.
+
+  The output mode of the DoFn is based on the input `data`:
+
+    - **None**: If `data` is None (by default), the output element will be the

Review Comment:
   I am afraid this will introduce a breaking change on the graph.



-- 
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] Move PeriodicStream into periodicsequence. [beam]

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


##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -91,7 +94,39 @@ class ImpulseSeqGenDoFn(beam.DoFn):
   ImpulseSeqGenDoFn can't guarantee that each element is output at exact time.
   ImpulseSeqGenDoFn guarantees that elements would not be output prior to
   given runtime timestamp.
+
+  The output mode of the DoFn is based on the input `data`:
+
+    - **None**: If `data` is None (by default), the output element will be the

Review Comment:
   Sure. But notice that this will introduce a breaking change on the  pipeline DAG.



-- 
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] Move PeriodicStream into periodicsequence. [beam]

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

   Assigning reviewers:
   
   R: @jrmccluskey for label python.
   
   
   Note: If you would like to opt out of this review, comment `assign to next reviewer`.
   
   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] Move PeriodicStream into periodicsequence. [beam]

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


##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -202,3 +244,79 @@ def expand(self, pbegin):
       result = result | 'ApplyWindowing' >> beam.WindowInto(
           window.FixedWindows(self.interval))
     return result
+
+
+class PeriodicStream(beam.PTransform):
+  """A PTransform that generates a periodic stream of elements from a sequence.
+
+  This transform creates a `PCollection` by emitting elements from a provided
+  Python sequence at a specified time interval. It is designed for use in
+  streaming pipelines to simulate a live, continuous source of data.
+
+  The transform can be configured to:
+  - Emit the sequence only once.
+  - Repeat the sequence indefinitely or for a maximum duration.
+  - Control the time interval between elements.
+
+  To ensure that the stream does not emit a burst of elements immediately at
+  pipeline startup, a fixed warmup period is added before the first element
+  is generated.
+
+  Args:
+      data: The sequence of elements to emit into the PCollection. The elements
+        can be raw values or pre-timestamped tuples in the format
+        `(apache_beam.utils.timestamp.Timestamp, value)`.
+      max_duration: The maximum total duration in seconds for the stream
+        generation. If `None` (the default) and `repeat` is `True`, the
+        stream is effectively infinite. If `repeat` is `False`, the stream's
+        duration is the shorter of this value and the time required to emit
+        the sequence once.
+      interval: The delay in seconds between consecutive elements.
+        Defaults to 0.1.
+      repeat: If `True`, the input `data` sequence is emitted repeatedly.
+        If `False` (the default), the sequence is emitted only once.
+      warmup_time: The extra wait time for the impulse element
+        (start, end, interval) to reach `ImpulseSeqGenDoFn`. It is used to
+        avoid the events clustering at the beginning.
+  """
+  def __init__(
+      self,
+      data: Sequence[Any],
+      max_duration: Optional[float] = None,
+      interval: float = 0.1,
+      repeat: bool = False,

Review Comment:
   I think max_duration and repeat are controlling two different things. One is to control how long is the stream going to be, and the other is to control the behavior **if** the duration is longer than interval * (len(data) - 1).



-- 
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] Move PeriodicStream into periodicsequence. [beam]

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


##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -202,3 +244,79 @@ def expand(self, pbegin):
       result = result | 'ApplyWindowing' >> beam.WindowInto(
           window.FixedWindows(self.interval))
     return result
+
+
+class PeriodicStream(beam.PTransform):
+  """A PTransform that generates a periodic stream of elements from a sequence.
+
+  This transform creates a `PCollection` by emitting elements from a provided
+  Python sequence at a specified time interval. It is designed for use in
+  streaming pipelines to simulate a live, continuous source of data.
+
+  The transform can be configured to:
+  - Emit the sequence only once.
+  - Repeat the sequence indefinitely or for a maximum duration.
+  - Control the time interval between elements.
+
+  To ensure that the stream does not emit a burst of elements immediately at
+  pipeline startup, a fixed warmup period is added before the first element
+  is generated.
+
+  Args:
+      data: The sequence of elements to emit into the PCollection. The elements
+        can be raw values or pre-timestamped tuples in the format
+        `(apache_beam.utils.timestamp.Timestamp, value)`.
+      max_duration: The maximum total duration in seconds for the stream
+        generation. If `None` (the default) and `repeat` is `True`, the
+        stream is effectively infinite. If `repeat` is `False`, the stream's
+        duration is the shorter of this value and the time required to emit
+        the sequence once.
+      interval: The delay in seconds between consecutive elements.
+        Defaults to 0.1.
+      repeat: If `True`, the input `data` sequence is emitted repeatedly.
+        If `False` (the default), the sequence is emitted only once.
+      warmup_time: The extra wait time for the impulse element
+        (start, end, interval) to reach `ImpulseSeqGenDoFn`. It is used to
+        avoid the events clustering at the beginning.
+  """
+  def __init__(
+      self,
+      data: Sequence[Any],
+      max_duration: Optional[float] = None,
+      interval: float = 0.1,
+      repeat: bool = False,
+      warmup_time: float = 2.0):

Review Comment:
   I find that the time domain of `start_timestamp`, `stop_timestamp` and `fire_interval` is a bit confusing in `PeriodicImpulse`. 
   
   It seems that the `start_timestamp` and `stop_timestamp` are about event time. The current behavior when `start_timestamp < now` is to catch up event time to current time, which is very unintuitive. Assuming someone specifies start_timestamp to 0, then most cpu time will be spent on emitting billions of events. 
   
   After the event time counter catches up to `now`, then `fire_interval` takes into affect but now this interval sounds like an interval of processing time.
   
   Given such inconsistency, my thought is to hide `start_timestamp`/`stop_timstamp` from the useage. By setting `start_timestamp = Timestamp.now()` (and plus some warm up time), we can let it behave as close to processing time as possible. If users want to inject events with old timestamps, they can specify the event-time timestamps in the `data` array. For example, they can put data = [(Timestamp(1), 1), (Timestamp(2), 2)], which then simulates two events coming from 1970s and it takes 55 years for them to arrive at and process at the current time.



-- 
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] Move PeriodicStream into periodicsequence. [beam]

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


##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -202,3 +244,79 @@ def expand(self, pbegin):
       result = result | 'ApplyWindowing' >> beam.WindowInto(
           window.FixedWindows(self.interval))
     return result
+
+
+class PeriodicStream(beam.PTransform):
+  """A PTransform that generates a periodic stream of elements from a sequence.
+
+  This transform creates a `PCollection` by emitting elements from a provided
+  Python sequence at a specified time interval. It is designed for use in
+  streaming pipelines to simulate a live, continuous source of data.
+
+  The transform can be configured to:
+  - Emit the sequence only once.
+  - Repeat the sequence indefinitely or for a maximum duration.
+  - Control the time interval between elements.
+
+  To ensure that the stream does not emit a burst of elements immediately at
+  pipeline startup, a fixed warmup period is added before the first element
+  is generated.
+
+  Args:
+      data: The sequence of elements to emit into the PCollection. The elements
+        can be raw values or pre-timestamped tuples in the format
+        `(apache_beam.utils.timestamp.Timestamp, value)`.
+      max_duration: The maximum total duration in seconds for the stream
+        generation. If `None` (the default) and `repeat` is `True`, the
+        stream is effectively infinite. If `repeat` is `False`, the stream's
+        duration is the shorter of this value and the time required to emit
+        the sequence once.
+      interval: The delay in seconds between consecutive elements.
+        Defaults to 0.1.
+      repeat: If `True`, the input `data` sequence is emitted repeatedly.
+        If `False` (the default), the sequence is emitted only once.
+      warmup_time: The extra wait time for the impulse element
+        (start, end, interval) to reach `ImpulseSeqGenDoFn`. It is used to
+        avoid the events clustering at the beginning.
+  """
+  def __init__(
+      self,
+      data: Sequence[Any],
+      max_duration: Optional[float] = None,
+      interval: float = 0.1,
+      repeat: bool = False,
+      warmup_time: float = 2.0):

Review Comment:
   I find that the time domain of `start_timestamp`, `stop_timestamp` and `fire_interval` is a bit confusing in `PeriodicImpulse`. 
   
   It seems that the `start_timestamp` and `stop_timestamp` are about event time. The current behavior when `start_timestamp < now` is to catch up event time to current time, which is very unintuitive. Assuming someone specifies `start_timestamp` to 0, then most cpu time will be spent on emitting billions of events. 
   
   After the event time counter catches up to `now`, then `fire_interval` takes into affect but now this interval sounds like an interval of processing time.
   
   Given such inconsistency, my thought is to hide `start_timestamp`/`stop_timstamp` from the useage. By setting `start_timestamp = Timestamp.now()` (and plus some warm up time), we can let it behave as close to processing time as possible. If users want to inject events with old timestamps, they can specify the event-time timestamps in the `data` array. For example, they can put `data = [(Timestamp(1), 1), (Timestamp(2), 2)]`, which then simulates two events coming from 1970s and it takes 55 years for them to arrive and be processed at the current time.



-- 
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] Move PeriodicStream into periodicsequence. [beam]

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


##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -202,3 +244,79 @@ def expand(self, pbegin):
       result = result | 'ApplyWindowing' >> beam.WindowInto(
           window.FixedWindows(self.interval))
     return result
+
+
+class PeriodicStream(beam.PTransform):
+  """A PTransform that generates a periodic stream of elements from a sequence.
+
+  This transform creates a `PCollection` by emitting elements from a provided
+  Python sequence at a specified time interval. It is designed for use in
+  streaming pipelines to simulate a live, continuous source of data.
+
+  The transform can be configured to:
+  - Emit the sequence only once.
+  - Repeat the sequence indefinitely or for a maximum duration.
+  - Control the time interval between elements.
+
+  To ensure that the stream does not emit a burst of elements immediately at
+  pipeline startup, a fixed warmup period is added before the first element
+  is generated.
+
+  Args:
+      data: The sequence of elements to emit into the PCollection. The elements
+        can be raw values or pre-timestamped tuples in the format
+        `(apache_beam.utils.timestamp.Timestamp, value)`.
+      max_duration: The maximum total duration in seconds for the stream
+        generation. If `None` (the default) and `repeat` is `True`, the
+        stream is effectively infinite. If `repeat` is `False`, the stream's
+        duration is the shorter of this value and the time required to emit
+        the sequence once.
+      interval: The delay in seconds between consecutive elements.
+        Defaults to 0.1.
+      repeat: If `True`, the input `data` sequence is emitted repeatedly.
+        If `False` (the default), the sequence is emitted only once.
+      warmup_time: The extra wait time for the impulse element
+        (start, end, interval) to reach `ImpulseSeqGenDoFn`. It is used to
+        avoid the events clustering at the beginning.
+  """
+  def __init__(
+      self,
+      data: Sequence[Any],
+      max_duration: Optional[float] = None,
+      interval: float = 0.1,

Review Comment:
   Sure.



-- 
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] Move PeriodicStream into periodicsequence. [beam]

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


##########
sdks/python/apache_beam/transforms/periodicsequence.py:
##########
@@ -202,3 +244,79 @@ def expand(self, pbegin):
       result = result | 'ApplyWindowing' >> beam.WindowInto(
           window.FixedWindows(self.interval))
     return result
+
+
+class PeriodicStream(beam.PTransform):
+  """A PTransform that generates a periodic stream of elements from a sequence.
+
+  This transform creates a `PCollection` by emitting elements from a provided
+  Python sequence at a specified time interval. It is designed for use in
+  streaming pipelines to simulate a live, continuous source of data.
+
+  The transform can be configured to:
+  - Emit the sequence only once.
+  - Repeat the sequence indefinitely or for a maximum duration.
+  - Control the time interval between elements.
+
+  To ensure that the stream does not emit a burst of elements immediately at
+  pipeline startup, a fixed warmup period is added before the first element
+  is generated.
+
+  Args:
+      data: The sequence of elements to emit into the PCollection. The elements
+        can be raw values or pre-timestamped tuples in the format
+        `(apache_beam.utils.timestamp.Timestamp, value)`.
+      max_duration: The maximum total duration in seconds for the stream
+        generation. If `None` (the default) and `repeat` is `True`, the
+        stream is effectively infinite. If `repeat` is `False`, the stream's
+        duration is the shorter of this value and the time required to emit
+        the sequence once.
+      interval: The delay in seconds between consecutive elements.
+        Defaults to 0.1.
+      repeat: If `True`, the input `data` sequence is emitted repeatedly.
+        If `False` (the default), the sequence is emitted only once.
+      warmup_time: The extra wait time for the impulse element
+        (start, end, interval) to reach `ImpulseSeqGenDoFn`. It is used to
+        avoid the events clustering at the beginning.
+  """
+  def __init__(
+      self,
+      data: Sequence[Any],
+      max_duration: Optional[float] = None,
+      interval: float = 0.1,
+      repeat: bool = False,

Review Comment:
   I think `max_duration` and `repeat` are controlling two different things. One is to control how long the stream is going to be, and the other is to control the behavior **if** the duration is longer than `interval * (len(data) - 1)`.



-- 
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