private void GetMetricValue()
        {
            if (DateTime.Now.Subtract(lastPoll).TotalSeconds > _pollingIntervalInSeconds)
            {
                _lastDelay = 0;
                var lastCommitDoc = _commitsCollection.FindAll()
                                    .SetSortOrder(SortBy.Descending("_id"))
                                    .SetFields(Fields.Include("_id"))
                                    .FirstOrDefault();
                if (lastCommitDoc == null)
                {
                    return;
                }

                var lastCommit = lastCommitDoc["_id"].AsInt64;

                //db.checkpoints.aggregate(
                //[
                //    {$match : {"Active": true}}
                //    ,{$project : {"Slot" : 1, "Current" : 1}}
                //    ,{$group : {"_id" : "$Slot", "Current" : {$min : "$Current"}}}
                //])
                BsonDocument[] pipeline =
                {
                    BsonDocument.Parse(@"{$match : {""Active"": true}}"),
                    BsonDocument.Parse(@"{$project : {""Slot"" : 1, ""Current"" : 1}}"),
                    BsonDocument.Parse(@"{$group : {""_id"" : ""$Slot"", ""Current"" : {$min : ""$Current""}}}")
                };
                AggregateArgs args = new AggregateArgs();
                args.Pipeline     = pipeline;
                args.AllowDiskUse = true; //important for large file systems

                var allCheckpoints = _checkpointCollection.Aggregate(args);
                foreach (BsonDocument metric in allCheckpoints)
                {
                    var   slotName = metric["_id"].AsString;
                    Int64 current;
                    if (!metric["Current"].IsBsonNull)
                    {
                        current = Int64.Parse(metric["Current"].AsString);
                    }
                    else
                    {
                        current = 0;
                    }

                    var delay = lastCommit - current;
                    if (delay > _lastDelay)
                    {
                        _lastDelay = delay;
                    }
                    _lastMetrics[slotName] = new SlotStatus(slotName, delay);
                }
                lastPoll = DateTime.Now;
            }
        }
 static SlotStatus()
 {
     Null = new SlotStatus("", 0);
 }
        private void GetMetricValue()
        {
            try
            {
                if (Interlocked.CompareExchange(ref _isRetrievingData, 1, 0) == 0 &&
                    DateTime.Now.Subtract(lastPoll).TotalSeconds > _pollingIntervalInSeconds)
                {
                    if (_eventStoreDatabase.Client.Cluster.Description.State != ClusterState.Connected ||
                        _readModelDatabase.Client.Cluster.Description.State != ClusterState.Connected)
                    {
                        //database is down, we cannot read values.
                        lastPoll = DateTime.Now;
                        return;
                    }

                    _lastDelay = 0;

                    var lastCommitDoc = _commitsCollection
                                        .FindAll()
                                        .Sort(Builders <BsonDocument> .Sort.Descending("_id"))
                                        .Project(Builders <BsonDocument> .Projection.Include("_id"))
                                        .FirstOrDefault();
                    if (lastCommitDoc == null)
                    {
                        return;
                    }

                    var lastCommit = lastCommitDoc["_id"].AsInt64;

                    BsonDocument[] pipeline =
                    {
                        BsonDocument.Parse(@"{""Active"": true}"),
                        BsonDocument.Parse(@"{""Slot"" : 1, ""Current"" : 1}"),
                        BsonDocument.Parse(@"{""_id"" : ""$Slot"", ""Current"" : {$min : ""$Current""}}")
                    };

                    var options = new AggregateOptions();
                    options.AllowDiskUse = true;
                    options.UseCursor    = true;
                    var allCheckpoints = _checkpointCollection.Aggregate(options)
                                         .Match(pipeline[0])
                                         .Project(pipeline[1])
                                         .Group(pipeline[2])
                                         .ToList();
                    foreach (BsonDocument metric in allCheckpoints)
                    {
                        var   slotName = metric["_id"].AsString;
                        Int64 current;
                        if (!metric["Current"].IsBsonNull)
                        {
                            current = metric["Current"].AsInt64;
                        }
                        else
                        {
                            current = 0;
                        }

                        var delay = lastCommit - current;
                        if (delay > _lastDelay)
                        {
                            _lastDelay = delay;
                        }
                        _lastMetrics[slotName] = new SlotStatus(slotName, delay);
                    }
                    lastPoll = DateTime.Now;
                }
            }
            finally
            {
                Interlocked.Exchange(ref _isRetrievingData, 0);
            }
        }
Ejemplo n.º 4
0
        private void UpdateSlot(long lastCommit, string slotName, long current)
        {
            var delay = lastCommit - current;

            _lastMetrics[slotName] = new SlotStatus(slotName, delay);
        }