示例#1
0
        public async Task ShouldRegisterAndUnregisterPeer()
        {
            var subscription1 = Subscription.Matching <DatabaseStatus>(status => status.DatacenterName == "Paris" && status.Status == "Ko");
            var subscription2 = Subscription.Any <DoSomething>();

            var bytes = _container.GetInstance <IMessageSerializer>().Serialize(subscription1);
            var json  = Encoding.UTF8.GetString(bytes);

            var obj = _container.GetInstance <IMessageSerializer>().Deserialize(bytes, typeof(Subscription));

            var command = new DoSomething();

            Assert.AreEqual(0, GlobalTestContext.Get());

            _bus.Start();

            await _bus.Subscribe(new SubscriptionRequest(subscription1));

            var peers = _directoryRepository.GetPeers(true);

            Assert.AreEqual(1, peers.Count());
            Assert.AreEqual(5, peers.First().Subscriptions.Count());

            await _bus.Subscribe(new SubscriptionRequest(subscription2));

            peers = _directoryRepository.GetPeers(true);
            Assert.AreEqual(1, peers.Count());
            Assert.AreEqual(6, peers.First().Subscriptions.Count());

            await _bus.Send(command);

            await Task.Delay(50);

            Assert.AreEqual(1, GlobalTestContext.Get());
        }
示例#2
0
 public IList <Peer> GetPeersHandlingMessage(MessageBinding messageBinding)
 {
     return(_peerRepository.GetPeers(!_configuration.DisableDynamicSubscriptionsForDirectoryOutgoingMessages)
            .Where(peer => peer.Subscriptions != null && peer.Subscriptions.Any(x => x.MessageTypeId == messageBinding.MessageTypeId && x.Matches(messageBinding.RoutingKey)))
            .Select(peerDesc => peerDesc.Peer)
            .ToList());
 }
示例#3
0
 public IList <Peer> GetPeersHandlingMessage(MessageBinding messageBinding)
 {
     return(_peerRepository.GetPeers()
            .Where(peer => peer.Subscriptions != null && peer.Subscriptions.Any(x => x.MessageTypeId == messageBinding.MessageTypeId && x.Matches(messageBinding.RoutingKey)))
            .Select(peerDesc => peerDesc.Peer)
            .ToList());
 }
        public void Handle(RegisterPeerCommand message)
        {
            if (_blacklistedMachines.Contains(Context !.Originator.SenderMachineName !))
            {
                throw new InvalidOperationException($"Peer {Context.SenderId} on host {Context.Originator.SenderMachineName} is not allowed to register on this directory");
            }

            var peerTimestampUtc = message.Peer.TimestampUtc;

            if (!peerTimestampUtc.HasValue)
            {
                throw new InvalidOperationException("The TimestampUtc must be provided when registering");
            }

            var utcNow = SystemDateTime.UtcNow;

            if (_configuration.MaxAllowedClockDifferenceWhenRegistering != null && peerTimestampUtc.Value > utcNow + _configuration.MaxAllowedClockDifferenceWhenRegistering)
            {
                throw new InvalidOperationException($"The client provided timestamp [{peerTimestampUtc}] is too far ahead of the the server's current time [{utcNow}]");
            }

            var stopwatch      = Stopwatch.StartNew();
            var peerDescriptor = message.Peer;

            peerDescriptor.Peer.IsUp         = true;
            peerDescriptor.Peer.IsResponding = true;

            var existingPeer = _peerRepository.Get(peerDescriptor.PeerId);

            if (IsPeerInConflict(existingPeer, peerDescriptor))
            {
                throw new DomainException(DirectoryErrorCodes.PeerAlreadyExists, string.Format("Peer {0} already exists (running on {1})", peerDescriptor.PeerId, existingPeer.Peer.EndPoint));
            }

            _peerRepository.RemoveAllDynamicSubscriptionsForPeer(peerDescriptor.PeerId, DateTime.SpecifyKind(peerDescriptor.TimestampUtc !.Value, DateTimeKind.Utc));
            _peerRepository.AddOrUpdatePeer(peerDescriptor);
            _bus.Publish(new PeerStarted(peerDescriptor));

            var registredPeerDescriptors = _peerRepository.GetPeers(loadDynamicSubscriptions: true);

            _bus.Reply(new RegisterPeerResponse(registredPeerDescriptors.ToArray()));
            _speedReporter.ReportRegistrationDuration(stopwatch.Elapsed);
        }
示例#5
0
        internal void DetectDeadPeers()
        {
            var timestampUtc = SystemDateTime.UtcNow;
            var entries      = _peerRepository.GetPeers(loadDynamicSubscriptions: false)
                               .Where(peer => peer.Subscriptions.All(sub => sub.MessageTypeId != MessageUtil.TypeId <RegisterPeerCommand>()))
                               .Select(ToPeerEntry)
                               .ToList();

            var shouldSendPing = ShouldSendPing(timestampUtc);

            if (shouldSendPing)
            {
                _lastPingTimeUtc = timestampUtc;
            }

            foreach (var entry in entries.Where(x => x.IsUp))
            {
                entry.Process(timestampUtc, shouldSendPing);
            }

            var decommissionedPeerIds = _peers.Keys.Except(entries.Select(x => x.Descriptor.PeerId)).ToList();

            _peers.RemoveRange(decommissionedPeerIds);
        }