aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/JettyHttpServer.java
blob: abebf109fc2e0ca5b801abd3f99402a25d7d5636 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;

import com.google.common.annotations.Beta;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.inject.Inject;
import com.yahoo.component.ComponentId;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.container.logging.AccessLog;
import com.yahoo.jdisc.Metric;
import com.yahoo.jdisc.application.OsgiFramework;
import com.yahoo.jdisc.http.ServerConfig;
import com.yahoo.jdisc.http.ServletPathsConfig;
import com.yahoo.jdisc.http.server.FilterBindings;
import com.yahoo.jdisc.service.AbstractServerProvider;
import com.yahoo.jdisc.service.CurrentContainer;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.ConnectorStatistics;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AbstractHandlerContainer;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;

import javax.servlet.DispatcherType;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.yahoo.jdisc.http.server.jetty.ConnectorFactory.JDiscServerConnector;
import static com.yahoo.jdisc.http.server.jetty.Exceptions.throwUnchecked;

/**
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
 */
@Beta
public class JettyHttpServer extends AbstractServerProvider {

    public interface Metrics {
        final String NAME_DIMENSION = "serverName";
        final String PORT_DIMENSION = "serverPort";

        final String NUM_ACTIVE_REQUESTS = "serverNumActiveRequests";
        final String NUM_OPEN_CONNECTIONS = "serverNumOpenConnections";
        final String NUM_CONNECTIONS_OPEN_MAX = "serverConnectionsOpenMax";
        final String CONNECTION_DURATION_MAX = "serverConnectionDurationMax";
        final String CONNECTION_DURATION_MEAN = "serverConnectionDurationMean";
        final String CONNECTION_DURATION_STD_DEV = "serverConnectionDurationStdDev";

        final String NUM_BYTES_RECEIVED = "serverBytesReceived";
        final String NUM_BYTES_SENT     = "serverBytesSent";
        final String MANHATTAN_NUM_BYTES_RECEIVED = "http.in.bytes";
        final String MANHATTAN_NUM_BYTES_SENT     = "http.out.bytes";

        final String NUM_CONNECTIONS = "serverNumConnections";
        final String NUM_CONNECTIONS_IDLE = "serverNumConnectionsIdle";
        final String NUM_UNEXPECTED_DISCONNECTS = "serverNumUnexpectedDisconnects";

        /* For historical reasons, these are all aliases for the same metric. 'jdisc.http' should ideally be the only one. */
        final String JDISC_HTTP_REQUESTS = "jdisc.http.requests";
        final String NUM_REQUESTS = "serverNumRequests";
        final String MANHATTAN_NUM_REQUESTS = "http.requests";

        final String NUM_SUCCESSFUL_RESPONSES = "serverNumSuccessfulResponses";
        final String NUM_FAILED_RESPONSES = "serverNumFailedResponses";
        final String NUM_SUCCESSFUL_WRITES = "serverNumSuccessfulResponseWrites";
        final String NUM_FAILED_WRITES = "serverNumFailedResponseWrites";

        final String NETWORK_LATENCY = "serverNetworkLatency";
        final String TOTAL_SUCCESSFUL_LATENCY = "serverTotalSuccessfulResponseLatency";
        final String MANHATTAN_TOTAL_SUCCESSFUL_LATENCY = "http.latency";
        final String TOTAL_FAILED_LATENCY = "serverTotalFailedResponseLatency";
        final String TIME_TO_FIRST_BYTE = "serverTimeToFirstByte";
        final String MANHATTAN_TIME_TO_FIRST_BYTE = "http.out.firstbytetime";

        final String RESPONSES_1XX = "http.status.1xx";
        final String RESPONSES_2XX = "http.status.2xx";
        final String RESPONSES_3XX = "http.status.3xx";
        final String RESPONSES_4XX = "http.status.4xx";
        final String RESPONSES_5XX = "http.status.5xx";

        final String STARTED_MILLIS = "serverStartedMillis";
        final String MANHATTAN_STARTED_MILLIS = "proc.uptime";
    }

    private final static Logger log = Logger.getLogger(JettyHttpServer.class.getName());
    private final long timeStarted = System.currentTimeMillis();
    private final ExecutorService janitor;
    private final ScheduledExecutorService metricReporterExecutor;
    private final Metric metric;
    private final Server server;

    @Inject
    public JettyHttpServer(
            final CurrentContainer container,
            final Metric metric,
            final ServerConfig serverConfig,
            final ServletPathsConfig servletPathsConfig,
            final ThreadFactory threadFactory,
            final FilterBindings filterBindings,
            final ComponentRegistry<ConnectorFactory> connectorFactories,
            final ComponentRegistry<ServletHolder> servletHolders,
            final OsgiFramework osgiFramework,
            final FilterInvoker filterInvoker,
            final AccessLog accessLog) {
        super(container);
        if (connectorFactories.allComponents().isEmpty()) {
            throw new IllegalArgumentException("No connectors configured.");
        }
        this.metric = metric;

        server = new Server();
        ((QueuedThreadPool)server.getThreadPool()).setMaxThreads(serverConfig.maxWorkerThreads());

        Map<Path, FileChannel> keyStoreChannels = getKeyStoreFileChannels(osgiFramework.bundleContext());

        for (ConnectorFactory connectorFactory : connectorFactories.allComponents()) {
            ServerSocketChannel preBoundChannel = getChannelFromServiceLayer(connectorFactory.getConnectorConfig().listenPort(), osgiFramework.bundleContext());
            server.addConnector(connectorFactory.createConnector(metric, server, preBoundChannel, keyStoreChannels));
        }

        janitor = newJanitor(threadFactory);

        JDiscContext jDiscContext = new JDiscContext(
                filterBindings.getRequestFilters().activate(),
                filterBindings.getResponseFilters().activate(),
                container,
                janitor,
                metric,
                serverConfig);

        ServletHolder jdiscServlet = new ServletHolder(new JDiscHttpServlet(jDiscContext));
        FilterHolder jDiscFilterInvokerFilter = new FilterHolder(new JDiscFilterInvokerFilter(jDiscContext, filterInvoker));

        final RequestLog requestLog = new AccessLogRequestLog(accessLog);

        server.setHandler(
                getHandlerCollection(
                        serverConfig,
                        servletPathsConfig,
                        jdiscServlet,
                        servletHolders,
                        jDiscFilterInvokerFilter,
                        requestLog));

        final int numMetricReporterThreads = 1;
        metricReporterExecutor = Executors.newScheduledThreadPool(
                numMetricReporterThreads,
                new ThreadFactoryBuilder()
                        .setDaemon(true)
                        .setNameFormat(JettyHttpServer.class.getName() + "-MetricReporter-%d")
                        .setThreadFactory(threadFactory)
                        .build()
        );
        metricReporterExecutor.scheduleAtFixedRate(new MetricTask(), 0, 2, TimeUnit.SECONDS);
    }

    private HandlerCollection getHandlerCollection(
            ServerConfig serverConfig,
            ServletPathsConfig servletPathsConfig,
            ServletHolder jdiscServlet,
            ComponentRegistry<ServletHolder> servletHolders,
            FilterHolder jDiscFilterInvokerFilter,
            RequestLog requestLog) {

        ServletContextHandler servletContextHandler = createServletContextHandler();

        servletHolders.allComponentsById().forEach((id, servlet) -> {
            String path = getServletPath(servletPathsConfig, id);
            servletContextHandler.addServlet(servlet, path);
            servletContextHandler.addFilter(jDiscFilterInvokerFilter, path, EnumSet.allOf(DispatcherType.class));
        });

        servletContextHandler.addServlet(jdiscServlet, "/*");

        final GzipHandler gzipHandler = newGzipHandler(serverConfig);
        gzipHandler.setHandler(servletContextHandler);

        final StatisticsHandler statisticsHandler = newStatisticsHandler();
        statisticsHandler.setHandler(gzipHandler);

        final RequestLogHandler requestLogHandler = new RequestLogHandler();
        requestLogHandler.setRequestLog(requestLog);

        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[]{statisticsHandler, requestLogHandler});
        return handlerCollection;
    }

    private static String getServletPath(ServletPathsConfig servletPathsConfig, ComponentId id) {
        return "/" + servletPathsConfig.servlets(id.stringValue()).path();
    }

    // Ugly trick to get generic type literal.
    @SuppressWarnings("unchecked")
    private static final Class<Map<?, ?>> mapClass = (Class<Map<?, ?>>) (Object) Map.class;

    private Map<Path, FileChannel> getKeyStoreFileChannels(BundleContext bundleContext) {
        try {
            Collection<ServiceReference<Map<?, ?>>> serviceReferences = bundleContext.getServiceReferences(mapClass,
                    "(role=com.yahoo.container.standalone.StandaloneContainerActivator.KeyStoreFileChannels)");

            if (serviceReferences == null || serviceReferences.isEmpty())
                return Collections.emptyMap();

            if (serviceReferences.size() != 1)
                throw new IllegalStateException("Multiple KeyStoreFileChannels registered");

            return getKeyStoreFileChannels(bundleContext, serviceReferences.iterator().next());
        } catch (InvalidSyntaxException e) {
            throw throwUnchecked(e);
        }
    }

    @SuppressWarnings("unchecked")
    private Map<Path, FileChannel> getKeyStoreFileChannels(BundleContext bundleContext, ServiceReference<Map<?, ?>> keyStoreFileChannelReference) {
        Map<?, ?> fileChannelMap = bundleContext.getService(keyStoreFileChannelReference);
        try {
            if (fileChannelMap == null)
                return Collections.emptyMap();

            Map<Path, FileChannel> result = (Map<Path, FileChannel>) fileChannelMap;
            log.fine("Using file channel for " + result.keySet());
            return result;
        } finally {
            //if we change this to be anything other than a simple map, we should hold the reference as long as the object is in use.
            bundleContext.ungetService(keyStoreFileChannelReference);
        }
    }

    private ServletContextHandler createServletContextHandler() {
        ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS);
        servletContextHandler.setContextPath("/");
        return servletContextHandler;
    }

    private ServerSocketChannel getChannelFromServiceLayer(int listenPort, BundleContext bundleContext) {
        log.log(Level.FINE, "Retrieving channel for port " + listenPort + " from " + bundleContext.getClass().getName());
        Collection<ServiceReference<ServerSocketChannel>> refs;
        final String filter = "(port=" + listenPort + ")";
        try {
            refs = bundleContext.getServiceReferences(ServerSocketChannel.class, filter);
        } catch (InvalidSyntaxException e) {
            throw new IllegalStateException("OSGi framework rejected filter " + filter, e);
        }
        if (refs.isEmpty()) {
            return null;
        }
        if (refs.size() != 1) {
            throw new IllegalStateException("Got more than one service reference for " + ServerSocketChannel.class + " port " + listenPort + ".");
        }
        ServiceReference<ServerSocketChannel> ref = refs.iterator().next();
        return bundleContext.getService(ref);
    }

    private static ExecutorService newJanitor(final ThreadFactory factory) {
        final int threadPoolSize = Runtime.getRuntime().availableProcessors();
        log.info("Creating janitor executor with " + threadPoolSize + " threads");
        return Executors.newFixedThreadPool(
                threadPoolSize,
                new ThreadFactoryBuilder()
                        .setDaemon(true)
                        .setNameFormat(JettyHttpServer.class.getName() + "-Janitor-%d")
                        .setThreadFactory(factory)
                        .build()
        );
    }

    @Override
    public void start() {
        try {
            server.start();
        } catch (final Exception e) {
            throw new RuntimeException("Failed to start server.", e);
        }
    }

    @Override
    public void close() {
        try {
            server.stop();
        } catch (final Exception e) {
            log.log(Level.SEVERE, "Server shutdown threw an unexpected exception.", e);
        }

        metricReporterExecutor.shutdown();
        janitor.shutdown();
    }

    public int getListenPort() {
        return ((ServerConnector)server.getConnectors()[0]).getLocalPort();
    }

    private class MetricTask implements Runnable {
        @Override
        public void run() {
            StatisticsHandler statisticsHandler = ((AbstractHandlerContainer)server.getHandler())
                    .getChildHandlerByClass(StatisticsHandler.class);
            if (statisticsHandler == null)
                return;

            setServerMetrics(statisticsHandler);

            for (Connector connector : server.getConnectors()) {
                setConnectorMetrics((JDiscServerConnector)connector);
            }
        }

    }

    private void setServerMetrics(StatisticsHandler statistics) {
        long timeSinceStarted = System.currentTimeMillis() - timeStarted;
        metric.set(Metrics.STARTED_MILLIS, timeSinceStarted, null);
        metric.set(Metrics.MANHATTAN_STARTED_MILLIS, timeSinceStarted, null);

        metric.add(Metrics.RESPONSES_1XX, statistics.getResponses1xx(), null);
        metric.add(Metrics.RESPONSES_2XX, statistics.getResponses2xx(), null);
        metric.add(Metrics.RESPONSES_3XX, statistics.getResponses3xx(), null);
        metric.add(Metrics.RESPONSES_4XX, statistics.getResponses4xx(), null);
        metric.add(Metrics.RESPONSES_5XX, statistics.getResponses5xx(), null);

        // Reset to only add the diff for count metrics.
        // (The alternative to reset would be to preserve the previous value, and only add the diff.)
        statistics.statsReset();
    }

    private void setConnectorMetrics(JDiscServerConnector connector) {
        ConnectorStatistics statistics = connector.getStatistics();
        metric.set(Metrics.NUM_CONNECTIONS, statistics.getConnections(), connector.getMetricContext());
        metric.set(Metrics.NUM_OPEN_CONNECTIONS, statistics.getConnectionsOpen(), connector.getMetricContext());
        metric.set(Metrics.NUM_CONNECTIONS_OPEN_MAX, statistics.getConnectionsOpenMax(), connector.getMetricContext());
        metric.set(Metrics.CONNECTION_DURATION_MAX, statistics.getConnectionDurationMax(), connector.getMetricContext());
        metric.set(Metrics.CONNECTION_DURATION_MEAN, statistics.getConnectionDurationMean(), connector.getMetricContext());
        metric.set(Metrics.CONNECTION_DURATION_STD_DEV, statistics.getConnectionDurationStdDev(), connector.getMetricContext());
    }

    private StatisticsHandler newStatisticsHandler() {
        StatisticsHandler statisticsHandler = new StatisticsHandler();
        statisticsHandler.statsReset();
        return statisticsHandler;
    }

    private GzipHandler newGzipHandler(ServerConfig serverConfig) {
        final GzipHandler gzipHandler = new GzipHandler();
        gzipHandler.setCompressionLevel(serverConfig.responseCompressionLevel());
        gzipHandler.setCheckGzExists(false);
        gzipHandler.setIncludedMethods("GET", "POST");
        return gzipHandler;
    }
}