예제 #1
0
        private async Task ApplyRetention(
            DocumentsOperationContext context,
            TimeSeriesCollectionConfiguration config,
            CollectionName collectionName,
            TimeSeriesPolicy policy,
            DateTime now)
        {
            var tss = context.DocumentDatabase.DocumentsStorage.TimeSeriesStorage;

            if (policy.RetentionTime == TimeValue.MaxValue)
            {
                return;
            }

            var to   = now.Add(-policy.RetentionTime);
            var list = new List <Slice>();

            while (true)
            {
                Cts.Token.ThrowIfCancellationRequested();

                context.Reset();
                context.Renew();
                list.Clear();

                using (context.OpenReadTransaction())
                {
                    foreach (var item in tss.Stats.GetTimeSeriesByPolicyFromStartDate(context, collectionName, policy.Name, to, TimeSeriesRollups.TimeSeriesRetentionCommand.BatchSize))
                    {
                        if (RequiredForNextPolicy(context, config, policy, item, to))
                        {
                            continue;
                        }

                        if (tss.Rollups.HasPendingRollupFrom(context, item, to) == false)
                        {
                            list.Add(item);
                        }
                    }

                    if (list.Count == 0)
                    {
                        return;
                    }

                    if (Logger.IsInfoEnabled)
                    {
                        Logger.Info($"Found {list.Count} time-series for retention in policy {policy.Name} with collection '{collectionName.Name}' up-to {to}"
#if DEBUG
                                    + $"{Environment.NewLine}{string.Join(Environment.NewLine, list)}"
#endif

                                    );
                    }

                    var cmd = new TimeSeriesRollups.TimeSeriesRetentionCommand(list, collectionName.Name, to);
                    await _database.TxMerger.Enqueue(cmd);
                }
            }
        }