private DeadPeerDetectorEntry ToPeerEntry(PeerDescriptor descriptor) { var peer = _peers.GetValueOrAdd(descriptor.PeerId, () => CreateEntry(descriptor)); peer.Descriptor = descriptor; return peer; }
public DeadPeerDetectorEntry(PeerDescriptor descriptor, IDirectoryConfiguration configuration, IBus bus, TaskScheduler taskScheduler) { Descriptor = descriptor; _configuration = configuration; _bus = bus; _taskScheduler = taskScheduler; }
private bool IsPeerInConflict(PeerDescriptor existingPeer, PeerDescriptor peerToAdd) { return(existingPeer != null && existingPeer.Peer.IsResponding && existingPeer.PeerId == peerToAdd.PeerId && existingPeer.Peer.GetMachineNameFromEndPoint() != peerToAdd.Peer.GetMachineNameFromEndPoint()); }
public async Task OpenAsync(string host, int port) { try { await Task.Run(() => { this.channel = new Channel($"{host}:{port}", ChannelCredentials.Insecure); this.adaptorImpl = new AdaptorClientImpl(this.channel, Guid.NewGuid(), this.serviceHosts.ToArray()); }); await this.adaptorImpl.OpenAsync(); this.descriptor = await this.instanceContext.CreateInstanceAsync(this.adaptorImpl); await Task.Run(() => { this.cancellation = new CancellationTokenSource(); this.serializer = this.serviceContext.GetService(typeof(ISerializer)) as ISerializer; }); this.task = this.PollAsync(this.cancellation.Token); } catch { if (this.channel != null) { await this.channel.ShutdownAsync(); this.channel = null; } throw; } }
private DeadPeerDetectorEntry ToPeerEntry(PeerDescriptor descriptor) { var peer = _peers.GetValueOrAdd(descriptor.PeerId, () => CreateEntry(descriptor)); peer.Descriptor = descriptor; return(peer); }
public void ShouldRetrieveClients() { var logger = new MockLogger(); var directoryClient = new PeerDirectoryClient(logger); var messageParisKo = new DatabaseStatus() { DatacenterName = "Paris", Status = "Ko" }; var messageParisOk = new DatabaseStatus() { DatacenterName = "Paris", Status = "Ok" }; var messageLondonKo = new DatabaseStatus() { DatacenterName = "London", Status = "Ko" }; var messageLondonOk = new DatabaseStatus() { DatacenterName = "London", Status = "Ok" }; var peer1 = new Peer(new PeerId("Abc.Testing." + Guid.NewGuid()), "tcp://localhost:8080"); var peer2 = new Peer(new PeerId("Abc.Testing." + Guid.NewGuid()), "tcp://localhost:8181"); var allParisKo = Subscription.Matching <DatabaseStatus>(x => x.DatacenterName == "Paris" && x.Status == "Ko"); var allParis = Subscription.Matching <DatabaseStatus>(x => x.DatacenterName == "Paris"); var allKo = Subscription.Matching <DatabaseStatus>(x => x.Status == "Ko"); var peer1Descriptor = new PeerDescriptor(peer1.Id, peer1.EndPoint, false, true, DateTime.Now, allParisKo, allParis); var peer2Descriptor = new PeerDescriptor(peer2.Id, peer2.EndPoint, false, true, DateTime.Now, allKo); directoryClient.Handle(new PeerActivated(peer1Descriptor)); directoryClient.Handle(new PeerActivated(peer2Descriptor)); var peers = directoryClient.GetPeersHandlingMessage(messageParisKo); Assert.AreEqual(2, peers.Count()); peers = directoryClient.GetPeersHandlingMessage(messageParisOk); Assert.AreEqual(1, peers.Count()); peers = directoryClient.GetPeersHandlingMessage(messageLondonKo); Assert.AreEqual(1, peers.Count()); peers = directoryClient.GetPeersHandlingMessage(messageLondonOk); Assert.AreEqual(0, peers.Count()); }
private DeadPeerDetectorEntry CreateEntry(PeerDescriptor descriptor) { var entry = new DeadPeerDetectorEntry(descriptor, _configuration, _bus, TaskScheduler); entry.PeerTimeoutDetected += OnPeerTimeout; entry.PeerRespondingDetected += OnPeerResponding; entry.PingMissed += (detectorEntry, time) => PingMissed(detectorEntry.Descriptor.PeerId, time); return entry; }
public void should_return_a_storage_peer_with_its_timestamp_kind_set_to_utc() { var unspecifiedKindUtcNow = new DateTime(DateTime.UtcNow.Ticks, DateTimeKind.Unspecified); var peerDescriptor = new PeerDescriptor(new PeerId("Abc.Titi.0"), "tcp://toto:123", false, true, true, unspecifiedKindUtcNow); var storagePeer = peerDescriptor.ToStoragePeer(); storagePeer.TimestampUtc.Kind.ShouldEqual(DateTimeKind.Utc); }
public void AddOrUpdatePeer(PeerDescriptor peerDescriptor) { var storagePeer = peerDescriptor.ToStoragePeer(); _dataContext.StoragePeers .Insert(storagePeer) .SetConsistencyLevel(ConsistencyLevel.LocalQuorum) .SetTimestamp(storagePeer.TimestampUtc) .Execute(); }
private DeadPeerDetectorEntry CreateEntry(PeerDescriptor descriptor) { var entry = new DeadPeerDetectorEntry(descriptor, _configuration, _bus, TaskScheduler); entry.PeerTimeoutDetected += OnPeerTimeout; entry.PeerRespondingDetected += OnPeerResponding; entry.PingTimeout += (detectorEntry, time) => PingTimeout?.Invoke(detectorEntry.Descriptor.PeerId, time); return(entry); }
public void should_return_the_corresponding_peer() { var descriptor = new PeerDescriptor(new PeerId("theID"), "tcp://endpoint:123", true, true, true, SystemDateTime.UtcNow, new[] { new Subscription(new MessageTypeId(typeof(string))) }); var peer = descriptor.Peer; peer.Id.ShouldEqual(descriptor.Peer.Id); peer.EndPoint.ShouldEqual(descriptor.Peer.EndPoint); peer.IsUp.ShouldEqual(descriptor.Peer.IsUp); }
private void StopPeer(UnregisterPeerCommand message, PeerDescriptor peer) { var peerId = message.PeerId; var endPoint = message.PeerEndPoint ?? peer.Peer.EndPoint; var timestampUtc = message.TimestampUtc ?? SystemDateTime.UtcNow; if (_peerRepository.SetPeerDown(peerId, timestampUtc)) { _bus.Publish(new PeerStopped(peerId, endPoint, timestampUtc)); } }
public void should_report_subscription_update_speed() { var originalPeerDescriptor = TestDataBuilder.CreatePersistentPeerDescriptor("tcp://abctest:123", typeof(FakeCommand)); _repositoryMock.Setup(x => x.Get(originalPeerDescriptor.Peer.Id)).Returns(originalPeerDescriptor); PeerDescriptor updatedPeerDescriptor = null; _repositoryMock.Setup(x => x.AddOrUpdatePeer(It.IsAny <PeerDescriptor>())).Callback <PeerDescriptor>(peer => updatedPeerDescriptor = peer); var newSubscriptions = new[] { new Subscription(new MessageTypeId("Another.Handled.Type")) }; _handler.Handle(new UpdatePeerSubscriptionsCommand(originalPeerDescriptor.Peer.Id, newSubscriptions, DateTime.UtcNow)); _speedReporter.Verify(x => x.ReportSubscriptionUpdateDuration(It.Is <TimeSpan>(t => t < 1.Second()))); }
public void should_not_erase_the_dynamic_subscriptions_of_a_peer_on_update() { var peerDescriptor = _peer1.ToPeerDescriptorWithRoundedTime(true, typeof(FakeCommand)); _repository.AddDynamicSubscriptionsForTypes(peerDescriptor.PeerId, peerDescriptor.TimestampUtc.Value, new[] { CreateSubscriptionsForType <int>() }); _repository.AddOrUpdatePeer(peerDescriptor); var expectedPeerDescriptor = new PeerDescriptor(peerDescriptor); expectedPeerDescriptor.Subscriptions = new[] { CreateSubscriptionFor <FakeCommand>(), CreateSubscriptionFor <int>() }; var peerFetched = _repository.Get(peerDescriptor.Peer.Id); peerFetched.ShouldHaveSamePropertiesAs(expectedPeerDescriptor); }
public static StoragePeer ToStoragePeer(this PeerDescriptor peerDescriptor) { var timestamp = peerDescriptor.TimestampUtc.HasValue ? new DateTime(peerDescriptor.TimestampUtc.Value.Ticks, DateTimeKind.Utc) : DateTime.UtcNow; return(new StoragePeer { PeerId = peerDescriptor.PeerId.ToString(), EndPoint = peerDescriptor.Peer.EndPoint, HasDebuggerAttached = peerDescriptor.HasDebuggerAttached, IsPersistent = peerDescriptor.IsPersistent, IsUp = peerDescriptor.Peer.IsUp, IsResponding = peerDescriptor.Peer.IsResponding, TimestampUtc = timestamp, StaticSubscriptionsBytes = SerializeSubscriptions(peerDescriptor.Subscriptions) }); }
public void Setup() { _transientAlivePeer0 = CreatePeerDescriptor("Abc.TransientAlive.0", isUp: true, isPersistent: false, hasDebuggerAttached: false); _transientAlivePeer1 = CreatePeerDescriptor("Abc.TransientAlive.1", isUp: true, isPersistent: false, hasDebuggerAttached: false); _transientDeadPeer = CreatePeerDescriptor("Abc.TransientDead.0", isUp: false, isPersistent: false, hasDebuggerAttached: false); _persistentAlivePeer = CreatePeerDescriptor("Abc.PersistentAlive.0", isUp: true, isPersistent: true, hasDebuggerAttached: false); _persistentDeadPeer = CreatePeerDescriptor("Abc.PersistentDead.0", isUp: false, isPersistent: true, hasDebuggerAttached: false); _debugPersistentAlivePeer = CreatePeerDescriptor("Abc.DebugPersistentAlive.0", isUp: true, isPersistent: true, hasDebuggerAttached: true); _debugTransientAlivePeer = CreatePeerDescriptor("Abc.DebugTransientAlive.0", isUp: true, isPersistent: false, hasDebuggerAttached: true); _persistencePeer = CreatePeerDescriptor("Abc.Zebus.PersistenceService.0", isUp: true, isPersistent: false, hasDebuggerAttached: false); _directoryPeer = CreatePeerDescriptor("NonStandardDirectoryName", isUp: true, isPersistent: false, hasDebuggerAttached: false); _directoryPeer.Subscriptions = new[] { new Subscription(new MessageTypeId(typeof(RegisterPeerCommand))) }; _peersNotToDecommission = new string[0]; _peerRepositoryMock = new Mock <IPeerRepository>(); var peerDescriptors = new List <PeerDescriptor> { _transientAlivePeer0, _transientAlivePeer1, _transientDeadPeer, _persistentAlivePeer, _persistentDeadPeer, _debugPersistentAlivePeer, _debugTransientAlivePeer, _persistencePeer }; _peerRepositoryMock.Setup(repo => repo.GetPeers(It.IsAny <bool>())).Returns(peerDescriptors); _peerRepositoryMock.Setup(repo => repo.Get(It.IsAny <PeerId>())).Returns <PeerId>(peerId => peerDescriptors.FirstOrDefault(x => x.Peer.Id == peerId)); _bus = new TestBus(); _configurationMock = new Mock <IDirectoryConfiguration>(); _configurationMock.As <IDirectoryConfiguration>().SetupGet(conf => conf.TransientPeerPingTimeout).Returns(_transientPeerTimeout.Seconds()); _configurationMock.As <IDirectoryConfiguration>().SetupGet(conf => conf.PersistentPeerPingTimeout).Returns(_persistentPeerTimeout.Seconds()); _configurationMock.As <IDirectoryConfiguration>().SetupGet(conf => conf.DebugPeerPingTimeout).Returns(_debugPeerTimeout.Seconds()); _configurationMock.As <IDirectoryConfiguration>().SetupGet(conf => conf.PeerPingInterval).Returns(_pingInterval); _configurationMock.As <IDirectoryConfiguration>().SetupGet(conf => conf.WildcardsForPeersNotToDecommissionOnTimeout).Returns(() => _peersNotToDecommission); _detector = new DeadPeerDetector(_bus, _peerRepositoryMock.Object, _configurationMock.Object); _detector.TaskScheduler = new CurrentThreadTaskScheduler(); _bus.HandlerExecutor = new HangOnThrowHandlerExecutor(); }
private bool IsNotInTheProtectedList(PeerDescriptor descriptor) { var peerId = descriptor.PeerId.ToString(); foreach (var wildcardPattern in _configuration.WildcardsForPeersNotToDecommissionOnTimeout ?? Array.Empty <string>()) { var pattern = Regex.Escape(wildcardPattern.Trim()); pattern = pattern.Replace(@"\?", "."); pattern = pattern.Replace(@"\*", ".*?"); pattern = pattern.Replace(@"\#", "[0-9]"); pattern = "^" + pattern + "$"; if (Regex.IsMatch(peerId, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)) { return(false); } } return(true); }
public void should_update_peer_handled_message_and_publish_event() { var originalPeerDescriptor = TestDataBuilder.CreatePersistentPeerDescriptor("tcp://abctest:123", typeof(FakeCommand)); _repositoryMock.Setup(x => x.Get(originalPeerDescriptor.Peer.Id)).Returns(originalPeerDescriptor); PeerDescriptor updatedPeerDescriptor = null; _repositoryMock.Setup(x => x.AddOrUpdatePeer(It.IsAny <PeerDescriptor>())).Callback <PeerDescriptor>(peer => updatedPeerDescriptor = peer); var newSubscriptions = new[] { new Subscription(new MessageTypeId("Another.Handled.Type")) }; _handler.Handle(new UpdatePeerSubscriptionsCommand(originalPeerDescriptor.Peer.Id, newSubscriptions, DateTime.UtcNow)); updatedPeerDescriptor.Subscriptions.ShouldBeEquivalentTo(newSubscriptions); var handledMessageUpdateds = _bus.Messages.OfType <PeerSubscriptionsUpdated>().ToList(); handledMessageUpdateds.Count.ShouldEqual(1); handledMessageUpdateds.Single().PeerDescriptor.ShouldHaveSamePropertiesAs(updatedPeerDescriptor); }
public void AddOrUpdatePeer(PeerDescriptor peerDescriptor) { var newPeerEntry = new PeerEntry(peerDescriptor); _peers.AddOrUpdate(peerDescriptor.PeerId, newPeerEntry, (peerId, existingPeerEntry) => peerDescriptor.TimestampUtc >= existingPeerEntry.PeerDescriptor.TimestampUtc ? newPeerEntry : existingPeerEntry); }
private static bool IsPeerInConflict([NotNullWhen(true)] PeerDescriptor?existingPeer, PeerDescriptor peerToAdd) { return(existingPeer != null && existingPeer.Peer.IsResponding && existingPeer.PeerId == peerToAdd.PeerId && existingPeer.Peer.GetMachineNameFromEndPoint() != peerToAdd.Peer.GetMachineNameFromEndPoint()); }
public PeerEntry(PeerDescriptor peerDescriptor) { PeerDescriptor = new PeerDescriptor(peerDescriptor); DynamicSubscriptions = new List <Subscription>(); }
private bool IsNotIntheProtectedList(PeerDescriptor descriptor) { var peerId = descriptor.PeerId.ToString(); return(!_configuration.WildcardsForPeersNotToDecommissionOnTimeout.Any(x => Operators.LikeString(peerId, x, CompareMethod.Text))); }
public PeerEntry(PeerDescriptor peerDescriptor) { PeerDescriptor = new PeerDescriptor(peerDescriptor); DynamicSubscriptions = new List<Subscription>(); }