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.io.File;
 import java.util.List;
 import java.util.Set;
 
 import static com.facebook.presto.guice.ConditionalModule.installIfPropertyEquals;
import static com.facebook.presto.guice.DbiProvider.bindDbiToDataSource;
import static com.google.common.base.Preconditions.checkState;
import static com.google.inject.multibindings.MapBinder.newMapBinder;
import static com.google.inject.multibindings.Multibinder.newSetBinder;
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.json.JsonBinder.jsonBinder;
import static io.airlift.json.JsonCodecBinder.jsonCodecBinder;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.weakref.jmx.guice.ExportBinder.newExporter;
public class ServerMainModule
        extends AbstractConfigurationAwareModule
    @Override
    protected void setup(Binder binder)
    {
        ServerConfig serverConfig = buildConfigObject(ServerConfig.class);
        // TODO: this should only be installed if this is a coordinator
        install(new CoordinatorModule());
        if (serverConfig.isCoordinator()) {
            discoveryBinder(binder).bindHttpAnnouncement("presto-coordinator");
        }
        bindFailureDetector(binderserverConfig.isCoordinator());
        // task execution
        binder.bind(TaskResource.class).in(.);
        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(.);
        binder.bind(ExpressionCompiler.class).in(.);
        newExporter(binder).export(ExpressionCompiler.class).withGeneratedName();
        bindConfig(binder).to(TaskManagerConfig.class);
        jsonCodecBinder(binder).bindJsonCodec(TaskInfo.class);
        binder.bind(PagesMapper.class).in(.);
        // exchange client
        binder.bind(new TypeLiteral<Supplier<ExchangeClient>>() {}).to(ExchangeClientFactory.class).in(.);
        httpClientBinder(binder).bindAsyncHttpClient("exchange"ForExchange.class).withTracing();
        bindConfig(binder).to(ExchangeClientConfig.class);
        // execution
        binder.bind(LocationFactory.class).to(HttpLocationFactory.class).in(.);
        binder.bind(RemoteTaskFactory.class).to(HttpRemoteTaskFactory.class).in(.);
        httpClientBinder(binder).bindAsyncHttpClient("scheduler"ForScheduler.class).withTracing();
        // local storage manager
        bindConfig(binder).to(DatabaseLocalStorageManagerConfig.class);
        // data stream provider
        binder.bind(DataStreamManager.class).in(.);
        binder.bind(DataStreamProvider.class).to(DataStreamManager.class).in(.);
        newSetBinder(binderConnectorDataStreamProvider.class);
        // record sink provider
        binder.bind(RecordSinkManager.class).in(.);
        binder.bind(RecordSinkProvider.class).to(RecordSinkManager.class).in(.);
        newSetBinder(binderConnectorRecordSinkProvider.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(.);
        // handle resolver
        binder.install(new HandleJsonModule());
        // connector
        binder.bind(ConnectorManager.class).in(.);
        MapBinder<StringConnectorFactoryconnectorFactoryBinder = newMapBinder(binderString.classConnectorFactory.class);
        // native
        connectorFactoryBinder.addBinding("native").to(NativeConnectorFactory.class);
        // jmx connector
        connectorFactoryBinder.addBinding("jmx").to(JmxConnectorFactory.class);
        // dual
        binder.install(new DualModule());
        // information schema
        binder.install(new InformationSchemaModule());
        // system tables
        binder.install(new SystemTablesModule());
        // splits
        jsonCodecBinder(binder).bindJsonCodec(TaskUpdateRequest.class);
        jsonCodecBinder(binder).bindJsonCodec(Split.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
        ServiceAnnouncementBuilder prestoAnnouncement = discoveryBinder(binder).bindHttpAnnouncement("presto")
                .addProperty("node_version"nodeVersion.toString());
        if (serverConfig.getDataSources() != null) {
            prestoAnnouncement.addProperty("datasources"serverConfig.getDataSources());
        }
        bindDataSource(binder"presto-metastore"ForMetadata.classForShardManager.class);
        // statement resource
        jsonCodecBinder(binder).bindJsonCodec(QueryInfo.class);
        jsonCodecBinder(binder).bindJsonCodec(TaskInfo.class);
        jsonCodecBinder(binder).bindJsonCodec(QueryResults.class);
        binder.bind(StatementResource.class).in(.);
        // execute resource
        binder.bind(ExecuteResource.class).in(.);
        httpClientBinder(binder).bindAsyncHttpClient("execute"ForExecute.class);
        // plugin manager
        binder.bind(PluginManager.class).in(.);
        bindConfig(binder).to(PluginManagerConfig.class);
        // optimizers
    }
    @Provides
    @Singleton
    {
        return Executors.newCachedThreadPool(Threads.daemonThreadsNamed("exchange-callback-%s"));
    }
    @Provides
    @Singleton
            throws Exception
    {
        return new DBI(new H2EmbeddedDataSource(new H2EmbeddedDataSourceConfig()
                .setFilename(new File(config.getDataDirectory(), "db/StorageManager").getAbsolutePath())
                .setMaxConnections(500)
                .setMaxConnectionWait(new Duration(1, ))));
    }
    private final void bindDataSource(Binder binderString typeClass<? extends AnnotationannotationClass<? extends Annotation>... aliases)
    {
        String property = type + ".db.type";
        install(installIfPropertyEquals(new MySqlDataSourceModule(typeannotationaliases), property"mysql"));
        install(installIfPropertyEquals(new H2EmbeddedDataSourceModule(typeannotationaliases), property"h2"));
        bindDbiToDataSource(binderannotation);
        for (Class<? extends Annotationalias : aliases) {
            bindDbiToDataSource(binderalias);
        }
    }
    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());
            binder.bind(NodeResource.class).in(.);
        }
        else {
            binder.bind(FailureDetector.class).toInstance(new FailureDetector() {
                @Override
                public Set<ServiceDescriptorgetFailed()
                {
                    return ImmutableSet.of();
                }
            });
        }
    }
New to GrepCode? Check out our FAQ X