private RoutingHandler buildRoutingHandler(MetricRegistry metricRegistry, Server server, Handler applicationHandler, Handler adminHandler) { final List<Connector> appConnectors = buildAppConnectors(metricRegistry, server); final List<Connector> adConnectors = buildAdminConnectors(metricRegistry, server); final Map<Connector, Handler> handlers = new LinkedHashMap<>(); for (Connector connector : appConnectors) { server.addConnector(connector); handlers.put(connector, applicationHandler); } for (Connector connector : adConnectors) { server.addConnector(connector); handlers.put(connector, adminHandler); } return new RoutingHandler(handlers); }
@Override public Server build(Environment environment) { printBanner(environment.getName()); final ThreadPool threadPool = createThreadPool(environment.metrics()); final Server server = buildServer(environment.lifecycle(), threadPool); final Handler applicationHandler = createAppServlet(server, environment.jersey(), environment.getObjectMapper(), environment.getValidator(), environment.getApplicationContext(), environment.getJerseyServletContainer(), environment.metrics()); final Handler adminHandler = createAdminServlet(server, environment.getAdminContext(), environment.metrics(), environment.healthChecks()); final RoutingHandler routingHandler = buildRoutingHandler(environment.metrics(), server, applicationHandler, adminHandler); final Handler gzipHandler = buildGzipHandler(routingHandler); server.setHandler(addStatsHandler(addRequestLog(server, gzipHandler, environment.getName()))); return server; }
public static DefaultServerFactory createServerFactory(final InetSocketAddress httpEndpoint, final InetSocketAddress adminEndpoint, final boolean noHttp) { final DefaultServerFactory serverFactory = new DefaultServerFactory(); if (noHttp) { serverFactory.setApplicationConnectors(Collections.<ConnectorFactory>emptyList()); serverFactory.setAdminConnectors(Collections.<ConnectorFactory>emptyList()); } else { final HttpConnectorFactory serviceConnector = new HttpConnectorFactory(); serviceConnector.setPort(httpEndpoint.getPort()); serviceConnector.setBindHost(httpEndpoint.getHostString()); serverFactory.setApplicationConnectors(ImmutableList.<ConnectorFactory>of(serviceConnector)); final HttpConnectorFactory adminConnector = new HttpConnectorFactory(); adminConnector.setPort(adminEndpoint.getPort()); adminConnector.setBindHost(adminEndpoint.getHostString()); serverFactory.setAdminConnectors(ImmutableList.<ConnectorFactory>of(adminConnector)); } return serverFactory; } }
@Inject SingularityHostAndPortProvider(final SingularityConfiguration configuration, @Named(HOST_NAME_PROPERTY) String hostname) { checkNotNull(configuration, "configuration is null"); this.hostname = configuration.getHostname().or(hostname); Integer port = null; if (configuration.getServerFactory() instanceof SimpleServerFactory) { SimpleServerFactory simpleServerFactory = (SimpleServerFactory) configuration.getServerFactory(); HttpConnectorFactory httpFactory = (HttpConnectorFactory) simpleServerFactory.getConnector(); port = httpFactory.getPort(); } else { DefaultServerFactory defaultServerFactory = (DefaultServerFactory) configuration.getServerFactory(); for (ConnectorFactory connectorFactory : defaultServerFactory.getApplicationConnectors()) { // Currently we will default to needing an http connector for service -> service communication if (connectorFactory instanceof HttpConnectorFactory && !(connectorFactory instanceof HttpsConnectorFactory)) { HttpConnectorFactory httpFactory = (HttpConnectorFactory) connectorFactory; port = httpFactory.getPort(); } } } if (port == null) { throw new RuntimeException("Could not determine http port"); } this.httpPort = port; }
@Override public Server build(Environment environment) { configure(environment); printBanner(environment.getName()); final ThreadPool threadPool = createThreadPool(environment.metrics()); final Server server = buildServer(environment.lifecycle(), threadPool); final Handler applicationHandler = createAppServlet(server, environment.jersey(), environment.getObjectMapper(), environment.metrics()); final Handler adminHandler = createAdminServlet(server, environment.getAdminContext(), environment.metrics(), adminContextPath, adminHandler); final ContextRoutingHandler routingHandler = new ContextRoutingHandler(handlers); final Handler gzipHandler = buildGzipHandler(routingHandler); server.setHandler(addStatsHandler(addRequestLog(server, gzipHandler, environment.getName())));
@Override public void run(Configuration configuration, Environment environment) { final SimpleServerFactory serverConfig = new SimpleServerFactory(); configuration.setServerFactory(serverConfig); final HttpConnectorFactory connectorConfig = (HttpConnectorFactory) serverConfig.getConnector(); connectorConfig.setPort(0); environment.healthChecks().register("dummy", new DummyHealthCheck()); for (Object resource : resources) { if (resource instanceof Class<?>) { environment.jersey().register((Class<?>) resource); } else { environment.jersey().register(resource); } } } }
private int computeNumberOfAvailableThreads() { Preconditions.checkState(getServerFactory() instanceof DefaultServerFactory, "Unexpected serverFactory instance on TimeLockServerConfiguration."); DefaultServerFactory serverFactory = (DefaultServerFactory) getServerFactory(); int maxServerThreads = serverFactory.getMaxThreads(); Preconditions.checkNotNull(serverFactory.getApplicationConnectors(), "applicationConnectors of TimeLockServerConfiguration must not be null."); Preconditions.checkState(serverFactory.getApplicationConnectors().get(0) instanceof HttpConnectorFactory, "applicationConnectors of TimeLockServerConfiguration must have a HttpConnectorFactory instance."); HttpConnectorFactory connectorFactory = (HttpConnectorFactory) serverFactory.getApplicationConnectors().get(0); int selectorThreads = connectorFactory.getSelectorThreads(); int acceptorThreads = connectorFactory.getAcceptorThreads(); // TODO(gmaretic): consider reserving numClients more threads or something similar for unlocks return maxServerThreads - selectorThreads - acceptorThreads - 1; } }
serverFactory.setRequestLogFactory(requestLog); serverFactory.setGzipFilterFactory(gzip); this.server = serverFactory.build(environment);
protected Handler addRequestLog(Server server, Handler handler, String name) { if (getRequestLogFactory().isEnabled()) { final RequestLogHandler requestLogHandler = new RequestLogHandler(); requestLogHandler.setRequestLog(getRequestLogFactory().build(name)); // server should own the request log's lifecycle since it's already started, // the handler might not become managed in case of an error which would leave // the request log stranded server.addBean(requestLogHandler.getRequestLog(), true); requestLogHandler.setHandler(handler); return requestLogHandler; } return handler; }
@Provides @Named(SingularityServiceUIModule.SINGULARITY_URI_BASE) String getSingularityUriBase(final SingularityConfiguration configuration) { final String singularityUiPrefix; if (configuration.getServerFactory() instanceof SimpleServerFactory) { singularityUiPrefix = configuration.getUiConfiguration().getBaseUrl().or(((SimpleServerFactory) configuration.getServerFactory()).getApplicationContextPath()); } else { singularityUiPrefix = configuration.getUiConfiguration().getBaseUrl().or(((DefaultServerFactory) configuration.getServerFactory()).getApplicationContextPath()); } return (singularityUiPrefix.endsWith("/")) ? singularityUiPrefix.substring(0, singularityUiPrefix.length() - 1) : singularityUiPrefix; }
config.getAdminEndpoint(), config.getNoHttp()) .build(environment); } else { this.server = null;
protected Server buildServer(LifecycleEnvironment lifecycle, ThreadPool threadPool) { final Server server = new Server(threadPool); server.addLifeCycleListener(buildSetUIDListener()); lifecycle.attach(server); final ErrorHandler errorHandler = new ErrorHandler(); errorHandler.setServer(server); errorHandler.setShowStacks(false); server.addBean(errorHandler); server.setStopAtShutdown(true); server.setStopTimeout(shutdownGracePeriod.toMilliseconds()); return server; }
protected Handler createAdminServlet(Server server, MutableServletContextHandler handler, MetricRegistry metrics, HealthCheckRegistry healthChecks) { configureSessionsAndSecurity(handler, server); handler.setServer(server); handler.getServletContext().setAttribute(MetricsServlet.METRICS_REGISTRY, metrics); handler.getServletContext().setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, healthChecks); handler.addServlet(new NonblockingServletHolder(new AdminServlet()), "/*"); final String allowedMethodsParam = allowedMethods.stream() .collect(Collectors.joining(",")); handler.addFilter(AllowedMethodsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)) .setInitParameter(AllowedMethodsFilter.ALLOWED_METHODS_PARAM, allowedMethodsParam); return handler; }
@Override protected void run(Environment environment, Namespace namespace, T configuration) throws Exception { final Server server = configuration.getServerFactory().build(environment); try { server.addLifeCycleListener(new LifeCycleListener()); cleanupAsynchronously(); server.start(); } catch (Exception e) { LOGGER.error("Unable to start server, shutting down", e); try { server.stop(); } catch (Exception e1) { LOGGER.warn("Failure during stop server", e1); } try { cleanup(); } catch (Exception e2) { LOGGER.warn("Failure during cleanup", e2); } throw e; } }
@Override protected void run(Bootstrap<T> bootstrap, Namespace namespace, T configuration) throws Exception { final Environment environment = new Environment(bootstrap.getApplication().getName(), bootstrap.getObjectMapper(), bootstrap.getValidatorFactory(), bootstrap.getMetricRegistry(), bootstrap.getClassLoader(), bootstrap.getHealthCheckRegistry()); configuration.getMetricsFactory().configure(environment.lifecycle(), bootstrap.getMetricRegistry()); configuration.getServerFactory().configure(environment); bootstrap.run(configuration, environment); application.run(configuration, environment); run(environment, namespace, configuration); }
@Override public Server build(Environment environment) { printBanner(environment.getName()); final ThreadPool threadPool = createThreadPool(environment.metrics()); final Server server = buildServer(environment.lifecycle(), threadPool); final Handler applicationHandler = createAppServlet(server, environment.jersey(), environment.getObjectMapper(), environment.getValidator(), environment.getApplicationContext(), environment.getJerseyServletContainer(), environment.metrics()); final Handler adminHandler = createAdminServlet(server, environment.getAdminContext(), environment.metrics(), environment.healthChecks()); final RoutingHandler routingHandler = buildRoutingHandler(environment.metrics(), server, applicationHandler, adminHandler); final Handler gzipHandler = buildGzipHandler(routingHandler); server.setHandler(addStatsHandler(addRequestLog(server, gzipHandler, environment.getName()))); return server; }
protected Handler addRequestLog(Server server, Handler handler, String name) { if (getRequestLogFactory().isEnabled()) { final RequestLogHandler requestLogHandler = new RequestLogHandler(); requestLogHandler.setRequestLog(getRequestLogFactory().build(name)); // server should own the request log's lifecycle since it's already started, // the handler might not become managed in case of an error which would leave // the request log stranded server.addBean(requestLogHandler.getRequestLog(), true); requestLogHandler.setHandler(handler); return requestLogHandler; } return handler; }
private RoutingHandler buildRoutingHandler(MetricRegistry metricRegistry, Server server, Handler applicationHandler, Handler adminHandler) { final List<Connector> appConnectors = buildAppConnectors(metricRegistry, server); final List<Connector> adConnectors = buildAdminConnectors(metricRegistry, server); final Map<Connector, Handler> handlers = new LinkedHashMap<>(); for (Connector connector : appConnectors) { server.addConnector(connector); handlers.put(connector, applicationHandler); } for (Connector connector : adConnectors) { server.addConnector(connector); handlers.put(connector, adminHandler); } return new RoutingHandler(handlers); }
protected Server buildServer(LifecycleEnvironment lifecycle, ThreadPool threadPool) { final Server server = new Server(threadPool); server.addLifeCycleListener(buildSetUIDListener()); lifecycle.attach(server); final ErrorHandler errorHandler = new ErrorHandler(); errorHandler.setServer(server); errorHandler.setShowStacks(false); server.addBean(errorHandler); server.setStopAtShutdown(true); server.setStopTimeout(shutdownGracePeriod.toMilliseconds()); return server; }
protected Handler createAppServlet(Server server, JerseyEnvironment jersey, ObjectMapper objectMapper, Validator validator, MutableServletContextHandler handler, @Nullable Servlet jerseyContainer, MetricRegistry metricRegistry) { configureSessionsAndSecurity(handler, server); final String allowedMethodsParam = allowedMethods.stream() .collect(Collectors.joining(",")); handler.addFilter(AllowedMethodsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)) .setInitParameter(AllowedMethodsFilter.ALLOWED_METHODS_PARAM, allowedMethodsParam); if (enableThreadNameFilter) { handler.addFilter(ThreadNameFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); } serverPush.addFilter(handler); if (jerseyContainer != null) { jerseyRootPath.ifPresent(jersey::setUrlPattern); jersey.register(new JacksonFeature(objectMapper)); jersey.register(new HibernateValidationBinder(validator)); if (registerDefaultExceptionMappers == null || registerDefaultExceptionMappers) { jersey.register(new ExceptionMapperBinder(detailedJsonProcessingExceptionMapper)); } handler.addServlet(new NonblockingServletHolder(jerseyContainer), jersey.getUrlPattern()); } final InstrumentedHandler instrumented = new InstrumentedHandler(metricRegistry); instrumented.setServer(server); instrumented.setHandler(handler); return instrumented; }