Search in sources :

Example 1 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class CuratorModule method makeCurator.

@Provides
@LazySingleton
@SuppressForbidden(reason = "System#err")
public CuratorFramework makeCurator(ZkEnablementConfig zkEnablementConfig, CuratorConfig config, EnsembleProvider ensembleProvider, Lifecycle lifecycle) {
    if (!zkEnablementConfig.isEnabled()) {
        throw new RuntimeException("Zookeeper is disabled, Can't create CuratorFramework.");
    }
    final CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
    if (!Strings.isNullOrEmpty(config.getZkUser()) && !Strings.isNullOrEmpty(config.getZkPwd())) {
        builder.authorization(config.getAuthScheme(), StringUtils.format("%s:%s", config.getZkUser(), config.getZkPwd()).getBytes(StandardCharsets.UTF_8));
    }
    RetryPolicy retryPolicy = new BoundedExponentialBackoffRetry(BASE_SLEEP_TIME_MS, MAX_SLEEP_TIME_MS, MAX_RETRIES);
    final CuratorFramework framework = builder.ensembleProvider(ensembleProvider).sessionTimeoutMs(config.getZkSessionTimeoutMs()).connectionTimeoutMs(config.getZkConnectionTimeoutMs()).retryPolicy(retryPolicy).compressionProvider(new PotentiallyGzippedCompressionProvider(config.getEnableCompression())).aclProvider(config.getEnableAcl() ? new SecuredACLProvider() : new DefaultACLProvider()).build();
    framework.getUnhandledErrorListenable().addListener((message, e) -> {
        log.error(e, "Unhandled error in Curator, stopping server.");
        shutdown(lifecycle);
    });
    lifecycle.addHandler(new Lifecycle.Handler() {

        @Override
        public void start() {
            log.debug("Starting Curator");
            framework.start();
        }

        @Override
        public void stop() {
            log.debug("Stopping Curator");
            framework.close();
        }
    });
    return framework;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) DefaultACLProvider(org.apache.curator.framework.imps.DefaultACLProvider) RetryPolicy(org.apache.curator.RetryPolicy) BoundedExponentialBackoffRetry(org.apache.curator.retry.BoundedExponentialBackoffRetry) LazySingleton(org.apache.druid.guice.LazySingleton) SuppressForbidden(io.netty.util.SuppressForbidden) Provides(com.google.inject.Provides)

Example 2 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class JettyServerModule method makeAndInitializeServer.

static Server makeAndInitializeServer(Injector injector, Lifecycle lifecycle, DruidNode node, ServerConfig config, TLSServerConfig tlsServerConfig, Binding<SslContextFactory.Server> sslContextFactoryBinding, TLSCertificateChecker certificateChecker) {
    // adjusting to make config.getNumThreads() mean, "number of threads
    // that concurrently handle the requests".
    int numServerThreads = config.getNumThreads() + getMaxJettyAcceptorsSelectorsNum(node);
    final QueuedThreadPool threadPool;
    if (config.getQueueSize() == Integer.MAX_VALUE) {
        threadPool = new QueuedThreadPool();
        threadPool.setMinThreads(numServerThreads);
        threadPool.setMaxThreads(numServerThreads);
    } else {
        threadPool = new QueuedThreadPool(numServerThreads, numServerThreads, // same default is used in other case when threadPool = new QueuedThreadPool()
        60000, new LinkedBlockingQueue<>(config.getQueueSize()));
    }
    threadPool.setDaemon(true);
    jettyServerThreadPool = threadPool;
    final Server server = new Server(threadPool);
    // Without this bean set, the default ScheduledExecutorScheduler runs as non-daemon, causing lifecycle hooks to fail
    // to fire on main exit. Related bug: https://github.com/apache/druid/pull/1627
    server.addBean(new ScheduledExecutorScheduler("JettyScheduler", true), true);
    final List<ServerConnector> serverConnectors = new ArrayList<>();
    if (node.isEnablePlaintextPort()) {
        log.info("Creating http connector with port [%d]", node.getPlaintextPort());
        HttpConfiguration httpConfiguration = new HttpConfiguration();
        if (config.isEnableForwardedRequestCustomizer()) {
            httpConfiguration.addCustomizer(new ForwardedRequestCustomizer());
        }
        httpConfiguration.setRequestHeaderSize(config.getMaxRequestHeaderSize());
        httpConfiguration.setSendServerVersion(false);
        final ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(httpConfiguration));
        if (node.isBindOnHost()) {
            connector.setHost(node.getHost());
        }
        connector.setPort(node.getPlaintextPort());
        serverConnectors.add(connector);
    }
    final SslContextFactory.Server sslContextFactory;
    if (node.isEnableTlsPort()) {
        log.info("Creating https connector with port [%d]", node.getTlsPort());
        if (sslContextFactoryBinding == null) {
            // Never trust all certificates by default
            sslContextFactory = new IdentityCheckOverrideSslContextFactory(tlsServerConfig, certificateChecker);
            sslContextFactory.setKeyStorePath(tlsServerConfig.getKeyStorePath());
            sslContextFactory.setKeyStoreType(tlsServerConfig.getKeyStoreType());
            sslContextFactory.setKeyStorePassword(tlsServerConfig.getKeyStorePasswordProvider().getPassword());
            sslContextFactory.setCertAlias(tlsServerConfig.getCertAlias());
            sslContextFactory.setKeyManagerFactoryAlgorithm(tlsServerConfig.getKeyManagerFactoryAlgorithm() == null ? KeyManagerFactory.getDefaultAlgorithm() : tlsServerConfig.getKeyManagerFactoryAlgorithm());
            sslContextFactory.setKeyManagerPassword(tlsServerConfig.getKeyManagerPasswordProvider() == null ? null : tlsServerConfig.getKeyManagerPasswordProvider().getPassword());
            if (tlsServerConfig.getIncludeCipherSuites() != null) {
                sslContextFactory.setIncludeCipherSuites(tlsServerConfig.getIncludeCipherSuites().toArray(new String[0]));
            }
            if (tlsServerConfig.getExcludeCipherSuites() != null) {
                sslContextFactory.setExcludeCipherSuites(tlsServerConfig.getExcludeCipherSuites().toArray(new String[0]));
            }
            if (tlsServerConfig.getIncludeProtocols() != null) {
                sslContextFactory.setIncludeProtocols(tlsServerConfig.getIncludeProtocols().toArray(new String[0]));
            }
            if (tlsServerConfig.getExcludeProtocols() != null) {
                sslContextFactory.setExcludeProtocols(tlsServerConfig.getExcludeProtocols().toArray(new String[0]));
            }
            sslContextFactory.setNeedClientAuth(tlsServerConfig.isRequireClientCertificate());
            sslContextFactory.setWantClientAuth(tlsServerConfig.isRequestClientCertificate());
            if (tlsServerConfig.isRequireClientCertificate() || tlsServerConfig.isRequestClientCertificate()) {
                if (tlsServerConfig.getCrlPath() != null) {
                    // setValidatePeerCerts is used just to enable revocation checking using a static CRL file.
                    // Certificate validation is always performed when client certificates are required.
                    sslContextFactory.setValidatePeerCerts(true);
                    sslContextFactory.setCrlPath(tlsServerConfig.getCrlPath());
                }
                if (tlsServerConfig.isValidateHostnames()) {
                    sslContextFactory.setEndpointIdentificationAlgorithm("HTTPS");
                }
                if (tlsServerConfig.getTrustStorePath() != null) {
                    sslContextFactory.setTrustStorePath(tlsServerConfig.getTrustStorePath());
                    sslContextFactory.setTrustStoreType(tlsServerConfig.getTrustStoreType() == null ? KeyStore.getDefaultType() : tlsServerConfig.getTrustStoreType());
                    sslContextFactory.setTrustManagerFactoryAlgorithm(tlsServerConfig.getTrustStoreAlgorithm() == null ? TrustManagerFactory.getDefaultAlgorithm() : tlsServerConfig.getTrustStoreAlgorithm());
                    sslContextFactory.setTrustStorePassword(tlsServerConfig.getTrustStorePasswordProvider() == null ? null : tlsServerConfig.getTrustStorePasswordProvider().getPassword());
                }
            }
        } else {
            sslContextFactory = sslContextFactoryBinding.getProvider().get();
        }
        final HttpConfiguration httpsConfiguration = new HttpConfiguration();
        if (config.isEnableForwardedRequestCustomizer()) {
            httpsConfiguration.addCustomizer(new ForwardedRequestCustomizer());
        }
        httpsConfiguration.setSecureScheme("https");
        httpsConfiguration.setSecurePort(node.getTlsPort());
        httpsConfiguration.addCustomizer(new SecureRequestCustomizer());
        httpsConfiguration.setRequestHeaderSize(config.getMaxRequestHeaderSize());
        httpsConfiguration.setSendServerVersion(false);
        final ServerConnector connector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HTTP_1_1_STRING), new HttpConnectionFactory(httpsConfiguration));
        if (node.isBindOnHost()) {
            connector.setHost(node.getHost());
        }
        connector.setPort(node.getTlsPort());
        serverConnectors.add(connector);
    } else {
        sslContextFactory = null;
    }
    final ServerConnector[] connectors = new ServerConnector[serverConnectors.size()];
    int index = 0;
    for (ServerConnector connector : serverConnectors) {
        connectors[index++] = connector;
        connector.setIdleTimeout(Ints.checkedCast(config.getMaxIdleTime().toStandardDuration().getMillis()));
        // workaround suggested in -
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=435322#c66 for jetty half open connection issues during failovers
        connector.setAcceptorPriorityDelta(-1);
        List<ConnectionFactory> monitoredConnFactories = new ArrayList<>();
        for (ConnectionFactory cf : connector.getConnectionFactories()) {
            // connection factories (in this case HTTP/1.1 after the connection is unencrypted for SSL)
            if (cf.getProtocol().equals(connector.getDefaultProtocol())) {
                monitoredConnFactories.add(new JettyMonitoringConnectionFactory(cf, ACTIVE_CONNECTIONS));
            } else {
                monitoredConnFactories.add(cf);
            }
        }
        connector.setConnectionFactories(monitoredConnFactories);
    }
    server.setConnectors(connectors);
    final long gracefulStop = config.getGracefulShutdownTimeout().toStandardDuration().getMillis();
    if (gracefulStop > 0) {
        server.setStopTimeout(gracefulStop);
    }
    server.addLifeCycleListener(new LifeCycle.Listener() {

        @Override
        public void lifeCycleStarting(LifeCycle event) {
            log.debug("Jetty lifecycle starting [%s]", event.getClass());
        }

        @Override
        public void lifeCycleStarted(LifeCycle event) {
            log.debug("Jetty lifeycle started [%s]", event.getClass());
        }

        @Override
        public void lifeCycleFailure(LifeCycle event, Throwable cause) {
            log.error(cause, "Jetty lifecycle event failed [%s]", event.getClass());
        }

        @Override
        public void lifeCycleStopping(LifeCycle event) {
            log.debug("Jetty lifecycle stopping [%s]", event.getClass());
        }

        @Override
        public void lifeCycleStopped(LifeCycle event) {
            log.debug("Jetty lifecycle stopped [%s]", event.getClass());
        }
    });
    // initialize server
    JettyServerInitializer initializer = injector.getInstance(JettyServerInitializer.class);
    try {
        initializer.initialize(server, injector);
    } catch (Exception e) {
        throw new RE(e, "server initialization exception");
    }
    lifecycle.addHandler(new Lifecycle.Handler() {

        @Override
        public void start() throws Exception {
            log.debug("Starting Jetty Server...");
            server.start();
            if (node.isEnableTlsPort()) {
                // Perform validation
                Preconditions.checkNotNull(sslContextFactory);
                final SSLEngine sslEngine = sslContextFactory.newSSLEngine();
                if (sslEngine.getEnabledCipherSuites() == null || sslEngine.getEnabledCipherSuites().length == 0) {
                    throw new ISE("No supported cipher suites found, supported suites [%s], configured suites include list: [%s] exclude list: [%s]", Arrays.toString(sslEngine.getSupportedCipherSuites()), tlsServerConfig.getIncludeCipherSuites(), tlsServerConfig.getExcludeCipherSuites());
                }
                if (sslEngine.getEnabledProtocols() == null || sslEngine.getEnabledProtocols().length == 0) {
                    throw new ISE("No supported protocols found, supported protocols [%s], configured protocols include list: [%s] exclude list: [%s]", Arrays.toString(sslEngine.getSupportedProtocols()), tlsServerConfig.getIncludeProtocols(), tlsServerConfig.getExcludeProtocols());
                }
            }
        }

        @Override
        public void stop() {
            try {
                final long unannounceDelay = config.getUnannouncePropagationDelay().toStandardDuration().getMillis();
                if (unannounceDelay > 0) {
                    log.info("Sleeping %s ms for unannouncement to propagate.", unannounceDelay);
                    Thread.sleep(unannounceDelay);
                } else {
                    log.debug("Skipping unannounce wait.");
                }
                log.debug("Stopping Jetty Server...");
                server.stop();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RE(e, "Interrupted waiting for jetty shutdown.");
            } catch (Exception e) {
                log.warn(e, "Unable to stop Jetty server.");
            }
        }
    }, Lifecycle.Stage.SERVER);
    if (!config.isShowDetailedJettyErrors()) {
        server.setErrorHandler(new ErrorHandler() {

            @Override
            public boolean isShowServlet() {
                return false;
            }

            @Override
            public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, null);
                super.handle(target, baseRequest, request, response);
            }
        });
    }
    return server;
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) Server(org.eclipse.jetty.server.Server) SSLEngine(javax.net.ssl.SSLEngine) ScheduledExecutorScheduler(org.eclipse.jetty.util.thread.ScheduledExecutorScheduler) ArrayList(java.util.ArrayList) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ConnectionFactory(org.eclipse.jetty.server.ConnectionFactory) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ISE(org.apache.druid.java.util.common.ISE) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ForwardedRequestCustomizer(org.eclipse.jetty.server.ForwardedRequestCustomizer) RE(org.apache.druid.java.util.common.RE)

Example 3 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class MockMemcachedClient method testBasicInjection.

@Test
public void testBasicInjection() throws Exception {
    final MemcachedCacheConfig config = new MemcachedCacheConfig() {

        @Override
        public String getHosts() {
            return "127.0.0.1:22";
        }
    };
    Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(new Module() {

        @Override
        public void configure(Binder binder) {
            binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/memcached");
            binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
            binder.bindConstant().annotatedWith(Names.named("tlsServicePort")).to(-1);
            binder.bind(MemcachedCacheConfig.class).toInstance(config);
            binder.bind(Cache.class).toProvider(MemcachedProviderWithConfig.class).in(ManageLifecycle.class);
        }
    }));
    Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
    lifecycle.start();
    try {
        Cache cache = injector.getInstance(Cache.class);
        Assert.assertEquals(MemcachedCache.class, cache.getClass());
    } finally {
        lifecycle.stop();
    }
}
Also used : Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) ManageLifecycle(org.apache.druid.guice.ManageLifecycle) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) Module(com.google.inject.Module) Test(org.junit.Test)

Example 4 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class CaffeineCacheProviderWithConfig method testBasicInjection.

@Test
public void testBasicInjection() throws Exception {
    final CaffeineCacheConfig config = new CaffeineCacheConfig();
    Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(binder -> {
        binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/redis");
        binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
        binder.bindConstant().annotatedWith(Names.named("tlsServicePort")).to(-1);
        binder.bind(CaffeineCacheConfig.class).toInstance(config);
        binder.bind(Cache.class).toProvider(CaffeineCacheProviderWithConfig.class).in(ManageLifecycle.class);
    }));
    final Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
    lifecycle.start();
    try {
        Cache cache = injector.getInstance(Cache.class);
        Assert.assertEquals(CaffeineCache.class, cache.getClass());
    } finally {
        lifecycle.stop();
    }
}
Also used : Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) Properties(java.util.Properties) JsonConfigProvider(org.apache.druid.guice.JsonConfigProvider) Inject(com.google.inject.Inject) StringUtils(org.apache.druid.java.util.common.StringUtils) Test(org.junit.Test) Random(java.util.Random) Names(com.google.inject.name.Names) UUID(java.util.UUID) Ints(com.google.common.primitives.Ints) GuiceInjectors(org.apache.druid.guice.GuiceInjectors) ArrayList(java.util.ArrayList) Injector(com.google.inject.Injector) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ForkJoinPool(java.util.concurrent.ForkJoinPool) Initialization(org.apache.druid.initialization.Initialization) Map(java.util.Map) ManageLifecycle(org.apache.druid.guice.ManageLifecycle) Assert(org.junit.Assert) JsonConfigurator(org.apache.druid.guice.JsonConfigurator) Collections(java.util.Collections) Before(org.junit.Before) Injector(com.google.inject.Injector) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) ManageLifecycle(org.apache.druid.guice.ManageLifecycle) ManageLifecycle(org.apache.druid.guice.ManageLifecycle) Test(org.junit.Test)

Example 5 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class SuiteListener method onStart.

@Override
public void onStart(ISuite suite) {
    Injector injector = DruidTestModuleFactory.getInjector();
    IntegrationTestingConfig config = injector.getInstance(IntegrationTestingConfig.class);
    DruidClusterAdminClient druidClusterAdminClient = injector.getInstance(DruidClusterAdminClient.class);
    druidClusterAdminClient.waitUntilCoordinatorReady();
    druidClusterAdminClient.waitUntilIndexerReady();
    druidClusterAdminClient.waitUntilBrokerReady();
    String routerHost = config.getRouterUrl();
    if (null != routerHost) {
        druidClusterAdminClient.waitUntilRouterReady();
    }
    Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
    try {
        lifecycle.start();
    } catch (Exception e) {
        LOG.error(e, "");
        throw new RuntimeException(e);
    }
}
Also used : Injector(com.google.inject.Injector) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) IntegrationTestingConfig(org.apache.druid.testing.IntegrationTestingConfig)

Aggregations

Lifecycle (org.apache.druid.java.util.common.lifecycle.Lifecycle)46 Test (org.junit.Test)21 Injector (com.google.inject.Injector)12 URL (java.net.URL)12 ExecutionException (java.util.concurrent.ExecutionException)12 StatusResponseHolder (org.apache.druid.java.util.http.client.response.StatusResponseHolder)10 Before (org.junit.Before)10 ExecutorService (java.util.concurrent.ExecutorService)6 Binder (com.google.inject.Binder)5 Module (com.google.inject.Module)5 OutputStream (java.io.OutputStream)5 Properties (java.util.Properties)5 ManageLifecycle (org.apache.druid.guice.ManageLifecycle)5 NoopServiceEmitter (org.apache.druid.server.metrics.NoopServiceEmitter)5 ChannelException (org.jboss.netty.channel.ChannelException)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)4 IOException (java.io.IOException)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Provides (com.google.inject.Provides)3