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

io.trino.dispatcher.DispatchExecutor 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.dispatcher;

import com.google.common.io.Closer;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.ListeningScheduledExecutorService;
import com.google.inject.Inject;
import io.airlift.concurrent.ThreadPoolExecutorMBean;
import io.trino.execution.QueryManagerConfig;
import io.trino.spi.VersionEmbedder;
import jakarta.annotation.PreDestroy;
import org.weakref.jmx.Flatten;
import org.weakref.jmx.Managed;
import org.weakref.jmx.Nested;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

import static com.google.common.util.concurrent.MoreExecutors.listeningDecorator;
import static io.airlift.concurrent.Threads.daemonThreadsNamed;
import static io.trino.util.Executors.decorateWithVersion;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.Executors.newCachedThreadPool;
import static java.util.concurrent.Executors.newScheduledThreadPool;

public class DispatchExecutor
{
    @SuppressWarnings("resource")
    private final Closer closer = Closer.create();

    private final ListeningExecutorService executor;
    private final ListeningScheduledExecutorService scheduledExecutor;

    private final DispatchExecutorMBeans mbeans;

    @Inject
    public DispatchExecutor(QueryManagerConfig config, VersionEmbedder versionEmbedder)
    {
        ExecutorService coreExecutor = newCachedThreadPool(daemonThreadsNamed("dispatcher-query-%s"));
        closer.register(coreExecutor::shutdownNow);
        executor = decorateWithVersion(coreExecutor, versionEmbedder);

        ScheduledExecutorService coreScheduledExecutor = newScheduledThreadPool(config.getQueryManagerExecutorPoolSize(), daemonThreadsNamed("dispatch-executor-%s"));
        closer.register(coreScheduledExecutor::shutdownNow);
        scheduledExecutor = listeningDecorator(coreScheduledExecutor);

        mbeans = new DispatchExecutorMBeans(coreExecutor, coreScheduledExecutor);
    }

    public ListeningExecutorService getExecutor()
    {
        return executor;
    }

    public ListeningScheduledExecutorService getScheduledExecutor()
    {
        return scheduledExecutor;
    }

    @Managed
    @Flatten
    public DispatchExecutorMBeans getMbeans()
    {
        return mbeans;
    }

    @PreDestroy
    public void shutdown()
            throws Exception
    {
        closer.close();
    }

    public static class DispatchExecutorMBeans
    {
        private final ThreadPoolExecutorMBean executor;
        private final ThreadPoolExecutorMBean scheduledExecutor;

        public DispatchExecutorMBeans(ExecutorService coreExecutor, ScheduledExecutorService coreScheduledExecutor)
        {
            requireNonNull(coreExecutor, "coreExecutor is null");
            requireNonNull(coreScheduledExecutor, "coreScheduledExecutor is null");
            executor = new ThreadPoolExecutorMBean((ThreadPoolExecutor) coreExecutor);
            scheduledExecutor = new ThreadPoolExecutorMBean((ThreadPoolExecutor) coreScheduledExecutor);
        }

        @Managed
        @Nested
        public ThreadPoolExecutorMBean getExecutor()
        {
            return executor;
        }

        @Managed
        @Nested
        public ThreadPoolExecutorMBean getScheduledExecutor()
        {
            return scheduledExecutor;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy