dabla commented on code in PR #77716: URL: https://github.com/apache/airflow/pull/77716#discussion_r4726801943
########## providers/ftp/docs/filesystems/ftp.rst: ########## @@ -0,0 +1,61 @@ + .. 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. + +FTP Filesystem +============== + +Use ``ObjectStoragePath`` with FTP servers through the FTP implementation that ships with +`fsspec <https://filesystem-spec.readthedocs.io/>`__. No extra package is needed. + +URL format: ``ftp://connection_id@hostname/path/to/file``. Review Comment: **[warning]** The `hostname` in the URL is ignored: the server always comes from the connection. With a connection whose host is `connhost`, I get this on the head of this PR (fsspec 2026.7.0, universal-pathlib 0.3.10, `ftplib.FTP` mocked): ``` ftp://my_conn@myserver/data/file.csv -> str(path) == "ftp://my_conn@/data/file.csv", fs.host == "connhost" ftp://my_conn@otherserver/data/file.csv -> str(path) == "ftp://my_conn@/data/file.csv", fs.host == "connhost" ObjectStoragePath("ftp://my_conn@myserver/x") == ObjectStoragePath("ftp://my_conn@otherserver/x") # True ``` So someone who writes `ftp://my_conn@backup-server/data` reads and writes on the host of `my_conn`, and the lineage URI of the path has no host at all. Could the page say that the host (and port) come from the connection and that the host part of the URL is not used, and show the example as `ftp://my_conn@/data/file.csv`? The sftp page has the same sentence; I only checked ftp. ########## providers/ftp/docs/filesystems/ftp.rst: ########## @@ -0,0 +1,61 @@ + .. 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. + +FTP Filesystem +============== + +Use ``ObjectStoragePath`` with FTP servers through the FTP implementation that ships with +`fsspec <https://filesystem-spec.readthedocs.io/>`__. No extra package is needed. + +URL format: ``ftp://connection_id@hostname/path/to/file``. + +Configuration +------------- + +Uses the standard FTP connection: host, port (21 by default), login and password. The following +extras are supported: + +* ``passive`` - set to ``false`` to use active mode. Passive mode is on by default. +* ``encoding`` - encoding of the file names on the server, for example ``cp1251``. + +FTPS is not supported. Review Comment: **[nit]** One more limit worth a line here: a copy between two paths of the same connection is not supported. `ObjectStoragePath.copy` takes the same-store branch and calls [`self.fs.copy(...)`](https://github.com/apache/airflow/blob/b4965c0ceb1b552e660bc7efa96400170b414c88/task-sdk/src/airflow/sdk/io/path.py#L382), and fsspec's FTP filesystem has no `cp_file` (FTP has no server-side copy). Against a local pyftpdlib server with fsspec 2026.9.0, `fs.copy("/w.bin", "/w2.bin")` raises a bare `NotImplementedError`. `move` within the connection works (it is a rename). The example below this line copies to `file://`, which works. ########## providers/ftp/src/airflow/providers/ftp/fs/ftp.py: ########## @@ -0,0 +1,64 @@ +# 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 + +from typing import TYPE_CHECKING, Any + +from fsspec.implementations.ftp import FTPFileSystem Review Comment: **[nit]** `fsspec` is now imported at runtime by the provider but is not in its `dependencies`. It arrives through `apache-airflow` today (`fsspec>=2023.10.0` in both Airflow 2.11 core and the Task SDK), so nothing breaks, but the google and azure providers declare the fsspec package their `fs` module imports. Declaring `fsspec>=2023.10.0` in `providers/ftp/pyproject.toml` keeps it explicit. That floor is enough for this code: `encoding=` is already in [`FTPFileSystem.__init__` at 2023.10.0](https://github.com/fsspec/filesystem_spec/blob/2023.10.0/fsspec/implementations/ftp.py). ########## providers/ftp/src/airflow/providers/ftp/fs/ftp.py: ########## @@ -0,0 +1,64 @@ +# 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 + +from typing import TYPE_CHECKING, Any + +from fsspec.implementations.ftp import FTPFileSystem + +from airflow.providers.common.compat.sdk import BaseHook +from airflow.providers.ftp.hooks.ftp import _get_passive_mode + +if TYPE_CHECKING: + from fsspec import AbstractFileSystem + +schemes = ["ftp"] + + +class _ActiveModeFTPFileSystem(FTPFileSystem): + """ + ``FTPFileSystem`` that turns passive mode off on every connection. + + fsspec has no option for it and keeps the ``ftplib`` default (passive), while ``FTPHook`` + honors the ``passive`` connection extra. ``_connect`` also runs when a read reconnects. + """ + + def _connect(self) -> None: + super()._connect() + self.ftp.set_pasv(False) + + +def get_fs(conn_id: str | None, storage_options: dict[str, Any] | None = None) -> AbstractFileSystem: Review Comment: **[warning]** After the server closes an idle control connection, listing a directory returns an empty list instead of failing. The filesystem returned here holds one `ftplib.FTP` control connection, and Airflow keeps it for the life of the process: [`ObjectStore.fs` is a `cached_property`](https://github.com/apache/airflow/blob/b4965c0ceb1b552e660bc7efa96400170b414c88/task-sdk/src/airflow/sdk/io/store.py#L59-L63) on a store held in the module-level [`_STORE_CACHE`](https://github.com/apache/airflow/blob/b4965c0ceb1b552e660bc7efa96400170b414c88/task-sdk/src/airflow/sdk/io/store.py#L127). fsspec only reconnects in one place (after a failed `ABOR` in `FTPFile._fetch_range`), so once the server's idle timeout has passed, the connection stays dead. Reproduced against a local pyftpdlib server with a 2 s idle timeout, fsspec 2026.9.0: list `/`, wait 3.5 s, then ``` fs.ls("/sub", detail=False) -> [] # /sub exists; no exception fs.open("/w.bin").read(10) -> BrokenPipeError(32, 'Broken pipe') ``` The empty list comes from `FTPFileSystem.ls`: `mlsd` raises an `ftplib.Error`, the `except Error` branch falls back to `info()`, which answers from the cached parent listing, and `out` stays `[]`. For a transfer that lists a source folder after a long step elsewhere (vsftpd's default `idle_session_timeout` is 300 s), that is "nothing to copy" and a green task. A triggerer process, for the deferrable mode the AIP plans, would hit it on every run after the first. Since the PR already subclasses `FTPFileSystem` for active mode, one way is a single subclass used for both modes (passive as a constructor argument) whose `ls` and `_open` check the connection first (a `NOOP`, reconnecting through `_connect` on failure). Reporting the `ls` fallback to fsspec is worth doing either way. If you would rather keep this PR small, a note on the docs page plus a follow-up before the first transfer operator moves onto it works for me too. -- 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]
