You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "jason810496 (via GitHub)" <gi...@apache.org> on 2025/06/22 10:50:07 UTC

[PR] Add validation for commit_cadence in Kafka ConsumeFromTopicOperator [airflow]

jason810496 opened a new pull request, #52015:
URL: https://github.com/apache/airflow/pull/52015

   
   closes: #34213
   
   ## Why
   
   Based on the discussion in the issue
   - if we need to respect `commit_cadence="never", "end_of_batch", "end_of_operator"`
     - we should set `enable.auto.commit` option in Kafka Connection to `false`
     - otherwise `enable.auto.commit` would be on by default, and the consumer will auto commit the offset every 5 seconds.
   
   ## What
   
   - Add validation for `commit_cadence` with `enable.auto.commit` option with corresponding Kafka Connection 
     - Will log warning if the `commit_cadence` is set but `enable.auto.commit` is not `false`
   - Point out this behavior in the documentation as well 
   
   
   


-- 
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@airflow.apache.org

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


Re: [PR] Add validation for commit_cadence in Kafka ConsumeFromTopicOperator [airflow]

Posted by "jason810496 (via GitHub)" <gi...@apache.org>.
jason810496 commented on code in PR #52015:
URL: https://github.com/apache/airflow/pull/52015#discussion_r2160301422


##########
providers/apache/kafka/src/airflow/providers/apache/kafka/operators/consume.py:
##########
@@ -184,3 +182,27 @@ def execute(self, context) -> Any:
         consumer.close()
 
         return
+
+    def _validate_commit_cadence(self):
+        """Validate the commit cadence configuration."""
+        if self.commit_cadence and self.commit_cadence not in VALID_COMMIT_CADENCE:
+            raise AirflowException(
+                f"commit_cadence must be one of {VALID_COMMIT_CADENCE}. Got {self.commit_cadence}"
+            )
+
+        kafka_config = self.hook.get_connection(self.kafka_config_id).extra_dejson
+        # Same as kafka's behavior, default to "true" if not set
+        enable_auto_commit = kafka_config.get("enable.auto.commit", "true").lower()
+
+        if self.commit_cadence and enable_auto_commit != "false":
+            self.log.warning(

Review Comment:
   I decide not to raise error but log warning to user for backward compatibitlity.



-- 
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@airflow.apache.org

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


Re: [PR] Add validation for commit_cadence in Kafka ConsumeFromTopicOperator [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #52015:
URL: https://github.com/apache/airflow/pull/52015#discussion_r2166085753


##########
providers/apache/kafka/src/airflow/providers/apache/kafka/operators/consume.py:
##########
@@ -82,7 +82,7 @@ def __init__(
         apply_function_batch: Callable[..., Any] | str | None = None,
         apply_function_args: Sequence[Any] | None = None,
         apply_function_kwargs: dict[Any, Any] | None = None,
-        commit_cadence: str | None = "end_of_operator",
+        commit_cadence: str = "end_of_operator",

Review Comment:
   Yeah makes sense.



-- 
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@airflow.apache.org

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


Re: [PR] Add validation for commit_cadence in Kafka ConsumeFromTopicOperator [airflow]

Posted by "jason810496 (via GitHub)" <gi...@apache.org>.
jason810496 commented on PR #52015:
URL: https://github.com/apache/airflow/pull/52015#issuecomment-3020260595

   Just rebased and resolved conflict.
   Any comment for this PR @eladkal ?


-- 
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@airflow.apache.org

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


Re: [PR] Add validation for commit_cadence in Kafka ConsumeFromTopicOperator [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #52015:
URL: https://github.com/apache/airflow/pull/52015#discussion_r2162953467


##########
providers/apache/kafka/src/airflow/providers/apache/kafka/operators/consume.py:
##########
@@ -82,7 +82,7 @@ def __init__(
         apply_function_batch: Callable[..., Any] | str | None = None,
         apply_function_args: Sequence[Any] | None = None,
         apply_function_kwargs: dict[Any, Any] | None = None,
-        commit_cadence: str | None = "end_of_operator",
+        commit_cadence: str = "end_of_operator",

Review Comment:
   Hm i wonder why there was a None before



##########
providers/apache/kafka/src/airflow/providers/apache/kafka/operators/consume.py:
##########
@@ -184,3 +182,27 @@ def execute(self, context) -> Any:
         consumer.close()
 
         return
+
+    def _validate_commit_cadence(self):
+        """Validate the commit cadence configuration."""
+        if self.commit_cadence and self.commit_cadence not in VALID_COMMIT_CADENCE:
+            raise AirflowException(
+                f"commit_cadence must be one of {VALID_COMMIT_CADENCE}. Got {self.commit_cadence}"
+            )
+
+        kafka_config = self.hook.get_connection(self.kafka_config_id).extra_dejson
+        # Same as kafka's behavior, default to "true" if not set
+        enable_auto_commit = kafka_config.get("enable.auto.commit", "true").lower()
+
+        if self.commit_cadence and enable_auto_commit != "false":
+            self.log.warning(

Review Comment:
   Sounds good!
   



##########
providers/apache/kafka/src/airflow/providers/apache/kafka/operators/consume.py:
##########
@@ -177,10 +176,33 @@ def execute(self, context) -> Any:
                 self.log.info("committing offset at %s", self.commit_cadence)
                 consumer.commit()
 
-        if self.commit_cadence:
+        if self.commit_cadence != "never":
             self.log.info("committing offset at %s", self.commit_cadence)
             consumer.commit()
 
         consumer.close()
 
         return
+
+    def _validate_commit_cadence_on_construct(self):
+        """Validate the commit_cadence parameter when the operator is constructed."""
+        if self.commit_cadence and self.commit_cadence not in VALID_COMMIT_CADENCE:
+            raise AirflowException(
+                f"commit_cadence must be one of {VALID_COMMIT_CADENCE}. Got {self.commit_cadence}"
+            )
+
+    def _validate_commit_cadence_before_execute(self):
+        """Validate the commit_cadence parameter before executing the operator."""
+        kafka_config = self.hook.get_connection(self.kafka_config_id).extra_dejson
+        # Same as kafka's behavior, default to "true" if not set
+        enable_auto_commit = str(kafka_config.get("enable.auto.commit", "true")).lower()
+
+        if self.commit_cadence and enable_auto_commit != "false":
+            self.log.warning(
+                "To respect commit_cadence='%s', "
+                "'enable.auto.commit' should be set to 'false' in the Kafka connection configuration. "
+                "Currently, 'enable.auto.commit' is not explicitly set, so it defaults to 'true', which causes "
+                "the consumer to auto-commit offsets every 5 seconds. "
+                "See: https://kafka.apache.org/documentation/#consumerconfigs_enable.auto.commit",

Review Comment:
   ```suggestion
                   "See: https://kafka.apache.org/documentation/#consumerconfigs_enable.auto.commit for more information",
   ```



-- 
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@airflow.apache.org

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


Re: [PR] Add validation for commit_cadence in Kafka ConsumeFromTopicOperator [airflow]

Posted by "jason810496 (via GitHub)" <gi...@apache.org>.
jason810496 commented on code in PR #52015:
URL: https://github.com/apache/airflow/pull/52015#discussion_r2163213786


##########
providers/apache/kafka/src/airflow/providers/apache/kafka/operators/consume.py:
##########
@@ -82,7 +82,7 @@ def __init__(
         apply_function_batch: Callable[..., Any] | str | None = None,
         apply_function_args: Sequence[Any] | None = None,
         apply_function_kwargs: dict[Any, Any] | None = None,
-        commit_cadence: str | None = "end_of_operator",
+        commit_cadence: str = "end_of_operator",

Review Comment:
   Before this fix, if `commit_cadence="never"`, the `self.commit_cadence` attribute will be set to None at ( https://github.com/apache/airflow/blob/201e66312bc35dbc8c34ade155c6958a8781e733/providers/apache/kafka/src/airflow/providers/apache/kafka/operators/consume.py#L120-L121 ) , then at the end of `execute` ( https://github.com/apache/airflow/blob/201e66312bc35dbc8c34ade155c6958a8781e733/providers/apache/kafka/src/airflow/providers/apache/kafka/operators/consume.py#L180-L182 ) will check if `self.commit_cadence` is set or not to decide whether to commit.
   
   IMO, we should just check whether the `self.commit_cadence` is `"never"` or not to decide whether to commit. Also I add a new `test_commit_cadence_behavior` test to ensure the consistent behavior for this small change.



-- 
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@airflow.apache.org

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


Re: [PR] Add validation for commit_cadence in Kafka ConsumeFromTopicOperator [airflow]

Posted by "jason810496 (via GitHub)" <gi...@apache.org>.
jason810496 merged PR #52015:
URL: https://github.com/apache/airflow/pull/52015


-- 
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@airflow.apache.org

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


Re: [PR] Add validation for commit_cadence in Kafka ConsumeFromTopicOperator [airflow]

Posted by "jason810496 (via GitHub)" <gi...@apache.org>.
jason810496 commented on code in PR #52015:
URL: https://github.com/apache/airflow/pull/52015#discussion_r2163213786


##########
providers/apache/kafka/src/airflow/providers/apache/kafka/operators/consume.py:
##########
@@ -82,7 +82,7 @@ def __init__(
         apply_function_batch: Callable[..., Any] | str | None = None,
         apply_function_args: Sequence[Any] | None = None,
         apply_function_kwargs: dict[Any, Any] | None = None,
-        commit_cadence: str | None = "end_of_operator",
+        commit_cadence: str = "end_of_operator",

Review Comment:
   Before this fix, if `commit_cadence="never"`, the `self.commit_cadence` attribute will be set to `None` at ( https://github.com/apache/airflow/blob/201e66312bc35dbc8c34ade155c6958a8781e733/providers/apache/kafka/src/airflow/providers/apache/kafka/operators/consume.py#L120-L121 ) , then at the end of `execute` ( https://github.com/apache/airflow/blob/201e66312bc35dbc8c34ade155c6958a8781e733/providers/apache/kafka/src/airflow/providers/apache/kafka/operators/consume.py#L180-L182 ) will check if `self.commit_cadence` is set or not to decide whether to commit.
   
   IMO, we should just check whether the `self.commit_cadence` is `"never"` or not to decide whether to commit. Also I add a new `test_commit_cadence_behavior` test to ensure the consistent behavior for this small change.



-- 
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@airflow.apache.org

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