Jackie-Jiang commented on code in PR #20320:
URL: https://github.com/apache/pinot/pull/20320#discussion_r4651820761


##########
pinot-core/src/main/java/org/apache/pinot/core/data/manager/BaseTableDataManager.java:
##########
@@ -540,6 +540,26 @@ protected void 
replaceSegmentIfCrcMismatch(SegmentDataManager segmentDataManager
     _logger.info("Replaced segment: {} with new CRC: {}", segmentName, 
zkMetadata.getCrc());
   }
 
+  /// Returns whether the registered segment is an upsert segment whose upsert 
metadata is not initialized. A new upsert
+  /// segment is registered before its metadata is initialized, so a failed 
add leaves it registered with partial valid
+  /// docs while its CRC still matches ZK.
+  protected boolean hasUninitializedUpsertMetadata(SegmentDataManager 
segmentDataManager) {
+    return isUpsertEnabled()
+        && segmentDataManager instanceof ImmutableSegmentDataManager 
immutableSegmentDataManager
+        && !immutableSegmentDataManager.isPartitionMetadataInitialized();
+  }
+
+  /// Rebuilds the upsert metadata of a registered segment by reloading the 
segment, from the local index directory when
+  /// its CRC matches ZK. The reloaded segment replaces the registered one 
with its upsert metadata initialized.
+  protected void reloadUninitializedUpsertSegment(SegmentDataManager 
segmentDataManager, SegmentZKMetadata zkMetadata,
+      IndexLoadingConfig indexLoadingConfig)
+      throws Exception {
+    String segmentName = segmentDataManager.getSegmentName();
+    _logger.info("Reloading segment: {} because its upsert metadata is not 
initialized", segmentName);
+    SegmentMetadata localMetadata = 
segmentDataManager.getSegment().getSegmentMetadata();
+    reloadSegment(segmentName, indexLoadingConfig, zkMetadata, localMetadata, 
false);

Review Comment:
   Fixed. When adding a new segment fails, the consistent-deletes manager now 
keeps the doc ids of the records the add counted. The add walks de-duplicated 
records in hash order, so these are a subset of the segment's keys rather than 
a doc-id prefix. Removing the segment, as the replacement does, discounts only 
those keys.
   
   
`ConcurrentMapPartitionUpsertMetadataManagerForConsistentDeletesTest.testReplaceSegmentAfterFailedAdd`
 fails an add on the tombstone and replaces the segment. It checks that the 
tombstone keeps a distinct segment count of 2 and survives a TTL sweep while 
the older segment still holds the key.
   



##########
pinot-core/src/main/java/org/apache/pinot/core/data/manager/realtime/RealtimeTableDataManager.java:
##########
@@ -539,6 +539,9 @@ protected void doAddOnlineSegment(String segmentName)
       } finally {
         releaseSegment(segmentDataManager);
       }
+    } else if (hasUninitializedUpsertMetadata(segmentDataManager)) {

Review Comment:
   Fixed in this PR, so it no longer depends on #20321. When rebuilding a 
segment with uninitialized upsert metadata fails, `replaceUpsertSegment()` puts 
the old manager back on its own in place of the Duo and untracks the new 
segment from the upsert view. The old segment stays uninitialized, so the next 
ONLINE retry rebuilds it again, including while ZK is still `COMMITTING`.
   
   The new copy stays loaded until a restart, because the upsert metadata can 
still point at it. Releasing it needs the upsert metadata changes of a failed 
replacement to be reverted, which is left as a TODO. A failed replacement of 
initialized metadata, such as a failed reload, keeps the Duo as before.
   
   
`BaseTableDataManagerPartitionMetadataTest.testOnlineRetryRebuildsAgainAfterFailedRebuild`
 fails the first add and then the first rebuild of a COMMITTING segment under 
each consistency mode. It checks that the old segment stays registered on its 
own and uninitialized, and that the second retry rebuilds the metadata.
   



##########
pinot-core/src/main/java/org/apache/pinot/core/data/manager/BaseTableDataManager.java:
##########
@@ -540,6 +540,26 @@ protected void 
replaceSegmentIfCrcMismatch(SegmentDataManager segmentDataManager
     _logger.info("Replaced segment: {} with new CRC: {}", segmentName, 
zkMetadata.getCrc());
   }
 
+  /// Returns whether the registered segment is an upsert segment whose upsert 
metadata is not initialized. A new upsert
+  /// segment is registered before its metadata is initialized, so a failed 
add leaves it registered with partial valid
+  /// docs while its CRC still matches ZK.
+  protected boolean hasUninitializedUpsertMetadata(SegmentDataManager 
segmentDataManager) {
+    return isUpsertEnabled()
+        && segmentDataManager instanceof ImmutableSegmentDataManager 
immutableSegmentDataManager
+        && !immutableSegmentDataManager.isPartitionMetadataInitialized();
+  }
+
+  /// Rebuilds the upsert metadata of a registered segment by reloading the 
segment, from the local index directory when
+  /// its CRC matches ZK. The reloaded segment replaces the registered one 
with its upsert metadata initialized.
+  protected void reloadUninitializedUpsertSegment(SegmentDataManager 
segmentDataManager, SegmentZKMetadata zkMetadata,
+      IndexLoadingConfig indexLoadingConfig)
+      throws Exception {
+    String segmentName = segmentDataManager.getSegmentName();
+    _logger.info("Reloading segment: {} because its upsert metadata is not 
initialized", segmentName);
+    SegmentMetadata localMetadata = 
segmentDataManager.getSegment().getSegmentMetadata();

Review Comment:
   Fixed. For a segment with uninitialized upsert metadata, `reloadSegment()` 
now deletes the leftover validDocIds snapshot before reloading, and fails if 
the delete fails. The replacement then scans all docs, as the initial add did.
   
   Because the fix is in the reload itself, it also covers the user-triggered 
reload paths: they now repair such a segment instead of trusting the stale 
snapshot, so they don't need to reject it.
   
   
`BaseTableDataManagerTest.testReloadUninitializedUpsertSegmentIgnoresValidDocIdsSnapshot`
 reloads a real segment that has a snapshot file, on a table with metadata TTL 
and snapshots, and checks that the replacement sees no snapshot. Without a 
snapshot, the real metadata manager replaces the segment through its existing 
full-scan path, which existing tests already cover, so the test stops at the 
snapshot.
   



##########
pinot-core/src/main/java/org/apache/pinot/core/data/manager/BaseTableDataManager.java:
##########
@@ -540,6 +540,26 @@ protected void 
replaceSegmentIfCrcMismatch(SegmentDataManager segmentDataManager
     _logger.info("Replaced segment: {} with new CRC: {}", segmentName, 
zkMetadata.getCrc());
   }
 
+  /// Returns whether the registered segment is an upsert segment whose upsert 
metadata is not initialized. A new upsert
+  /// segment is registered before its metadata is initialized, so a failed 
add leaves it registered with partial valid
+  /// docs while its CRC still matches ZK.
+  protected boolean hasUninitializedUpsertMetadata(SegmentDataManager 
segmentDataManager) {
+    return isUpsertEnabled()
+        && segmentDataManager instanceof ImmutableSegmentDataManager 
immutableSegmentDataManager
+        && !immutableSegmentDataManager.isPartitionMetadataInitialized();
+  }
+
+  /// Rebuilds the upsert metadata of a registered segment by reloading the 
segment, from the local index directory when
+  /// its CRC matches ZK. The reloaded segment replaces the registered one 
with its upsert metadata initialized.
+  protected void reloadUninitializedUpsertSegment(SegmentDataManager 
segmentDataManager, SegmentZKMetadata zkMetadata,

Review Comment:
   Fixed. For a segment with uninitialized upsert metadata, the reload no 
longer restores the validDocIds snapshot, so the metadata-TTL guard against a 
CRC mismatch no longer applies. A completed segment whose CRC changed is 
downloaded, and no bitmap is carried across different segment contents.
   
   The changed-CRC case of 
`BaseTableDataManagerTest.testReloadUninitializedUpsertSegmentIgnoresValidDocIdsSnapshot`
 checks that the reload downloads the committed copy.
   



##########
pinot-core/src/main/java/org/apache/pinot/core/data/manager/BaseTableDataManager.java:
##########
@@ -1048,7 +1068,8 @@ protected void handleUpsert(ImmutableSegment 
immutableSegment, @Nullable Segment
       // When adding a new segment, register it *before* 
partitionUpsertMetadataManager.addSegment() fully initializes
       // the validDocIds bitmap. This lets queries access the new segment 
immediately while the bitmap is being built.
       // Without early registration, docs in existing segments that get 
invalidated by this new segment would become
-      // invisible to queries until addSegment() completes.
+      // invisible to queries until addSegment() completes. If addSegment() 
fails, the segment stays registered with
+      // partial metadata until the retried ONLINE transition rebuilds it (see 
hasUninitializedUpsertMetadata()).

Review Comment:
   The routing part is fixed. Before replacing a segment with uninitialized 
upsert metadata, `handleUpsert()` tracks it as newly added again, so queries 
include it while the replacement moves keys from other segments to it. 
`testOnlineRetryRebuildsOnlyUninitializedUpsertSegment` checks that this 
happens before the replacement.
   
   With consistency mode `NONE`, the gap is the same as for any replacement in 
that mode, such as a reload. Only one copy per segment name can be registered, 
so publishing the rebuilt copy early would hide the docs of the old copy that 
haven't moved yet. Serving both copies during the replacement is what the Duo 
does in `SYNC` and `SNAPSHOT`, so I kept the existing order for `NONE`.
   



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