public PeerDescriptor Get(PeerId peerId) { var peerDynamicSubscriptions = _dataContext.DynamicSubscriptions .SetConsistencyLevel(ConsistencyLevel.LocalQuorum) .Where(sub => sub.UselessKey == false && sub.PeerId == peerId.ToString()) .Execute() .SelectMany(sub => sub.ToSubscriptionsForType().ToSubscriptions()); return _dataContext.StoragePeers .SetConsistencyLevel(ConsistencyLevel.LocalQuorum) .Where(peer => peer.UselessKey == false && peer.PeerId == peerId.ToString()) .Execute() .FirstOrDefault() .ToPeerDescriptor(peerDynamicSubscriptions); }
public void RemovePeer(PeerId peerId) { var now = DateTime.UtcNow; _dataContext.StoragePeers .SetConsistencyLevel(ConsistencyLevel.LocalQuorum) .Where(peer => peer.UselessKey == false && peer.PeerId == peerId.ToString()) .Delete() .SetTimestamp(now) .Execute(); _dataContext.DynamicSubscriptions .SetConsistencyLevel(ConsistencyLevel.LocalQuorum) .Where(sub => sub.UselessKey == false && sub.PeerId == peerId.ToString()) .Delete() .SetTimestamp(now) .Execute(); }
private Task RemovePersistentMessages(PeerId peerId) { var allPossibleBuckets = BucketIdHelper.GetBucketsCollection(SystemDateTime.UtcNow.Ticks - PeerState.MessagesTimeToLive.Ticks).ToArray(); return(_dataContext.PersistentMessages .Where(x => x.PeerId == peerId.ToString() && allPossibleBuckets.Contains(x.BucketId)) .Delete() .ExecuteAsync()); }
public static StorageSubscription ToStorageSubscription(this SubscriptionsForType subscriptionFortype, PeerId peerId) { return new StorageSubscription { PeerId = peerId.ToString(), MessageTypeId = subscriptionFortype.MessageTypeId.FullName, SubscriptionBindings = SerializeBindingKeys(subscriptionFortype.BindingKeys) }; }
public async Task should_store_messages_in_different_buckets() { MessageId.ResetLastTimestamp(); var firstTime = DateTime.Now; using (SystemDateTime.Set(firstTime)) using (MessageId.PauseIdGenerationAtDate(firstTime)) { var peerId = new PeerId("Abc.Testing.Target"); var firstMessageId = MessageId.NextId(); await _storage.Write(new[] { MatcherEntry.Message(peerId, firstMessageId, new MessageTypeId("Abc.Message"), new byte[] { 0x01, 0x02, 0x03 }) }); var secondTime = firstTime.AddHours(1); SystemDateTime.Set(secondTime); MessageId.PauseIdGenerationAtDate(secondTime); var secondMessageId = MessageId.NextId(); await _storage.Write(new[] { MatcherEntry.Message(peerId, secondMessageId, new MessageTypeId("Abc.OtherMessage"), new byte[] { 0x04, 0x05, 0x06 }) }); var persistedMessages = DataContext.PersistentMessages.Execute().OrderBy(x => x.UniqueTimestampInTicks).ToList(); // Results are only ordered withing a partition persistedMessages.Count.ShouldEqual(2); persistedMessages.First().ShouldHaveSamePropertiesAs(new PersistentMessage { BucketId = BucketIdHelper.GetBucketId(firstTime), PeerId = peerId.ToString(), IsAcked = false, MessageId = firstMessageId.Value, TransportMessage = new byte[] { 0x01, 0x02, 0x03 }, UniqueTimestampInTicks = firstTime.Ticks }); persistedMessages.Last().ShouldHaveSamePropertiesAs(new PersistentMessage { BucketId = BucketIdHelper.GetBucketId(secondTime), PeerId = peerId.ToString(), IsAcked = false, MessageId = secondMessageId.Value, TransportMessage = new byte[] { 0x04, 0x05, 0x06 }, UniqueTimestampInTicks = secondTime.Ticks }); } }
public void SetPeerResponding(PeerId peerId, bool isResponding) { _dataContext.StoragePeers .SetConsistencyLevel(ConsistencyLevel.LocalQuorum) .Where(peer => peer.UselessKey == false && peer.PeerId == peerId.ToString()) .Select(peer => new StoragePeer { IsResponding = isResponding }) .Update() .SetTimestamp(DateTime.UtcNow) .Execute(); }
private ZmqSocket CreateSocket() { var socket = new ZmqSocket(_context, ZmqSocketType.PULL); socket.SetOption(ZmqSocketOption.RCVHWM, _options.ReceiveHighWaterMark); socket.SetOption(ZmqSocketOption.RCVTIMEO, (int)_options.ReceiveTimeout.TotalMilliseconds); socket.SetOption(ZmqSocketOption.ROUTING_ID, Encoding.ASCII.GetBytes(_peerId.ToString())); _lastReceiveTimeout = _options.ReceiveTimeout; return(socket); }
public void should_convert_peer_id_to_json_string() { var peerId = new PeerId("Abc.Testing.42"); var text = JsonConvert.SerializeObject(new MessageWithPeerId { PeerId1 = peerId }); text.ShouldContain(peerId.ToString()); Console.WriteLine(text); var message = JsonConvert.DeserializeObject<MessageWithPeerId>(text); message.PeerId1.ShouldEqual(peerId); message.PeerId2.ShouldBeNull(); }
private void InsertPersistentMessage(PeerId peerId, DateTime timestamp, AckState ackState = AckState.Acked) { var message = new PersistentMessage { PeerId = peerId.ToString(), BucketId = BucketIdHelper.GetBucketId(timestamp), IsAcked = ackState == AckState.Acked, UniqueTimestampInTicks = timestamp.Ticks, TransportMessage = new byte[0] }; DataContext.PersistentMessages.Insert(message).Execute(); }
private void InsertPersistentMessage(PeerId peerId, DateTime timestamp, Action <PersistentMessage> updateMessage = null) { var message = new PersistentMessage { PeerId = peerId.ToString(), BucketId = BucketIdHelper.GetBucketId(timestamp), IsAcked = true, UniqueTimestampInTicks = timestamp.Ticks, TransportMessage = new byte[0] }; updateMessage?.Invoke(message); DataContext.PersistentMessages.Insert(message).Execute(); }
public PeerDescriptor?Get(PeerId peerId) { var peerDynamicSubscriptions = _dataContext.DynamicSubscriptions .SetConsistencyLevel(ConsistencyLevel.LocalQuorum) .Where(sub => sub.UselessKey == false && sub.PeerId == peerId.ToString()) .Execute() .SelectMany(sub => sub.ToSubscriptionsForType().ToSubscriptions()); return(_dataContext.StoragePeers .SetConsistencyLevel(ConsistencyLevel.LocalQuorum) .Where(peer => peer.UselessKey == false && peer.PeerId == peerId.ToString()) .Execute() .FirstOrDefault() .ToPeerDescriptor(peerDynamicSubscriptions)); }
public void should_convert_peer_id_to_json_string() { var peerId = new PeerId("Abc.Testing.42"); var text = JsonConvert.SerializeObject(new MessageWithPeerId { PeerId1 = peerId }); text.ShouldContain(peerId.ToString()); Console.WriteLine(text); var message = JsonConvert.DeserializeObject <MessageWithPeerId>(text); message.PeerId1.ShouldEqual(peerId); message.PeerId2.ShouldBeNull(); }
private void CleanBuckets(PeerId peerId, long previousOldestMessageTimestamp, long newOldestMessageTimestamp) { var firstBucketToDelete = BucketIdHelper.GetBucketId(previousOldestMessageTimestamp); var lastBucketToDelete = BucketIdHelper.GetPreviousBucketId(newOldestMessageTimestamp); if (firstBucketToDelete == lastBucketToDelete) { return; } var bucketsToDelete = BucketIdHelper.GetBucketsCollection(firstBucketToDelete, lastBucketToDelete).ToArray(); _dataContext.PersistentMessages .Where(x => x.PeerId == peerId.ToString() && bucketsToDelete.Contains(x.BucketId)) .Delete() .ExecuteAsync(); }
public void TestIdMatchesPublicKey() { Action <KeySet> test = (ks) => { var p1 = PeerId.Decode(ks.hpkp); Assert.Equal(p1.ToString(Multibase.Base16), ks.hpk); Assert.True(p1.MatchesPublicKey(ks.pk)); var p2 = new PeerId(ks.pk); Assert.Equal(p1, p2); Assert.Equal(p2.ToString(Multibase.Base58), ks.hpkp); }; test(gen1); test(gen2); test(man); }
public static void FillKey(byte[] key, PeerId peerId, long ticks, Guid messageId) { var peerPart = Encoding.UTF8.GetBytes(peerId.ToString()); Buffer.BlockCopy(peerPart, 0, key, 0, peerPart.Length); var tickPart = BitConverter.GetBytes(ticks); if (BitConverter.IsLittleEndian) { Array.Reverse(tickPart); // change endianness so sorting will be correct } Buffer.BlockCopy(tickPart, 0, key, peerPart.Length, sizeof(long)); var messageIdPart = messageId.ToByteArray(); Buffer.BlockCopy(messageIdPart, 0, key, peerPart.Length + sizeof(long), _guidLength); }
public void should_not_forward_messages_to_upper_layer_when_stopping() { var receivedMessages = new List <TransportMessage>(); var receivingPeerId = new PeerId("Abc.Receiving.0"); var stopwatch = Stopwatch.StartNew(); var receivingTransport = CreateAndStartZmqTransport(onMessageReceived: receivedMessages.Add, peerId: receivingPeerId.ToString(), transportFactory: conf => new CapturingIsListeningTimeZmqTransport(conf, stopwatch)); var receivingPeer = new Peer(receivingPeerId, receivingTransport.InboundEndPoint); var messageSerializer = new MessageSerializer(); bool receivedWhileNotListening = false; receivingTransport.MessageReceived += message => { var cmdWithTimetamp = (FakeCommandWithTimestamp)messageSerializer.Deserialize(message.MessageTypeId, message.MessageBytes); if (cmdWithTimetamp.Timestamp > ((CapturingIsListeningTimeZmqTransport)receivingTransport).IsListeningSwitchTimestamp) { receivedWhileNotListening = true; } }; var sendingTransport = CreateAndStartZmqTransport(); var shouldSendMessages = true; var sendTask = Task.Factory.StartNew(() => { while (shouldSendMessages) { sendingTransport.Send(new FakeCommandWithTimestamp(stopwatch.Elapsed).ToTransportMessage(), new[] { receivingPeer }); } }); Wait.Until(() => sendTask.Status == TaskStatus.Running, 10.Seconds()); Wait.Until(() => receivedMessages.Count > 1, 10.Seconds()); receivingTransport.Stop(); receivedWhileNotListening.ShouldBeFalse(); shouldSendMessages = false; sendingTransport.Stop(); }
public void should_not_forward_messages_to_upper_layer_when_stopping() { var receivedMessages = new List<TransportMessage>(); var receivingPeerId = new PeerId("Abc.Receiving.0"); var stopwatch = Stopwatch.StartNew(); var receivingTransport = CreateAndStartZmqTransport(onMessageReceived: receivedMessages.Add, peerId: receivingPeerId.ToString(), transportFactory: conf => new CapturingIsListeningTimeZmqTransport(conf, stopwatch)); var receivingPeer = new Peer(receivingPeerId, receivingTransport.InboundEndPoint); var messageSerializer = new MessageSerializer(); bool receivedWhileNotListening = false; receivingTransport.MessageReceived += message => { var cmdWithTimetamp = (FakeCommandWithTimestamp)messageSerializer.Deserialize(message.MessageTypeId, message.MessageBytes); if (cmdWithTimetamp.Timestamp > ((CapturingIsListeningTimeZmqTransport)receivingTransport).IsListeningSwitchTimestamp) receivedWhileNotListening = true; }; var sendingTransport = CreateAndStartZmqTransport(); var shouldSendMessages = true; var sendTask = Task.Factory.StartNew(() => { while (shouldSendMessages) sendingTransport.Send(new FakeCommandWithTimestamp(stopwatch.Elapsed).ToTransportMessage(), new[] { receivingPeer }); }); Wait.Until(() => sendTask.Status == TaskStatus.Running, 10.Seconds()); Wait.Until(() => receivedMessages.Count > 1, 10.Seconds()); receivingTransport.Stop(); receivedWhileNotListening.ShouldBeFalse(); shouldSendMessages = false; sendingTransport.Stop(); }
public void RemoveDynamicSubscriptionsForTypes(PeerId peerId, DateTime timestampUtc, MessageTypeId[] messageTypeIds) { if (messageTypeIds == null) return; var batch = _dataContext.Session.CreateBatch(); batch.SetConsistencyLevel(ConsistencyLevel.LocalQuorum); foreach (var messageTypeId in messageTypeIds) { var deleteQuery = _dataContext.DynamicSubscriptions .Where(sub => sub.UselessKey == false && sub.PeerId == peerId.ToString() && sub.MessageTypeId == messageTypeId.FullName) .Delete() .SetTimestamp(timestampUtc); batch.Append(deleteQuery); } batch.Execute(); }
public void RemoveDynamicSubscriptionsForTypes(PeerId peerId, DateTime timestampUtc, MessageTypeId[] messageTypeIds) { if (messageTypeIds == null) { return; } var batch = _dataContext.Session.CreateBatch(); batch.SetConsistencyLevel(ConsistencyLevel.LocalQuorum); foreach (var messageTypeId in messageTypeIds) { var deleteQuery = _dataContext.DynamicSubscriptions .Where(sub => sub.UselessKey == false && sub.PeerId == peerId.ToString() && sub.MessageTypeId == messageTypeId.FullName) .Delete() .SetTimestamp(timestampUtc); batch.Append(deleteQuery); } batch.Execute(); }
public void RemoveAllDynamicSubscriptionsForPeer(PeerId peerId, DateTime timestampUtc) { _dataContext.DynamicSubscriptions .SetConsistencyLevel(ConsistencyLevel.LocalQuorum) .Where(sub => sub.UselessKey == false && sub.PeerId == peerId.ToString()) .Delete() .SetTimestamp(timestampUtc) .Execute(); }
public void ConnectFor(TransportMessage message) { if (!CanSendOrConnect(message)) { return; } try { _socket = new ZmqSocket(_context, ZmqSocketType.PUSH); _socket.SetOption(ZmqSocketOption.SNDHWM, _options.SendHighWaterMark); _socket.SetOption(ZmqSocketOption.SNDTIMEO, (int)_options.SendTimeout.TotalMilliseconds); _socket.SetOption(ZmqSocketOption.TCP_KEEPALIVE, 1); _socket.SetOption(ZmqSocketOption.TCP_KEEPALIVE_IDLE, 30); _socket.SetOption(ZmqSocketOption.TCP_KEEPALIVE_INTVL, 3); _socket.SetOption(ZmqSocketOption.ROUTING_ID, Encoding.ASCII.GetBytes(PeerId.ToString())); _socket.Connect(EndPoint); IsConnected = true; _logger.InfoFormat("Socket connected, Peer: {0}, EndPoint: {1}", PeerId, EndPoint); } catch (Exception ex) { _socket.Dispose(); _socket = null; IsConnected = false; _logger.ErrorFormat("Unable to connect socket, Peer: {0}, EndPoint: {1}, Exception: {2}", PeerId, EndPoint, ex); _errorHandler.OnConnectException(PeerId, EndPoint, ex); SwitchToClosedState(_options.ClosedStateDurationAfterConnectFailure); } }
public static void SetPeerId(this ZmqSocket socket, PeerId peerId) { socket.Identity = Encoding.ASCII.GetBytes(peerId.ToString()); }
public override string ToString() { return(PeerId.ToString()); }
private static byte[] GetPeerKey(PeerId peerId) => Encoding.UTF8.GetBytes(peerId.ToString());
public override string ToString() => PeerId.ToString();
public static byte[] CreateKeyBuffer(PeerId entryPeerId) => new byte[Encoding.UTF8.GetByteCount(entryPeerId.ToString()) + sizeof(long) + _guidLength];
private Task <RowSet> DeletePeerState(PeerId peerId) { return(_dataContext.PeerStates.Where(x => x.PeerId == peerId.ToString()).Delete().ExecuteAsync()); }