-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Model no memory fragment handling as wrapper PartitionMemoryEstimator #19285
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
losipiuk
merged 3 commits into
trinodb:master
from
losipiuk:lo/refactor-no-memory-estimator
Oct 13, 2023
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
103 changes: 103 additions & 0 deletions
103
...ain/src/main/java/io/trino/execution/scheduler/NoMemoryAwarePartitionMemoryEstimator.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,103 @@ | ||
| /* | ||
| * Licensed 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 io.trino.execution.scheduler; | ||
|
|
||
| import com.google.inject.BindingAnnotation; | ||
| import com.google.inject.Inject; | ||
| import io.trino.Session; | ||
| import io.trino.connector.informationschema.InformationSchemaTableHandle; | ||
| import io.trino.connector.system.GlobalSystemConnector; | ||
| import io.trino.connector.system.SystemTableHandle; | ||
| import io.trino.sql.planner.PlanFragment; | ||
| import io.trino.sql.planner.optimizations.PlanNodeSearcher; | ||
| import io.trino.sql.planner.plan.PlanFragmentId; | ||
| import io.trino.sql.planner.plan.PlanNode; | ||
| import io.trino.sql.planner.plan.RefreshMaterializedViewNode; | ||
| import io.trino.sql.planner.plan.TableScanNode; | ||
|
|
||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.Target; | ||
| import java.util.List; | ||
| import java.util.function.Function; | ||
|
|
||
| import static java.lang.annotation.ElementType.FIELD; | ||
| import static java.lang.annotation.ElementType.METHOD; | ||
| import static java.lang.annotation.ElementType.PARAMETER; | ||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class NoMemoryAwarePartitionMemoryEstimator | ||
| { | ||
| @Retention(RUNTIME) | ||
| @Target({FIELD, PARAMETER, METHOD}) | ||
| @BindingAnnotation | ||
| public @interface ForNoMemoryAwarePartitionMemoryEstimator {} | ||
|
|
||
| public static class Factory | ||
| implements PartitionMemoryEstimatorFactory | ||
| { | ||
| private final PartitionMemoryEstimatorFactory delegateFactory; | ||
|
|
||
| @Inject | ||
| public Factory(@ForNoMemoryAwarePartitionMemoryEstimator PartitionMemoryEstimatorFactory delegateFactory) | ||
| { | ||
| this.delegateFactory = requireNonNull(delegateFactory, "delegateFactory is null"); | ||
| } | ||
|
|
||
| @Override | ||
| public PartitionMemoryEstimator createPartitionMemoryEstimator( | ||
| Session session, | ||
| PlanFragment planFragment, | ||
| Function<PlanFragmentId, PlanFragment> sourceFragmentLookup) | ||
| { | ||
| if (isNoMemoryFragment(planFragment, sourceFragmentLookup)) { | ||
| return NoMemoryPartitionMemoryEstimator.INSTANCE; | ||
| } | ||
| return delegateFactory.createPartitionMemoryEstimator(session, planFragment, sourceFragmentLookup); | ||
| } | ||
|
|
||
| private boolean isNoMemoryFragment(PlanFragment fragment, Function<PlanFragmentId, PlanFragment> childFragmentLookup) | ||
| { | ||
| if (fragment.getRoot().getSources().stream() | ||
| .anyMatch(planNode -> planNode instanceof RefreshMaterializedViewNode)) { | ||
| // REFRESH MATERIALIZED VIEW will issue other SQL commands under the hood. If its task memory is | ||
| // non-zero, then a deadlock scenario is possible if we only have a single node in the cluster. | ||
| return true; | ||
| } | ||
|
|
||
| // If source fragments are not tagged as "no-memory" assume that they may produce significant amount of data. | ||
| // We stay on the safe side an assume that we should use standard memory estimation for this fragment | ||
| if (!fragment.getRemoteSourceNodes().stream().flatMap(node -> node.getSourceFragmentIds().stream()) | ||
| // TODO: childFragmentLookup will be executed for subtree of every fragment in query plan. That means fragment will be | ||
| // analyzed multiple time. Given fact that logic here is not extremely expensive and plans are not gigantic (up to ~200 fragments) | ||
| // we can keep it as a first approach. Ultimately we should profile execution and possibly put in place some mechanisms to avoid repeated work. | ||
| .allMatch(sourceFragmentId -> isNoMemoryFragment(childFragmentLookup.apply(sourceFragmentId), childFragmentLookup))) { | ||
| return false; | ||
| } | ||
|
|
||
| // If fragment source is not reading any external tables or only accesses information_schema assume it does not need significant amount of memory. | ||
| // Allow scheduling even if whole server memory is pre allocated. | ||
| List<PlanNode> tableScanNodes = PlanNodeSearcher.searchFrom(fragment.getRoot()).whereIsInstanceOfAny(TableScanNode.class).findAll(); | ||
| return tableScanNodes.stream().allMatch(node -> isMetadataTableScan((TableScanNode) node)); | ||
| } | ||
|
|
||
| private static boolean isMetadataTableScan(TableScanNode tableScanNode) | ||
| { | ||
| return (tableScanNode.getTable().getConnectorHandle() instanceof InformationSchemaTableHandle) || | ||
| (tableScanNode.getTable().getCatalogHandle().getCatalogName().equals(GlobalSystemConnector.NAME) && | ||
| (tableScanNode.getTable().getConnectorHandle() instanceof SystemTableHandle systemHandle) && | ||
| systemHandle.getSchemaName().equals("jdbc")); | ||
| } | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.