Example #1
0
        public void can_write_epochs()
        {
            //can write first epoch
            _published.Clear();
            var beforeWrite = DateTime.UtcNow;

            _epochManager.WriteNewEpoch(GetNextEpoch());
            Assert.That(_published.Count == 1);
            var epochWritten = _published[0] as SystemMessage.EpochWritten;

            Assert.NotNull(epochWritten);
            Assert.That(epochWritten.Epoch.EpochNumber == 0);
            Assert.That(epochWritten.Epoch.PrevEpochPosition == -1);
            Assert.That(epochWritten.Epoch.EpochPosition == 0);
            Assert.That(epochWritten.Epoch.LeaderInstanceId == _instanceId);
            Assert.That(epochWritten.Epoch.TimeStamp < DateTime.UtcNow);
            Assert.That(epochWritten.Epoch.TimeStamp >= beforeWrite);
            _published.Clear();

            // will_cache_epochs_written() {

            for (int i = 0; i < 4; i++)
            {
                _epochManager.WriteNewEpoch(GetNextEpoch());
            }
            Assert.That(_cache.Count == 5);
            Assert.That(_cache.First.Value.EpochNumber == 0);
            Assert.That(_cache.Last.Value.EpochNumber == 4);
            var epochs = new List <int>();
            var epoch  = _cache.First;

            while (epoch != null)
            {
                epochs.Add(epoch.Value.EpochNumber);
                epoch = epoch.Next;
            }
            CollectionAssert.IsOrdered(epochs);

            // can_write_more_epochs_than_cache_size

            for (int i = 0; i < 16; i++)
            {
                _epochManager.WriteNewEpoch(GetNextEpoch());
            }
            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == 11);
            Assert.That(_cache.Last.Value.EpochNumber == 20);
            epochs = new List <int>();
            epoch  = _cache.First;
            while (epoch != null)
            {
                epochs.Add(epoch.Value.EpochNumber);
                epoch = epoch.Next;
            }
            CollectionAssert.IsOrdered(epochs);
        }
Example #2
0
        public override void When()
        {
            // The EpochManager for these tests only caches 5 epochs
            // Epochs 2 and 3 don't exist
            EpochManager.WriteNewEpoch(0);
            Writer.Write(CreateLogRecord(0), out _);
            EpochManager.WriteNewEpoch(1);
            Writer.Write(CreateLogRecord(1), out _);

            _uncachedLeaderEpochs = EpochManager.GetLastEpochs(2);

            EpochManager.WriteNewEpoch(4);
            Writer.Write(CreateLogRecord(2), out _);
            EpochManager.WriteNewEpoch(5);
            Writer.Write(CreateLogRecord(3), out _);
            EpochManager.WriteNewEpoch(6);
            Writer.Write(CreateLogRecord(4), out _);
            EpochManager.WriteNewEpoch(7);
            Writer.Write(CreateLogRecord(5), out _);
            EpochManager.WriteNewEpoch(8);

            _replicaEpochs = new List <Epoch> {
                new Epoch(_uncachedLeaderEpochs[0].EpochPosition + 8000, 3, Guid.NewGuid()),
                new Epoch(_uncachedLeaderEpochs[0].EpochPosition + 4000, 2, Guid.NewGuid()),
                new Epoch(_uncachedLeaderEpochs[0].EpochPosition, _uncachedLeaderEpochs[0].EpochNumber, _uncachedLeaderEpochs[0].EpochId),
                new Epoch(_uncachedLeaderEpochs[1].EpochPosition, _uncachedLeaderEpochs[1].EpochNumber, _uncachedLeaderEpochs[1].EpochId)
            };

            AddSubscription(_replicaId, true, _replicaEpochs.ToArray(), _replicaEpochs[0].EpochPosition, out _replicaManager);
        }
Example #3
0
        public override void When()
        {
            EpochManager.WriteNewEpoch(0);
            Writer.Write(CreateLogRecord(0), out _);
            Writer.Write(CreateLogRecord(1), out _);
            Writer.Write(CreateLogRecord(2), out _);
            Writer.Write(CreateLogRecord(3), out _);
            Writer.Write(CreateLogRecord(4), out _subscribedPosition);
            EpochManager.WriteNewEpoch(1);

            _lastEpoch = EpochManager.GetLastEpoch();
            var epochs = EpochManager.GetLastEpochs(10)
                         .Select(e => new Epoch(e.EpochPosition, e.EpochNumber, e.EpochId)).ToArray();

            AddSubscription(_replicaId, true, epochs, _subscribedPosition, out _replicaManager);
        }
Example #4
0
        public void starting_epoch_manager_loads_without_epochs()
        {
            _epochManager = GetManager();
            _epochManager.Init();
            _cache = GetCache(_epochManager);
            Assert.NotNull(_cache);

            Assert.That(_cache.Count == 0);
            Assert.That(_cache?.First?.Value == null);
            Assert.That(_cache?.Last?.Value == null);
            Assert.That(_epochManager.LastEpochNumber == -1);
            _epochManager.WriteNewEpoch(0);
            Assert.That(_cache.Count == 1);
            Assert.That(_cache.First.Value.EpochNumber == 0);
            Assert.That(_cache.Last.Value.EpochNumber == 0);
            Assert.That(_epochManager.LastEpochNumber == 0);
        }
Example #5
0
        public override void When()
        {
            EpochManager.WriteNewEpoch(0);
            Writer.Write(CreateLogRecord(0), out _);
            Writer.Write(CreateLogRecord(1), out _);
            Writer.Write(CreateLogRecord(2), out _);
            Writer.Write(CreateLogRecord(3), out _);
            Writer.Write(CreateLogRecord(4), out _);
            EpochManager.WriteNewEpoch(1);

            var epochs = new [] {
                new Epoch(1010, 1, Guid.NewGuid()),
                new Epoch(999, 2, Guid.NewGuid())
            };

            AddSubscription(_replicaId, true, epochs, 1010, out _replicaManager);
        }
Example #6
0
        public override void When()
        {
            EpochManager.WriteNewEpoch(0);
            Writer.Write(CreateLogRecord(0), out _);
            Writer.Write(CreateLogRecord(1), out _);
            Writer.Write(CreateLogRecord(2), out var otherEpochLogPosition);
            Writer.Write(CreateLogRecord(3), out _);
            Writer.Write(CreateLogRecord(4), out _);
            EpochManager.WriteNewEpoch(2);

            var firstEpoch = EpochManager.GetLastEpochs(10).First(e => e.EpochNumber == 0);

            _replicaEpochs = new List <Epoch> {
                new Epoch(otherEpochLogPosition, 1, Guid.NewGuid()),
                new Epoch(firstEpoch.EpochPosition, firstEpoch.EpochNumber, firstEpoch.EpochId)
            };

            AddSubscription(_replicaId, true, _replicaEpochs.ToArray(), _replicaEpochs[0].EpochPosition, out _replicaManager);
        }
Example #7
0
        public override void When()
        {
            EpochManager.WriteNewEpoch(0);
            Writer.Write(CreateLogRecord(0), out _);
            Writer.Write(CreateLogRecord(1), out _);
            Writer.Write(CreateLogRecord(2), out _);
            Writer.Write(CreateLogRecord(3), out _);
            Writer.Write(CreateLogRecord(4), out _);
            EpochManager.WriteNewEpoch(1);

            var subscribePosition = Db.Config.WriterCheckpoint.ReadNonFlushed() + 1000;

            _replicaEpochs = new List <Epoch> {
                new Epoch(subscribePosition, 2, Guid.NewGuid()),
            };
            _replicaEpochs.AddRange(EpochManager.GetLastEpochs(10)
                                    .Select(e => new Epoch(e.EpochPosition, e.EpochNumber, e.EpochId)).ToList());

            AddSubscription(_replicaId, true, _replicaEpochs.ToArray(), subscribePosition, out _replicaManager);
        }
Example #8
0
        public override void When()
        {
            EpochManager.WriteNewEpoch(0);
            Writer.Write(CreateLogRecord(0), out _);
            EpochManager.WriteNewEpoch(1);

            // The EpochManager for these tests only caches 5 epochs
            _replicaEpochs = EpochManager.GetLastEpochs(2)
                             .Select(e => new Epoch(e.EpochPosition, e.EpochNumber, e.EpochId)).ToList();

            Writer.Write(CreateLogRecord(1), out _);
            EpochManager.WriteNewEpoch(2);
            Writer.Write(CreateLogRecord(2), out _);
            EpochManager.WriteNewEpoch(3);
            Writer.Write(CreateLogRecord(3), out _);
            EpochManager.WriteNewEpoch(4);
            Writer.Write(CreateLogRecord(4), out _);
            EpochManager.WriteNewEpoch(5);
            Writer.Write(CreateLogRecord(5), out _);
            EpochManager.WriteNewEpoch(6);

            AddSubscription(_replicaId, true, _replicaEpochs.ToArray(), _replicaEpochs[0].EpochPosition, out _replicaManager);
        }
Example #9
0
        public override void When()
        {
            EpochManager.WriteNewEpoch(0);
            Writer.Write(CreateLogRecord(0), out _);
            Writer.Write(CreateLogRecord(1), out _);
            Writer.Write(CreateLogRecord(2), out _);
            Writer.Write(CreateLogRecord(3), out _);
            Writer.Write(CreateLogRecord(4), out _);
            EpochManager.WriteNewEpoch(1);
            Writer.Write(CreateLogRecord(5), out _);
            Writer.Write(CreateLogRecord(6), out _);
            Writer.Write(CreateLogRecord(7), out var lastWritePosition);
            Writer.Flush();

            _replicaEpochs = new List <Epoch> {
                new Epoch(lastWritePosition + 2000, 4, Guid.NewGuid()),
                new Epoch(lastWritePosition + 1000, 3, Guid.NewGuid()),
                new Epoch(lastWritePosition, 2, Guid.NewGuid()),
            };
            _replicaEpochs.AddRange(EpochManager.GetLastEpochs(10)
                                    .Select(e => new Epoch(e.EpochPosition, e.EpochNumber, e.EpochId)).ToList());

            AddSubscription(_replicaId, true, _replicaEpochs.ToArray(), lastWritePosition + 2000, out _replicaManager);
        }
Example #10
0
        public void can_add_epochs_to_cache()
        {
            Assert.That(_cache.Count == 0);
            //add fist epoch to empty cache
            _epochManager.AddEpochToCache(_epochs[3]);

            Assert.That(_cache.Count == 4);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[0].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[3].EpochNumber);

            //add new last epoch
            _epochManager.AddEpochToCache(_epochs[4]);

            Assert.That(_cache.Count == 5);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[0].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[4].EpochNumber);

            //idempotent add
            _epochManager.AddEpochToCache(_epochs[1]);
            _epochManager.AddEpochToCache(_epochs[2]);
            _epochManager.AddEpochToCache(_epochs[3]);

            Assert.That(_cache.Count == 5);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[0].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[4].EpochNumber);

            //add new skip 1 last epoch
            _epochManager.AddEpochToCache(_epochs[6]);

            Assert.That(_cache.Count == 7);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[0].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[6].EpochNumber);

            //add new skip 5 last epoch
            _epochManager.AddEpochToCache(_epochs[11]);

            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[2].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[11].EpochNumber);

            //add last rolls cache
            _epochManager.AddEpochToCache(_epochs[12]);

            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[3].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[12].EpochNumber);


            //add epoch before cache
            _epochManager.AddEpochToCache(_epochs[1]);

            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[3].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[12].EpochNumber);

            //add idempotent first epoch
            _epochManager.AddEpochToCache(_epochs[2]);

            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[3].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[12].EpochNumber);

            //add idempotent last epoch
            _epochManager.AddEpochToCache(_epochs[12]);

            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[3].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[12].EpochNumber);

            //add disjunct skip epoch
            _epochManager.AddEpochToCache(_epochs[24]);

            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber);


            //cannot get epoch ahead of last cached on master
            var nextEpoch = _epochManager.GetEpochAfter(_epochs[24].EpochNumber, false);

            Assert.Null(nextEpoch);

            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber);

            //cannot get epoch ahead of cache on master
            nextEpoch = _epochManager.GetEpochAfter(_epochs[25].EpochNumber, false);
            Assert.Null(nextEpoch);

            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber);

            //can get next  in cache
            nextEpoch = _epochManager.GetEpochAfter(_epochs[20].EpochNumber, false);

            Assert.That(nextEpoch.EpochPosition == _epochs[21].EpochPosition);
            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber);

            //can get next from first
            nextEpoch = _epochManager.GetEpochAfter(_epochs[15].EpochNumber, false);

            Assert.That(nextEpoch.EpochPosition == _epochs[16].EpochPosition);
            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber);

            //can get next epoch from just before cache
            nextEpoch = _epochManager.GetEpochAfter(_epochs[14].EpochNumber, false);

            Assert.That(nextEpoch.EpochPosition == _epochs[15].EpochPosition);
            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber);

            //can get next epoch from before cache
            nextEpoch = _epochManager.GetEpochAfter(_epochs[10].EpochNumber, false);

            Assert.That(nextEpoch.EpochPosition == _epochs[11].EpochPosition);
            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber);

            //can get next epoch from 0 epoch
            nextEpoch = _epochManager.GetEpochAfter(_epochs[0].EpochNumber, false);

            Assert.That(nextEpoch.EpochPosition == _epochs[1].EpochPosition);
            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[15].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[24].EpochNumber);


            //can add last epoch in log
            _epochManager.AddEpochToCache(_epochs[29]);

            Assert.That(_cache.Count == 10);
            Assert.That(_cache.First.Value.EpochNumber == _epochs[20].EpochNumber);
            Assert.That(_cache.Last.Value.EpochNumber == _epochs[29].EpochNumber);

            // can write an epoch with epoch information (even though previous epochs
            // dont have epoch information)
            _epochManager.WriteNewEpoch(GetNextEpoch());
            _epochManager.WriteNewEpoch(GetNextEpoch());
            var epochsWritten = _published.OfType <SystemMessage.EpochWritten>().ToArray();

            Assert.AreEqual(2, epochsWritten.Length);
            for (int i = 0; i < epochsWritten.Length; i++)
            {
                _reader.Reposition(epochsWritten[i].Epoch.EpochPosition);
                _reader.TryReadNext();                 // read epoch
                IPrepareLogRecord <TStreamId> epochInfo;
                while (true)
                {
                    var result = _reader.TryReadNext();
                    Assert.True(result.Success);
                    if (result.LogRecord is IPrepareLogRecord <TStreamId> prepare)
                    {
                        epochInfo = prepare;
                        break;
                    }
                }
                var expectedStreamId = LogFormatHelper <TLogFormat, TStreamId> .Choose <TStreamId>(
                    SystemStreams.EpochInformationStream,
                    LogV3SystemStreams.EpochInformationStreamNumber);

                var expectedEventType = LogFormatHelper <TLogFormat, TStreamId> .Choose <TStreamId>(
                    SystemEventTypes.EpochInformation,
                    LogV3SystemEventTypes.EpochInformationNumber);

                Assert.AreEqual(expectedStreamId, epochInfo.EventStreamId);
                Assert.AreEqual(expectedEventType, epochInfo.EventType);
                Assert.AreEqual(i - 1, epochInfo.ExpectedVersion);
                Assert.AreEqual(_instanceId, epochInfo.Data.ParseJson <EpochDto>().LeaderInstanceId);
            }
        }