wenjin272 commented on code in PR #943: URL: https://github.com/apache/flink-agents/pull/943#discussion_r3740848307
########## python/flink_agents/runtime/_python_dependency.py: ########## @@ -0,0 +1,180 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################ + +from __future__ import annotations + +import importlib +import os +import sys +import threading +from pathlib import Path +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from collections.abc import Iterator + from types import ModuleType + from typing import Any + +_GENERATION_LOCK = threading.RLock() +_JOB_GENERATIONS: dict[str, str] = {} + + +def ensure_python_dependency_generation(job_id: str, generation: str) -> bool: + """Activate a Flink-managed dependency generation in the Pemja interpreter. + + When Flink replaces a job's temporary dependency directory, remove imports + owned by the previous directory before user actions or resources are loaded. + + Returns: + ``True`` when a different generation was activated, otherwise ``False``. + """ + if not job_id: + msg = "job_id must not be empty" + raise ValueError(msg) + + current_generation = _normalize_path(generation) + if not Path(current_generation).is_dir(): + msg = f"Python dependency generation does not exist: {current_generation}" + raise RuntimeError(msg) + + with _GENERATION_LOCK: + previous_generation = _JOB_GENERATIONS.get(job_id) + if previous_generation == current_generation: + # Pemja inserts configured paths for every interpreter sharing this + # generation. + _deduplicate_and_prepend_paths( + _paths_for_generation(sys.path, current_generation) + ) + return False + + if previous_generation is not None: Review Comment: Thanks for pointing this out. I think #941 and this PR should focus on dependency-generation changes during failover of the same Job. Reusing the TaskManager across different Job IDs is a separate multi-job lifecycle problem, relevant to applications containing multiple jobs and Session Mode. Supporting that scenario requires additional generation cleanup and coordination of process-global import/cache state. I suggest tracking it in a follow-up issue/PR and clarifying that this PR only guarantees same-Job failover recovery. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
