public void DeltaPropagationSelector_must_bump_version_for_each_update()
        {
            var delta1 = GSet <string> .Empty.Add("a1");

            var delta2 = GSet <string> .Empty.Add("a2");

            var delta3 = GSet <string> .Empty.Add("a3");

            var selector = new TestSelector(selfUniqueAddress, nodes.Take(1).ToImmutableArray());

            selector.Update("A", delta1);
            selector.CurrentVersion("A").Should().Be(1L);
            selector.Update("A", delta2);
            selector.CurrentVersion("A").Should().Be(2L);
            var expected1 = new DeltaPropagation(selfUniqueAddress, false, ImmutableDictionary <string, Delta> .Empty
                                                 .Add("A", new Delta(new DataEnvelope(delta1.Merge(delta2)), 1L, 2L)));

            selector.CollectPropagations().Should().Equal(ImmutableDictionary.CreateRange(new[] { new KeyValuePair <Address, DeltaPropagation>(nodes[0], expected1), }));
            selector.Update("A", delta3);
            selector.CurrentVersion("A").Should().Be(3L);
            var expected2 = new DeltaPropagation(selfUniqueAddress, false, ImmutableDictionary <string, Delta> .Empty
                                                 .Add("A", new Delta(new DataEnvelope(delta3), 3L, 3L)));

            selector.CollectPropagations().Should().Equal(ImmutableDictionary.CreateRange(new[] { new KeyValuePair <Address, DeltaPropagation>(nodes[0], expected2), }));
            selector.CollectPropagations().Should().BeEmpty();
        }
        public void DeltaPropagationSelector_must_collect_1_when_one_node()
        {
            var selector = new TestSelector(selfUniqueAddress, nodes.Take(1).ToImmutableArray());

            selector.Update("A", DeltaA);
            selector.Update("B", DeltaB);
            selector.CleanupDeltaEntries();
            selector.HasDeltaEntries("A").Should().BeTrue();
            selector.HasDeltaEntries("B").Should().BeTrue();
            var expected = new DeltaPropagation(selfUniqueAddress, false, ImmutableDictionary <string, Delta> .Empty
                                                .Add("A", new Delta(new DataEnvelope(DeltaA), 1L, 1L))
                                                .Add("B", new Delta(new DataEnvelope(DeltaB), 1L, 1L)));

            selector.CollectPropagations().Should().Equal(ImmutableDictionary.CreateRange(new[] { new KeyValuePair <Address, DeltaPropagation>(nodes[0], expected), }));
            selector.CollectPropagations().Should().BeEmpty();
            selector.CleanupDeltaEntries();
            selector.HasDeltaEntries("A").Should().BeFalse();
            selector.HasDeltaEntries("B").Should().BeFalse();
        }
Esempio n. 3
0
 public WriteAggregator(IKey key, DataEnvelope envelope, Delta delta, IWriteConsistency consistency, object req, IImmutableSet <Address> nodes,
                        IImmutableSet <Address> unreachable, IActorRef replyTo, bool durable)
     : base(nodes, unreachable, consistency.Timeout)
 {
     _selfUniqueAddress = Cluster.Cluster.Get(Context.System).SelfUniqueAddress;
     _key         = key;
     _envelope    = envelope;
     _consistency = consistency;
     _req         = req;
     _replyTo     = replyTo;
     _durable     = durable;
     _write       = new Write(key.Id, envelope);
     _delta       = delta == null
         ? null
         : new DeltaPropagation(_selfUniqueAddress, true,
                                ImmutableDictionary <string, Delta> .Empty.Add(key.Id, delta));
     _gotLocalStoreReply   = !durable;
     _gotNackFrom          = ImmutableHashSet <Address> .Empty;
     DoneWhenRemainingSize = GetDoneWhenRemainingSize();
 }
        public void DeltaPropagationSelector_must_merge_deltas()
        {
            var delta1 = GSet <string> .Empty.Add("a1");

            var delta2 = GSet <string> .Empty.Add("a2");

            var delta3 = GSet <string> .Empty.Add("a3");

            var selector = new TestSelector2(selfUniqueAddress, nodes.Take(3).ToImmutableArray());

            selector.Update("A", delta1);
            var expected1 = new DeltaPropagation(selfUniqueAddress, false, ImmutableDictionary <string, Delta> .Empty
                                                 .Add("A", new Delta(new DataEnvelope(delta1), 1L, 1L)));

            selector.CollectPropagations().Should().Equal(ImmutableDictionary.CreateRange(new[] { new KeyValuePair <Address, DeltaPropagation>(nodes[0], expected1), }));

            selector.Update("A", delta2);
            var expected2 = new DeltaPropagation(selfUniqueAddress, false, ImmutableDictionary <string, Delta> .Empty
                                                 .Add("A", new Delta(new DataEnvelope(delta1.Merge(delta2)), 1L, 2L)));

            selector.CollectPropagations().Should().Equal(ImmutableDictionary.CreateRange(new[] { new KeyValuePair <Address, DeltaPropagation>(nodes[1], expected2), }));

            selector.Update("A", delta3);
            var expected3 = new DeltaPropagation(selfUniqueAddress, false, ImmutableDictionary <string, Delta> .Empty
                                                 .Add("A", new Delta(new DataEnvelope(delta1.Merge(delta2).Merge(delta3)), 1L, 3L)));

            selector.CollectPropagations().Should().Equal(ImmutableDictionary.CreateRange(new[] { new KeyValuePair <Address, DeltaPropagation>(nodes[2], expected3), }));

            var expected4 = new DeltaPropagation(selfUniqueAddress, false, ImmutableDictionary <string, Delta> .Empty
                                                 .Add("A", new Delta(new DataEnvelope(delta2.Merge(delta3)), 2L, 3L)));

            selector.CollectPropagations().Should().Equal(ImmutableDictionary.CreateRange(new[] { new KeyValuePair <Address, DeltaPropagation>(nodes[0], expected4), }));

            var expected5 = new DeltaPropagation(selfUniqueAddress, false, ImmutableDictionary <string, Delta> .Empty
                                                 .Add("A", new Delta(new DataEnvelope(delta3), 3L, 3L)));

            selector.CollectPropagations().Should().Equal(ImmutableDictionary.CreateRange(new[] { new KeyValuePair <Address, DeltaPropagation>(nodes[1], expected5), }));

            selector.CollectPropagations().Should().BeEmpty();
        }
        public void DeltaPropagationSelector_must_discard_too_large_deltas()
        {
            var selector = new TestSelector2(selfUniqueAddress, nodes.Take(3).ToImmutableArray());
            var data     = PNCounterDictionary <string> .Empty;

            for (int i = 1; i <= 1000; i++)
            {
                var d = data.ResetDelta().Increment(selfUniqueAddress, (i % 2).ToString(), 1);
                selector.Update("A", d.Delta);
                data = d;
            }

            var expected = new DeltaPropagation(selfUniqueAddress, false, new Dictionary <string, Delta>
            {
                { "A", new Delta(new DataEnvelope(DeltaPropagation.NoDeltaPlaceholder), 1L, 1000L) }
            }.ToImmutableDictionary());

            selector.CollectPropagations().Should().Equal(new Dictionary <Address, DeltaPropagation>
            {
                { nodes[0], expected }
            });
        }
        public void DeltaPropagationSelector_must_keep_track_of_deltas_per_node()
        {
            var selector = new TestSelector(selfUniqueAddress, nodes.Take(3).ToImmutableArray());

            selector.Update("A", DeltaA);
            selector.Update("B", DeltaB);
            var expected = new DeltaPropagation(selfUniqueAddress, false, ImmutableDictionary <string, Delta> .Empty
                                                .Add("A", new Delta(new DataEnvelope(DeltaA), 1L, 1L))
                                                .Add("B", new Delta(new DataEnvelope(DeltaB), 1L, 1L)));

            selector.CollectPropagations().Should().Equal(ImmutableDictionary.CreateRange(new[]
            {
                new KeyValuePair <Address, DeltaPropagation>(nodes[0], expected),
                new KeyValuePair <Address, DeltaPropagation>(nodes[1], expected),
            }));
            // new update before previous was propagated to all nodes
            selector.Update("C", DeltaC);
            var expected2 = new DeltaPropagation(selfUniqueAddress, false, ImmutableDictionary <string, Delta> .Empty
                                                 .Add("A", new Delta(new DataEnvelope(DeltaA), 1L, 1L))
                                                 .Add("B", new Delta(new DataEnvelope(DeltaB), 1L, 1L))
                                                 .Add("C", new Delta(new DataEnvelope(DeltaC), 1L, 1L)));
            var expected3 = new DeltaPropagation(selfUniqueAddress, false, ImmutableDictionary <string, Delta> .Empty
                                                 .Add("C", new Delta(new DataEnvelope(DeltaC), 1L, 1L)));

            selector.CollectPropagations().Should().Equal(ImmutableDictionary.CreateRange(new[]
            {
                new KeyValuePair <Address, DeltaPropagation>(nodes[2], expected2),
                new KeyValuePair <Address, DeltaPropagation>(nodes[0], expected3),
            }));
            selector.CleanupDeltaEntries();
            selector.HasDeltaEntries("A").Should().BeFalse();
            selector.HasDeltaEntries("B").Should().BeFalse();
            selector.HasDeltaEntries("C").Should().BeTrue();
            selector.CollectPropagations().Should().Equal(ImmutableDictionary.CreateRange(new[] { new KeyValuePair <Address, DeltaPropagation>(nodes[1], expected3), }));
            selector.CollectPropagations().Should().BeEmpty();
            selector.CleanupDeltaEntries();
            selector.HasDeltaEntries("C").Should().BeFalse();
        }