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.Map;
 import java.util.Set;
 
 import static com.facebook.presto.server.PrestoJvmRequirements.verifyJvmRequirements;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Strings.nullToEmpty;
 import static io.airlift.discovery.client.ServiceAnnouncement.ServiceAnnouncementBuilder;
 import static io.airlift.discovery.client.ServiceAnnouncement.serviceAnnouncement;
 
 public class PrestoServer
         implements Runnable
 {
     public static void main(String[] args)
     {
         new PrestoServer().run();
     }
 
     private final SqlParserOptions sqlParserOptions;
 
     public PrestoServer()
     {
         this(new SqlParserOptions());
     }
 
     public PrestoServer(SqlParserOptions sqlParserOptions)
     {
         this. = checkNotNull(sqlParserOptions"sqlParserOptions is null");
     }
 
     @Override
     public void run()
     {
         verifyJvmRequirements();
 
         Logger log = Logger.get(PrestoServer.class);
 
         ImmutableList.Builder<Modulemodules = ImmutableList.builder();
         modules.add(
                 new NodeModule(),
                 new DiscoveryModule(),
                 new HttpServerModule(),
                 new JsonModule(),
                 new JaxrsModule(true),
                 new MBeanModule(),
                 new JmxModule(),
                 new JmxHttpModule(),
                 new LogJmxModule(),
                 new TraceTokenModule(),
                 new JsonEventModule(),
                 new HttpEventModule(),
                 new EmbeddedDiscoveryModule(),
                 new ServerMainModule());
 
         modules.addAll(getAdditionalModules());
 
        Bootstrap app = new Bootstrap(modules.build());
        try {
            Injector injector = app.strictConfig().initialize();
            injector.getInstance(PluginManager.class).loadPlugins();
            injector.getInstance(CatalogManager.class).loadCatalogs();
            // TODO: remove this huge hack
            updateDatasources(
                    injector.getInstance(Announcer.class),
                    injector.getInstance(Metadata.class),
                    injector.getInstance(ServerConfig.class),
                    injector.getInstance(NodeSchedulerConfig.class));
            injector.getInstance(Announcer.class).start();
            log.info("======== SERVER STARTED ========");
        }
        catch (Throwable e) {
            log.error(e);
            System.exit(1);
        }
    }
    protected Iterable<? extends ModulegetAdditionalModules()
    {
        return ImmutableList.of();
    }
    private static void updateDatasources(Announcer announcerMetadata metadataServerConfig serverConfigNodeSchedulerConfig schedulerConfig)
    {
        // get existing announcement
        ServiceAnnouncement announcement = getPrestoAnnouncement(announcer.getServiceAnnouncements());
        // get existing sources
        String property = nullToEmpty(announcement.getProperties().get("datasources"));
        List<Stringvalues = Splitter.on(',').trimResults().omitEmptyStrings().splitToList(property);
        Set<Stringdatasources = new LinkedHashSet<>(values);
        // automatically build sources if not configured
        if (datasources.isEmpty()) {
            Set<Stringcatalogs = metadata.getCatalogNames().keySet();
            // if this is a dedicated coordinator, only add jmx
            if (serverConfig.isCoordinator() && !schedulerConfig.isIncludeCoordinator()) {
                if (catalogs.contains("jmx")) {
                    datasources.add("jmx");
                }
            }
            else {
                datasources.addAll(catalogs);
            }
        }
        // build announcement with updated sources
        ServiceAnnouncementBuilder builder = serviceAnnouncement(announcement.getType());
        for (Map.Entry<StringStringentry : announcement.getProperties().entrySet()) {
            if (!entry.getKey().equals("datasources")) {
                builder.addProperty(entry.getKey(), entry.getValue());
            }
        }
        builder.addProperty("datasources", Joiner.on(',').join(datasources));
        // update announcement
        announcer.removeServiceAnnouncement(announcement.getId());
        announcer.addServiceAnnouncement(builder.build());
    }
    private static ServiceAnnouncement getPrestoAnnouncement(Set<ServiceAnnouncementannouncements)
    {
        for (ServiceAnnouncement announcement : announcements) {
            if (announcement.getType().equals("presto")) {
                return announcement;
            }
        }
        throw new IllegalArgumentException("Presto announcement not found: " + announcements);
    }
New to GrepCode? Check out our FAQ X