-
Notifications
You must be signed in to change notification settings - Fork 1.5k
add SegmentTierAssigner and refine restful APIs to get segment tier info #9598
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
112 changes: 112 additions & 0 deletions
112
.../src/main/java/org/apache/pinot/controller/helix/core/relocation/SegmentTierAssigner.java
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 |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.pinot.controller.helix.core.relocation; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.base.Preconditions; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import org.apache.commons.collections.CollectionUtils; | ||
| import org.apache.helix.zookeeper.datamodel.ZNRecord; | ||
| import org.apache.pinot.common.metadata.segment.SegmentZKMetadata; | ||
| import org.apache.pinot.common.metrics.ControllerMetrics; | ||
| import org.apache.pinot.common.tier.Tier; | ||
| import org.apache.pinot.common.tier.TierSegmentSelector; | ||
| import org.apache.pinot.common.utils.config.TierConfigUtils; | ||
| import org.apache.pinot.controller.ControllerConf; | ||
| import org.apache.pinot.controller.LeadControllerManager; | ||
| import org.apache.pinot.controller.helix.core.PinotHelixResourceManager; | ||
| import org.apache.pinot.controller.helix.core.periodictask.ControllerPeriodicTask; | ||
| import org.apache.pinot.spi.config.table.TableConfig; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
|
|
||
| /** | ||
| * Periodic task to calculate the target tier the segment belongs to and set it into segment ZK metadata as goal | ||
| * state, which can be checked by servers when loading the segment to put it onto the target storage tier. | ||
| */ | ||
| public class SegmentTierAssigner extends ControllerPeriodicTask<Void> { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(SegmentTierAssigner.class); | ||
|
|
||
| public SegmentTierAssigner(PinotHelixResourceManager pinotHelixResourceManager, | ||
| LeadControllerManager leadControllerManager, ControllerConf config, ControllerMetrics controllerMetrics) { | ||
| super(SegmentTierAssigner.class.getSimpleName(), config.getSegmentTierAssignerFrequencyInSeconds(), | ||
| config.getSegmentTierAssignerInitialDelayInSeconds(), pinotHelixResourceManager, leadControllerManager, | ||
| controllerMetrics); | ||
| } | ||
|
|
||
| @Override | ||
| protected void processTable(String tableNameWithType) { | ||
| TableConfig tableConfig = _pinotHelixResourceManager.getTableConfig(tableNameWithType); | ||
| Preconditions.checkState(tableConfig != null, "Failed to find table config for table: {}", tableNameWithType); | ||
| List<Tier> sortedTiers; | ||
| if (CollectionUtils.isEmpty(tableConfig.getTierConfigsList())) { | ||
| LOGGER.info("No tierConfigs so use default tier for segments of table: {}", tableNameWithType); | ||
| sortedTiers = Collections.emptyList(); | ||
| } else { | ||
| LOGGER.info("Checking and updating target tiers for segments of table: {}", tableNameWithType); | ||
| sortedTiers = TierConfigUtils | ||
| .getSortedTiers(tableConfig.getTierConfigsList(), _pinotHelixResourceManager.getHelixZkManager()); | ||
| LOGGER.debug("Sorted tiers: {} configured for table: {}", sortedTiers, tableNameWithType); | ||
| } | ||
| for (String segmentName : _pinotHelixResourceManager.getSegmentsFor(tableNameWithType, true)) { | ||
klsince marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| updateSegmentTier(tableNameWithType, segmentName, sortedTiers); | ||
| } | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| void updateSegmentTier(String tableNameWithType, String segmentName, List<Tier> sortedTiers) { | ||
| ZNRecord segmentMetadataZNRecord = | ||
| _pinotHelixResourceManager.getSegmentMetadataZnRecord(tableNameWithType, segmentName); | ||
| if (segmentMetadataZNRecord == null) { | ||
| LOGGER.debug("No ZK metadata for segment: {} of table: {}", segmentName, tableNameWithType); | ||
| return; | ||
| } | ||
| Tier targetTier = null; | ||
| for (Tier tier : sortedTiers) { | ||
| TierSegmentSelector tierSegmentSelector = tier.getSegmentSelector(); | ||
| if (tierSegmentSelector.selectSegment(tableNameWithType, segmentName)) { | ||
| targetTier = tier; | ||
| break; | ||
| } | ||
| } | ||
| SegmentZKMetadata segmentZKMetadata = new SegmentZKMetadata(segmentMetadataZNRecord); | ||
| String targetTierName = null; | ||
| if (targetTier == null) { | ||
| if (segmentZKMetadata.getTier() == null) { | ||
| LOGGER.debug("Segment: {} of table: {} is already on the default tier", segmentName, tableNameWithType); | ||
| return; | ||
| } | ||
| LOGGER.info("Segment: {} of table: {} is put back on default tier", segmentName, tableNameWithType); | ||
klsince marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| targetTierName = targetTier.getName(); | ||
| if (targetTierName.equals(segmentZKMetadata.getTier())) { | ||
| LOGGER.debug("Segment: {} of table: {} is already on the target tier: {}", segmentName, tableNameWithType, | ||
| targetTierName); | ||
| return; | ||
| } | ||
| LOGGER.info("Segment: {} of table: {} is put onto new tier: {}", segmentName, tableNameWithType, targetTierName); | ||
| } | ||
| // Update the tier in segment ZK metadata and write it back to ZK. | ||
| segmentZKMetadata.setTier(targetTierName); | ||
| _pinotHelixResourceManager | ||
| .updateZkMetadata(tableNameWithType, segmentZKMetadata, segmentMetadataZNRecord.getVersion()); | ||
| } | ||
| } | ||
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
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
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
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
86 changes: 86 additions & 0 deletions
86
.../test/java/org/apache/pinot/controller/helix/core/relocation/SegmentTierAssignerTest.java
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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.pinot.controller.helix.core.relocation; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import org.apache.helix.zookeeper.datamodel.ZNRecord; | ||
| import org.apache.pinot.common.metadata.segment.SegmentZKMetadata; | ||
| import org.apache.pinot.common.tier.FixedTierSegmentSelector; | ||
| import org.apache.pinot.common.tier.Tier; | ||
| import org.apache.pinot.controller.ControllerConf; | ||
| import org.apache.pinot.controller.helix.core.PinotHelixResourceManager; | ||
| import org.apache.pinot.spi.utils.CommonConstants; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertNull; | ||
|
|
||
|
|
||
| public class SegmentTierAssignerTest { | ||
| @Test | ||
| public void testUpdateSegmentTierBackToDefault() { | ||
| String tableName = "table01_OFFLINE"; | ||
| String segmentName = "seg01"; | ||
| PinotHelixResourceManager helixMgrMock = mock(PinotHelixResourceManager.class); | ||
| when(helixMgrMock.getSegmentMetadataZnRecord(tableName, segmentName)) | ||
| .thenReturn(createSegmentMetadataZNRecord(segmentName, "hotTier")); | ||
| SegmentTierAssigner assigner = new SegmentTierAssigner(helixMgrMock, null, new ControllerConf(), null); | ||
|
|
||
| // Move back to default as not tier configs. | ||
| List<Tier> sortedTiers = new ArrayList<>(); | ||
| assigner.updateSegmentTier(tableName, "seg01", sortedTiers); | ||
| ArgumentCaptor<SegmentZKMetadata> recordCapture = ArgumentCaptor.forClass(SegmentZKMetadata.class); | ||
| verify(helixMgrMock).updateZkMetadata(eq(tableName), recordCapture.capture(), eq(10)); | ||
| SegmentZKMetadata record = recordCapture.getValue(); | ||
| assertNull(record.getTier()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUpdateSegmentTierToNewTier() { | ||
| String tableName = "table01_OFFLINE"; | ||
| String segmentName = "seg01"; | ||
| PinotHelixResourceManager helixMgrMock = mock(PinotHelixResourceManager.class); | ||
| when(helixMgrMock.getSegmentMetadataZnRecord(tableName, segmentName)) | ||
| .thenReturn(createSegmentMetadataZNRecord(segmentName, "hotTier")); | ||
| SegmentTierAssigner assigner = new SegmentTierAssigner(helixMgrMock, null, new ControllerConf(), null); | ||
|
|
||
| // Move back to default as not tier configs. | ||
| List<Tier> sortedTiers = new ArrayList<>(); | ||
| sortedTiers.add(new Tier("coldTier", new FixedTierSegmentSelector(null, Collections.singleton("seg01")), null)); | ||
| assigner.updateSegmentTier(tableName, "seg01", sortedTiers); | ||
| ArgumentCaptor<SegmentZKMetadata> recordCapture = ArgumentCaptor.forClass(SegmentZKMetadata.class); | ||
| verify(helixMgrMock).updateZkMetadata(eq(tableName), recordCapture.capture(), eq(10)); | ||
| SegmentZKMetadata record = recordCapture.getValue(); | ||
| assertEquals(record.getTier(), "coldTier"); | ||
| } | ||
|
|
||
| private static ZNRecord createSegmentMetadataZNRecord(String segmentName, String tierName) { | ||
| ZNRecord segmentMetadataZNRecord = new ZNRecord(segmentName); | ||
| segmentMetadataZNRecord.setVersion(10); | ||
| segmentMetadataZNRecord.setSimpleField(CommonConstants.Segment.TIER, tierName); | ||
| return segmentMetadataZNRecord; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.