aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/status/statuspage/StatusPageServer.java
blob: 8b3bec1b2cf09302bec95b282873451807d77138 (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core.status.statuspage;

import com.yahoo.exception.ExceptionUtils;
import java.util.logging.Level;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Shows status pages with debug information through a very simple HTTP interface.
 */
public class StatusPageServer implements Runnable, StatusPageServerInterface {

    public static Logger log = Logger.getLogger(StatusPageServer.class.getName());

    private final com.yahoo.vespa.clustercontroller.core.Timer timer;
    private final Object monitor;
    private ServerSocket ssocket;
    private final Thread runner;
    private int port = 0;
    private boolean running = true;
    private boolean shouldBeConnected = false;
    private HttpRequest currentHttpRequest = null;
    private StatusPageResponse currentResponse = null;
    private long lastConnectErrorTime = 0;
    private String lastConnectError = "";
    private PatternRequestRouter staticContentRouter = new PatternRequestRouter();
    private Date startTime = new Date();

    public StatusPageServer(com.yahoo.vespa.clustercontroller.core.Timer timer, Object monitor, int port) throws java.io.IOException, InterruptedException {
        this.timer = timer;
        this.monitor = monitor;
        this.port = port;
        connect();
        runner = new Thread(this);
        runner.start();
    }

    public boolean isConnected() {
        if (ssocket != null && ssocket.isBound() && (ssocket.getLocalPort() == port || port == 0)) {
            return true;
        } else {
            log.log(Level.FINEST, "Status page server socket is no longer connected: "+ (ssocket != null) + " " + ssocket.isBound() + " " + ssocket.getLocalPort() + " " + port);
            return false;
        }
    }

    public void connect() throws java.io.IOException, InterruptedException {
        synchronized(monitor) {
            if (ssocket != null) {
                if (ssocket.isBound() && ssocket.getLocalPort() == port) {
                    return;
                }
                disconnect();
            }
            ssocket = new ServerSocket();
            if (port != 0) {
                ssocket.setReuseAddress(true);
            }
            ssocket.setSoTimeout(100);
            ssocket.bind(new InetSocketAddress(port));
            shouldBeConnected = true;
            for (int i=0; i<200; ++i) {
                if (isConnected()) break;
                Thread.sleep(10);
            }
            if (!isConnected()) {
                log.log(Level.INFO, "Fleetcontroller: Server Socket not ready after connect()");
            }
            log.log(Level.FINE, "Fleet controller status page viewer listening to " + ssocket.getLocalSocketAddress());
            monitor.notifyAll();
        }
    }

    public void disconnect() throws java.io.IOException {
        synchronized(monitor) {
            shouldBeConnected = false;
            if (ssocket != null) ssocket.close();
            ssocket = null;
            monitor.notifyAll();
        }
    }

    public void setPort(int port) throws java.io.IOException, InterruptedException {
            // Only bother to reconnect if we were connected to begin with, we care about what port it runs on, and it's not already running there
        if (port != 0 && isConnected() && port != ((InetSocketAddress) ssocket.getLocalSocketAddress()).getPort()) {
            log.log(Level.INFO, "Exchanging port used by status server. Moving from port "
                    + ((InetSocketAddress) ssocket.getLocalSocketAddress()).getPort() + " to port " + port);
            disconnect();
            this.port = port;
            if (ssocket == null || !ssocket.isBound() || ssocket.getLocalPort() != port) {
                connect();
            }
        } else {
            this.port = port;
        }
    }

    public int getPort() {
        // Cannot use  this.port, because of tests using port 0 to get any address
        if (ssocket == null || !ssocket.isBound()) {
            throw new IllegalStateException("Cannot ask for port before server socket is bound");
        }
        return ((InetSocketAddress) ssocket.getLocalSocketAddress()).getPort();
    }

    public void shutdown() throws InterruptedException, java.io.IOException {
        running = false;
        runner.interrupt();
        runner.join();
        disconnect();
    }

    public void run() {
        try{
            while (running) {
                Socket connection = null;
                ServerSocket serverSocket = null;
                synchronized(monitor) {
                    if (ssocket == null || !ssocket.isBound()) {
                        monitor.wait(1000);
                        continue;
                    }
                    serverSocket = ssocket;
                }
                try{
                    connection = serverSocket.accept();
                } catch (SocketTimeoutException e) {
                    // Ignore, since timeout is set to 100 ms
                } catch (java.io.IOException e) {
                    log.log(shouldBeConnected ? LogLevel.WARNING : LogLevel.DEBUG, "Caught IO exception in ServerSocket.accept(): " + e.getMessage());
                }
                if (connection == null) continue;
                log.log(Level.FINE, "Got a status page request.");
                String requestString = "";
                OutputStream output = null;
                try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                    StringBuilder sb = new StringBuilder();
                    while (true) {
                        String s = br.readLine();
                        if (s == null) throw new java.io.IOException("No data in HTTP request on socket " + connection.toString());
                        if (s.length() > 4 && s.substring(0,4).equals("GET ")) {
                            int nextSpace = s.indexOf(' ', 4);
                            if (nextSpace == -1) {
                                requestString = s.substring(4);
                            } else {
                                requestString = s.substring(4, nextSpace);
                            }
                        }
                        if (s == null || s.equals("")) break;
                        sb.append(s).append("\n");
                    }
                    log.log(Level.FINE, "Got HTTP request: " + sb.toString());

                    HttpRequest httpRequest = null;
                    StatusPageResponse response = null;
                    try {
                        httpRequest = new HttpRequest(requestString);
                        // Static files are served directly by the HTTP server thread, since
                        // it makes no sense to go via the fleetcontroller logic for these.
                        RequestHandler contentHandler = staticContentRouter.resolveHandler(httpRequest);
                        if (contentHandler != null) {
                            response = contentHandler.handle(httpRequest);
                        }
                    } catch (Exception e) {
                        response = new StatusPageResponse();
                        response.setResponseCode(StatusPageResponse.ResponseCode.INTERNAL_SERVER_ERROR);
                        StringBuilder content = new StringBuilder();
                        response.writeHtmlHeader(content, "Internal Server Error");
                        response.writeHtmlFooter(content, ExceptionUtils.getStackTraceAsString(e));
                        response.writeContent(content.toString());
                    }
                    if (response == null) {
                        synchronized(monitor) {
                            currentHttpRequest = httpRequest;
                            currentResponse = null;
                            while (running) {
                                if (currentResponse != null) {
                                    response = currentResponse;
                                    break;
                                }
                                monitor.wait(100);
                            }
                        }
                    }
                    if (response == null) {
                        response = new StatusPageResponse();
                        StringBuilder content = new StringBuilder();
                        response.setContentType("text/html");
                        response.writeHtmlHeader(content, "Failed to get response. Fleet controller probably in the process of shutting down.");
                        response.writeHtmlFooter(content, "");
                        response.writeContent(content.toString());
                    }

                    output = connection.getOutputStream();
                    StringBuilder header = new StringBuilder();
                    // TODO: per-response cache control
                    header.append("HTTP/1.1 ")
                            .append(response.getResponseCode().getCode())
                            .append(" ")
                            .append(response.getResponseCode().getMessage())
                            .append("\r\n")
                            .append("Date: ").append(new Date().toString()).append("\r\n")
                            .append("Connection: Close\r\n")
                            .append("Content-type: ").append(response.getContentType()).append("\r\n");
                    if (response.isClientCachingEnabled()) {
                        // TODO(vekterli): would be better to let HTTP handlers set header values in response
                        DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
                        df.setTimeZone(TimeZone.getTimeZone("GMT"));
                        header.append("Last-Modified: ").append(df.format(startTime)).append("\r\n");
                    } else {
                        header.append("Expires: Fri, 01 Jan 1990 00:00:00 GMT\r\n")
                                .append("Pragma: no-cache\r\n")
                                .append("Cache-control: no-cache, must-revalidate\r\n");
                    }
                    header.append("\r\n");
                    output.write(header.toString().getBytes());
                    output.write(response.getOutputStream().toByteArray());
                } catch (java.io.IOException e) {
                   log.log(e.getMessage().indexOf("Broken pipe") >= 0 ? LogLevel.DEBUG : LogLevel.INFO,
                           "Failed to process HTTP request : " + e.getMessage());
                } catch (Exception e) {
                    log.log(LogLevel.WARNING, "Caught exception in HTTP server thread: "
                            + e.getClass().getName() + ": " + e.getMessage());
                } finally {
                    if (output != null) try {
                        output.close();
                    } catch (IOException e) {
                        log.log(e.getMessage().indexOf("Broken pipe") >= 0 ? LogLevel.DEBUG : LogLevel.INFO,
                                "Failed to close output stream on socket " + connection + ": " + e.getMessage());
                    }
                    if (connection != null) try{
                        connection.close();
                    } catch (IOException e) {
                        log.log(Level.INFO, "Failed to close socket " + connection + ": " + e.getMessage());
                    }
                }
            }
        } catch (InterruptedException e) {
            log.log(Level.FINE,  "Status processing thread shut down by interrupt exception: " + e);
        }
    }

    /**
     * Very simple HTTP request class. This should be replaced the second
     * the fleetcontroller e.g. moves into the container.
     */
    public static class HttpRequest {
        private final String request;
        private String pathPrefix = "";
        private final Map<String, String> params = new HashMap<String, String>();
        private String path;

        static Pattern pathPattern;
        static {
            // NOTE: allow [=.] in path to be backwards-compatible with legacy node
            // status pages.
            // If you stare at it for long enough, this sorta looks like one of those
            // magic eye pictures.
            pathPattern = Pattern.compile("^(/([\\w=\\./]+)?)(?:\\?((?:&?\\w+(?:=[\\w\\.]*)?)*))?$");
        }

        public HttpRequest(String request) {
            this.request = request;
            Matcher m = pathPattern.matcher(request);
            if (!m.matches()) {
                throw new IllegalArgumentException("Illegal HTTP request path: " + request);
            }
            path = m.group(1);
            if (m.group(3) != null) {
                String[] rawParams = m.group(3).split("&");
                for (String param : rawParams) {
                    // Parameter values are optional.
                    String[] queryParts = param.split("=");
                    params.put(queryParts[0], queryParts.length > 1 ? queryParts[1] : null);
                }
            }
        }

        public String getPathPrefix() { return pathPrefix; }

        public String toString() {
            return "HttpRequest(" + request + ")";
        }

        public String getRequest() {
            return request;
        }

        public String getPath() {
            return path;
        }

        public boolean hasQueryParameters() {
            return !params.isEmpty();
        }

        public String getQueryParameter(String name) {
            return params.get(name);
        }

        public boolean hasQueryParameter(String name) {
            return params.containsKey(name);
        }

        public void setPathPrefix(String pathPrefix) {
            this.pathPrefix = pathPrefix;
        }
    }

    public interface RequestHandler {
        StatusPageResponse handle(HttpRequest request);
    }

    public interface RequestRouter {
        /**
         * Resolve a request's handler based on its path.
         * @param request HTTP request to resolve for.
         * @return the request handler, or null if none matched.
         */
        RequestHandler resolveHandler(HttpRequest request);
    }

    /**
     * Request router inspired by the Django framework's regular expression
     * based approach. Patterns are matched in the same order as they were
     * added to the router and the first matching one is used as the handler.
     */
    public static class PatternRequestRouter implements RequestRouter {
        private static class PatternRouting {
            public Pattern pattern;
            public RequestHandler handler;

            private PatternRouting(Pattern pattern, RequestHandler handler) {
                this.pattern = pattern;
                this.handler = handler;
            }
        }

        private List<PatternRouting> patterns = new ArrayList<>();

        public void addHandler(Pattern pattern, RequestHandler handler) {
            patterns.add(new PatternRouting(pattern, handler));
        }

        public void addHandler(String pattern, RequestHandler handler) {
            addHandler(Pattern.compile(pattern), handler);
        }

        @Override
        public RequestHandler resolveHandler(HttpRequest request) {
            for (PatternRouting routing : patterns) {
                Matcher m = routing.pattern.matcher(request.getPath());
                if (m.matches()) {
                    return routing.handler;
                }
            }
            return null;
        }
    }

    public HttpRequest getCurrentHttpRequest() {
        synchronized (monitor) {
            return currentHttpRequest;
        }
    }

    public void answerCurrentStatusRequest(StatusPageResponse r) {
        if (!isConnected()) {
            long time = timer.getCurrentTimeInMillis();
            try{
                connect();
            } catch (Exception e) {
                if (!e.getMessage().equals(lastConnectError) || time - lastConnectErrorTime > 60 * 1000) {
                    lastConnectError = e.getMessage();
                    lastConnectErrorTime = time;
                    log.log(LogLevel.WARNING, "Failed to initialize HTTP status server server socket: " + e.getMessage());
                }
            }
        }
        synchronized (monitor) {
            currentResponse = r;
            currentHttpRequest = null; // Avoid fleetcontroller processing request more than once
        }
    }

}