File: TSaslNonblockingServer.java

package info (click to toggle)
libthrift-java 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,108 kB
  • sloc: java: 13,229; xml: 66; makefile: 6
file content (488 lines) | stat: -rw-r--r-- 15,951 bytes parent folder | download | duplicates (3)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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 org.apache.thrift.server;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import javax.security.auth.callback.CallbackHandler;
import org.apache.thrift.TProcessor;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.apache.thrift.transport.TNonblockingTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.sasl.NonblockingSaslHandler;
import org.apache.thrift.transport.sasl.NonblockingSaslHandler.Phase;
import org.apache.thrift.transport.sasl.TBaseSaslProcessorFactory;
import org.apache.thrift.transport.sasl.TSaslProcessorFactory;
import org.apache.thrift.transport.sasl.TSaslServerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** TServer with sasl support, using asynchronous execution and nonblocking io. */
public class TSaslNonblockingServer extends TServer {
  private static final Logger LOGGER = LoggerFactory.getLogger(TSaslNonblockingServer.class);

  private static final int DEFAULT_NETWORK_THREADS = 1;
  private static final int DEFAULT_AUTHENTICATION_THREADS = 1;
  private static final int DEFAULT_PROCESSING_THREADS = Runtime.getRuntime().availableProcessors();

  private final AcceptorThread acceptor;
  private final NetworkThreadPool networkThreadPool;
  private final ExecutorService authenticationExecutor;
  private final ExecutorService processingExecutor;
  private final TSaslServerFactory saslServerFactory;
  private final TSaslProcessorFactory saslProcessorFactory;

  public TSaslNonblockingServer(Args args) throws IOException {
    super(args);
    acceptor = new AcceptorThread((TNonblockingServerSocket) serverTransport_);
    networkThreadPool = new NetworkThreadPool(args.networkThreads);
    authenticationExecutor = Executors.newFixedThreadPool(args.saslThreads);
    processingExecutor = Executors.newFixedThreadPool(args.processingThreads);
    saslServerFactory = args.saslServerFactory;
    saslProcessorFactory = args.saslProcessorFactory;
  }

  @Override
  public void serve() {
    if (eventHandler_ != null) {
      eventHandler_.preServe();
    }
    networkThreadPool.start();
    acceptor.start();
    setServing(true);
  }

  /** Trigger a graceful shutdown, but it does not block to wait for the shutdown to finish. */
  @Override
  public void stop() {
    if (!stopped_) {
      setServing(false);
      stopped_ = true;
      acceptor.wakeup();
      networkThreadPool.wakeupAll();
      authenticationExecutor.shutdownNow();
      processingExecutor.shutdownNow();
    }
  }

  /**
   * Gracefully shut down the server and block until all threads are stopped.
   *
   * @throws InterruptedException if is interrupted while waiting for shutdown.
   */
  public void shutdown() throws InterruptedException {
    stop();
    acceptor.join();
    for (NetworkThread networkThread : networkThreadPool.networkThreads) {
      networkThread.join();
    }
    while (!authenticationExecutor.isTerminated()) {
      authenticationExecutor.awaitTermination(10, TimeUnit.SECONDS);
    }
    while (!processingExecutor.isTerminated()) {
      processingExecutor.awaitTermination(10, TimeUnit.SECONDS);
    }
  }

  private class AcceptorThread extends Thread {

    private final TNonblockingServerTransport serverTransport;
    private final Selector acceptSelector;

    private AcceptorThread(TNonblockingServerSocket serverTransport) throws IOException {
      super("acceptor-thread");
      this.serverTransport = serverTransport;
      acceptSelector = Selector.open();
      serverTransport.registerSelector(acceptSelector);
    }

    @Override
    public void run() {
      try {
        serverTransport.listen();
        while (!stopped_) {
          select();
          acceptNewConnection();
        }
      } catch (TTransportException e) {
        // Failed to listen.
        LOGGER.error("Failed to listen on server socket, error " + e.getType(), e);
      } catch (Throwable e) {
        // Unexpected errors.
        LOGGER.error("Unexpected error in acceptor thread.", e);
      } finally {
        TSaslNonblockingServer.this.stop();
        close();
      }
    }

    void wakeup() {
      acceptSelector.wakeup();
    }

    private void acceptNewConnection() {
      Iterator<SelectionKey> selectedKeyItr = acceptSelector.selectedKeys().iterator();
      while (!stopped_ && selectedKeyItr.hasNext()) {
        SelectionKey selected = selectedKeyItr.next();
        selectedKeyItr.remove();
        if (selected.isAcceptable()) {
          try {
            while (true) {
              // Accept all available connections from the backlog.
              TNonblockingTransport connection = serverTransport.accept();
              if (connection == null) {
                break;
              }
              if (!networkThreadPool.acceptNewConnection(connection)) {
                LOGGER.error("Network thread does not accept: " + connection);
                connection.close();
              }
            }
          } catch (TTransportException e) {
            LOGGER.warn("Failed to accept incoming connection.", e);
          }
        } else {
          LOGGER.error("Not acceptable selection: " + selected.channel());
        }
      }
    }

    private void select() {
      try {
        acceptSelector.select();
      } catch (IOException e) {
        LOGGER.error("Failed to select on the server socket.", e);
      }
    }

    private void close() {
      LOGGER.info("Closing acceptor thread.");
      serverTransport.close();
      try {
        acceptSelector.close();
      } catch (IOException e) {
        LOGGER.error("Failed to close accept selector.", e);
      }
    }
  }

  private class NetworkThread extends Thread {
    private final BlockingQueue<TNonblockingTransport> incomingConnections =
        new LinkedBlockingQueue<>();
    private final BlockingQueue<NonblockingSaslHandler> stateTransitions =
        new LinkedBlockingQueue<>();
    private final Selector ioSelector;

    NetworkThread(String name) throws IOException {
      super(name);
      ioSelector = Selector.open();
    }

    @Override
    public void run() {
      try {
        while (!stopped_) {
          handleIncomingConnections();
          handleStateChanges();
          select();
          handleIO();
        }
      } catch (Throwable e) {
        LOGGER.error("Unreoverable error in " + getName(), e);
      } finally {
        close();
      }
    }

    private void handleStateChanges() {
      while (true) {
        NonblockingSaslHandler statemachine = stateTransitions.poll();
        if (statemachine == null) {
          return;
        }
        tryRunNextPhase(statemachine);
      }
    }

    private void select() {
      try {
        ioSelector.select();
      } catch (IOException e) {
        LOGGER.error("Failed to select in " + getName(), e);
      }
    }

    private void handleIO() {
      Iterator<SelectionKey> selectedKeyItr = ioSelector.selectedKeys().iterator();
      while (!stopped_ && selectedKeyItr.hasNext()) {
        SelectionKey selected = selectedKeyItr.next();
        selectedKeyItr.remove();
        if (!selected.isValid()) {
          closeChannel(selected);
        }
        NonblockingSaslHandler saslHandler = (NonblockingSaslHandler) selected.attachment();
        if (selected.isReadable()) {
          saslHandler.handleRead();
        } else if (selected.isWritable()) {
          saslHandler.handleWrite();
        } else {
          LOGGER.error("Invalid intrest op " + selected.interestOps());
          closeChannel(selected);
          continue;
        }
        if (saslHandler.isCurrentPhaseDone()) {
          tryRunNextPhase(saslHandler);
        }
      }
    }

    // The following methods are modifying the registered channel set on the selector, which itself
    // is not thread safe. Thus we need a lock to protect it from race condition.

    private synchronized void handleIncomingConnections() {
      while (true) {
        TNonblockingTransport connection = incomingConnections.poll();
        if (connection == null) {
          return;
        }
        if (!connection.isOpen()) {
          LOGGER.warn("Incoming connection is already closed");
          continue;
        }
        try {
          SelectionKey selectionKey = connection.registerSelector(ioSelector, SelectionKey.OP_READ);
          if (selectionKey.isValid()) {
            NonblockingSaslHandler saslHandler =
                new NonblockingSaslHandler(
                    selectionKey,
                    connection,
                    saslServerFactory,
                    saslProcessorFactory,
                    inputProtocolFactory_,
                    outputProtocolFactory_,
                    eventHandler_);
            selectionKey.attach(saslHandler);
          }
        } catch (IOException e) {
          LOGGER.error("Failed to register connection for the selector, close it.", e);
          connection.close();
        }
      }
    }

    private synchronized void close() {
      LOGGER.warn("Closing " + getName());
      while (true) {
        TNonblockingTransport incomingConnection = incomingConnections.poll();
        if (incomingConnection == null) {
          break;
        }
        incomingConnection.close();
      }
      Set<SelectionKey> registered = ioSelector.keys();
      for (SelectionKey selection : registered) {
        closeChannel(selection);
      }
      try {
        ioSelector.close();
      } catch (IOException e) {
        LOGGER.error("Failed to close io selector " + getName(), e);
      }
    }

    private synchronized void closeChannel(SelectionKey selectionKey) {
      if (selectionKey.attachment() == null) {
        try {
          selectionKey.channel().close();
        } catch (IOException e) {
          LOGGER.error("Failed to close channel.", e);
        } finally {
          selectionKey.cancel();
        }
      } else {
        NonblockingSaslHandler saslHandler = (NonblockingSaslHandler) selectionKey.attachment();
        saslHandler.close();
      }
    }

    private void tryRunNextPhase(NonblockingSaslHandler saslHandler) {
      Phase nextPhase = saslHandler.getNextPhase();
      saslHandler.stepToNextPhase();
      switch (nextPhase) {
        case EVALUATING_SASL_RESPONSE:
          authenticationExecutor.submit(new Computation(saslHandler));
          break;
        case PROCESSING:
          processingExecutor.submit(new Computation(saslHandler));
          break;
        case CLOSING:
          saslHandler.runCurrentPhase();
          break;
        default: // waiting for next io event for the current state machine
      }
    }

    public boolean accept(TNonblockingTransport connection) {
      if (stopped_) {
        return false;
      }
      if (incomingConnections.offer(connection)) {
        wakeup();
        return true;
      }
      return false;
    }

    private void wakeup() {
      ioSelector.wakeup();
    }

    private class Computation implements Runnable {

      private final NonblockingSaslHandler statemachine;

      private Computation(NonblockingSaslHandler statemachine) {
        this.statemachine = statemachine;
      }

      @Override
      public void run() {
        try {
          while (!statemachine.isCurrentPhaseDone()) {
            statemachine.runCurrentPhase();
          }
          stateTransitions.add(statemachine);
          wakeup();
        } catch (Throwable e) {
          LOGGER.error("Damn it!", e);
        }
      }
    }
  }

  private class NetworkThreadPool {
    private final List<NetworkThread> networkThreads;
    private int accepted = 0;

    NetworkThreadPool(int size) throws IOException {
      networkThreads = new ArrayList<>(size);
      int digits = (int) Math.log10(size) + 1;
      String threadNamePattern = "network-thread-%0" + digits + "d";
      for (int i = 0; i < size; i++) {
        networkThreads.add(new NetworkThread(String.format(threadNamePattern, i)));
      }
    }

    /**
     * Round robin new connection among all the network threads.
     *
     * @param connection incoming connection.
     * @return true if the incoming connection is accepted by network thread pool.
     */
    boolean acceptNewConnection(TNonblockingTransport connection) {
      return networkThreads.get((accepted++) % networkThreads.size()).accept(connection);
    }

    public void start() {
      for (NetworkThread thread : networkThreads) {
        thread.start();
      }
    }

    void wakeupAll() {
      for (NetworkThread networkThread : networkThreads) {
        networkThread.wakeup();
      }
    }
  }

  public static class Args extends AbstractServerArgs<Args> {

    private int networkThreads = DEFAULT_NETWORK_THREADS;
    private int saslThreads = DEFAULT_AUTHENTICATION_THREADS;
    private int processingThreads = DEFAULT_PROCESSING_THREADS;
    private TSaslServerFactory saslServerFactory = new TSaslServerFactory();
    private TSaslProcessorFactory saslProcessorFactory;

    public Args(TNonblockingServerTransport transport) {
      super(transport);
    }

    public Args networkThreads(int networkThreads) {
      this.networkThreads = networkThreads <= 0 ? DEFAULT_NETWORK_THREADS : networkThreads;
      return this;
    }

    public Args saslThreads(int authenticationThreads) {
      this.saslThreads =
          authenticationThreads <= 0 ? DEFAULT_AUTHENTICATION_THREADS : authenticationThreads;
      return this;
    }

    public Args processingThreads(int processingThreads) {
      this.processingThreads =
          processingThreads <= 0 ? DEFAULT_PROCESSING_THREADS : processingThreads;
      return this;
    }

    public Args processor(TProcessor processor) {
      saslProcessorFactory = new TBaseSaslProcessorFactory(processor);
      return this;
    }

    public Args saslProcessorFactory(TSaslProcessorFactory saslProcessorFactory) {
      if (saslProcessorFactory == null) {
        throw new NullPointerException("Processor factory cannot be null");
      }
      this.saslProcessorFactory = saslProcessorFactory;
      return this;
    }

    public Args addSaslMechanism(
        String mechanism,
        String protocol,
        String serverName,
        Map<String, String> props,
        CallbackHandler cbh) {
      saslServerFactory.addSaslMechanism(mechanism, protocol, serverName, props, cbh);
      return this;
    }

    public Args saslServerFactory(TSaslServerFactory saslServerFactory) {
      if (saslServerFactory == null) {
        throw new NullPointerException("saslServerFactory cannot be null");
      }
      this.saslServerFactory = saslServerFactory;
      return this;
    }
  }
}