YifanWu created FLINK-41826:
-------------------------------

             Summary: PyFlink DataStream RuntimeContext.get_attempt_number() 
fails in thread mode
                 Key: FLINK-41826
                 URL: https://issues.apache.org/jira/browse/FLINK-41826
             Project: Flink
          Issue Type: Bug
          Components: API / Python
    Affects Versions: 2.2.1, 2.3.0
            Reporter: YifanWu


Calling {{RuntimeContext.get_attempt_number()}} in {{MapFunction.open()}} fails 
during operator initialization in thread mode. The same DataStream job succeeds 
in process mode.

h3. Reproduction
Save as {{attempt.py}} and run {{python attempt.py thread}} and {{python 
attempt.py process}} with Apache Flink 2.2.1.

{code:python}
import sys
from pyflink.common import Configuration, Types
from pyflink.datastream import StreamExecutionEnvironment
from pyflink.datastream.functions import MapFunction


class ReadAttempt(MapFunction):
    def open(self, ctx):
        self.attempt = ctx.get_attempt_number()

    def map(self, value):
        return self.attempt


if __name__ == "__main__":
    config = Configuration()
    config.set_string("python.execution-mode", sys.argv[1])
    config.set_string("python.executable", sys.executable)
    config.set_string("restart-strategy.type", "none")
    env = StreamExecutionEnvironment.get_execution_environment(config)
    env.set_parallelism(1)
    result = env.from_collection([1], type_info=Types.INT()).map(
        ReadAttempt(), output_type=Types.INT()
    )
    with result.execute_and_collect() as output:
        print(list(output))
{code}

*Expected:* Both commands print {{[0]}} for the first execution attempt.
*Actual:* Process mode prints {{[0]}}. Thread mode fails while opening the 
operator, with the following error at 
{{pyflink/fn_execution/datastream/embedded/runtime_context.py:64}}:
{noformat}
pemja.core.PythonException: <class 'AttributeError'>: 'pemja.PyJObject' object 
has no attribute 'getAttemptNumber'
{noformat}

h3. Cause and scope
FLINK-36225 removed the deprecated Java getter in Flink 2.0. The embedded 
Python wrapper still calls {{self._runtime_context.getAttemptNumber()}}. The 
attempt number is now available via 
{{self._runtime_context.getTaskInfo().getAttemptNumber()}}. Other task metadata 
getters in the same wrapper also call removed Java methods and are worth 
checking when fixing this.

* *2.2.1*: Reproduced end to end with the official release in a local 
MiniCluster on macOS arm64, CPython 3.12.16, Java 21.0.10 and PemJa 0.5.7. Both 
modes used the same job.
* *2.3.0*: A native PemJa bridge test with the 2.3.0 Java classes and Python 
method reproduced the exception. With the TaskInfo accessor, the same test 
returned the configured attempt number {{7}}.
* *master*: The [embedded wrapper at 
fce1e478|https://github.com/apache/flink/blob/fce1e478350c60e7cd98bdad7060e45369870d71/flink-python/pyflink/fn_execution/datastream/embedded/runtime_context.py#L34-L72]
 still uses the removed getter.

I'd be happy to contribute a fix and a regression test. Could a committer 
assign this issue to me if the approach looks good?



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to