aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/io/Listener.java
blob: c76ffeaabad383789cdd6eede142147d528aa29e (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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;


import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import java.net.InetSocketAddress;

import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;

import java.util.logging.Logger;
import java.util.logging.Level;

import java.nio.channels.SocketChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.Selector;
import java.nio.channels.SelectionKey;


/**
 * A basic Reactor implementation using NIO.
 *
 * @author <a href="mailto:travisb@yahoo-inc.com">Bob Travis</a>
 * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
 *
 */
public class Listener extends Thread {
    private static Logger log = Logger.getLogger(Listener.class.getName());
    private Selector selector;
    Map<Integer, Acceptor> acceptors = new HashMap<>();
    Map<ServerSocketChannel, ConnectionFactory> factories = new IdentityHashMap<>();

    private FatalErrorHandler fatalErrorHandler;

    private List<SelectLoopHook> selectLoopPreHooks;
    private List<SelectLoopHook> selectLoopPostHooks;

    final private LinkedList<Connection> newConnections = new LinkedList<>();

    // queue of SelectionKeys that need to be updated
    final private LinkedList<UpdateInterest> modifyInterestOpsQueue = new LinkedList<>();

    public Listener(String name) {
        super("Listener-" + name);

        try {
            selector = Selector.open();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        log.fine(name + " listener created " + this);
    }

    /**
     * Register a handler for fatal errors.
     *
     * @param f The FatalErrorHandler instance to be registered
     */
    public synchronized void setFatalErrorHandler(FatalErrorHandler f) {
        fatalErrorHandler = f;
    }

    /**
     * Add pre-select loop hook.  Not threadsafe so please do this
     * during initial setup before you start the listener.
     */
    public void addSelectLoopPreHook(SelectLoopHook hook) {
        if (selectLoopPreHooks == null) {
            selectLoopPreHooks = new ArrayList<>(5);
        }
        selectLoopPreHooks.add(hook);
    }

    /**
     * Add pre-select loop hook.  Not threadsafe so please do this
     * during initial setup before you start the listener.
     */
    public void addSelectLoopPostHook(SelectLoopHook hook) {
        if (selectLoopPostHooks == null) {
            selectLoopPostHooks = new ArrayList<>(5);
        }
        selectLoopPostHooks.add(hook);
    }

    /**
     * Run all the select loop pre hooks
     */
    private void runSelectLoopPreHooks() {
        if (selectLoopPreHooks == null) {
            return;
        }

        for (SelectLoopHook hook : selectLoopPreHooks) {
            hook.selectLoopHook(true);
        }
    }

    /**
     * Run all the select loop post hooks
     */
    private void runSelectLoopPostHooks() {
        if (selectLoopPostHooks == null) {
            return;
        }

        for (SelectLoopHook hook : selectLoopPostHooks) {
            hook.selectLoopHook(false);
        }
    }

    /**
     * Add a listening port and create an Acceptor thread which accepts
     * new connections on this port.
     *
     * @param factory The connection factory for new connections
     *                on this port
     * @param port The port we are going to listen to.
     */
    public synchronized void listen(ConnectionFactory factory, int port)
        throws IOException {
        // make sure we have only one acceptor per listen port
        if (acceptors.containsKey(port)) {
            log.warning("Already listening to port=" + port);
            return;
        }

        Acceptor a = new Acceptor(this, factory, port);

        // inherit the fatal error handling of listener
        if (fatalErrorHandler != null) {
            a.setFatalErrorHandler(fatalErrorHandler);
        }

        a.listen().start();
        acceptors.put(port, a);
    }

    /**
     * Add a listening port without creating a separate acceptor
     * thread.
     *
     * @param factory The connection factory for new connections
     *                on this port
     * @param port The port we are going to listen to.
     */
    public synchronized void listenNoAcceptor(ConnectionFactory factory, int port)
        throws IOException {
        ServerSocketChannel s = ServerSocketChannel.open();

        s.configureBlocking(false);
        s.socket().setReuseAddress(true);
        s.socket().bind(new InetSocketAddress(port)); // use non-specific IP
        String host = s.socket().getInetAddress().getHostName();

        factories.put(s, factory);
        s.register(selector, SelectionKey.OP_ACCEPT);
        log.fine("listener " + host + ":" + port);
    }

    // ==================================================================
    // ==================================================================
    // ==================================================================


    /**
     * This is the preferred way of modifying interest ops, giving a
     * Connection rather than a SelectionKey as input.  This way the
     * we can look it up and ensure the correct SelectionKey is always
     * used.
     *
     * @return Returns a <code>this</code> reference for chaining
     */
    public Listener modifyInterestOps(Connection connection,
            int op, boolean set) {
        return modifyInterestOps(connection.socketChannel().keyFor(selector), op,
                set);
    }

    /**
     * Batch version of modifyInterestOps().
     *
     * @return Returns a <code>this</code> reference for chaining
     */
    public Listener modifyInterestOpsBatch(Connection connection,
            int op, boolean set) {
        return modifyInterestOpsBatch(
                connection.socketChannel().keyFor(selector), op, set);
    }

    /**
     * Enqueue change to interest set of SelectionKey.  This is a workaround
     * for an NIO design error that makes it impossible to update interest
     * sets for a SelectionKey while a select is in progress -- and sometimes
     * you actually want to do this from other threads, which will then
     * block.  Hence, we make it possible to enqueue requests for
     * SelectionKey modification in the thread where select runs.
     *
     * @return Returns a <code>this</code> reference for chaining
     */
    public Listener modifyInterestOps(SelectionKey key, int op, boolean set) {
        synchronized (modifyInterestOpsQueue) {
            modifyInterestOpsQueue.addLast(new UpdateInterest(key, op, set));
        }
        selector.wakeup();
        return this;
    }

    /**
     * Does the same as modifyInterestOps(), but does not call
     * wakeup on the selector.  Allows adding more modifications
     * before we wake up the selector.
     *
     * @return Returns a <code>this</code> reference for chaining
     */
    public Listener modifyInterestOpsBatch(SelectionKey key,
            int op,
            boolean set) {
        synchronized (modifyInterestOpsQueue) {
            modifyInterestOpsQueue.addLast(new UpdateInterest(key, op, set));
        }
        return this;
    }

    /**
     * Signal that a batch update of SelectionKey is done and the
     * selector should be awoken.  Also see modifyInterestOps().
     *
     * @return Returns a <code>this</code> reference for chaining
     */
    public Listener modifyInterestOpsDone() {
        selector.wakeup();
        return this;
    }

    /**
     * Process enqueued changes to SelectionKeys. Also see
     * modifyInterestOps().
     */
    private void processModifyInterestOps() {
        synchronized (modifyInterestOpsQueue) {
            while (!modifyInterestOpsQueue.isEmpty()) {
                UpdateInterest u = modifyInterestOpsQueue.removeFirst();

                u.doUpdate();
            }
        }
    }

    // ==================================================================
    // ==================================================================
    // ==================================================================


    /**
     * Thread entry point
     */
    public void run() {
        log.fine("Started listener");
        try {
            selectLoop();
        } catch (Throwable t) {
            if (fatalErrorHandler != null) {
                fatalErrorHandler.handle(t, null);
            }
        }
    }

    /**
     * Check channels for readiness and deal with channels that have
     * pending operations.
     */
    private void selectLoop() {
        while (!Thread.currentThread().isInterrupted()) {
            processNewConnections();
            processModifyInterestOps();

            try {
                int n = selector.select();

                if (0 == n) {
                    continue;
                }
            } catch (java.io.IOException e) {
                log.log(Level.WARNING, "error during select", e);
                return;
            }

            runSelectLoopPreHooks();

            Iterator<SelectionKey> i = selector.selectedKeys().iterator();

            while (i.hasNext()) {
                SelectionKey key = i.next();

                i.remove();

                if (!key.isValid()) {
                    continue;
                }

                if (key.isReadable()) {
                    performRead(key);
                    if (!key.isValid()) {
                        continue;
                    }
                }

                if (key.isWritable()) {
                    performWrite(key);
                    if (!key.isValid()) {
                        continue;
                    }
                }

                if (key.isConnectable()) {
                    performConnect(key);
                    if (!key.isValid()) {
                        continue;
                    }
                }

                if (key.isAcceptable()) {
                    performAccept(key);
                }
            }

            runSelectLoopPostHooks();
        }
    }

    /**
     * This method is used by the Acceptor to hand off newly accepted
     * connections to the Listener.  Note that this is run in the
     * context of the Acceptor thread, so doing things here versus
     * doing them in the acceptNewConnections(), which runs in the context
     * of the Listener thread, is a tradeoff that may need to be
     * re-evaluated
     *
     */
    public Connection addNewConnection(Connection newConn) {

        // ensure nonblocking and handle possible errors
        // if setting nonblocking fails.  this code is really redundant
        // but necessary because the older version of this method set
        // the connection nonblocking, and clients might still expect
        // this behavior.
        //
        SocketChannel channel = newConn.socketChannel();

        if (channel.isBlocking()) {
            try {
                channel.configureBlocking(false);
            } catch (java.nio.channels.IllegalBlockingModeException e) {
                log.log(Level.SEVERE, "Unable to set nonblocking", e);
                try {
                    channel.close();
                } catch (java.io.IOException ee) {
                    log.log(Level.WARNING, "channel close failed", ee);
                }
                return newConn;
            } catch (java.io.IOException e) {
                log.log(Level.SEVERE, "Unable to set nonblocking", e);
                return newConn;
            }
        }

        synchronized (newConnections) {
            newConnections.addLast(newConn);
        }
        selector.wakeup();
        return newConn;
    }

    /**
     * This method is called from the selectLoop() method in order to
     * process new incoming connections.
     */
    private synchronized void processNewConnections() {
        synchronized (newConnections) {
            while (!newConnections.isEmpty()) {
                Connection conn = newConnections.removeFirst();

                try {
                    conn.socketChannel().register(selector, conn.selectOps(),
                            conn);
                } catch (ClosedChannelException e) {
                    log.log(Level.WARNING, "register channel failed", e);
                    return;
                }
            }
        }
    }

    /**
     * Accept new connection.  This will loop over accept() until
     * there are no more new connections to accept.  If any error
     * occurs after a successful accept, the socket in question will
     * be discarded, but we will continue to try to accept new
     * connections if available.
     *
     */
    private void performAccept(SelectionKey key) {
        SocketChannel channel;
        ServerSocketChannel ssChannel;

        if (Thread.currentThread().isInterrupted()) {
            return;
        }

        while (true) {
            try {
                ssChannel = (ServerSocketChannel) key.channel();
                channel = ssChannel.accept();

                // if for some reason there was no connection we just
                // ignore it.
                if (null == channel) {
                    return;
                }
            } catch (java.io.IOException e) {
                log.log(Level.WARNING, "accept failed", e);
                return;
            }

            // set nonblocking and handle possible errors
            try {
                channel.configureBlocking(false);
            } catch (java.nio.channels.IllegalBlockingModeException e) {
                log.log(Level.SEVERE, "Unable to set nonblocking", e);
                try {
                    channel.close();
                } catch (java.io.IOException ee) {
                    log.log(Level.WARNING, "channel close failed", ee);
                    continue;
                }
                continue;
            } catch (java.io.IOException e) {
                log.log(Level.WARNING, "IO error occurred", e);
                try {
                    channel.close();
                } catch (java.io.IOException ee) {
                    log.log(Level.WARNING, "channel close failed", ee);
                    continue;
                }
                continue;
            }

            ConnectionFactory factory = factories.get(ssChannel);
            Connection conn = factory.newConnection(channel, this);

            try {
                channel.register(selector, conn.selectOps(), conn);
            } catch (java.nio.channels.ClosedChannelException e) {
                log.log(Level.WARNING, "register channel failed", e);
            }
        }
    }

    /**
     * Complete asynchronous connect operation.  <em>Note that
     * asynchronous connect does not work properly in 1.4,
     * so you should not use this if you run anything older
     * than 1.5/5.0</em>.
     *
     */
    private void performConnect(SelectionKey key) {
        if (Thread.currentThread().isInterrupted()) {
            return;
        }

        Connection c = (Connection) key.attachment();

        try {
            c.connect();
        } catch (IOException e) {
            log.log(Level.FINE, "connect failed", e);
            try {
                c.close();
            } catch (IOException e2) {
                log.log(Level.FINE, "close failed", e);
            }
        }
    }

    /**
     * Perform read operation on channel which is now ready for reading
     */
    private void performRead(SelectionKey key) {
        if (Thread.currentThread().isInterrupted()) {
            return;
        }

        Connection c = (Connection) key.attachment();

        try {
            c.read();
        } catch (IOException e) {
            log.log(Level.FINE, "read failed", e);
            try {
                c.close();
            } catch (IOException e2) {
                log.log(Level.FINE, "close failed", e);
            }
        }
    }

    /**
     * Perform write operation(s) on channel which is now ready for
     * writing
     */
    private void performWrite(SelectionKey key) {
        if (Thread.currentThread().isInterrupted()) {
            return;
        }

        Connection c = (Connection) key.attachment();

        try {
            c.write();
        } catch (IOException e) {
            log.log(Level.FINE, " write failed", e);
            try {
                c.close();
            } catch (IOException e2) {// ignore
            }
        }
    }

    // ============================================================
    // ==== connections made outside listener
    // ============================================================

    /**
     * Register a connection that was set up outside the listener.
     * Typically what we do when we actively reach out and connect
     * somewhere.
     */
    public void registerConnection(Connection connection) {
        synchronized (newConnections) {
            newConnections.addLast(connection);
        }
        selector.wakeup();
    }

    /**
     * Perform clean shutdown of Listener.
     *
     * TODO: implement
     */
    public void shutdown() {// make writing impossible
        // make listening on new ports impossible
        // close all listening connections (kill all listener threads)
        // flush outbound data if the connection wants it
        // close all connections
        // have some sort of grace-period before forcibly shutting down
    }
}