You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "david-streamlio (via GitHub)" <gi...@apache.org> on 2026/09/01 22:26:35 UTC

Re: [PR] [fix][fn] Support deadLetterTopic and maxMessageRetries for Python functions [pulsar]

david-streamlio commented on code in PR #26400:
URL: https://github.com/apache/pulsar/pull/26400#discussion_r3908966948


##########
pulsar-functions/instance/src/main/python/python_instance.py:
##########
@@ -184,6 +186,7 @@ def run(self):
         unacked_messages_timeout_ms=int(self.timeout_ms) if self.timeout_ms else None,
         initial_position=position,
         properties=properties,
+        dead_letter_policy=dead_letter_policy,

Review Comment:
   Agreed — documenting the requirement, not defending against old clients. Done in `python/README.md`.
   
   I checked the floor against the client sources rather than taking it on trust, and it is where you
   place it: `pulsar/__init__.py` at tag `v3.2.0` contains neither `dead_letter_policy` nor
   `ConsumerDeadLetterPolicy`; `v3.3.0` introduces both and guards with `if dead_letter_policy:`, so
   passing `None` is a no-op from 3.3.0 on.
   
   One extra data point for the README wording: I diffed every keyword the runtime passes to
   `subscribe()` and `create_producer()` against the v3.2.0 signature, and `dead_letter_policy` is the
   only one 3.2.0 lacks. So 3.3.0 is exactly the floor this introduces — nothing else in the runtime was
   already above it.
   
   The section states the requirement, notes it is only visible on a self-managed worker (where
   `RuntimeUtils` launches the host `python3` and the installed client is the operator's), and gives the
   `requirements.txt` pin for a zip-packaged function. The matching note for apache/pulsar-site#1214
   follows.
   
   One thing worth your call, since it is a project-level statement rather than a PR-level one: this
   README line is the first declared minimum pulsar-client-python version anywhere in the repo. 3.3.0 is
   the technical floor for this change, but if the project wants to state a higher supported minimum,
   that number should be yours rather than mine and I will use whatever you prefer.
   



##########
pulsar-functions/instance/src/test/python/test_python_instance.py:
##########
@@ -396,3 +397,75 @@ def test_result_is_splattable_into_subscribe_kwargs(self):
     args = self._instance(delay_ms=250).get_negative_ack_args()
     self.assertIsInstance(args, dict)
     self.assertEqual(["negative_ack_redelivery_delay_ms"], list(args.keys()))
+
+class TestDeadLetterPolicy(unittest.TestCase):
+  """Covers FunctionDetails.retryDetails -> ConsumerDeadLetterPolicy.
+
+  The Java runtime applies these in JavaInstanceRunnable (guarded on hasRetryDetails) and
+  PulsarSource (maxMessageRetries >= 0, deadLetterTopic only when non-empty). The Python runtime
+  previously ignored retryDetails entirely.
+
+  The configured redelivery count is passed straight through, matching Java: PulsarSource builds a
+  policy for any value >= 0 and ConsumerBuilderImpl.deadLetterPolicy then rejects anything below 1.
+  Whether a subscription type can act on the policy is a client concern, so the runtime does not
+  gate on it.
+  """
+
+  def _instance(self, max_message_retries=None, dead_letter_topic=None):
+    function_details = Function_pb2.FunctionDetails()
+    function_details.sink.topic = "test_sink_topic"
+    if max_message_retries is not None:
+      function_details.retryDetails.maxMessageRetries = max_message_retries
+    if dead_letter_topic is not None:
+      function_details.retryDetails.deadLetterTopic = dead_letter_topic
+
+    return PythonInstance('test_instance', 'test_func', '1.0', function_details, 100, 30,
+                          'user_code', Mock(), Mock(), 'test_cluster', 'test_url', None)
+
+  def test_no_retry_details_means_no_policy(self):
+    instance = self._instance()
+    self.assertIsNone(instance.get_dead_letter_policy())
+
+  def test_policy_built_from_retry_details(self):
+    instance = self._instance(max_message_retries=3,
+                              dead_letter_topic="persistent://public/default/my-dlq")
+    policy = instance.get_dead_letter_policy()
+
+    self.assertIsNotNone(policy)
+    self.assertEqual(3, policy.max_redeliver_count)
+    self.assertEqual("persistent://public/default/my-dlq", policy.dead_letter_topic)
+
+  def test_empty_dead_letter_topic_defers_to_client_default(self):
+    # The Java runtime only sets the topic when non-empty, leaving the client to derive
+    # "<topic>-<subscription>-DLQ". Passing "" through would override that with an invalid name.
+    instance = self._instance(max_message_retries=2)
+    policy = instance.get_dead_letter_policy()
+
+    self.assertIsNotNone(policy)
+    self.assertEqual(2, policy.max_redeliver_count)
+
+  def test_zero_retries_fails_fast(self):
+    # Java does not start with this value either: PulsarSource forwards 0 and
+    # ConsumerBuilderImpl.deadLetterPolicy rejects "MaxRedeliverCount must be > 0". Returning None
+    # here instead would let localrun - which bypasses validateNonJavaFunction - start with retries
+    # silently disabled while Java fails.
+    instance = self._instance(max_message_retries=0,
+                              dead_letter_topic="persistent://public/default/my-dlq")
+    with self.assertRaises(ValueError):
+      instance.get_dead_letter_policy()
+
+  def test_negative_retries_fails_fast(self):
+    instance = self._instance(max_message_retries=-1)
+    with self.assertRaises(ValueError):
+      instance.get_dead_letter_policy()
+
+  def test_policy_is_not_gated_on_subscription_type(self):

Review Comment:
   You are right, and I reproduced it the same way before changing anything: restoring the gate leaves
   the old test green. The reason is that `_instance()` builds a `FunctionDetails` with no ordering
   fields set, which selects **Shared** — the one subscription type the gate allowed — so the assertion
   never reached the branch it claimed to pin.
   
   Rewritten to exercise the types the gate actually suppressed. `run()` derives the consumer type from
   `FunctionDetails`: `retainOrdering`, `processingGuarantees == EFFECTIVELY_ONCE` and
   `source.subscriptionType == FAILOVER` each select Failover, and `retainKeyOrdering` selects
   KeyShared. The test now runs all four as subTests, so a gate reintroduced here would have to read one
   of those fields and would fail.
   
   Mutation-tested: with the `5cc6207b` gate restored (derived from `FunctionDetails`, since the method
   no longer takes a `consumer_type`), 3 of the 4 subTests fail — `retainOrdering`, `EFFECTIVELY_ONCE`
   and `FAILOVER subscriptionType`. The `retainKeyOrdering` case correctly still passes, because the old
   gate permitted KeyShared.
   



##########
pulsar-functions/instance/src/test/python/test_python_instance.py:
##########
@@ -396,3 +397,75 @@ def test_result_is_splattable_into_subscribe_kwargs(self):
     args = self._instance(delay_ms=250).get_negative_ack_args()
     self.assertIsInstance(args, dict)
     self.assertEqual(["negative_ack_redelivery_delay_ms"], list(args.keys()))
+
+class TestDeadLetterPolicy(unittest.TestCase):
+  """Covers FunctionDetails.retryDetails -> ConsumerDeadLetterPolicy.
+
+  The Java runtime applies these in JavaInstanceRunnable (guarded on hasRetryDetails) and
+  PulsarSource (maxMessageRetries >= 0, deadLetterTopic only when non-empty). The Python runtime
+  previously ignored retryDetails entirely.
+
+  The configured redelivery count is passed straight through, matching Java: PulsarSource builds a
+  policy for any value >= 0 and ConsumerBuilderImpl.deadLetterPolicy then rejects anything below 1.
+  Whether a subscription type can act on the policy is a client concern, so the runtime does not
+  gate on it.
+  """
+
+  def _instance(self, max_message_retries=None, dead_letter_topic=None):
+    function_details = Function_pb2.FunctionDetails()
+    function_details.sink.topic = "test_sink_topic"
+    if max_message_retries is not None:
+      function_details.retryDetails.maxMessageRetries = max_message_retries
+    if dead_letter_topic is not None:
+      function_details.retryDetails.deadLetterTopic = dead_letter_topic
+
+    return PythonInstance('test_instance', 'test_func', '1.0', function_details, 100, 30,
+                          'user_code', Mock(), Mock(), 'test_cluster', 'test_url', None)
+
+  def test_no_retry_details_means_no_policy(self):
+    instance = self._instance()
+    self.assertIsNone(instance.get_dead_letter_policy())
+
+  def test_policy_built_from_retry_details(self):
+    instance = self._instance(max_message_retries=3,
+                              dead_letter_topic="persistent://public/default/my-dlq")
+    policy = instance.get_dead_letter_policy()
+
+    self.assertIsNotNone(policy)
+    self.assertEqual(3, policy.max_redeliver_count)
+    self.assertEqual("persistent://public/default/my-dlq", policy.dead_letter_topic)
+
+  def test_empty_dead_letter_topic_defers_to_client_default(self):

Review Comment:
   Right on both counts, and the reason the test could not pin the `or None` is that there was nothing
   to pin — so I removed the guard rather than writing a better test for it.
   
   Verified against the CI-pinned client (3.13.0, and 3.10.0 behaves the same):
   `ConsumerDeadLetterPolicy.__init__` forwards the topic only `if dead_letter_topic is not None`, but
   `DeadLetterPolicyBuilder.deadLetterTopic("")` and never calling it produce an indistinguishable
   object — `getDeadLetterTopic()` returns `""` in both cases. So `or None` could not change what the
   consumer receives, and no assertion on the returned policy could have separated the two.
   
   That also makes the old comment's rationale wrong, as you say: passing `""` does not "override the
   default with an invalid name", it is simply the same as leaving it unset, and the client derives
   `<topic>-<subscription>-DLQ` from there either way.
   
   The guard is gone, the docstring now describes what the client actually does, and the test pins the
   half that is the runtime's to get right: `retryDetails` with no topic still yields a policy carrying
   the redelivery count, and the runtime does not invent a DLQ name of its own. Mutation-tested both
   ways — returning `None` for an empty topic fails it, and inventing a name fails it.
   



-- 
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: commits-unsubscribe@pulsar.apache.org

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