Пример #1
0
        private void InitializeRollingDeployment(string indexName, DateTime createdAt, long raftIndex)
        {
            RollingIndexes ??= new Dictionary <string, RollingIndex>();

            var rollingIndex = new RollingIndex();

            RollingIndexes[indexName] = rollingIndex;

            var chosenNode = ChooseFirstNode();

            foreach (var node in Topology.AllNodes)
            {
                var deployment = new RollingIndexDeployment
                {
                    CreatedAt = createdAt
                };

                if (node.Equals(chosenNode))
                {
                    deployment.State     = RollingIndexState.Running;
                    deployment.StartedAt = createdAt;
                }
                else
                {
                    deployment.State = RollingIndexState.Pending;
                }

                rollingIndex.ActiveDeployments[node] = deployment;
                rollingIndex.RaftCommandIndex        = raftIndex;
            }
        }
        public StoreError Add(string participant, string hash, int index)
        {
            var ok = ParticipantEvents.TryGetValue(participant, out var pe);

            if (!ok)
            {
                pe = new RollingIndex <string>(Size);
                ParticipantEvents.Add(participant, pe);
            }

            return(pe.Add(hash, index));
        }
Пример #3
0
        private void FinishOneNode(DatabaseRecord record, string finishedNodeTag, RollingIndex rollingIndex)
        {
            if (string.IsNullOrEmpty(finishedNodeTag))
            {
                return;
            }

            if (rollingIndex.ActiveDeployments.TryGetValue(finishedNodeTag, out var rollingDeployment) == false)
            {
                return;
            }

            if (rollingDeployment.State == RollingIndexState.Done)
            {
                return;
            }

            rollingDeployment.State      = RollingIndexState.Done;
            rollingDeployment.FinishedAt = FinishedAt;

            // If we are done and there is already a running node, there is nothing to do. That running node will continue from here.
            if (rollingIndex.ActiveDeployments.Any(node => node.Value.State == RollingIndexState.Running))
            {
                return;
            }

            var chosenNode = ChooseNextNode(record, rollingIndex.ActiveDeployments);

            if (chosenNode == null)
            {
                if (rollingIndex.ActiveDeployments.All(x => x.Value.State == RollingIndexState.Done))
                {
                    if (record.IndexesHistory.TryGetValue(IndexName, out var indexHistoryEntries))
                    {
                        if (indexHistoryEntries.Count > 0)
                        {
                            indexHistoryEntries[0].RollingDeployment = rollingIndex.ActiveDeployments;
                        }
                    }

                    record.RollingIndexes.Remove(IndexName);
                }

                return;
            }

            rollingIndex.ActiveDeployments[chosenNode].State     = RollingIndexState.Running;
            rollingIndex.ActiveDeployments[chosenNode].StartedAt = FinishedAt;
        }
Пример #4
0
        public StoreError Reset(Dictionary <string, Root> newRoots)
        {
            roots = newRoots;

            eventCache = new LruCache <string, Event>(cacheSize, null, logger, "EventCache");

            roundCache = new LruCache <int, RoundInfo>(cacheSize, null, logger, "RoundCache");

            consensusCache = new RollingIndex <string>(cacheSize);

            var err = participantEventsCache.Reset();

            lastRound = -1;

            return(err);
        }
Пример #5
0
        public InmemStore(Dictionary <string, int> participants, int cacheSize, ILogger logger)
        {
            var rts = new Dictionary <string, Root>();

            foreach (var p in participants)
            {
                rts.Add(p.Key, Root.NewBaseRoot());
            }

            this.participants      = participants;
            this.cacheSize         = cacheSize;
            this.logger            = logger.AddNamedContext("InmemStore");
            eventCache             = new LruCache <string, Event>(cacheSize, null, logger, "EventCache");
            roundCache             = new LruCache <int, RoundInfo>(cacheSize, null, logger, "RoundCache");
            consensusCache         = new RollingIndex <string>(cacheSize);
            participantEventsCache = new ParticipantEventsCache(cacheSize, participants, logger);
            roots     = rts;
            lastRound = -1;
        }
Пример #6
0
        public void TestRollingIndexSkip()
        {
            const int size = 10;

            const int testSize = 25;

            var rollingIndex = new RollingIndex <string>(size);

            var items = new List <string>();
            int i;

            for (i = 0; i < testSize; i++)
            {
                var item = $"item {i}";

                rollingIndex.Add(item, i);

                items.Add(item);
            }


            var(_, err) = rollingIndex.Get(0);
            Assert.Equal(StoreErrorType.TooLate, err.StoreErrorType);

            // 1

            var skipIndex1 = 9;

            var expected1 = items.Skip(skipIndex1 + 1).ToArray();

            var(cached1, err1) = rollingIndex.Get(skipIndex1);
            Assert.Null(err1);


            var convertedItems = new List <string>();

            foreach (var it in cached1)
            {
                convertedItems.Add(it);
            }
            convertedItems.ToArray().ShouldCompareTo(expected1);

            // 2

            var skipIndex2 = 15;

            var expected2 = items.Skip(skipIndex2 + 1).ToArray();

            var(cached2, err2) = rollingIndex.Get(skipIndex2);
            Assert.Null(err2);

            convertedItems = new List <string>();
            foreach (var it in cached2)
            {
                convertedItems.Add(it);
            }
            convertedItems.ToArray().ShouldCompareTo(expected2);

            // 3

            var skipIndex3 = 27;

            string[] expected3 = { };
            var(cached3, err3) = rollingIndex.Get(skipIndex3);

            Assert.Null(err3);

            convertedItems = new List <string>();
            foreach (var it in cached3)
            {
                convertedItems.Add(it);
            }
            convertedItems.ToArray().ShouldCompareTo(expected3);
        }
Пример #7
0
        public void TestRollingIndex()
        {
            const int size = 10;

            const int testSize = 3 * size;

            var rollingIndex = new RollingIndex <string>(size);

            var items = new List <string>();
            int i;

            string item;

            for (i = 0; i < testSize; i++)
            {
                item = $"item {i}";

                rollingIndex.Add(item, i);

                items.Add(item);
            }
            var(cached, lastIndex) = rollingIndex.GetLastWindow();

            var expectedLastIndex = testSize - 1;

            // last index
            Assert.Equal(expectedLastIndex, lastIndex);

            var start = testSize / (2 * size) * size;
            var count = testSize - start;

            for (i = 0; i < count; i++)
            {
                //Console.WriteLine("{0},{1}", items[start + i], cached[i]);
                Assert.Equal(items[start + i], cached[i]);
            }


            var err = rollingIndex.Add("PassedIndex", expectedLastIndex - 1);

            Assert.Equal(StoreErrorType.PassedIndex, err.StoreErrorType);


            err = rollingIndex.Add("PassedIndex", expectedLastIndex + 2);
            Assert.Equal(StoreErrorType.SkippedIndex, err.StoreErrorType);


            (_, err) = rollingIndex.GetItem(9);
            Assert.Equal(StoreErrorType.TooLate, err.StoreErrorType);


            var indexes = new[] { 10, 17, 29 };

            foreach (var id in indexes)
            {
                (item, err) = rollingIndex.GetItem(id);

                Assert.Null(err);

                item.ShouldCompareTo(items[id]);
            }


            (_, err) = rollingIndex.GetItem(lastIndex + 1);
            Assert.Equal(StoreErrorType.KeyNotFound, err.StoreErrorType);
        }