deepthi912 commented on code in PR #20321:
URL: https://github.com/apache/pinot/pull/20321#discussion_r4639877133
##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/upsert/BasePartitionUpsertMetadataManager.java:
##########
@@ -761,6 +779,67 @@ public void replaceSegment(ImmutableSegment segment,
@Nullable ThreadSafeMutable
}
}
+ /// Reverts a segment replacement that failed partway, so that no primary
key points at the new segment and the new
+ /// segment can be destroyed. The keys moved off the old segment point at it
again ([#revertReplacedKeys]), and the
+ /// other keys still pointing at the new segment are removed. The old
segment then serves as before the replacement,
+ /// and replacing it again converges to the same metadata as a replacement
that never failed, except that:
+ /// - Keys the new segment took from other segments stay removed until the
replacement is retried, because the rows
+ /// they had in those segments are already invalidated.
+ /// - For tables with inconsistent state, the previous record locations
dropped for the moved keys are not restored.
+ ///
+ /// A failure of the revert is attached to the replacement failure and
reported, but not thrown.
+ ///
+ /// @param validDocIdsForOldSegment Copy of the old segment's valid doc ids
that the replacement updated instead of
+ /// the old segment's own bitmap, or `null`
if it updated the bitmap in place
+ /// @param oldValidDocIdsBeforeReplace Old segment's valid doc ids before a
replacement that updated them in place
+ protected void revertFailedReplace(ImmutableSegment segment, IndexSegment
oldSegment,
+ @Nullable MutableRoaringBitmap validDocIdsForOldSegment,
+ @Nullable MutableRoaringBitmap oldValidDocIdsBeforeReplace,
RuntimeException replaceFailure) {
+ String segmentName = segment.getSegmentName();
+ try {
+ MutableRoaringBitmap oldValidDocIds =
getValidDocIdsForOldSegment(oldSegment);
+ if (oldValidDocIds != null) {
+ // The docs moved off the old segment are missing from the copy if the
old segment's bitmap is kept intact, and
+ // missing from the old segment's bitmap otherwise.
+ boolean oldValidDocIdsIntact = validDocIdsForOldSegment != null;
+ MutableRoaringBitmap movedDocIds = oldValidDocIdsIntact
+ ? MutableRoaringBitmap.andNot(oldValidDocIds,
validDocIdsForOldSegment)
+ : MutableRoaringBitmap.andNot(oldValidDocIdsBeforeReplace,
oldValidDocIds);
+ if (!movedDocIds.isEmpty()) {
+ _logger.info("Pointing primary keys of up to {} docs back at the old
segment: {} after failing to replace it",
+ movedDocIds.getCardinality(), segmentName);
+ revertReplacedKeys(segment, oldSegment, movedDocIds,
oldValidDocIdsIntact);
+ }
+ }
+ MutableRoaringBitmap validDocIds =
UpsertUtils.getValidDocIdsSnapshotFromSegment(segment, true);
+ if (!validDocIds.isEmpty()) {
+ _logger.warn("Removing up to {} primary keys added by the failed
replacement of segment: {}",
+ validDocIds.getCardinality(), segmentName);
+ removeSegment(segment, validDocIds);
+ }
+ updatePrimaryKeyGauge();
+ } catch (RuntimeException revertFailure) {
+ replaceFailure.addSuppressed(revertFailure);
+ _logger.error("UPSERT_METADATA_REVERT_FAILED: table={}, partition={},
segment={}. Failed to revert the failed "
+ + "segment replacement, primary keys might still point at the
new segment", _tableNameWithType,
+ _partitionId, segmentName, revertFailure);
+ _serverMetrics.addMeteredTableValue(_tableNameWithType,
ServerMeter.UPSERT_METADATA_REVERT_FAILURES, 1);
+ }
+ }
+
+ /// Points the primary keys a failed replacement moved off the old segment
back at it, along with their valid (and
+ /// queryable) docs. A key is pointed back only while it still points at the
new segment. When the replacement kept
+ /// the old segment's bitmap intact, the old doc of a key that has moved on
since, e.g. to a consuming segment, must
+ /// be invalidated instead.
+ ///
+ /// The default implementation leaves the keys pointing at the new segment,
so [#revertFailedReplace] removes them.
+ ///
+ /// @param movedDocIds Docs of the old segment whose primary keys might have
moved to the new segment
+ /// @param oldValidDocIdsIntact Whether the replacement kept the old
segment's bitmap intact
+ protected void revertReplacedKeys(IndexSegment segment, IndexSegment
oldSegment, MutableRoaringBitmap movedDocIds,
Review Comment:
**[upserts-theseus] MAJOR: the default `revertReplacedKeys` is destructive**
(judged statically)
Can this be abstract, or throw, instead of a no-op? With the no-op, the
removal step deletes the moved keys. Under NONE the old copy's bits are still
set, so those rows stay visible with no metadata key and the next update adds a
second valid row. Under SYNC/SNAPSHOT the bits were cleared in place, so the
keys vanish. Any manager configured through `metadataManagerClass` that doesn't
override this hook takes that path without any signal. An abstract method turns
that into a compile error.
##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/upsert/BasePartitionUpsertMetadataManager.java:
##########
@@ -761,6 +779,67 @@ public void replaceSegment(ImmutableSegment segment,
@Nullable ThreadSafeMutable
}
}
+ /// Reverts a segment replacement that failed partway, so that no primary
key points at the new segment and the new
+ /// segment can be destroyed. The keys moved off the old segment point at it
again ([#revertReplacedKeys]), and the
+ /// other keys still pointing at the new segment are removed. The old
segment then serves as before the replacement,
+ /// and replacing it again converges to the same metadata as a replacement
that never failed, except that:
+ /// - Keys the new segment took from other segments stay removed until the
replacement is retried, because the rows
+ /// they had in those segments are already invalidated.
+ /// - For tables with inconsistent state, the previous record locations
dropped for the moved keys are not restored.
+ ///
+ /// A failure of the revert is attached to the replacement failure and
reported, but not thrown.
+ ///
+ /// @param validDocIdsForOldSegment Copy of the old segment's valid doc ids
that the replacement updated instead of
+ /// the old segment's own bitmap, or `null`
if it updated the bitmap in place
+ /// @param oldValidDocIdsBeforeReplace Old segment's valid doc ids before a
replacement that updated them in place
+ protected void revertFailedReplace(ImmutableSegment segment, IndexSegment
oldSegment,
+ @Nullable MutableRoaringBitmap validDocIdsForOldSegment,
+ @Nullable MutableRoaringBitmap oldValidDocIdsBeforeReplace,
RuntimeException replaceFailure) {
+ String segmentName = segment.getSegmentName();
+ try {
+ MutableRoaringBitmap oldValidDocIds =
getValidDocIdsForOldSegment(oldSegment);
+ if (oldValidDocIds != null) {
+ // The docs moved off the old segment are missing from the copy if the
old segment's bitmap is kept intact, and
+ // missing from the old segment's bitmap otherwise.
+ boolean oldValidDocIdsIntact = validDocIdsForOldSegment != null;
+ MutableRoaringBitmap movedDocIds = oldValidDocIdsIntact
+ ? MutableRoaringBitmap.andNot(oldValidDocIds,
validDocIdsForOldSegment)
+ : MutableRoaringBitmap.andNot(oldValidDocIdsBeforeReplace,
oldValidDocIds);
Review Comment:
**[upserts-theseus] MAJOR [UPS-P1]: under SYNC/SNAPSHOT the revert can
restore a row that already lost** (judged statically)
`before andNot live` contains more than the docs this replace moved. It also
contains docs another thread invalidated in place during the replace.
Sketch (not replayed):
1. The bitmap copy is taken.
2. Consuming segment C takes K from the old segment S, which clears S's doc
in place.
3. The new copy R reaches K and takes it from C, on a higher value or a tie
it wins.
4. A later record makes the replace throw.
`revertReplacedKeys` sees K on R and points it back at S's doc with S's
comparison value, while C's newer row stays invalid. The replica serves the
stale row, and a partial upsert loses its merge base. Under NONE the same
history only loses K, which is the documented gap.
Could the replace record the old doc ids it actually moved, in an
accumulator bitmap for both modes, instead of diffing the live bitmap?
--
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]