xiangfu0 commented on code in PR #20263:
URL: https://github.com/apache/pinot/pull/20263#discussion_r4762304340
##########
pinot-common/src/main/java/org/apache/pinot/common/utils/FileUtils.java:
##########
@@ -105,6 +114,69 @@ public static void close(Closeable... closeables)
close(Arrays.asList(closeables));
}
+ /// Forces a regular file's contents and metadata to durable storage.
+ ///
+ /// This flushes the file's dirty pages in the OS page cache rather than a
particular buffer, so on platforms with a
+ /// unified page cache (Linux, macOS, Windows) it also covers changes made
through memory mappings of the file,
+ /// including mappings that have already been unmapped. Java's
[FileChannel#force] contract does not guarantee this
+ /// on every platform.
+ /// @param file the regular file to sync
+ /// @throws IOException if the file cannot be opened or synced
+ public static void syncFile(File file)
+ throws IOException {
+ try (FileChannel channel = FileChannel.open(file.toPath(),
StandardOpenOption.WRITE)) {
+ channel.force(true);
+ }
+ }
+
+ /// Forces a directory's entries to durable storage, so that files and
sub-directories created, renamed or deleted in
+ /// it survive a crash.
+ ///
+ /// Syncing a file makes its contents durable but not its name; the name
lives in the parent directory. Java has no
+ /// dedicated API for this, so the directory is opened read-only and forced.
Windows does not allow opening a
+ /// directory this way, and NTFS journals directory changes, so failures
there are ignored.
+ /// @param dir the directory to sync
+ /// @throws IOException if the directory cannot be synced on a platform that
supports directory sync
+ public static void syncDirectory(File dir)
+ throws IOException {
+ try (FileChannel channel = FileChannel.open(dir.toPath(),
StandardOpenOption.READ)) {
+ channel.force(true);
+ } catch (IOException e) {
+ if (!IS_WINDOWS) {
+ throw new IOException("Failed to fsync directory: " + dir, e);
Review Comment:
I'd make a failed `force` on a directory non-fatal here, the way Lucene's
`IOUtils.fsync(path, isDir)` does. It has the same READ open and the same
Windows special case, and then it swallows the `IOException` when `isDir` is
true. Some filesystems reject fsync on a directory (network and FUSE mounts are
the usual offenders), and PostgreSQL ignores `EINVAL`/`EBADF` on a directory
fsync for the same reason.
As written, every durable build fails on such a filesystem, and the load
paths are worse off: `downloadAndLoadSegment`, the reprocess path and the tier
move all treat the failure as an untrusted copy and delete the segment, so a
server would download, delete and fail in a loop. Logging a warning and
continuing seems like the right trade-off for the directory entry, while a
failed file sync stays fatal.
##########
pinot-core/src/main/java/org/apache/pinot/core/data/manager/BaseTableDataManager.java:
##########
@@ -1730,6 +1750,21 @@ public void createBackup(File indexDir) {
"Failed to rename index directory: %s to segment backup directory:
%s", indexDir, segmentBackupDir);
}
+ /// Syncs the directory a just-loaded segment was loaded from to durable
storage, after this server downloaded, copied
+ /// or rewrote it (download, reload, preprocessing, a move to another tier):
every file, every directory bottom-up,
+ /// and the segment directory's own entry in its parent. The loaded
directory is used rather than the original path
+ /// because a tier-aware loader may have moved the segment. Must be called
before the segment is registered, so no
+ /// other thread writes into the directory while it is walked. Segments
without a local directory are skipped.
+ private static void syncLoadedSegmentDirectory(ImmutableSegment segment)
Review Comment:
The assumption that no other thread writes into the directory during the
walk doesn't quite hold on the reload path for upsert tables. The old segment
object stays registered until `addSegment`, and it resolves its snapshot files
from the same `indexDir` path, so a concurrent `persistDocIdsSnapshot` (tmp
file, then rename) or `deleteSnapshotFile` can add and remove entries under the
walk. If a listed file is gone by the time it is visited, `walkFileTree` throws
`NoSuchFileException` and the reload fails.
It's a narrow window and the backup gets restored, but treating a vanished
file as nothing to sync in `syncDirectoryRecursively` would avoid a spurious
reload failure.
##########
pinot-core/src/main/java/org/apache/pinot/core/data/manager/BaseTableDataManager.java:
##########
@@ -1822,8 +1858,21 @@ public ImmutableSegment
tryLoadExistingSegmentWithoutRegistering(SegmentZKMetada
closeSegmentDirectoryQuietly(segmentDirectory);
ImmutableSegmentLoader.preprocess(indexDir, indexLoadingConfig,
_segmentOperationsThrottlerSet, zkMetadata);
segmentDirectory = initSegmentDirectory(segmentName,
zkMetadata.getCrc(), indexLoadingConfig, zkMetadata);
+ reprocessed = true;
}
ImmutableSegment segment = ImmutableSegmentLoader.load(segmentDirectory,
indexLoadingConfig);
+ if (reprocessed) {
+ // Preprocessing rewrote the segment in place, so make it durable
before it is registered and served.
+ try {
+ syncLoadedSegmentDirectory(segment);
+ } catch (Exception e) {
+ segment.destroy();
+ // The copy failed to sync, so it can't be trusted, but it has
already been reprocessed and its CRC still
+ // matches ZK, so a later load would use it as is. Delete it so the
segment is downloaded again.
+ FileUtils.deleteQuietly(indexDir);
Review Comment:
I wouldn't delete the copy here, and the same goes for the tier move in
`TierBasedSegmentDirectoryLoader`. Both act on a segment the server already
had, and it may be the only copy we can get. With peer download or a failed
deep-store upload there may be nothing to download again, and the directory
also holds server-local files that aren't in the deep-store copy, like the
upsert validDocIds snapshots.
Without the delete, the exception still makes this method return null, and
`addNewOnlineSegment` falls back to `downloadAndLoadSegment`, which replaces
the directory only after a download has succeeded. So the delete only changes
the case where that download fails, and there it turns a copy that may not be
durable yet into no copy at all. An unsynced existing copy is the exposure
every segment had before this PR, and I think that's the better failure mode.
If the delete stays on the download path, note that it removes `indexDir`
while the sync ran on the loaded directory, and those differ once a tier loader
has moved the segment.
##########
pinot-core/src/main/java/org/apache/pinot/core/data/manager/BaseTableDataManager.java:
##########
@@ -547,6 +547,17 @@ public void downloadAndLoadSegment(SegmentZKMetadata
zkMetadata, IndexLoadingCon
File indexDir = downloadSegment(zkMetadata);
ImmutableSegment immutableSegment =
ImmutableSegmentLoader.load(indexDir, indexLoadingConfig,
_segmentOperationsThrottlerSet, zkMetadata);
+ // The untar synced the file contents, but not the directory entries
created by the untar and the move into the
+ // data dir; loading may also have reprocessed the segment. Make it
durable before it is registered and served.
+ try {
+ syncLoadedSegmentDirectory(immutableSegment);
Review Comment:
`RealtimeTableDataManager.downloadAndReplaceConsumingSegment` does the same
download and load for a replica that didn't build the segment itself, but it
doesn't get this sync, so the realtime download path still leaves the untar and
move directory entries unsynced.
The reason given in this comment applies to every caller of
`downloadSegment`, so it might be simpler to sync inside `downloadSegment`
after the move, and keep the post-load sync only for the cases where loading
rewrote something.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]