Skip to content

Commit

Permalink
Add HttpServer object (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkokar authored Jan 16, 2020
1 parent 7db053b commit 933db8b
Show file tree
Hide file tree
Showing 22 changed files with 867 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@
/**
* Factory for proxy connectors.
*/
class ProxyConnectorFactory implements ServerConnectorFactory {
public class ProxyConnectorFactory implements ServerConnectorFactory {
private final MetricRegistry metrics;
private final HttpErrorStatusListener errorStatusListener;
private final NettyServerConfig serverConfig;
private final String unwiseCharacters;
private final ResponseEnhancer responseEnhancer;
private final boolean requestTracking;

ProxyConnectorFactory(NettyServerConfig serverConfig,
public ProxyConnectorFactory(NettyServerConfig serverConfig,
MetricRegistry metrics,
HttpErrorStatusListener errorStatusListener,
String unwiseCharacters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
/**
* Formats response info into a string.
*/
class ResponseInfoFormat {

// This can be make package private after we'll deprecate the old proxy server. But at the moment
// this has to be public because it is shared with com.hotels.styx.servers.StyxHttpServer.kt
public class ResponseInfoFormat {
private final String format;

ResponseInfoFormat(Environment environment) {
public ResponseInfoFormat(Environment environment) {
String releaseTag = environment.buildInfo().releaseTag();
String jvmRoute = environment.configuration().get("jvmRouteName").orElse(NO_JVM_ROUTE_SET);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2019 Expedia Inc.
Copyright (C) 2013-2020 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,6 +36,7 @@
import static com.hotels.styx.config.schema.SchemaDsl.union;
import static com.hotels.styx.config.validator.DocumentFormat.newDocument;
import static com.hotels.styx.routing.config.Builtins.BUILTIN_HANDLER_SCHEMAS;
import static com.hotels.styx.routing.config.Builtins.BUILTIN_SERVER_SCHEMAS;
import static com.hotels.styx.routing.config.Builtins.BUILTIN_SERVICE_PROVIDER_SCHEMAS;
import static com.hotels.styx.routing.config.Builtins.INTERCEPTOR_SCHEMAS;

Expand All @@ -58,8 +59,7 @@ final class ServerConfigSchema {
optional("sessionCacheSize", integer()),
optional("cipherSuites", list(string())),
optional("protocols", list(string()))
)),
atLeastOne("http", "https")
))
);

Schema.FieldType logFormatSchema = object(
Expand All @@ -70,7 +70,7 @@ final class ServerConfigSchema {

STYX_SERVER_CONFIGURATION_SCHEMA_BUILDER = newDocument()
.rootSchema(object(
field("proxy", object(
optional("proxy", object(
optional("compressResponses", bool()),
field("connectors", serverConnectorsSchema),
optional("bossThreadsCount", integer()),
Expand Down Expand Up @@ -115,6 +115,7 @@ final class ServerConfigSchema {
field("factories", map(object(opaque())))
)),
optional("providers", map(routingObject())),
optional("servers", map(routingObject())),
optional("url", object(
field("encoding", object(
field("unwiseCharactersToEncode", string())
Expand Down Expand Up @@ -163,6 +164,7 @@ final class ServerConfigSchema {

BUILTIN_HANDLER_SCHEMAS.forEach(STYX_SERVER_CONFIGURATION_SCHEMA_BUILDER::typeExtension);
BUILTIN_SERVICE_PROVIDER_SCHEMAS.forEach(STYX_SERVER_CONFIGURATION_SCHEMA_BUILDER::typeExtension);
BUILTIN_SERVER_SCHEMAS.forEach(STYX_SERVER_CONFIGURATION_SCHEMA_BUILDER::typeExtension);
INTERCEPTOR_SCHEMAS.forEach(STYX_SERVER_CONFIGURATION_SCHEMA_BUILDER::typeExtension);
}

Expand Down
13 changes: 7 additions & 6 deletions components/proxy/src/main/java/com/hotels/styx/StyxServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,10 @@ public StyxServer(StyxServerComponents components, Stopwatch stopwatch) {

registerCoreMetrics(components.environment().buildInfo(), components.environment().metricRegistry());

// The plugins are loaded, but not initialised.
// And therefore not able to accept traffic.
HttpHandler httpHandler = new StyxPipelineFactory(
// The plugins are loaded, but not initialised. And therefore not able to accept traffic.
// This handler is for the "old" proxy servers, that are started from proxy.connectors configuration.
// The new `HttpServer` object (https://github.com/HotelsDotCom/styx/pull/591) doesn't use it.
HttpHandler handlerForOldProxyServer = new StyxPipelineFactory(
components.routingObjectFactoryContext(),
components.environment(),
components.services(),
Expand All @@ -181,7 +182,7 @@ public StyxServer(StyxServerComponents components, Stopwatch stopwatch) {
.create();

// Startup phase 1: start plugins, control plane providers, and other services:
ArrayList<Service> services = new ArrayList<>();
ArrayList<Service> services = new ArrayList<>();
adminServer = createAdminServer(components);
services.add(toGuavaService(adminServer));
services.add(toGuavaService(new PluginsManager("Sty§x-Plugins-Manager", components)));
Expand All @@ -193,12 +194,12 @@ public StyxServer(StyxServerComponents components, Stopwatch stopwatch) {
StyxConfig styxConfig = components.environment().configuration();
httpServer = styxConfig.proxyServerConfig()
.httpConnectorConfig()
.map(it -> httpServer(components.environment(), it, httpHandler))
.map(it -> httpServer(components.environment(), it, handlerForOldProxyServer))
.orElse(null);

httpsServer = styxConfig.proxyServerConfig()
.httpsConnectorConfig()
.map(it -> httpServer(components.environment(), it, httpHandler))
.map(it -> httpServer(components.environment(), it, handlerForOldProxyServer))
.orElse(null);

ArrayList<Service> services2 = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public class AdminServerBuilder {
private final RoutingObjectFactory.Context routingObjectFactoryContext;
private final StyxObjectStore<RoutingObjectRecord> routeDatabase;
private final StyxObjectStore<StyxObjectRecord<StyxService>> providerDatabase;
private final StyxObjectStore<StyxObjectRecord<InetServer>> serverDatabase;
private final StartupConfig startupConfig;

private Registry<BackendService> backendServicesRegistry;
Expand All @@ -108,6 +109,7 @@ public AdminServerBuilder(StyxServerComponents serverComponents) {
this.providerDatabase = requireNonNull(serverComponents.servicesDatabase());
this.configuration = this.environment.configuration();
this.startupConfig = serverComponents.startupConfig();
this.serverDatabase = requireNonNull(serverComponents.serversDatabase());
}

public AdminServerBuilder backendServicesRegistry(Registry<BackendService> backendServicesRegistry) {
Expand Down Expand Up @@ -188,6 +190,10 @@ private HttpHandler adminEndpoints(StyxConfig styxConfig, StartupConfig startupC
httpRouter.aggregate("/admin/providers", providerHandler);
httpRouter.aggregate("/admin/providers/", providerHandler);

ProviderRoutingHandler serverHandler = new ProviderRoutingHandler("/admin/servers", serverDatabase);
httpRouter.aggregate("/admin/servers", serverHandler);
httpRouter.aggregate("/admin/servers/", serverHandler);

return httpRouter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public class ProviderListHandler implements WebServiceHandler {

private static final String TITLE = "List of Providers";

private final ObjectStore<StyxObjectRecord<StyxService>> providerDb;
private final ObjectStore<? extends StyxObjectRecord<? extends StyxService>> providerDb;

/**
* Create a new handler linked to a provider object store.
* @param providerDb the provider store.
*/
public ProviderListHandler(ObjectStore<StyxObjectRecord<StyxService>> providerDb) {
public ProviderListHandler(ObjectStore<? extends StyxObjectRecord<? extends StyxService>> providerDb) {
this.providerDb = providerDb;
}

Expand All @@ -77,7 +77,7 @@ public Eventual<HttpResponse> handle(HttpRequest request, HttpInterceptor.Contex
.build());
}

private static String htmlForProvider(String name, StyxObjectRecord<StyxService> provider) {
private static String htmlForProvider(String name, StyxObjectRecord<? extends StyxService> provider) {
String endpointList = provider.getStyxService()
.adminInterfaceHandlers(adminPath("providers", name))
.keySet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class ProviderRoutingHandler implements WebServiceHandler {
* @param pathPrefix the path prefix added to each provider admin URL
* @param providerDb the provider object store
*/
public ProviderRoutingHandler(String pathPrefix, StyxObjectStore<StyxObjectRecord<StyxService>> providerDb) {
public ProviderRoutingHandler(String pathPrefix, StyxObjectStore<? extends StyxObjectRecord<? extends StyxService>> providerDb) {
this.pathPrefix = pathPrefix;
Flux.from(providerDb.watch()).subscribe(
this::refreshRoutes,
Expand All @@ -60,14 +60,15 @@ public Eventual<HttpResponse> handle(HttpRequest request, HttpInterceptor.Contex
return router.handle(request, context);
}

private void refreshRoutes(ObjectStore<StyxObjectRecord<StyxService>> db) {
private void refreshRoutes(ObjectStore<? extends StyxObjectRecord<? extends StyxService>> db) {
LOG.info("Refreshing provider admin endpoint routes");
router = buildRouter(db);
}

private UrlPatternRouter buildRouter(ObjectStore<StyxObjectRecord<StyxService>> db) {
private UrlPatternRouter buildRouter(ObjectStore<? extends StyxObjectRecord<? extends StyxService>> db) {
UrlPatternRouter.Builder routeBuilder = new UrlPatternRouter.Builder(pathPrefix)
.get("", new ProviderListHandler(db));

db.entrySet().forEach(entry -> {
String providerName = entry.getKey();
entry.getValue().getStyxService().adminInterfaceHandlers(pathPrefix + "/" + providerName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.hotels.styx.routing.handlers.StaticResponseHandler;
import com.hotels.styx.StyxObjectRecord;
import com.hotels.styx.routing.interceptors.RewriteInterceptor;
import com.hotels.styx.servers.StyxHttpServer;
import com.hotels.styx.servers.StyxHttpServerFactory;
import com.hotels.styx.serviceproviders.ServiceProviderFactory;
import com.hotels.styx.serviceproviders.StyxServerFactory;
import com.hotels.styx.services.HealthCheckMonitoringService;
Expand Down Expand Up @@ -84,8 +86,13 @@ YAML_FILE_CONFIGURATION_SERVICE, new YamlFileConfigurationServiceFactory()
ImmutableMap.of(HEALTH_CHECK_MONITOR, HealthCheckMonitoringService.SCHEMA,
YAML_FILE_CONFIGURATION_SERVICE, YamlFileConfigurationService.SCHEMA);

public static final ImmutableMap<String, StyxServerFactory> BUILTIN_SERVER_FACTORIES = ImmutableMap.of();
public static final ImmutableMap<String, Schema.FieldType> BUILTIN_SERVER_SCHEMAS = ImmutableMap.of();
public static final ImmutableMap<String, StyxServerFactory> BUILTIN_SERVER_FACTORIES = ImmutableMap.of(
"HttpServer", new StyxHttpServerFactory()
);

public static final ImmutableMap<String, Schema.FieldType> BUILTIN_SERVER_SCHEMAS = ImmutableMap.of(
"HttpServer", StyxHttpServer.SCHEMA
);

public static final RouteRefLookup DEFAULT_REFERENCE_LOOKUP = reference -> (request, ctx) ->
Eventual.of(response(NOT_FOUND)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
Copyright (C) 2013-2020 Expedia Inc.
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.hotels.styx.servers

import com.fasterxml.jackson.databind.JsonNode
import com.hotels.styx.InetServer
import com.hotels.styx.NettyExecutor
import com.hotels.styx.ProxyConnectorFactory
import com.hotels.styx.ResponseInfoFormat
import com.hotels.styx.StyxObjectRecord
import com.hotels.styx.config.schema.SchemaDsl.*
import com.hotels.styx.infrastructure.configuration.yaml.JsonNodeConfig
import com.hotels.styx.proxy.ProxyServerConfig
import com.hotels.styx.proxy.encoders.ConfigurableUnwiseCharsEncoder.ENCODE_UNWISECHARS
import com.hotels.styx.routing.config.RoutingObjectFactory
import com.hotels.styx.routing.config.StyxObjectReference
import com.hotels.styx.routing.db.StyxObjectStore
import com.hotels.styx.server.HttpConnectorConfig
import com.hotels.styx.server.HttpsConnectorConfig
import com.hotels.styx.server.netty.NettyServerBuilder
import com.hotels.styx.server.netty.connectors.ResponseEnhancer
import com.hotels.styx.serviceproviders.StyxServerFactory
import org.slf4j.LoggerFactory

object StyxHttpServer {
@JvmField
val SCHEMA = `object`(
field("port", integer()),
field("handler", string()),
optional("tlsSettings", `object`(
optional("sslProvider", string()),
optional("certificateFile", string()),
optional("certificateKeyFile", string()),
optional("sessionTimeoutMillis", integer()),
optional("sessionCacheSize", integer()),
optional("cipherSuites", list(string())),
optional("protocols", list(string()))
)),

optional("maxInitialLength", integer()),
optional("maxHeaderSize", integer()),
optional("maxChunkSize", integer()),

optional("requestTimeoutMillis", integer()),
optional("keepAliveTimeoutMillis", integer()),
optional("maxConnectionsCount", integer()),

optional("bossThreadsCount", integer()),
optional("workerThreadsCount", integer())
)

internal val LOGGER = LoggerFactory.getLogger(StyxHttpServer::class.java)
}

private data class StyxHttpServerTlsSettings(
val certificateFile: String,
val certificateKeyFile: String,
val sslProvider: String = "JDK",
val sessionTimeoutMillis: Int = 300000,
val sessionCacheSize: Int = 0,
val cipherSuites: List<String> = listOf(),
val protocols: List<String> = listOf()
)


private data class StyxHttpServerConfiguration(
val port: Int,
val handler: String,
val tlsSettings: StyxHttpServerTlsSettings?,

val maxInitialLength: Int = 4096,
val maxHeaderSize: Int = 8192,
val maxChunkSize: Int = 8192,

val requestTimeoutMillis: Int = 60000,
val keepAliveTimeoutMillis: Int = 120000,
val maxConnectionsCount: Int = 512,

val bossThreadsCount: Int = 0,
val workerThreadsCount: Int = 0
)

internal class StyxHttpServerFactory : StyxServerFactory {
private fun serverConfig(configuration: JsonNode) = JsonNodeConfig(configuration).`as`(StyxHttpServerConfiguration::class.java)

override fun create(name: String, context: RoutingObjectFactory.Context, configuration: JsonNode, serverDb: StyxObjectStore<StyxObjectRecord<InetServer>>): InetServer {
val config = serverConfig(configuration)

val environment = context.environment()
val proxyServerConfig = ProxyServerConfig.Builder()
.setMaxInitialLength(config.maxInitialLength)
.setMaxHeaderSize(config.maxHeaderSize)
.setMaxChunkSize(config.maxChunkSize)
.setRequestTimeoutMillis(config.requestTimeoutMillis)
.setKeepAliveTimeoutMillis(config.keepAliveTimeoutMillis)
.setMaxConnectionsCount(config.maxConnectionsCount)
.setBossThreadsCount(config.bossThreadsCount)
.setWorkerThreadsCount(config.workerThreadsCount)
.build()

return NettyServerBuilder()
.setMetricsRegistry(environment.metricRegistry())
.setProtocolConnector(
ProxyConnectorFactory(
proxyServerConfig,
environment.metricRegistry(),
environment.errorListener(),
environment.configuration().get(ENCODE_UNWISECHARS).orElse(""),
ResponseEnhancer { builder, request ->
builder.header(
environment.configuration().styxHeaderConfig().styxInfoHeaderName(),
ResponseInfoFormat(environment).format(request))
},
false)
.create(
if (config.tlsSettings == null) {
HttpConnectorConfig(config.port)
} else {
HttpsConnectorConfig.Builder()
.port(config.port)
.sslProvider(config.tlsSettings.sslProvider)
.certificateFile(config.tlsSettings.certificateFile)
.certificateKeyFile(config.tlsSettings.certificateKeyFile)
.cipherSuites(config.tlsSettings.cipherSuites)
.protocols(*config.tlsSettings.protocols.toTypedArray())
.build()
}))
.workerExecutor(NettyExecutor.create("Http-Server(localhost-${config.port})", config.workerThreadsCount))
.bossExecutor(NettyExecutor.create("Http-Server(localhost-${config.port})", config.bossThreadsCount))
.handler({ request, ctx ->
context.refLookup()
.apply(StyxObjectReference(config.handler))
.handle(request, ctx)
})
.build();
}
}
Loading

0 comments on commit 933db8b

Please sign in to comment.