-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Refactor BaseTableDataManager #18381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
krishan1390
wants to merge
15
commits into
apache:master
Choose a base branch
from
krishan1390:basetdm_refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f3cd165
Refactor BaseTableDataManager so multi-segment managers can compose i…
krishan1390 cc2afba
Cleanup comment
krishan1390 ab53cb5
Refactor BaseTDM and its tests to enable cleaner extensions, abstract…
krishan1390 1ae36c4
Merge remote-tracking branch 'upstream/master' into basetdm_refactor
krishan1390 0eb9597
Cleanup deleteSegment
krishan1390 953952b
Move offloadSegment to deleteSegment
krishan1390 c818a8c
Reload should take lock first and then acquire semaphore
krishan1390 ccdfbb4
Cleanup BaseTDM
krishan1390 52fa8e5
Merge remote-tracking branch 'upstream/master' into basetdm_refactor
krishan1390 9a9c567
Acquire then take segment lock in reload
krishan1390 3ea1b9c
Acquire reload semaphore for realtime force commit too
krishan1390 23afba3
Make some accessors public in TDM
krishan1390 eb0602b
Merge remote-tracking branch 'upstream/master' into basetdm_refactor
krishan1390 1f67c75
Make some accessors public in TDM
krishan1390 25af3f2
Merge remote-tracking branch 'upstream/master' into basetdm_refactor
krishan1390 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -606,6 +606,56 @@ protected void doOffloadSegment(String segmentName) { | |
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteSegment(String segmentName) | ||
| throws Exception { | ||
| _logger.info("Deleting segment: {}", segmentName); | ||
| Lock segmentLock = getSegmentLock(segmentName); | ||
| segmentLock.lock(); | ||
| try { | ||
| if (hasSegment(segmentName)) { | ||
| _logger.warn("Segment: {} is still loaded, offloading it before delete", segmentName); | ||
| offloadSegment(segmentName); | ||
| } | ||
| doDeleteSegment(segmentName); | ||
| } catch (Exception e) { | ||
| addSegmentError(segmentName, | ||
| new SegmentErrorInfo(System.currentTimeMillis(), "Caught exception while deleting segment", e)); | ||
| throw e; | ||
| } finally { | ||
| segmentLock.unlock(); | ||
| } | ||
| } | ||
|
|
||
| protected void doDeleteSegment(String segmentName) | ||
| throws Exception { | ||
| deleteSegmentFilesFromDisk(_tableDataDir, segmentName, _instanceDataManagerConfig); | ||
| _logger.info("Deleted segment: {}", segmentName); | ||
| } | ||
|
|
||
| /** | ||
| * Removes the segment directory locally and does tier-aware cleanup too | ||
| */ | ||
| public static void deleteSegmentFilesFromDisk(String tableDataDir, String segmentName, | ||
| InstanceDataManagerConfig instanceConfig) | ||
| throws Exception { | ||
| File segmentDir = new File(tableDataDir, segmentName); | ||
| if (segmentDir.exists()) { | ||
| FileUtils.deleteQuietly(segmentDir); | ||
| LOGGER.info("Deleted segment directory {} on default tier", segmentDir); | ||
| } | ||
| SegmentDirectoryLoader segmentLoader = | ||
| SegmentDirectoryLoaderRegistry.getSegmentDirectoryLoader(instanceConfig.getSegmentDirectoryLoader()); | ||
| if (segmentLoader != null) { | ||
| LOGGER.info("Deleting segment: {} further with segment loader: {}", segmentName, | ||
| instanceConfig.getSegmentDirectoryLoader()); | ||
| SegmentDirectoryLoaderContext ctx = new SegmentDirectoryLoaderContext.Builder().setSegmentName(segmentName) | ||
| .setTableDataDir(tableDataDir) | ||
| .build(); | ||
| segmentLoader.delete(ctx); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void reloadSegment(String segmentName, boolean forceDownload, String reloadJobId) | ||
| throws Exception { | ||
|
|
@@ -616,12 +666,7 @@ public void reloadSegment(String segmentName, boolean forceDownload, String relo | |
| SegmentDataManager segmentDataManager = _segmentDataManagerMap.get(segmentName); | ||
| if (segmentDataManager != null) { | ||
| IndexLoadingConfig indexLoadingConfig = fetchIndexLoadingConfig(); | ||
| _segmentReloadSemaphore.acquire(segmentName, _logger); | ||
| try { | ||
| reloadSegment(segmentDataManager, indexLoadingConfig, forceDownload); | ||
| } finally { | ||
| _segmentReloadSemaphore.release(); | ||
| } | ||
| reloadSegment(segmentDataManager, indexLoadingConfig, forceDownload); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved the sempahore to inside reloadSegment |
||
| } else { | ||
| _logger.warn("Failed to find segment: {}, skipping reloading it", segmentName); | ||
| } | ||
|
|
@@ -947,12 +992,7 @@ protected void reloadSegments(List<SegmentDataManager> segmentDataManagers, Inde | |
| CompletableFuture.allOf(segmentDataManagers.stream().map(segmentDataManager -> CompletableFuture.runAsync(() -> { | ||
| String segmentName = segmentDataManager.getSegmentName(); | ||
| try { | ||
| _segmentReloadSemaphore.acquire(segmentName, _logger); | ||
| try { | ||
| reloadSegment(segmentDataManager, indexLoadingConfig, forceDownload); | ||
| } finally { | ||
| _segmentReloadSemaphore.release(); | ||
| } | ||
| reloadSegment(segmentDataManager, indexLoadingConfig, forceDownload); | ||
| } catch (Throwable t) { | ||
| _logger.error("Caught exception while reloading segment: {}", segmentName, t); | ||
| failedSegments.add(segmentName); | ||
|
|
@@ -1016,7 +1056,10 @@ public void reloadSegment(String segmentName, IndexLoadingConfig indexLoadingCon | |
| File indexDir = getSegmentDataDir(segmentName, segmentTier, indexLoadingConfig.getTableConfig()); | ||
| Lock segmentLock = getSegmentLock(segmentName); | ||
| segmentLock.lock(); | ||
| boolean semaphoreAcquired = false; | ||
| try { | ||
| _segmentReloadSemaphore.acquire(segmentName, _logger); | ||
|
krishan1390 marked this conversation as resolved.
Outdated
|
||
| semaphoreAcquired = true; | ||
| /* | ||
| Determines if a segment should be downloaded from deep storage based on: | ||
| 1. A forced download flag. | ||
|
|
@@ -1091,6 +1134,9 @@ public void reloadSegment(String segmentName, IndexLoadingConfig indexLoadingCon | |
| reloadFailureException)); | ||
| throw reloadFailureException; | ||
| } finally { | ||
| if (semaphoreAcquired) { | ||
| _segmentReloadSemaphore.release(); | ||
| } | ||
| segmentLock.unlock(); | ||
| } | ||
| _logger.info("Reloaded segment: {}", segmentName); | ||
|
|
@@ -1410,6 +1456,22 @@ protected void removeBackup(File indexDir) | |
|
|
||
| @Override | ||
| public boolean tryLoadExistingSegment(SegmentZKMetadata zkMetadata, IndexLoadingConfig indexLoadingConfig) { | ||
| ImmutableSegment segment = tryLoadExistingSegmentInternal(zkMetadata, indexLoadingConfig); | ||
| if (segment == null) { | ||
| return false; | ||
| } | ||
| addSegment(segment, zkMetadata); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Just Loads a segment from the existing on-disk copy without registering it in {@code _segmentDataManagerMap} or | ||
| * invoking other hooks. | ||
| * Returns {@code null} when the on-disk copy is absent, has a stale CRC under or fails to load | ||
| */ | ||
| @Nullable | ||
| protected ImmutableSegment tryLoadExistingSegmentInternal(SegmentZKMetadata zkMetadata, | ||
| IndexLoadingConfig indexLoadingConfig) { | ||
| String segmentName = zkMetadata.getSegmentName(); | ||
| Preconditions.checkState(!_shutDown, | ||
| "Table data manager is already shut down, cannot load existing segment: %s of table: %s", segmentName, | ||
|
|
@@ -1442,14 +1504,14 @@ public boolean tryLoadExistingSegment(SegmentZKMetadata zkMetadata, IndexLoading | |
| if (segmentMetadata == null) { | ||
| _logger.info("Segment: {} does not exist", segmentName); | ||
| closeSegmentDirectoryQuietly(segmentDirectory); | ||
| return false; | ||
| return null; | ||
| } | ||
| if (isSegmentStatusCompleted(zkMetadata) && !hasSameCRC(zkMetadata, segmentMetadata)) { | ||
| _logger.warn("Segment: {} has CRC changed from: {} to: {}", segmentName, segmentMetadata.getCrc(), | ||
| zkMetadata.getCrc()); | ||
| if (_instanceDataManagerConfig.shouldCheckCRCOnSegmentLoad()) { | ||
| closeSegmentDirectoryQuietly(segmentDirectory); | ||
| return false; | ||
| return null; | ||
| } | ||
| _logger.info("Skipping CRC check for segment: {} as configured. Proceed to load segment.", segmentName); | ||
| } | ||
|
|
@@ -1470,15 +1532,14 @@ public boolean tryLoadExistingSegment(SegmentZKMetadata zkMetadata, IndexLoading | |
| indexLoadingConfig, zkMetadata); | ||
| } | ||
| ImmutableSegment segment = ImmutableSegmentLoader.load(segmentDirectory, indexLoadingConfig); | ||
| addSegment(segment, zkMetadata); | ||
| _logger.info("Loaded existing segment: {} with CRC: {} on tier: {}", segmentName, zkMetadata.getCrc(), | ||
| TierConfigUtils.normalizeTierName(segmentTier)); | ||
| return true; | ||
| return segment; | ||
| } catch (Exception e) { | ||
| _logger.error("Failed to load existing segment: {} with CRC: {} on tier: {}", segmentName, zkMetadata.getCrc(), | ||
| TierConfigUtils.normalizeTierName(segmentTier), e); | ||
| closeSegmentDirectoryQuietly(segmentDirectory); | ||
| return false; | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1542,8 +1603,7 @@ public List<StaleSegment> getStaleSegments() { | |
| return staleSegments; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| StaleSegment isSegmentStale(IndexLoadingConfig indexLoadingConfig, SegmentDataManager segmentDataManager) { | ||
| protected StaleSegment isSegmentStale(IndexLoadingConfig indexLoadingConfig, SegmentDataManager segmentDataManager) { | ||
| TableConfig tableConfig = indexLoadingConfig.getTableConfig(); | ||
| Schema schema = indexLoadingConfig.getSchema(); | ||
| assert tableConfig != null && schema != null; | ||
|
|
@@ -1847,7 +1907,7 @@ protected SegmentDirectory initSegmentDirectory(String segmentName, String segme | |
|
|
||
| // CRC check can be performed on both segment CRC and data CRC (if available) based on the ZK property value of | ||
| // useDataCRC. | ||
| protected static boolean hasSameCRC(SegmentZKMetadata zkMetadata, SegmentMetadata localMetadata) { | ||
| public static boolean hasSameCRC(SegmentZKMetadata zkMetadata, SegmentMetadata localMetadata) { | ||
| if (zkMetadata.getCrc() == Long.parseLong(localMetadata.getCrc())) { | ||
| return true; | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is moved from HelixInstanceDataManager to TableDataManager as this is the right place