RajaVinoth96 commented on code in PR #20263:
URL: https://github.com/apache/pinot/pull/20263#discussion_r4802133946


##########
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:
   Agreed, done in ea43b66. A failed directory sync now logs a warning and 
continues, like Lucene; file syncs still fail.



##########
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:
   Agreed, the reprocess and tier move paths keep the copy now (9f4d72d). The 
only delete left is for a copy we just downloaded, and that moved into 
`moveSegment` in 7b292d2, before anything can move it to a tier, so the 
wrong-directory issue is gone too.



##########
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:
   Good catch, done in 7b292d2. The sync is in `moveSegment` now, so every 
caller of `downloadSegment` gets it, including 
`downloadAndReplaceConsumingSegment`. For "only when loading rewrote 
something", the reprocess sync is at the end of 
`ImmutableSegmentLoader.preprocess`, since callers can't tell from outside 
whether `load` reprocessed. The tier loader syncs its own move.



##########
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:
   Done in 499ee2d: `syncDirectoryRecursively` now skips an entry that 
disappears after its directory was listed. A missing root still fails.



-- 
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]

Reply via email to