github-actions[bot] commented on code in PR #41569:
URL: https://github.com/apache/beam/pull/41569#discussion_r4659345007
##########
sdks/python/apache_beam/io/filebasedsink.py:
##########
@@ -252,14 +258,16 @@ def _get_final_name_glob(self, num_shards, w=None):
window_utc = (
'[' + w.start.to_utc_datetime().strftime("%Y-%m-%dT%H-%M-%S") + ', '
+
w.end.to_utc_datetime().strftime("%Y-%m-%dT%H-%M-%S") + ')')
+ # Only the shard name part is a glob. The prefix and suffix must match
+ # literally, or pre_finalize could delete unrelated files.
return ''.join([
- self.file_path_prefix.get(),
+ _escape_glob(self.file_path_prefix.get()),
self.shard_name_glob_format % dict(
num_shards=num_shards,
uuid=(uuid.uuid4()),
window=w,
window_utc=window_utc),
- self.file_name_suffix.get()
+ _escape_glob(self.file_name_suffix.get())
])
Review Comment:
**🔴 Critical**: Escaping `dst_glob` and `src_glob` fixes the
`FileSystems.match()` calls here and in `_check_state_for_finalize_write`, but
`FileBasedSink` subsequently passes the unescaped file/directory paths
(`dst_glob_files` in `pre_finalize` / `pre_finalize_windowed`, `delete_files`
in `finalize_windowed_write`, and `[init_result]` at the end of
`finalize_windowed_write`) to `FileSystems.delete()`.
Because `LocalFileSystem.delete()` and `GCSFileSystem.delete()` call
`self.match(paths)` on their inputs, glob characters in those paths are still
interpreted as patterns:
1. **Overwriting existing outputs in `pre_finalize` /
`pre_finalize_windowed`:** When target shards from a previous run or retry
already exist (e.g. `out[12]-00000-of-00002.output`),
`FileSystems.match([dst_glob])` returns the unescaped path
`.../out[12]-00000-of-00002.output`. Passing `dst_glob_files` to
`FileSystems.delete()` then matches the unescaped path as a glob—deleting
unrelated files like `out1-00000-of-00002.output`, leaving
`out[12]-00000-of-00002.output` untouched, or raising `BeamIOError: Delete
operation failed ... No files found to delete` if nothing matches the unescaped
pattern (such as in `run[1]/out`).
2. **Retrying finalize when `src` and `dst` checksums match
(`delete_files`):** `FileSystems.delete(delete_files)` passes unescaped `src`
paths containing `beam-temp-out[12]-...` or `run[1]/...` to `delete()`, raising
`BeamIOError: No files found to delete`.
3. **Cleaning up `init_result` (`FileSystems.delete([init_result])`):** At
the end of `finalize_windowed_write`, `init_result`
(`.../beam-temp-out[12]-<uuid>` or `.../run[1]/beam-temp-out-<uuid>`) is an
empty directory. Because `init_result` contains `[`, `FileSystem.match()` skips
the `prefix_or_dir == pattern` short-circuit (which checks
`self.exists(pattern)`) and falls back to `self._list()`, which on
`LocalFileSystem` only yields files and never directories.
`FileSystems.delete([init_result])` therefore raises `BeamIOError` (silently
swallowed by `except IOError:`) and leaks the temporary directory.
Note that simply passing `_escape_glob(...)` to `FileSystems.delete()` would
break `S3FileSystem`, `BlobStorageFileSystem`, and `HadoopFileSystem` (whose
`delete()` methods do not call `self.match()` and expect literal paths) and
still would not match an empty directory in `LocalFileSystem.delete()`.
##########
sdks/python/apache_beam/io/filebasedsink_test.py:
##########
@@ -141,6 +141,47 @@ def test_file_sink_writing(self):
# Check that any temp files are deleted.
self.assertCountEqual([shard1, shard2], glob.glob(temp_path + '*'))
+ def _write_and_finalize(self, sink):
+ init_token, writer_results = self._common_init(sink)
+ pre_finalize_results = sink.pre_finalize(init_token, writer_results)
+ return list(
+ sink.finalize_write(init_token, writer_results, pre_finalize_results))
+
+ def test_file_sink_prefix_with_glob_characters(self):
+ temp_dir = self._new_tempdir()
+ temp_path = os.path.join(temp_dir, 'out[12]')
+ # Files that '[12]' would match as a glob, which must not be touched.
+ other_files = [
+ os.path.join(temp_dir, 'out1-00000-of-00002.output'),
+ os.path.join(temp_dir, 'out2-00001-of-00002.output')
+ ]
+ for other_file in other_files:
+ with open(other_file, 'w') as f:
+ f.write('other')
+ sink = MyFileBasedSink(
+ temp_path, file_name_suffix='.output', coder=coders.ToBytesCoder())
+
+ shard1 = temp_path + '-00000-of-00002.output'
+ shard2 = temp_path + '-00001-of-00002.output'
+ self.assertEqual(self._write_and_finalize(sink), [shard1, shard2])
+ self.assertEqual(open(shard1).read(), '[start][a][b][end]')
+ self.assertEqual(open(shard2).read(), '[start][x][y][z][end]')
+ for other_file in other_files:
Review Comment:
**🟠High**: These two tests only run `_write_and_finalize(sink)` when no
prior target shards exist (so `dst_glob_files` is empty in `pre_finalize` and
`FileSystems.delete()` is not called on the target shards), and they do not
verify that the `beam-temp-*` directory was removed:
- In both `test_file_sink_prefix_with_glob_characters` and
`test_file_sink_directory_with_glob_characters`, the `beam-temp-*` directory is
currently leaked inside `temp_dir` because `FileSystems.delete([init_result])`
fails to match the empty directory and silently catches the resulting
`BeamIOError`.
- If `self._write_and_finalize(sink)` is called a second time to exercise
`pre_finalize` deleting existing target shards,
`test_file_sink_prefix_with_glob_characters` deletes `other_files` (`out1-...`
and `out2-...`), and `test_file_sink_directory_with_glob_characters` fails with
`BeamIOError: Delete operation failed`.
Please add assertions that no `beam-temp-*` directories remain in `temp_dir`
and test running `_write_and_finalize(sink)` when target shards already exist.
--
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]