Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 com.facebook.presto.server;
 
 
 
 import java.util.List;
 import java.util.Set;
 
 import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.inject.multibindings.MapBinder.newMapBinder;
import static com.google.inject.multibindings.Multibinder.newSetBinder;
import static io.airlift.concurrent.Threads.daemonThreadsNamed;
import static io.airlift.configuration.ConfigurationModule.bindConfig;
import static io.airlift.discovery.client.DiscoveryBinder.discoveryBinder;
import static io.airlift.event.client.EventBinder.eventBinder;
import static io.airlift.http.client.HttpClientBinder.httpClientBinder;
import static io.airlift.jaxrs.JaxrsBinder.jaxrsBinder;
import static io.airlift.json.JsonBinder.jsonBinder;
import static io.airlift.json.JsonCodecBinder.jsonCodecBinder;
import static java.util.concurrent.Executors.newScheduledThreadPool;
import static org.weakref.jmx.guice.ExportBinder.newExporter;
public class ServerMainModule
        extends AbstractConfigurationAwareModule
    private final SqlParserOptions sqlParserOptions;
    public ServerMainModule(SqlParserOptions sqlParserOptions)
    {
        this. = checkNotNull(sqlParserOptions"sqlParserOptions is null");
    }
    @Override
    protected void setup(Binder binder)
    {
        ServerConfig serverConfig = buildConfigObject(ServerConfig.class);
        // TODO: this should only be installed if this is a coordinator
        binder.install(new CoordinatorModule());
        if (serverConfig.isCoordinator()) {
            discoveryBinder(binder).bindHttpAnnouncement("presto-coordinator");
        }
        binder.bind(SqlParser.class).in(.);
        binder.bind(SqlParserOptions.class).toInstance();
        bindFailureDetector(binderserverConfig.isCoordinator());
        jaxrsBinder(binder).bind(ThrowableMapper.class);
        // task execution
        jaxrsBinder(binder).bind(TaskResource.class);
        binder.bind(TaskManager.class).to(SqlTaskManager.class).in(.);
        newExporter(binder).export(TaskManager.class).withGeneratedName();
        binder.bind(TaskExecutor.class).in(.);
        newExporter(binder).export(TaskExecutor.class).withGeneratedName();
        binder.bind(LocalExecutionPlanner.class).in(.);
        bindConfig(binder).to(CompilerConfig.class);
        binder.bind(ExpressionCompiler.class).in(.);
        newExporter(binder).export(ExpressionCompiler.class).withGeneratedName();
        bindConfig(binder).to(TaskManagerConfig.class);
        binder.bind(IndexJoinLookupStats.class).in(.);
        binder.bind(AsyncHttpExecutionMBean.class).in(.);
        jsonCodecBinder(binder).bindJsonCodec(TaskInfo.class);
        jaxrsBinder(binder).bind(PagesResponseWriter.class);
        // exchange client
        binder.bind(new TypeLiteral<Supplier<ExchangeClient>>() {}).to(ExchangeClientFactory.class).in(.);
        httpClientBinder(binder).bindHttpClient("exchange"ForExchange.class).withTracing();
        bindConfig(binder).to(ExchangeClientConfig.class);
        binder.bind(ExchangeExecutionMBean.class).in(.);
        // execution
        binder.bind(LocationFactory.class).to(HttpLocationFactory.class).in(.);
        binder.bind(RemoteTaskFactory.class).to(HttpRemoteTaskFactory.class).in(.);
        newExporter(binder).export(RemoteTaskFactory.class).withGeneratedName();
        httpClientBinder(binder).bindHttpClient("scheduler"ForScheduler.class).withTracing();
        // data stream provider
        binder.bind(PageSourceManager.class).in(.);
        binder.bind(PageSourceProvider.class).to(PageSourceManager.class).in(.);
        newSetBinder(binderConnectorPageSourceProvider.class);
        // page sink provider
        binder.bind(PageSinkManager.class).in(.);
        binder.bind(PageSinkProvider.class).to(PageSinkManager.class).in(.);
        newSetBinder(binderConnectorPageSinkProvider.class);
        // metadata
        binder.bind(CatalogManager.class).in(.);
        bindConfig(binder).to(CatalogManagerConfig.class);
        binder.bind(MetadataManager.class).in(.);
        binder.bind(Metadata.class).to(MetadataManager.class).in(.);
        // type
        binder.bind(TypeRegistry.class).in(.);
        binder.bind(TypeManager.class).to(TypeRegistry.class).in(.);
        jsonBinder(binder).addDeserializerBinding(Type.class).to(TypeDeserializer.class);
        newSetBinder(binderType.class);
        // index manager
        binder.bind(IndexManager.class).in(.);
        // handle resolver
        binder.install(new HandleJsonModule());
        // connector
        binder.bind(ConnectorManager.class).in(.);
        MapBinder<StringConnectorFactoryconnectorFactoryBinder = newMapBinder(binderString.classConnectorFactory.class);
        // jmx connector
        connectorFactoryBinder.addBinding("jmx").to(JmxConnectorFactory.class);
        // information schema
        binder.install(new InformationSchemaModule());
        // system tables
        binder.install(new SystemTablesModule());
        // splits
        jsonCodecBinder(binder).bindJsonCodec(TaskUpdateRequest.class);
        jsonCodecBinder(binder).bindJsonCodec(ConnectorSplit.class);
        jsonBinder(binder).addSerializerBinding(Slice.class).to(SliceSerializer.class);
        jsonBinder(binder).addDeserializerBinding(Slice.class).to(SliceDeserializer.class);
        // query monitor
        binder.bind(QueryMonitor.class).in(.);
        eventBinder(binder).bindEventClient(QueryCreatedEvent.class);
        eventBinder(binder).bindEventClient(QueryCompletionEvent.class);
        eventBinder(binder).bindEventClient(SplitCompletionEvent.class);
        // Determine the NodeVersion
        String prestoVersion = serverConfig.getPrestoVersion();
        if (prestoVersion == null) {
            prestoVersion = detectPrestoVersion();
        }
        checkState(prestoVersion != null"presto.version must be provided when it cannot be automatically determined");
        NodeVersion nodeVersion = new NodeVersion(prestoVersion);
        binder.bind(NodeVersion.class).toInstance(nodeVersion);
        // presto announcement
        discoveryBinder(binder).bindHttpAnnouncement("presto")
                .addProperty("node_version"nodeVersion.toString())
                .addProperty("coordinator", String.valueOf(serverConfig.isCoordinator()))
                .addProperty("datasources"nullToEmpty(serverConfig.getDataSources()));
        // statement resource
        jsonCodecBinder(binder).bindJsonCodec(QueryInfo.class);
        jsonCodecBinder(binder).bindJsonCodec(TaskInfo.class);
        jsonCodecBinder(binder).bindJsonCodec(QueryResults.class);
        jaxrsBinder(binder).bind(StatementResource.class);
        // execute resource
        jaxrsBinder(binder).bind(ExecuteResource.class);
        httpClientBinder(binder).bindHttpClient("execute"ForExecute.class);
        // plugin manager
        binder.bind(PluginManager.class).in(.);
        bindConfig(binder).to(PluginManagerConfig.class);
        // optimizers
        // block encodings
        binder.bind(BlockEncodingManager.class).in(.);
        binder.bind(BlockEncodingSerde.class).to(BlockEncodingManager.class).in(.);
        Multibinder<BlockEncodingFactory<?>> blockEncodingFactoryBinder = newSetBinder(bindernew TypeLiteral<BlockEncodingFactory<?>>() {});
        blockEncodingFactoryBinder.addBinding().toInstance(.);
        blockEncodingFactoryBinder.addBinding().toInstance(.);
        blockEncodingFactoryBinder.addBinding().toInstance(.);
        blockEncodingFactoryBinder.addBinding().toInstance(.);
        // thread visualizer
        jaxrsBinder(binder).bind(ThreadResource.class);
        // thread execution visualizer
        jaxrsBinder(binder).bind(QueryExecutionResource.class);
        // PageSorter
        binder.bind(PageSorter.class).to(PagesIndexPageSorter.class).in(.);
    }
    @Provides
    @Singleton
    {
        return newScheduledThreadPool(config.getClientThreads(), daemonThreadsNamed("exchange-client-%s"));
    }
    @Provides
    @Singleton
    {
        return newScheduledThreadPool(config.getHttpNotificationThreads(), daemonThreadsNamed("async-http-response-%s"));
    }
    private static String detectPrestoVersion()
    {
        String title = PrestoServer.class.getPackage().getImplementationTitle();
        String version = PrestoServer.class.getPackage().getImplementationVersion();
        return ((title == null) || (version == null)) ? null : (title + ":" + version);
    }
    private static void bindFailureDetector(Binder binderboolean coordinator)
    {
        // TODO: this is a hack until the coordinator module works correctly
        if (coordinator) {
            binder.install(new FailureDetectorModule());
            jaxrsBinder(binder).bind(NodeResource.class);
        }
        else {
            binder.bind(FailureDetector.class).toInstance(new FailureDetector()
            {
                @Override
                public Set<ServiceDescriptorgetFailed()
                {
                    return ImmutableSet.of();
                }
            });
        }
    }
New to GrepCode? Check out our FAQ X