All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.trino.execution.scheduler.faulttolerant.CompositeOutputStatsEstimator Maven / Gradle / Ivy

There is a newer version: 465
Show newest version
/*
 * 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.faulttolerant;

import com.google.common.collect.ImmutableList;
import com.google.inject.BindingAnnotation;
import com.google.inject.Inject;
import io.trino.Session;
import io.trino.execution.StageId;
import io.trino.execution.scheduler.faulttolerant.EventDrivenFaultTolerantQueryScheduler.StageExecution;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
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;

public class CompositeOutputStatsEstimator
        implements OutputStatsEstimator
{
    public static class Factory
            implements OutputStatsEstimatorFactory
    {
        private final List delegateFactories;

        @Inject
        public Factory(@ForCompositeOutputDataSizeEstimator List delegateFactories)
        {
            checkArgument(!delegateFactories.isEmpty(), "Got empty list of delegates");
            this.delegateFactories = ImmutableList.copyOf(delegateFactories);
        }

        @Override
        public OutputStatsEstimator create(Session session)
        {
            List estimators = delegateFactories.stream().map(factory -> factory.create(session))
                    .collect(toImmutableList());
            return new CompositeOutputStatsEstimator(estimators);
        }
    }

    @Retention(RUNTIME)
    @Target({FIELD, PARAMETER, METHOD})
    @BindingAnnotation
    public @interface ForCompositeOutputDataSizeEstimator {}

    private final List estimators;

    private CompositeOutputStatsEstimator(List estimators)
    {
        this.estimators = ImmutableList.copyOf(estimators);
    }

    @Override
    public Optional getEstimatedOutputStats(
            StageExecution stageExecution,
            Function stageExecutionLookup,
            boolean parentEager)
    {
        for (OutputStatsEstimator estimator : estimators) {
            Optional result = estimator.getEstimatedOutputStats(stageExecution, stageExecutionLookup, parentEager);
            if (result.isPresent()) {
                return result;
            }
        }
        return Optional.empty();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy