need to synchronize replacing upsert segment #12105
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For upsert table, replacing segment could happen in two threads concurrently, but there are multiple steps to replace upsert segments and not thread safe. This PR changes to do them with segmentLock to be thread safe. In fact, the partitionUpsertMetadataManager.addSegment/replaceSegment takes segmentLock inside anyway.
For upsert table, when consuming thread commits a newly built immutable segment, it needs to replace the old mutable segment, in order to update the upsert states to point to the new immutable segment. But committing segment can take longer than expected. When committing takes long to complete, server decides to download the segment as a way to help catch up ingestion quicker. The HelixTaskExecutor thread, to download and replace segment, tries to stop the consuming thread by setting a _shouldStop flag, but consuming thread doesn't check the flag once in the middle of replacing the segment. The HelixTaskExecutor thread waits up to 10min and then continues to replace the segment. Now we would get two threads replacing the same upsert segment concurrently.
In comparison, for non-upsert RT tables, it's thread safe to add or replace segments as the operation is made atomic; for OFFLINE tables, the segmentLock is taken to add or replace segment.