Exemplo n.º 1
0
        public InboundDisruptorImpl(
            ILogger <InboundDisruptorImpl <TDomainEvent> > logger,
            IServiceProvider serviceProvider,
            IOptions <MdaOptions> options,
            IEventStore eventStore)
        {
            _logger = logger;

            var ops      = options.Value;
            var disOps   = ops.DisruptorOptions;
            var settings = ops.ClusterSetting;

            _disruptor = new Disruptor <TDomainEvent>(() => new TDomainEvent(), disOps.InboundRingBufferSize, TaskScheduler.Current);

            var journaler         = new InboundEventJournaler <TDomainEvent>(eventStore);
            var businessProcessor = serviceProvider.GetRequiredService <IInBoundDomainEventHandler <TDomainEvent> >();

            // Only the master node listens directly to input events and runs a replicator.
            if (settings.AppMode.Environment.IsMaster)
            {
                // The replicator broadcasts the input events to the slave nodes.
                // Should the master node go down, it's lack of heartbeat will be noticed, another node becomes master, starts processing input events, and starts its replicator.
                var replicator = new InboundEventReplicator <TDomainEvent>();
                _disruptor.HandleEventsWith(journaler, replicator).Then(businessProcessor);
            }
            else
            {
                _disruptor.HandleEventsWith(journaler).Then(businessProcessor);
            }

            _disruptor.Start();
        }
Exemplo n.º 2
0
 public EventPublisher(int frequency, Disruptor <Event> disruptor)
 {
     _cancel    = new CancellationTokenSource();
     _disruptor = disruptor;
     _frequency = frequency;
     _workProc  = Task.Run(DoWork, _cancel.Token);
 }
        public void Start()
        {
            var performanceLogger = new LoggerConfiguration()
                                    .WriteTo.RollingFile("perf.log")
                                    .CreateLogger();


            var eventHandler = new EventHandler("A");


            metricsEventHandler = new FixedSecondCountReporter <QueueItem>(
                new ISink[] {
                new SerilogSink(performanceLogger, "DemoBuffer"),
                new ColoredConsoleSink()
            },
                1);

            //disruptor = new Disruptor<QueueItem>(() => new QueueItem(), ringbufferSize, TaskScheduler.Default);

            disruptor = new Disruptor <QueueItem>(() => new QueueItem(), new SingleThreadedClaimStrategy(ringbufferSize), new BusySpinWaitStrategy(), TaskScheduler.Default);
            disruptor.HandleExceptionsWith(new FatalExceptionHandler());
            disruptor.HandleEventsWith(eventHandler, metricsEventHandler);


            ringBuffer = disruptor.Start();
            metricsEventHandler.Setup(ringBuffer);
        }
Exemplo n.º 4
0
        public void CreatePipeline()
        {
            _disruptor = new Disruptor <EventExample>(() => new EventExample(), 1024, TaskScheduler.Default, ProducerType.Multi, new BlockingSpinWaitWaitStrategy());

            _disruptor.HandleEventsWithWorkerPool(new MyHandler()).HandleEventsWithWorkerPool(new IWorkHandler <EventExample>[] { new MyHandler2() });
            _disruptor.Start();
        }
Exemplo n.º 5
0
        public static void Main(string[] args)
        {
            var disruptor = new Disruptor <ObjectBox>(() => new ObjectBox(), _ringSize, TaskScheduler.Current, ProducerType.Multi, new BlockingWaitStrategy());

            disruptor.HandleEventsWith(new Consumer()).Then(new Consumer());
            var ringBuffer    = disruptor.RingBuffer;
            var message       = new Message();
            var transportable = new Transportable();
            var streamName    = "com.lmax.wibble";

            Console.WriteLine($"publishing {_ringSize} messages");
            var p = new Publisher();

            for (var i = 0; i < _ringSize; i++)
            {
                ringBuffer.PublishEvent(p, message, transportable, streamName);
                Thread.Sleep(10);
            }
            Console.WriteLine("start disruptor");
            disruptor.Start();
            Console.WriteLine("continue publishing disruptor");
            while (true)
            {
                ringBuffer.PublishEvent(p, message, transportable, streamName);
                Thread.Sleep(10);
            }
        }
        public DisruptorDslScenario()
        {
            _disruptor = new Disruptor <Message>(() => new Message(), new SingleThreadedClaimStrategy(4096), new YieldingWaitStrategy(), new RoundRobinThreadAffinedTaskScheduler(1));
            _handler   = new Handler(_tcs);

            _disruptor.HandleEventsWith(_handler);
        }
Exemplo n.º 7
0
 public DisruptorProducerFactory(int producerCount, int itemsToProduce, TaskScheduler scheduler, Disruptor <ChannelsBenchmark.Event> disruptor)
 {
     _producerCount  = producerCount;
     _itemsToProduce = itemsToProduce;
     _scheduler      = scheduler;
     _disruptor      = disruptor;
 }
Exemplo n.º 8
0
        private static Disruptor <T> CreateDisruptor <T>(IClaimStrategy claim, IWaitStrategy wait) where T : class, new()
        {
            var disruptor = new Disruptor <T>(() => new T(), claim, wait, TaskScheduler.Default);

            disruptor.HandleExceptionsWith(new LogAndTerminateProcessHandler());
            return(disruptor);
        }
        /// <summary>
        /// Initialize Disruptor
        /// </summary>
        public static void InitializeDisruptor(bool enablePersistence, IPersistRepository <object> persistRepository)
        {
            EnablePersistence = enablePersistence;
            try
            {
                if (enablePersistence)
                {
                    if (_disruptor == null)
                    {
                        Journaler journaler = new Journaler(persistRepository);
                        // Initialize Disruptor
                        _disruptor = new Disruptor <byte[]>(() => new byte[10000], _ringSize, TaskScheduler.Default);

                        // Add Consumer
                        _disruptor.HandleEventsWith(journaler);
                        // Start Disruptor
                        _ringBuffer = _disruptor.Start();
                        // Get Publisher
                        _publisher = new EventPublisher <byte[]>(_ringBuffer);
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception, _type.FullName, "InitializeDisruptor");
            }
        }
        public async Task ShouldTestDisruptor()
        {
            var disruptor = new Disruptor <Event>(() => new Event(), 32, TaskScheduler.Default, ProducerType.Single, new BusySpinWaitStrategy());

            var handlerA = new EventHandlerA();
            var handlerB = new EventHandlerB();
            var cleaner  = new CleanerHandler();

            disruptor.HandleEventsWith(handlerA)
            .Then(handlerB)
            .Then(cleaner);

            var before = GC.CollectionCount(0);

            var ringBuffer = disruptor.Start();

            var publisher = new EventPublisher(500, disruptor);

            await Task.Delay(750);

            publisher.Dispose();

            var after = GC.CollectionCount(0);

            Assert.AreEqual(before, after);

            Assert.AreEqual(2, handlerA.HandledEvents.Count);
            Assert.AreEqual(2, handlerB.HandledEvents.Count);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Default Constructor
        /// </summary>
        public StrategyController()
        {
            //_asyncClassLogger = ContextRegistry.GetContext()["OEEClientLogger"] as AsyncClassLogger;
            _asyncClassLogger = new AsyncClassLogger("StrategyController");
            if (_asyncClassLogger != null)
            {
                _asyncClassLogger.SetLoggingLevel();
                //set logging path
                string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) +
                              "\\TradeHub Logs\\Client";
                _asyncClassLogger.LogDirectory(path);
            }

            _strategiesCollection = new ConcurrentDictionary <string, StrategyExecutor>();

            _disruptor = new Disruptor <Execution>(() => new Execution(new Fill(new Security(), "", ""), new Order("")),
                                                   _ringSize, TaskScheduler.Default);
            _disruptor.HandleEventsWith(this);
            _ringBuffer = _disruptor.Start();

            #region Event Aggregator

            EventSystem.Subscribe <LoadStrategy>(LoadUserStrategy);
            EventSystem.Subscribe <InitializeStrategy>(InitializeUserStrategy);
            EventSystem.Subscribe <RunStrategy>(RunUserStrategy);
            EventSystem.Subscribe <StopStrategy>(StopUserStrategy);
            EventSystem.Subscribe <string>(ManageUserCommands);
            #endregion
        }
Exemplo n.º 12
0
        public static void Main(string[] args)
        {
            var executor   = new BasicExecutor(TaskScheduler.Current);
            var disruptor  = new Disruptor <StubEvent>(() => new StubEvent(-1), 1024, TaskScheduler.Current);
            var ringBuffer = disruptor.Start();

            // Construct 2 batch event processors.
            var handler1   = new DynamicHandler();
            var processor1 = new BatchEventProcessor <StubEvent>(ringBuffer, ringBuffer.NewBarrier(), handler1);

            var handler2   = new DynamicHandler();
            var processor2 = new BatchEventProcessor <StubEvent>(ringBuffer, ringBuffer.NewBarrier(processor1.Sequence), handler2);

            // Dynamically add both sequences to the ring buffer
            ringBuffer.AddGatingSequences(processor1.Sequence, processor2.Sequence);

            // Start the new batch processors.
            executor.Execute(processor1.Run);
            executor.Execute(processor2.Run);

            // Remove a processor.

            // Stop the processor
            processor2.Halt();
            // Wait for shutdown the complete
            handler2.WaitShutdown();
            // Remove the gating sequence from the ring buffer
            ringBuffer.RemoveGatingSequence(processor2.Sequence);
        }
Exemplo n.º 13
0
    public static void Main(string[] args)
    {
        var disruptor  = new Disruptor <DynamicEvent>(() => new DynamicEvent(), 1024, TaskScheduler.Current);
        var ringBuffer = disruptor.Start();

        // Construct 2 batch event processors.
        var handler1   = new DynamicHandler();
        var processor1 = EventProcessorFactory.Create(ringBuffer, ringBuffer.NewBarrier(), handler1);

        var handler2   = new DynamicHandler();
        var processor2 = EventProcessorFactory.Create(ringBuffer, ringBuffer.NewBarrier(processor1.Sequence), handler2);

        // Dynamically add both sequences to the ring buffer
        ringBuffer.AddGatingSequences(processor1.Sequence, processor2.Sequence);

        // Start the new batch processors.
        var t1 = processor1.Start();
        var t2 = processor2.Start();

        Thread.Sleep(5000);

        // Remove a processor.

        // Stop the processor
        processor2.Halt();

        // Wait for shutdown the complete
        handler2.WaitShutdown(); // Or simply t2.Wait()

        // Remove the gating sequence from the ring buffer
        ringBuffer.RemoveGatingSequence(processor2.Sequence);
    }
Exemplo n.º 14
0
        public static async Task Main(string[] args)
        {
            var disruptor = new Disruptor <SampleEvent>(() => new SampleEvent(), 1024, TaskScheduler.Default);

            disruptor.HandleEventsWith(new SampleEventHandler())
            .Then(new AwaitableEventContinuationInvoker <SampleEvent>());

            disruptor.Start();

            var s = disruptor.RingBuffer.Next();

            disruptor.RingBuffer[s].Id = 42;
            await disruptor.RingBuffer.PublishAsync(s);

            var allocatedBytesBefore = GC.GetTotalAllocatedBytes(true);

            for (int i = 0; i < 1_000_000; i++)
            {
                var sequence = disruptor.RingBuffer.Next();
                disruptor.RingBuffer[sequence].Id = 42;
                await disruptor.RingBuffer.PublishAsync(sequence);
            }

            var allocatedBytesAfter = GC.GetTotalAllocatedBytes(true);

            Console.WriteLine(allocatedBytesAfter - allocatedBytesBefore);
            Console.WriteLine("A");

            await Task.Yield();

            disruptor.Shutdown();

            Console.WriteLine("B");
            Console.ReadLine();
        }
Exemplo n.º 15
0
 public DisruptorQueueNoDelegate()
 {
     _disruptor = new Disruptor <Event>(() => new Event(), 32768, TaskScheduler.Default, ProducerType.Single, new BusySpinWaitStrategy());
     _handlers  = new EntryHandler();
     _disruptor.HandleEventsWith(_handlers);
     _disruptor.Start();
 }
Exemplo n.º 16
0
        public long Start()
        {
            OzzyLogger <IDomainModelTracing> .Log.TraceInformationalEvent("Start event loop");

            if (Interlocked.CompareExchange(ref _stage, 1, 0) == 0)
            {
                var minCheckpoint = 0L;
                var checkpoints   = _eventProcessors.Select(e => e.GetCheckpoint()).ToList();
                if (checkpoints.Any())
                {
                    minCheckpoint = checkpoints.Min();
                }

                _disruptor = new Disruptor <DomainEventEntry>(() => new DomainEventEntry(),
                                                              new MultiThreadedClaimStrategy(_bufferSize),
                                                              _waitStrategy ?? new BlockingWaitStrategy(),
                                                              TaskScheduler.Default,
                                                              minCheckpoint);
                _disruptor.HandleExceptionsWith(_exceptionHandler);
                foreach (var domainEventsProcessor in _eventProcessors)
                {
                    _disruptor.HandleEventsWith(domainEventsProcessor.GetCheckpoint(), domainEventsProcessor);
                }
                _ringBuffer = _disruptor.Start();
                _barrier    = _ringBuffer.NewBarrier();
                StartCursorSupervisor(minCheckpoint, TimeSpan.FromMilliseconds(_pollTimeout));
                Interlocked.Exchange(ref _stage, 2);
                return(minCheckpoint);
            }
            else
            {
                throw new InvalidOperationException("Domain Manager already started");
            }
        }
Exemplo n.º 17
0
        public void ShouldBatch(ProducerType producerType)
        {
            var d = new Disruptor <LongEvent>(() => new LongEvent(), 2048, TaskScheduler.Current, producerType, new SleepingWaitStrategy());

            var handler1 = new ParallelEventHandler(1, 0);
            var handler2 = new ParallelEventHandler(1, 1);

            d.HandleEventsWith(handler1, handler2);

            var buffer = d.Start();

            IEventTranslator <LongEvent> translator = new EventTranslator <LongEvent>();

            const int eventCount = 10000;

            for (var i = 0; i < eventCount; i++)
            {
                buffer.PublishEvent(translator);
            }

            while (Volatile.Read(ref handler1.Processed) != eventCount - 1 ||
                   Volatile.Read(ref handler2.Processed) != eventCount - 1)
            {
                Thread.Sleep(1);
            }

            Assert.That(handler1.PublishedValue, Is.EqualTo((long)eventCount - 2));
            Assert.That(handler1.EventCount, Is.EqualTo((long)eventCount / 2));
            Assert.That(handler2.PublishedValue, Is.EqualTo((long)eventCount - 1));
            Assert.That(handler2.EventCount, Is.EqualTo((long)eventCount / 2));
        }
Exemplo n.º 18
0
 public DisruptorTest()
 {
     _delayedEventHandlers = new List <DelayedEventHandler>();
     _testWorkHandlers     = new List <TestWorkHandler>();
     _executor             = new StubExecutor();
     _disruptor            = new Disruptor <TestEvent>(() => new TestEvent(), 4, _executor);
 }
Exemplo n.º 19
0
        //wire up the disruptor ring buffers and handlers
        public DisruptorEngine(M model, int batchSize, IJournalWriter journalWriter)
        {
            _journalWriter = journalWriter;
            var kernel = new Kernel(model);

            _transactionHandler = new Disruptor <Request>(
                () => new Request(),
                new MultiThreadedClaimStrategy(1024 * 64),
                new YieldingWaitStrategy(),
                TaskScheduler.Default);

            _commandJournaler = new Disruptor <Request>(
                () => new Request(),
                new MultiThreadedClaimStrategy(1024 * 64),
                new YieldingWaitStrategy(),
                TaskScheduler.Default);

            _transactionHandler.HandleEventsWith(new SerializedTransactionHandler(kernel));

            _commandJournaler.HandleEventsWith(new Journaler(_journalWriter, batchSize))
            .Then(new CommandDispatcher(_transactionHandler));

            _transactionHandler.Start();
            _commandJournaler.Start();
        }
Exemplo n.º 20
0
        public static void InitializeDisruptor(IEventHandler <byte[]>[] eventHandler)
        {
            if (_disruptor == null)
            {
                // Initialize Disruptor
                _disruptor = new Disruptor <byte[]>(() => new byte[Constants.OUTPUT_DISRUPTOR_BYTE_ARRAY_SIZE], _ringSize, TaskScheduler.Default);

                // Intialize Consumer Queue
                _disruptorConsumerQueue      = new ConcurrentQueue <Object>();
                _disruptorConsumerCollection = new BlockingCollection <Object>(_disruptorConsumerQueue);

                // Initialize Consumer Token
                _disruptorConsumerCancellationToken = new CancellationTokenSource();

                // Consumes order notification messages from local collection
                _disruptorConsumerTask = Task.Factory.StartNew(ConsumeIncomingObjects, _disruptorConsumerCancellationToken.Token);
            }
            else
            {
                // Shutdown disruptor if it was already running
                _disruptor.Shutdown();
            }

            // Add Consumer
            _disruptor.HandleEventsWith(eventHandler);

            // Start Disruptor
            _ringBuffer = _disruptor.Start();

            // Get Publisher
            _publisher = new EventPublisher <byte[]>(_ringBuffer);
        }
Exemplo n.º 21
0
 public void SetUp()
 {
     _ringBuffer           = null;
     _delayedEventHandlers = new List <DelayedEventHandler>();
     _testWorkHandlers     = new List <TestWorkHandler>();
     _executor             = new StubExecutor();
     _disruptor            = new Disruptor <TestEvent>(() => new TestEvent(), 4, _executor);
 }
Exemplo n.º 22
0
        public void Start()
        {
            messageDisrupter = new Disruptor <ServerToClientMessage>(() => new ServerToClientMessage(), ringbufferSize, TaskScheduler.Default, ProducerType.Single, new SleepingWaitStrategy());
            messageDisrupter.HandleEventsWith(publisher);
            messageRingBuffer = messageDisrupter.Start();

            logger.Info("Outgoing queue message queue started.");
        }
Exemplo n.º 23
0
 public void SetUp()
 {
     random       = new Random(Guid.NewGuid().GetHashCode());
     eventHandler = new FailingEventHandler();
     disruptor    = new Disruptor <byte[]>(() => new byte[256], 1024, TaskScheduler.Default, ProducerType.SINGLE, new BlockingWaitStrategy());
     disruptor.HandleEventsWith(eventHandler);
     disruptor.HandleExceptionsWith(new FatalExceptionHandler());
 }
Exemplo n.º 24
0
 /// <summary>
 /// Shuts down the Disruptor
 /// </summary>
 public static void Shutdown()
 {
     if (_disruptor != null)
     {
         _disruptor.Shutdown();
         _disruptor = null;
     }
 }
Exemplo n.º 25
0
        public void Start()
        {
            var eventHandler = new EventHandler();

            disruptor = new Disruptor <QueueItem>(() => new QueueItem(), (int)Math.Pow(64, 2), TaskScheduler.Default);
            disruptor.HandleEventsWith(eventHandler);
            ringBuffer = disruptor.Start();
        }
Exemplo n.º 26
0
        private Disruptor <Event> CreateDisruptor(ProducerType producerType, int ringBufferSize, TaskScheduler scheduler)
        {
            var disruptor = new Disruptor <Event>(() => new Event(), ringBufferSize, scheduler, producerType, new BusySpinWaitStrategy());

            disruptor.HandleEventsWith(_eventProcessor);
            disruptor.Start();
            return(disruptor);
        }
Exemplo n.º 27
0
    public static void Run()
    {
        var disruptor = new Disruptor <SampleEvent>(() => new SampleEvent(), 1024);
        var handler1  = new H1(new SampleEventPersister());
        var handler2  = new H2();

        disruptor.HandleEventsWith(handler1).Then(handler2);
    }
        private Disruptor <DisruptorTransportEvent> CreateAndStartDisruptor()
        {
            var newDisruptor = new Disruptor <DisruptorTransportEvent>(() => new DisruptorTransportEvent(), 1024);

            newDisruptor.HandleEventsWith(new DisruptorTransportEventHandler());
            newDisruptor.Start();

            return(newDisruptor);
        }
Exemplo n.º 29
0
 /// <summary>
 /// ShutDown
 /// </summary>
 public static void ShutDown()
 {
     if (_disruptor != null)
     {
         // Shutdown disruptor if it was already running
         _disruptor.Shutdown();
         _disruptor = null;
     }
 }
Exemplo n.º 30
0
        public MultipleConsumersProcessor(MultipleConsumersProcessorOptions options)
        {
            _disruptor = new Disruptor <Event>(() => new Event(), options.BufferLength, TaskScheduler.Default);

            var logHandler = new LogEventHandler();
            var zipHandler = new ZipEventHandler();

            _disruptor.HandleEventsWith(logHandler, zipHandler);
        }
 public UniCast1P1CBatchDisruptorPerfTest()
     : base(100 * Million)
 {
     _disruptor = new Disruptor<ValueEvent>(()=>new ValueEvent(),
                                            new SingleThreadedClaimStrategy(BufferSize),
                                            new YieldingWaitStrategy());
     _mru = new ManualResetEvent(false);
     _eventHandler = new ValueAdditionEventHandler(Iterations, _mru);
     _disruptor.HandleEventsWith(_eventHandler);
     _ringBuffer = _disruptor.RingBuffer;
 }
        public Pipeline3StepDisruptorPerfTest()
            : base(100 * Million)
        {
            _disruptor = new Disruptor<FunctionEvent>(() => new FunctionEvent(),
                                                      new SingleThreadedClaimStrategy(Size),
                                                      new YieldingWaitStrategy());

            _mru = new ManualResetEvent(false);
            _stepThreeFunctionEventHandler = new FunctionEventHandler(FunctionStep.Three, Iterations, _mru);

            _disruptor.HandleEventsWith(new FunctionEventHandler(FunctionStep.One, Iterations, _mru))
                .Then(new FunctionEventHandler(FunctionStep.Two, Iterations, _mru))
                .Then(_stepThreeFunctionEventHandler);

            _ringBuffer = _disruptor.RingBuffer;
        }
        public DiamondPath1P3CDisruptorPerfTest()
            : base(100 * Million)
        {
            _disruptor = new Disruptor<FizzBuzzEvent>(() => new FizzBuzzEvent(),
                                                      new SingleThreadedClaimStrategy(Size),
                                                      new YieldingWaitStrategy());

            _mru = new ManualResetEvent(false);
            _fizzEventHandler = new FizzBuzzEventHandler(FizzBuzzStep.Fizz, Iterations, _mru);
            _buzzEventHandler = new FizzBuzzEventHandler(FizzBuzzStep.Buzz, Iterations, _mru);
            _fizzBuzzEventHandler = new FizzBuzzEventHandler(FizzBuzzStep.FizzBuzz, Iterations, _mru);

            _disruptor.HandleEventsWith(_fizzEventHandler, _buzzEventHandler)
                      .Then(_fizzBuzzEventHandler);
            _ringBuffer = _disruptor.RingBuffer;
        }
        public Sequencer3P1CDisruptorPerfTest()
            : base(20 * Million)
        {
            _disruptor = new Disruptor<ValueEvent>(()=>new ValueEvent(),
                                                   new MultiThreadedClaimStrategy(Size),
                                                   new YieldingWaitStrategy());
            _mru = new ManualResetEvent(false);
            _eventHandler = new ValueAdditionEventHandler(Iterations * NumProducers, _mru);
            _disruptor.HandleEventsWith(_eventHandler);

            _valueProducers = new ValueProducer[NumProducers];
            _ringBuffer = _disruptor.RingBuffer;

            for (int i = 0; i < NumProducers; i++)
            {
                _valueProducers[i] = new ValueProducer(_testStartBarrier, _ringBuffer, Iterations);
            }
        }
Exemplo n.º 35
0
 public void When_Creating_A_New_Disruptor()
 {
     var disruptor = new Disruptor();
 }