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.testing;
 
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import static com.facebook.presto.server.testing.FileUtils.deleteRecursively;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Strings.nullToEmpty;
 import static io.airlift.discovery.client.ServiceAnnouncement.serviceAnnouncement;
 
 public class TestingPrestoServer
         implements Closeable
 {
     public static final String TEST_CATALOG = "default"// TODO: change this to test_catalog
 
     private final Path baseDataDir;
     private final LifeCycleManager lifeCycleManager;
     private final PluginManager pluginManager;
     private final ConnectorManager connectorManager;
     private final TestingHttpServer server;
     private final Metadata metadata;
     private final InternalNodeManager nodeManager;
     private final Announcer announcer;
     private QueryManager queryManager;
 
     public TestingPrestoServer()
             throws Exception
     {
         this(ImmutableList.<Module>of());
     }
 
     public TestingPrestoServer(List<ModuleadditionalModules)
             throws Exception
     {
         this(true, ImmutableMap.<StringString>of(), nullnulladditionalModules);
     }
 
     public TestingPrestoServer(boolean coordinatorMap<StringStringpropertiesString environmentURI discoveryUriList<ModuleadditionalModules)
             throws Exception
     {
          = Files.createTempDirectory("PrestoTest");
        ImmutableMap.Builder<StringStringserverProperties = ImmutableMap.<StringString>builder()
                .putAll(properties)
                .put("coordinator", String.valueOf(coordinator))
                .put("presto.version""testversion")
                .put("analyzer.experimental-syntax-enabled""true");
        if (coordinator) {
            // TODO: enable failure detector
            serverProperties.put("failure-detector.enabled""false");
        }
        ImmutableList.Builder<Modulemodules = ImmutableList.<Module>builder()
                .add(new TestingNodeModule(Optional.fromNullable(environment)))
                .add(new TestingHttpServerModule())
                .add(new JsonModule())
                .add(new JaxrsModule(true))
                .add(new MBeanModule())
                .add(new TestingJmxModule())
                .add(new InMemoryEventModule())
                .add(new TraceTokenModule())
                .add(new ServerMainModule(new SqlParserOptions()));
        if (discoveryUri != null) {
            checkNotNull(environment"environment required when discoveryUri is present");
            serverProperties.put("discovery.uri"discoveryUri.toString());
            modules.add(new DiscoveryModule());
        }
        else {
            modules.add(new TestingDiscoveryModule());
        }
        modules.addAll(additionalModules);
        Bootstrap app = new Bootstrap(modules.build());
        Map<StringStringoptionalProperties = new HashMap<>();
        if (environment != null) {
            optionalProperties.put("node.environment"environment);
        }
        Injector injector = app
                .strictConfig()
                .doNotInitializeLogging()
                .setRequiredConfigurationProperties(serverProperties.build())
                .setOptionalConfigurationProperties(optionalProperties)
                .initialize();
        injector.getInstance(Announcer.class).start();
         = injector.getInstance(LifeCycleManager.class);
         = injector.getInstance(QueryManager.class);
         = injector.getInstance(PluginManager.class);
         = injector.getInstance(ConnectorManager.class);
         = injector.getInstance(TestingHttpServer.class);
         = injector.getInstance(Metadata.class);
         = injector.getInstance(InternalNodeManager.class);
         = injector.getInstance(ServiceSelectorManager.class);
         = injector.getInstance(Announcer.class);
        .forceAnnounce();
        refreshNodes();
    }
    @Override
    public void close()
    {
        try {
            if ( != null) {
                .stop();
            }
        }
        catch (Exception e) {
            throw Throwables.propagate(e);
        }
        finally {
            deleteRecursively();
        }
    }
    public void installPlugin(Plugin plugin)
    {
        .installPlugin(plugin);
    }
    public QueryManager getQueryManager()
    {
        return ;
    }
    public void createCatalog(String catalogNameString connectorName)
    {
        createCatalog(catalogNameconnectorName, ImmutableMap.<StringString>of());
    }
    public void createCatalog(String catalogNameString connectorNameMap<StringStringproperties)
    {
        .createConnection(catalogNameconnectorNameproperties);
        updateDatasourcesAnnouncement(catalogName);
    }
    public Path getBaseDataDir()
    {
        return ;
    }
    public URI getBaseUrl()
    {
        return .getBaseUrl();
    }
    public URI resolve(String path)
    {
        return .getBaseUrl().resolve(path);
    }
    public HostAndPort getAddress()
    {
        return HostAndPort.fromParts(getBaseUrl().getHost(), getBaseUrl().getPort());
    }
    public Metadata getMetadata()
    {
        return ;
    }
    public final AllNodes refreshNodes()
    {
        .refreshNodes();
        return .getAllNodes();
    }
    public Set<NodegetActiveNodesWithConnector(String connectorName)
    {
        return .getActiveDatasourceNodes(connectorName);
    }
    private static void updateDatasourcesAnnouncement(Announcer announcerString connectorId)
    {
        //
        // This code was copied from PrestoServer, and is a hack that should be removed when the data source property is removed
        //
        // get existing announcement
        ServiceAnnouncement announcement = getPrestoAnnouncement(announcer.getServiceAnnouncements());
        // update datasources property
        Map<StringStringproperties = new LinkedHashMap<>(announcement.getProperties());
        String property = nullToEmpty(properties.get("datasources"));
        Set<Stringdatasources = new LinkedHashSet<>(Splitter.on(',').trimResults().omitEmptyStrings().splitToList(property));
        datasources.add(connectorId);
        properties.put("datasources", Joiner.on(',').join(datasources));
        // update announcement
        announcer.removeServiceAnnouncement(announcement.getId());
        announcer.addServiceAnnouncement(serviceAnnouncement(announcement.getType()).addProperties(properties).build());
        announcer.forceAnnounce();
    }
    private static ServiceAnnouncement getPrestoAnnouncement(Set<ServiceAnnouncementannouncements)
    {
        for (ServiceAnnouncement announcement : announcements) {
            if (announcement.getType().equals("presto")) {
                return announcement;
            }
        }
        throw new RuntimeException("Presto announcement not found: " + announcements);
    }
New to GrepCode? Check out our FAQ X