/
TDisruptorServer.java
779 lines (650 loc) · 24.7 KB
/
TDisruptorServer.java
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/**
* 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 com.thinkaurelius.thrift;
import javax.management.*;
import java.io.Closeable;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.spi.SelectorProvider;
import java.util.Iterator;
import java.util.concurrent.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.lmax.disruptor.*;
import com.thinkaurelius.thrift.util.TBinaryProtocol;
import com.thinkaurelius.thrift.util.ThriftFactories;
import org.apache.thrift.server.AbstractNonblockingServer;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.apache.thrift.transport.TNonblockingTransport;
import org.apache.thrift.transport.TTransportException;
public abstract class TDisruptorServer extends TNonblockingServer implements TDisruptorServerMBean
{
private static final Logger logger = LoggerFactory.getLogger(TDisruptorServer.class);
private static final boolean isJNAPresent;
static
{
boolean jna = false;
try
{
new com.sun.jna.Pointer(0);
jna = true;
}
catch (NoClassDefFoundError e)
{
// can't do anything about it, going to use on-heap buffers instead
}
isJNAPresent = jna;
}
public static final String MBEAN_NAME = "com.thinkaurelius.thrift.server:type=TDisruptorServer";
public static class Args extends AbstractNonblockingServer.AbstractNonblockingServerArgs<Args>
{
private Integer numAcceptors, numSelectors, numWorkersPerSelector, ringSize, maxFrameSizeInBytes = 16384000;
private boolean useHeapBasedAllocation = true, alwaysReallocateBuffers = true;
private ThreadPoolExecutor invocationExecutor;
public Args(TNonblockingServerTransport transport)
{
super(transport);
}
@SuppressWarnings("unused")
public Args numAcceptors(int numAcceptors)
{
this.numAcceptors = numAcceptors;
return this;
}
@SuppressWarnings("unused")
public Args numSelectors(int numSelectors)
{
this.numSelectors = numSelectors;
return this;
}
@SuppressWarnings("unused")
public Args numWorkersPerSelector(int numWorkers)
{
this.numWorkersPerSelector = numWorkers;
return this;
}
@SuppressWarnings("unused")
public Args ringSizePerSelector(int ringSize)
{
this.ringSize = ringSize;
return this;
}
@SuppressWarnings("unused")
public Args useHeapBasedAllocation(boolean flag)
{
this.useHeapBasedAllocation = flag;
return this;
}
@SuppressWarnings("unused")
public Args maxFrameSizeInBytes(int maxFrameSizeInBytes)
{
this.maxFrameSizeInBytes = maxFrameSizeInBytes;
return this;
}
@SuppressWarnings("unused")
public Args alwaysReallocateBuffers(boolean flag) {
this.alwaysReallocateBuffers = flag;
return this;
}
/**
* Executor if set takes precedence over numWorkersPerSelector.
*
* @param executor The executor to use for message execution.
*
* @return self.
*/
@SuppressWarnings("unused")
public Args invocationExecutor(ThreadPoolExecutor executor) {
this.invocationExecutor = executor;
return this;
}
}
private final AcceptorThread[] acceptorThreads;
private final SelectorThread[] selectorThreads;
private final SelectorLoadBalancer selectorLoadBalancer;
private final ThreadPoolExecutor globalInvoker;
private final ThriftFactories thriftFactories;
private volatile boolean useHeapBasedAllocation, alwaysReallocateBuffers, isStopped;
public TDisruptorServer(Args args)
{
super(args);
final int numCores = Runtime.getRuntime().availableProcessors();
final int numAcceptors = (args.numAcceptors == null)
? 2
: args.numAcceptors;
final int numSelectors = (args.numSelectors == null)
? numCores
: args.numSelectors;
// by default, setting number of workers to 2 as we have selector per core
final int numWorkersPerSelector = (args.numWorkersPerSelector == null)
? 2
: args.numWorkersPerSelector;
// by default, setting size of the ring buffer so each worker has 2048 slots available,
// rounded to the nearest power 2 (as requirement of Disruptor).
final int ringSize = (args.ringSize == null)
? 2048
: args.ringSize;
// unfortunate fact that Thrift transports still rely on byte arrays forces us to do this :(
if (!(inputProtocolFactory_ instanceof TBinaryProtocol.Factory) || !(outputProtocolFactory_ instanceof TBinaryProtocol.Factory))
throw new IllegalArgumentException("Please use " + TBinaryProtocol.Factory.class.getCanonicalName() + " or it's subclass as protocol factories.");
// there is no need to force people to have JNA in classpath,
// let's just warn them that it's not there and we are switching to on-heap allocation.
if (!args.useHeapBasedAllocation && !isJNAPresent)
{
logger.warn("Off-heap allocation couldn't be used as JNA is not present in classpath or broken, using on-heap instead.");
args.useHeapBasedAllocation = true;
}
useHeapBasedAllocation = args.useHeapBasedAllocation;
alwaysReallocateBuffers = args.alwaysReallocateBuffers;
thriftFactories = new ThriftFactories(inputTransportFactory_, outputTransportFactory_,
inputProtocolFactory_, outputProtocolFactory_,
processorFactory_,
args.maxFrameSizeInBytes);
try
{
acceptorThreads = new AcceptorThread[numAcceptors];
for (int i = 0; i < numAcceptors; i++)
acceptorThreads[i] = new AcceptorThread("Thrift-Acceptor_" + i, (TNonblockingServerTransport) serverTransport_);
}
catch (IOException e)
{
throw new RuntimeException("Could not create acceptor threads", e);
}
if (args.invocationExecutor != null)
logger.info(String.format("Going to use %s for all of the Selector threads..", args.invocationExecutor));
try
{
selectorThreads = new SelectorThread[numSelectors];
globalInvoker = (args.invocationExecutor != null) ? args.invocationExecutor : null;
int numHandlers = (globalInvoker != null)
? globalInvoker.getMaximumPoolSize() / numSelectors
: numWorkersPerSelector;
for (int i = 0; i < numSelectors; i++)
{
ThreadPoolExecutor invoker = (globalInvoker != null)
? globalInvoker
: new ThreadPoolExecutor(numWorkersPerSelector, numWorkersPerSelector,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
selectorThreads[i] = new SelectorThread("Thrift-Selector_" + i,
nextPowerOfTwo(ringSize),
invoker,
numHandlers == 0 ? 1 : numHandlers,
(globalInvoker != null));
}
}
catch (IOException e)
{
throw new RuntimeException("Could not create selector threads", e);
}
selectorLoadBalancer = new RandomSelectorLoadBalancer(selectorThreads);
/* Register JXM listener */
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
try
{
mbs.registerMBean(this, new ObjectName(MBEAN_NAME));
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
@Override
protected boolean startThreads()
{
isStopped = false;
for (int i = 0; i < selectorThreads.length; i++)
{
selectorThreads[i].start(); // start the selector
logger.debug("Thrift Selector thread {} is started.", i);
}
for (int i = 0; i < acceptorThreads.length; i++)
{
acceptorThreads[i].start();
logger.debug("Thrift Acceptor thread {} is started.", i);
}
return true;
}
@Override
protected void waitForShutdown()
{
joinSelector();
gracefullyShutdownInvokerPool();
}
@Override
protected void joinSelector()
{
try
{
for (SelectorThread selector : selectorThreads)
selector.join(); // wait until the selector thread exits
}
catch (InterruptedException e)
{
logger.error("Interruption: " + e.getMessage());
e.printStackTrace();
}
}
protected void gracefullyShutdownInvokerPool()
{
for (SelectorThread selector : selectorThreads)
selector.shutdown();
if (globalInvoker != null)
globalInvoker.shutdown();
}
@Override
public void stop()
{
isStopped = true;
for (AcceptorThread acceptor : acceptorThreads)
acceptor.wakeupSelector();
for (SelectorThread selector : selectorThreads)
selector.wakeupSelector();
unregisterMBean();
}
void unregisterMBean()
{
try
{
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName nameObj = new ObjectName(MBEAN_NAME);
if (mbs.isRegistered(nameObj))
mbs.unregisterMBean(nameObj);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
@Override
public boolean isStopped()
{
for (SelectorThread selector : selectorThreads)
{
if (selector.selector.isOpen())
return false;
}
return true;
}
@Override
protected boolean requestInvoke(FrameBuffer frameBuffer)
{
throw new UnsupportedOperationException();
}
/**
* All implementations should use this method to provide custom behaviour in pre-invoke stage.
*
* @param message The "message" or buffer associated with request in pre-invoke state.
*/
protected abstract void beforeInvoke(Message message);
protected abstract class AbstractSelectorThread extends Thread implements Closeable
{
protected final Selector selector = SelectorProvider.provider().openSelector();
public AbstractSelectorThread(String name) throws IOException
{
super(name);
}
/**
* Must only be called within {@link AbstractSelectorThread#run()} and {@link AbstractSelectorThread#select()}.
*
* Returns true if the selector is stopped, false otherwise. Overriden in {@link AcceptorThread} and {@link SelectorThread}.
*/
protected abstract boolean isStopped();
@Override
public void run()
{
try
{
while (!isStopped())
select();
for (SelectionKey key: selector.keys())
cleanupSelectionKey(key);
}
catch (Throwable t)
{
logger.error("run() exiting due to uncaught error", t);
}
finally
{
isStopped = true;
close();
}
}
private void select()
{
try
{
// wait for io events.
selector.select();
// process the io events we received
Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator();
while (!isStopped() && selectedKeys.hasNext())
{
SelectionKey key = selectedKeys.next();
selectedKeys.remove();
// skip if not valid
if (!key.isValid())
{
cleanupSelectionKey(key);
continue;
}
processKey(key);
}
selectorIterationComplete();
}
catch (IOException e)
{
logger.warn("Got an IOException while selecting: {}!", e);
}
catch (CancelledKeyException e)
{
logger.debug("Non-fatal exception in select loop (probably somebody closed the channel)...", e);
}
}
/**
* Process each individual key that reported ready state
*
* @param key The key to process
*
* @throws IOException any I/O related error
*/
protected abstract void processKey(SelectionKey key) throws IOException;
/**
* Called after each selector event loop completion, useful for booking (e.g. registering new connections)
* @throws IOException any I/O related error
*/
protected abstract void selectorIterationComplete() throws IOException;
protected void cleanupSelectionKey(SelectionKey key)
{
// remove the records from the two maps
Message message = (Message) key.attachment();
if (message != null)
{
beforeClose(message);
message.close();
// discard message
key.attach(null);
}
// cancel the selection key
key.cancel();
}
public void wakeupSelector()
{
selector.wakeup();
}
public void close()
{
try
{
selector.close();
}
catch (IOException e)
{
logger.warn("Failed to close selector: {}.", getName(), e);
}
}
}
protected class AcceptorThread extends AbstractSelectorThread
{
private final TNonblockingServerTransport serverTransport;
/**
* Set up the thread that will handle the non-blocking accepts.
*/
public AcceptorThread(String name, TNonblockingServerTransport serverTransport) throws IOException {
super(name);
this.serverTransport = serverTransport;
this.serverTransport.registerSelector(selector); // registers with OP_ACCEPT interests
}
@Override
protected void processKey(SelectionKey key) throws IOException
{
if (!key.isAcceptable())
return;
try
{
// accept the connection
SelectorThread selector = selectorLoadBalancer.nextSelector();
selector.subscribe((TNonblockingTransport) serverTransport.accept());
selector.wakeupSelector();
}
catch (TTransportException tte)
{
// accept() shouldn't be NULL if fine because are are raising for a socket
logger.debug("Non-fatal exception trying to accept!", tte);
}
}
@Override
protected void selectorIterationComplete() {
// no post-processing is required
}
/**
* Returns true if the enclosing {@link TDisruptorServer} is stopped.
*/
@Override
protected boolean isStopped()
{
return isStopped;
}
}
protected class SelectorThread extends AbstractSelectorThread
{
private final ThreadPoolExecutor invoker;
private final boolean isSharedInvoker;
private final RingBuffer<Message.Invocation> ringBuffer;
private final WorkerPool<Message.Invocation> workerPool;
private final ConcurrentLinkedQueue<TNonblockingTransport> newConnections;
/**
* Set up the thread that will handle the non-blocking reads, and writes.
*/
public SelectorThread(String name, int ringSize, ThreadPoolExecutor invoker, int numHandlers, boolean isSharedInvoker) throws IOException
{
super(name);
this.newConnections = new ConcurrentLinkedQueue<>();
InvocationHandler handlers[] = new InvocationHandler[numHandlers];
for (int i = 0; i < handlers.length; i++)
handlers[i] = new InvocationHandler();
this.invoker = invoker;
this.isSharedInvoker = isSharedInvoker;
/**
* YieldingWaitStrategy claims to be better compromise between throughput/latency and CPU usage comparing to
* BlockingWaitStrategy, but actual tests show quite the opposite, where YieldingWaitStrategy just constantly
* burns CPU cycles with no performance benefit when coupled with networking.
*/
ringBuffer = RingBuffer.createSingleProducer(Message.Invocation.FACTORY, ringSize, new BlockingWaitStrategy());
workerPool = new WorkerPool<>(ringBuffer, ringBuffer.newBarrier(), new FatalExceptionHandler(), handlers);
workerPool.start(invoker);
}
/**
* Must only be called in {@link AbstractSelectorThread#run()} and {@link AbstractSelectorThread#select()}. Overrides base
* implementation.
* <p>
* Checks whether this selector thread is stopped. It's stopped, and returns true, if the enclosing {@code TDisruptorServer}
* instance is stopped and:
* <ul>
* <li> this selector thread's {@code Selector} is closed, or
* <li> all of this selector thread's registered {@code Message}s are inactive
* </ul>
* <p>
* While checking the active state of messages, this method cleans up the selection key for inactive ones using
* {@link #cleanupSelectionKey(SelectionKey)}. This method return false as soon as an active message is found.
*/
@Override
protected boolean isStopped()
{
if (!isStopped)
{
return false;
}
else if (!selector.isOpen())
{
// selector thread closes itself, this must be called after drain
return true;
}
// still in select loop
Iterator<SelectionKey> keys = selector.keys().iterator();
while(keys.hasNext())
{
SelectionKey key = keys.next();
Message message = (Message) key.attachment();
if (message != null && message.isActive())
{
return false;
}
// cleanup preemptively, key could still be in selected key set, but will be invalid
cleanupSelectionKey(key);
}
return true;
}
@Override
protected void processKey(SelectionKey key)
{
Message message = (Message) key.attachment();
if (message == null)
return;
if (message.isReadyToRead())
handleRead(message);
else if (message.isReadyToWrite())
handleWrite(message);
}
@Override
protected void selectorIterationComplete() throws IOException
{
TNonblockingTransport newClient;
while ((newClient = newConnections.poll()) != null)
{
SelectionKey clientKey = newClient.registerSelector(selector, SelectionKey.OP_READ);
clientKey.attach(new Message(newClient, clientKey, thriftFactories, useHeapBasedAllocation, alwaysReallocateBuffers));
}
}
protected void handleRead(Message message)
{
message.changeSelectInterests();
if (!message.read())
cancelMessage(message);
else if (message.isFrameFullyRead())
dispatchInvoke(message);
}
protected void handleWrite(Message message)
{
message.changeSelectInterests();
if (!message.write())
cancelMessage(message);
}
protected void dispatchInvoke(final Message message)
{
boolean success = ringBuffer.tryPublishEvent(new EventTranslator<Message.Invocation>()
{
@Override
public void translateTo(Message.Invocation invocation, long sequence)
{
invocation.setMessage(message);
}
});
if (!success)
{ // looks like we are overloaded, let's cancel this request (connection) and drop warn in the log
logger.warn(this + " ring buffer is full, dropping client message.");
cancelMessage(message);
}
}
private void cancelMessage(Message message)
{
beforeClose(message);
message.cancel();
}
public void subscribe(TNonblockingTransport newClient)
{
newConnections.add(newClient); // always returns true as queue is unbounded
}
public void shutdown()
{
workerPool.drainAndHalt();
if (!isSharedInvoker)
invoker.shutdown();
}
public int getRingBufferSize()
{
return ringBuffer.getBufferSize();
}
}
/**
* Allows derived classes to react when a connection is closed.
*/
protected void beforeClose(@SuppressWarnings("unused") Message buffer)
{
// NOP by default
}
private static int nextPowerOfTwo(int v)
{
return 1 << (32 - Integer.numberOfLeadingZeros(v - 1));
}
/* JMX section */
@Override
public int getRingBufferSize()
{
return selectorThreads[0].getRingBufferSize();
}
@Override
public int getNumberOfSelectors()
{
return selectorThreads.length;
}
@Override
public boolean isHeapBasedAllocationUsed()
{
return useHeapBasedAllocation;
}
@Override
public void useHeapBasedAllocation(boolean flag)
{
if (!flag && !isJNAPresent)
throw new IllegalArgumentException("Off-Heap allocation method could not be used because JNA is missing.");
useHeapBasedAllocation = flag;
}
/* Helper classes */
public class InvocationHandler implements WorkHandler<Message.Invocation>
{
@Override
public void onEvent(Message.Invocation invocation) throws Exception
{
beforeInvoke(invocation.getMessage());
invocation.execute();
}
}
public static interface SelectorLoadBalancer
{
public SelectorThread nextSelector();
}
public static class RandomSelectorLoadBalancer implements SelectorLoadBalancer
{
private final SelectorThread[] selectors;
public RandomSelectorLoadBalancer(SelectorThread[] selectorThreads)
{
this.selectors = selectorThreads;
}
@Override
public SelectorThread nextSelector()
{
return selectors[ThreadLocalRandom.current().nextInt(selectors.length)];
}
}
}