-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add a preferred tier selection strategy #18136
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
12 commits
Select commit
Hold shift + click to select a range
08ad56b
Add PreferredTierSelectorStrategy
FrankChen021 19c9122
Fix spelling
FrankChen021 326f46d
Fix class name
FrankChen021 739f465
Fix doc
FrankChen021 ed048a4
Fix static check
FrankChen021 12baa0b
Fix doc
FrankChen021 f94f44d
Update docs/configuration/index.md
FrankChen021 874976c
Update docs/configuration/index.md
FrankChen021 9950244
Update docs/configuration/index.md
FrankChen021 a82d427
Add comments
FrankChen021 5ed6c38
Fix tests
FrankChen021 2fdfe60
Fix logic
FrankChen021 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
114 changes: 114 additions & 0 deletions
114
server/src/main/java/org/apache/druid/client/selector/PreferredTierSelectorStrategy.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,114 @@ | ||
| /* | ||
| * 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.druid.client.selector; | ||
|
|
||
|
|
||
| import com.fasterxml.jackson.annotation.JacksonInject; | ||
| import it.unimi.dsi.fastutil.ints.Int2ObjectRBTreeMap; | ||
| import org.apache.druid.client.QueryableDruidServer; | ||
| import org.apache.druid.java.util.common.IAE; | ||
| import org.apache.druid.java.util.common.logger.Logger; | ||
| import org.apache.druid.query.Query; | ||
| import org.apache.druid.timeline.DataSegment; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public class PreferredTierSelectorStrategy extends AbstractTierSelectorStrategy | ||
| { | ||
| private static final Logger log = new Logger(PreferredTierSelectorStrategy.class); | ||
|
|
||
| private final String preferredTier; | ||
| private final TierSelectorStrategy priorityStrategy; | ||
|
|
||
| public PreferredTierSelectorStrategy( | ||
| @JacksonInject ServerSelectorStrategy serverSelectorStrategy, | ||
| @JacksonInject PreferredTierSelectorStrategyConfig config | ||
| ) | ||
| { | ||
| super(serverSelectorStrategy); | ||
| this.preferredTier = config.getTier(); | ||
|
|
||
| if (config.getPriority() == null) { | ||
| this.priorityStrategy = new HighestPriorityTierSelectorStrategy(serverSelectorStrategy); | ||
| } else { | ||
| if ("highest".equalsIgnoreCase(config.getPriority())) { | ||
| this.priorityStrategy = new HighestPriorityTierSelectorStrategy(serverSelectorStrategy); | ||
| } else if ("lowest".equalsIgnoreCase(config.getPriority())) { | ||
| this.priorityStrategy = new LowestPriorityTierSelectorStrategy(serverSelectorStrategy); | ||
| } else { | ||
| throw new IAE("druid.broker.select.tier.preferred.priority must be either 'highest' or 'lowest'"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Comparator<Integer> getComparator() | ||
| { | ||
| return priorityStrategy.getComparator(); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> List<QueryableDruidServer> pick( | ||
| Query<T> query, | ||
| Int2ObjectRBTreeMap<Set<QueryableDruidServer>> prioritizedServers, | ||
| DataSegment segment, | ||
| int numServersToPick | ||
| ) | ||
| { | ||
| if (log.isDebugEnabled()) { | ||
| log.debug( | ||
| "Picking [%d] servers from preferred tier [%s] for segment [%s] with priority [%s]", | ||
| numServersToPick, preferredTier, segment.getId(), this.priorityStrategy.getClass().getSimpleName() | ||
| ); | ||
| } | ||
|
|
||
| Int2ObjectRBTreeMap<Set<QueryableDruidServer>> preferred = new Int2ObjectRBTreeMap<>(priorityStrategy.getComparator()); | ||
| Int2ObjectRBTreeMap<Set<QueryableDruidServer>> nonPreferred = new Int2ObjectRBTreeMap<>(priorityStrategy.getComparator()); | ||
| for (Set<QueryableDruidServer> priorityServers : prioritizedServers.values()) { | ||
| for (QueryableDruidServer server : priorityServers) { | ||
| if (preferredTier.equals(server.getServer().getMetadata().getTier())) { | ||
| preferred.computeIfAbsent(server.getServer().getPriority(), k -> new HashSet<>()) | ||
| .add(server); | ||
| } else { | ||
| nonPreferred.computeIfAbsent(server.getServer().getPriority(), k -> new HashSet<>()) | ||
| .add(server); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| List<QueryableDruidServer> picks = new ArrayList<>(numServersToPick); | ||
| if (!preferred.isEmpty()) { | ||
| // If we have preferred servers, pick them first | ||
| picks.addAll(priorityStrategy.pick(query, preferred, segment, numServersToPick)); | ||
| } | ||
|
|
||
| if (picks.size() < numServersToPick && !nonPreferred.isEmpty()) { | ||
| // If we don't have enough preferred servers, pick from the non-preferred ones | ||
| int remaining = numServersToPick - picks.size(); | ||
| picks.addAll(priorityStrategy.pick(query, nonPreferred, segment, remaining)); | ||
| } | ||
|
|
||
| return picks; | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
...r/src/main/java/org/apache/druid/client/selector/PreferredTierSelectorStrategyConfig.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,70 @@ | ||
| /* | ||
| * 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.druid.client.selector; | ||
|
|
||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.google.common.base.Preconditions; | ||
|
|
||
| public class PreferredTierSelectorStrategyConfig | ||
| { | ||
| @JsonProperty | ||
| private String tier; | ||
|
|
||
| /** | ||
| * Only two options: high or low | ||
| */ | ||
| @JsonProperty | ||
| private String priority; | ||
|
|
||
| @JsonCreator | ||
| public PreferredTierSelectorStrategyConfig( | ||
| @JsonProperty("tier") String tier, | ||
| @JsonProperty("priority") String priority | ||
| ) | ||
| { | ||
| Preconditions.checkState(tier != null && !tier.isEmpty(), | ||
| "druid.broker.select.tier.preferred.tier can't be empty"); | ||
| this.tier = tier.trim(); | ||
| this.priority = priority; | ||
| } | ||
|
|
||
| public String getTier() | ||
| { | ||
| return tier; | ||
| } | ||
|
|
||
| public void setTier(String tier) | ||
| { | ||
| this.tier = tier; | ||
| } | ||
|
|
||
| public String getPriority() | ||
| { | ||
| return priority; | ||
| } | ||
|
|
||
| public void setPriority(String priority) | ||
| { | ||
| this.priority = priority; | ||
| } | ||
| } |
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
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.
I think this can be restored to private now that PreferredTierSelectorStrategy is delegating to highest or lowest priority strategy instead of calling server selector direct
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.
reverted. thanks for pointing out