public override Task StartAsync(CancellationToken cancellationToken)
        {
            // Start connection with RabbitMQ and keep alive while the worker is up
            if (!cancellationToken.IsCancellationRequested)
            {
                this.Connection = ConnectionFactory.CreateConnection();
                this.Channel    = Connection.CreateModel();

                MainQueue.DeclareQueue(Channel);
            }

            return(base.StartAsync(cancellationToken));
        }
Beispiel #2
0
        public void SendExampleMessage2()
        {
            var content = new ExampleMessage2()
            {
                Guid = Guid.NewGuid()
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    MainQueue.DeclareQueue(channel);

                    var body = WrappedMessage.Wrap(ExampleMessage2.TypeID, content).ToBytes();
                    channel.BasicPublish(exchange: DefaultExchange.ExchangeName, routingKey: MainQueue.QueueName, basicProperties: null, body: body);
                }
        }
        void ShowMessage()
        {
            var message = MainQueue.Peek();

            MainQueue.Dequeue();
            if (message.MessageType >= Configuration.MinLevel)
            {
                var foregroundGlass = Console.ForegroundColor;
                var backgroundGlass = Console.BackgroundColor;
                var profile         = Dict[message.MessageType];
                Console.ForegroundColor = profile.ForegroundColor;
                Console.BackgroundColor = profile.BackgroundColor;
                Console.WriteLine(message.Message);
                Console.ForegroundColor = foregroundGlass;
                Console.BackgroundColor = backgroundGlass;
            }
        }
Beispiel #4
0
        public SingleVNode(TFChunkDb db, SingleVNodeSettings vNodeSettings, SingleVNodeAppSettings appSettings)
        {
            Ensure.NotNull(db, "db");
            Ensure.NotNull(vNodeSettings, "vNodeSettings");

            db.OpenVerifyAndClean();

            _tcpEndPoint  = vNodeSettings.ExternalTcpEndPoint;
            _httpEndPoint = vNodeSettings.HttpEndPoint;

            _outputBus  = new InMemoryBus("OutputBus");
            _controller = new SingleVNodeController(Bus, _httpEndPoint);
            _mainQueue  = new QueuedHandler(_controller, "MainQueue");
            _controller.SetMainQueue(MainQueue);

            //MONITORING
            var monitoringInnerBus   = new InMemoryBus("MonitoringInnerBus", watchSlowMsg: false);
            var monitoringRequestBus = new InMemoryBus("MonitoringRequestBus", watchSlowMsg: false);
            var monitoringQueue      = new QueuedHandler(monitoringInnerBus, "MonitoringQueue", watchSlowMsg: true, slowMsgThresholdMs: 100);
            var monitoring           = new MonitoringService(monitoringQueue, monitoringRequestBus, db.Config.WriterCheckpoint, appSettings.StatsPeriod);

            Bus.Subscribe(monitoringQueue.WidenFrom <SystemMessage.SystemInit, Message>());
            Bus.Subscribe(monitoringQueue.WidenFrom <SystemMessage.BecomeShuttingDown, Message>());
            monitoringInnerBus.Subscribe <SystemMessage.SystemInit>(monitoring);
            monitoringInnerBus.Subscribe <SystemMessage.BecomeShuttingDown>(monitoring);
            monitoringInnerBus.Subscribe <MonitoringMessage.GetFreshStats>(monitoring);

            //STORAGE SUBSYSTEM
            var indexPath  = Path.Combine(db.Config.Path, "index");
            var tableIndex = new TableIndex(indexPath,
                                            () => new HashListMemTable(),
                                            new InMemoryCheckpoint(),
                                            maxSizeForMemory: 1000000,
                                            maxTablesPerLevel: 2);

            var readIndex = new ReadIndex(_mainQueue,
                                          pos => new TFChunkChaser(db, db.Config.WriterCheckpoint, new InMemoryCheckpoint(pos)),
                                          () => new TFChunkReader(db, db.Config.WriterCheckpoint),
                                          TFConsts.ReadIndexReaderCount,
                                          tableIndex,
                                          new XXHashUnsafe());
            var writer        = new TFChunkWriter(db);
            var storageWriter = new StorageWriter(_mainQueue, _outputBus, writer, readIndex);
            var storageReader = new StorageReader(_mainQueue, _outputBus, readIndex, TFConsts.StorageReaderHandlerCount);

            monitoringRequestBus.Subscribe <MonitoringMessage.InternalStatsRequest>(storageReader);

            var chaser = new TFChunkChaser(db,
                                           db.Config.WriterCheckpoint,
                                           db.Config.GetNamedCheckpoint(Checkpoint.Chaser));
            var storageChaser = new StorageChaser(_mainQueue, chaser);

            _outputBus.Subscribe <SystemMessage.SystemInit>(storageChaser);
            _outputBus.Subscribe <SystemMessage.SystemStart>(storageChaser);
            _outputBus.Subscribe <SystemMessage.BecomeShuttingDown>(storageChaser);

            var storageScavenger = new StorageScavenger(db, readIndex);

            _outputBus.Subscribe <SystemMessage.ScavengeDatabase>(storageScavenger);

            //TCP
            var tcpService = new TcpService(MainQueue, _tcpEndPoint);

            Bus.Subscribe <SystemMessage.SystemInit>(tcpService);
            Bus.Subscribe <SystemMessage.SystemStart>(tcpService);
            Bus.Subscribe <SystemMessage.BecomeShuttingDown>(tcpService);

            //HTTP
            HttpService = new HttpService(MainQueue, vNodeSettings.HttpPrefixes);
            Bus.Subscribe <SystemMessage.SystemInit>(HttpService);
            Bus.Subscribe <SystemMessage.BecomeShuttingDown>(HttpService);
            Bus.Subscribe <HttpMessage.SendOverHttp>(HttpService);
            Bus.Subscribe <HttpMessage.UpdatePendingRequests>(HttpService);
            HttpService.SetupController(new AdminController(MainQueue));
            HttpService.SetupController(new PingController());
            HttpService.SetupController(new StatController(monitoringQueue));
            HttpService.SetupController(new ReadEventDataController(MainQueue));
            HttpService.SetupController(new AtomController(MainQueue));
            HttpService.SetupController(new WebSiteController(MainQueue));

            //REQUEST MANAGEMENT
            var requestManagement = new RequestManagementService(MainQueue, 1, 1);

            Bus.Subscribe <ReplicationMessage.EventCommited>(requestManagement);
            Bus.Subscribe <ReplicationMessage.CreateStreamRequestCreated>(requestManagement);
            Bus.Subscribe <ReplicationMessage.WriteRequestCreated>(requestManagement);
            Bus.Subscribe <ReplicationMessage.TransactionStartRequestCreated>(requestManagement);
            Bus.Subscribe <ReplicationMessage.TransactionWriteRequestCreated>(requestManagement);
            Bus.Subscribe <ReplicationMessage.TransactionCommitRequestCreated>(requestManagement);
            Bus.Subscribe <ReplicationMessage.DeleteStreamRequestCreated>(requestManagement);
            Bus.Subscribe <ReplicationMessage.RequestCompleted>(requestManagement);
            Bus.Subscribe <ReplicationMessage.CommitAck>(requestManagement);
            Bus.Subscribe <ReplicationMessage.PrepareAck>(requestManagement);
            Bus.Subscribe <ReplicationMessage.WrongExpectedVersion>(requestManagement);
            Bus.Subscribe <ReplicationMessage.InvalidTransaction>(requestManagement);
            Bus.Subscribe <ReplicationMessage.StreamDeleted>(requestManagement);
            Bus.Subscribe <ReplicationMessage.PreparePhaseTimeout>(requestManagement);
            Bus.Subscribe <ReplicationMessage.CommitPhaseTimeout>(requestManagement);

            var clientService = new ClientService();

            Bus.Subscribe <TcpMessage.ConnectionClosed>(clientService);
            Bus.Subscribe <ClientMessage.SubscribeToStream>(clientService);
            Bus.Subscribe <ClientMessage.UnsubscribeFromStream>(clientService);
            Bus.Subscribe <ClientMessage.SubscribeToAllStreams>(clientService);
            Bus.Subscribe <ClientMessage.UnsubscribeFromAllStreams>(clientService);
            Bus.Subscribe <ReplicationMessage.EventCommited>(clientService);

            //TIMER
            //var timer = new TimerService(new TimerBasedScheduler(new RealTimer(), new RealTimeProvider()));
            TimerService = new TimerService(new ThreadBasedScheduler(new RealTimeProvider()));
            Bus.Subscribe <TimerMessage.Schedule>(TimerService);

            MainQueue.Start();
            monitoringQueue.Start();
        }
Beispiel #5
0
 public void Stop()
 {
     MainQueue.Publish(new SystemMessage.BecomeShuttingDown());
 }
Beispiel #6
0
 public void Start()
 {
     MainQueue.Publish(new SystemMessage.SystemInit());
 }
Beispiel #7
0
        public PlaygroundVNode(PlaygroundVNodeSettings vNodeSettings)
        {
            _tcpEndPoint  = vNodeSettings.ExternalTcpEndPoint;
            _httpEndPoint = vNodeSettings.ExternalHttpEndPoint;

            _mainBus    = new InMemoryBus("MainBus");
            _controller = new PlaygroundVNodeController(Bus, _httpEndPoint);
            _mainQueue  = new QueuedHandler(_controller, "MainQueue");
            _controller.SetMainQueue(MainQueue);

            // MONITORING
            var monitoringInnerBus = new InMemoryBus("MonitoringInnerBus", watchSlowMsg: false);
            var monitoringQueue    = new QueuedHandler(
                monitoringInnerBus, "MonitoringQueue", true, TimeSpan.FromMilliseconds(100));

            Bus.Subscribe(monitoringQueue.WidenFrom <SystemMessage.SystemInit, Message>());
            Bus.Subscribe(monitoringQueue.WidenFrom <SystemMessage.StateChangeMessage, Message>());
            Bus.Subscribe(monitoringQueue.WidenFrom <SystemMessage.BecomeShuttingDown, Message>());


            // MISC WORKERS
            _workerBuses = Enumerable.Range(0, vNodeSettings.WorkerThreads).Select(queueNum =>
                                                                                   new InMemoryBus(string.Format("Worker #{0} Bus", queueNum + 1),
                                                                                                   watchSlowMsg: true,
                                                                                                   slowMsgThreshold: TimeSpan.FromMilliseconds(50))).ToArray();
            _workersHandler = new MultiQueuedHandler(
                vNodeSettings.WorkerThreads,
                queueNum => new QueuedHandlerThreadPool(_workerBuses[queueNum],
                                                        string.Format("Worker #{0}", queueNum + 1),
                                                        groupName: "Workers",
                                                        watchSlowMsg: true,
                                                        slowMsgThreshold: TimeSpan.FromMilliseconds(50)));

            // AUTHENTICATION INFRASTRUCTURE
            var dispatcher                       = new IODispatcher(_mainBus, new PublishEnvelope(_workersHandler, crossThread: true));
            var passwordHashAlgorithm            = new Rfc2898PasswordHashAlgorithm();
            var internalAuthenticationProvider   = new InternalAuthenticationProvider(dispatcher, passwordHashAlgorithm, 1000);
            var passwordChangeNotificationReader = new PasswordChangeNotificationReader(_mainQueue, dispatcher);

            _mainBus.Subscribe <SystemMessage.SystemStart>(passwordChangeNotificationReader);
            _mainBus.Subscribe <SystemMessage.BecomeShutdown>(passwordChangeNotificationReader);
            _mainBus.Subscribe(internalAuthenticationProvider);
            _mainBus.Subscribe(dispatcher);

            SubscribeWorkers(bus =>
            {
                bus.Subscribe(dispatcher.ForwardReader);
                bus.Subscribe(dispatcher.BackwardReader);
                bus.Subscribe(dispatcher.Writer);
                bus.Subscribe(dispatcher.StreamDeleter);
            });

            // TCP
            var tcpService = new TcpService(
                MainQueue, _tcpEndPoint, _workersHandler, TcpServiceType.External, TcpSecurityType.Normal, new ClientTcpDispatcher(),
                ESConsts.ExternalHeartbeatInterval, ESConsts.ExternalHeartbeatTimeout, internalAuthenticationProvider, null);

            Bus.Subscribe <SystemMessage.SystemInit>(tcpService);
            Bus.Subscribe <SystemMessage.SystemStart>(tcpService);
            Bus.Subscribe <SystemMessage.BecomeShuttingDown>(tcpService);

            // HTTP
            {
                var authenticationProviders = new AuthenticationProvider[]
                {
                    new BasicHttpAuthenticationProvider(internalAuthenticationProvider),
                    new TrustedAuthenticationProvider(),
                    new AnonymousAuthenticationProvider()
                };

                var httpPipe        = new HttpMessagePipe();
                var httpSendService = new HttpSendService(httpPipe, forwardRequests: false);
                _mainBus.Subscribe <SystemMessage.StateChangeMessage>(httpSendService);
                _mainBus.Subscribe(new WideningHandler <HttpMessage.SendOverHttp, Message>(_workersHandler));
                SubscribeWorkers(bus =>
                {
                    bus.Subscribe <HttpMessage.HttpSend>(httpSendService);
                    bus.Subscribe <HttpMessage.HttpSendPart>(httpSendService);
                    bus.Subscribe <HttpMessage.HttpBeginSend>(httpSendService);
                    bus.Subscribe <HttpMessage.HttpEndSend>(httpSendService);
                    bus.Subscribe <HttpMessage.SendOverHttp>(httpSendService);
                });

                _httpService = new HttpService(ServiceAccessibility.Private, _mainQueue, new TrieUriRouter(),
                                               _workersHandler, vNodeSettings.HttpPrefixes);

                _mainBus.Subscribe <SystemMessage.SystemInit>(_httpService);
                _mainBus.Subscribe <SystemMessage.BecomeShuttingDown>(_httpService);
                _mainBus.Subscribe <HttpMessage.PurgeTimedOutRequests>(_httpService);
                HttpService.SetupController(new AdminController(_mainQueue));
                HttpService.SetupController(new PingController());
                HttpService.SetupController(new StatController(monitoringQueue, _workersHandler));
                HttpService.SetupController(new AtomController(httpSendService, _mainQueue, _workersHandler));
                HttpService.SetupController(new GuidController(_mainQueue));
                HttpService.SetupController(new UsersController(httpSendService, _mainQueue, _workersHandler));

                SubscribeWorkers(bus => HttpService.CreateAndSubscribePipeline(bus, authenticationProviders));
            }


            // TIMER
            _timerService = new TimerService(new ThreadBasedScheduler(new RealTimeProvider()));
            Bus.Subscribe(TimerService);

            monitoringQueue.Start();
            MainQueue.Start();
        }
Beispiel #8
0
 public void Stop(bool exitProcess)
 {
     MainQueue.Publish(new ClientMessage.RequestShutdown(exitProcess));
 }
 public void Add(MessageEvent message)
 {
     message.Message = StringProcessor.Expand(Configuration.Pattern, message.Message, message.MessageType);
     MainQueue.Enqueue(message);
     ShowMessage();
 }