Skip to content

Commit

Permalink
Address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Jan 6, 2025
1 parent b1bbb5c commit f82862a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand All @@ -7,7 +8,6 @@
using System.Threading.Tasks;
using Exceptionless.DateTimeExtensions;
using Foundatio.Caching;
using Foundatio.Lock;
using Foundatio.Parsers.ElasticQueries;
using Foundatio.Parsers.ElasticQueries.Extensions;
using Foundatio.Repositories.Elasticsearch.Extensions;
Expand All @@ -31,16 +31,14 @@ public class DailyIndex : VersionedIndex
private TimeSpan? _maxIndexAge;
protected readonly Func<object, DateTime> _getDocumentDateUtc;
protected readonly string[] _defaultIndexes;
private readonly CacheLockProvider _ensureIndexLock;
private readonly Dictionary<DateTime, object> _ensuredDates = new();
private readonly ConcurrentDictionary<DateTime, object> _ensuredDates = new();

public DailyIndex(IElasticConfiguration configuration, string name, int version = 1, Func<object, DateTime> getDocumentDateUtc = null)
: base(configuration, name, version)
{
AddAlias(Name);
_frozenAliases = new Lazy<IReadOnlyCollection<IndexAliasAge>>(() => _aliases.AsReadOnly());
_aliasCache = new ScopedCacheClient(configuration.Cache, "alias");
_ensureIndexLock = new CacheLockProvider(configuration.Cache, configuration.MessageBus, configuration.LoggerFactory);
_getDocumentDateUtc = getDocumentDateUtc;
_defaultIndexes = new[] { Name };
HasMultipleIndexes = true;
Expand Down Expand Up @@ -141,13 +139,6 @@ protected async Task EnsureDateIndexAsync(DateTime utcDate)
if (_ensuredDates.ContainsKey(utcDate))
return;

await using var indexLock = await _ensureIndexLock.AcquireAsync($"Index:{GetVersionedIndex(utcDate)}", TimeSpan.FromMinutes(1)).AnyContext();
if (indexLock is null)
throw new Exception("Unable to acquire index lock");

if (_ensuredDates.ContainsKey(utcDate))
return;

var indexExpirationUtcDate = GetIndexExpirationDate(utcDate);
if (Configuration.TimeProvider.GetUtcNow().UtcDateTime > indexExpirationUtcDate)
throw new ArgumentException($"Index max age exceeded: {indexExpirationUtcDate}", nameof(utcDate));
Expand Down Expand Up @@ -175,10 +166,10 @@ await CreateIndexAsync(index, descriptor =>
foreach (var a in Aliases.Where(a => ShouldCreateAlias(utcDate, a)))
aliasesDescriptor.Alias(a.Name);

_ensuredDates[utcDate] = null;
return ConfigureIndex(descriptor).Aliases(a => aliasesDescriptor);
}).AnyContext();

_ensuredDates[utcDate] = null;
await _aliasCache.SetAsync(unversionedIndexAlias, unversionedIndexAlias, expires).AnyContext();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,12 +979,12 @@ public async Task ScriptPatchAllAsync()
};

await _dailyRepository.AddAsync(logs, o => o.Cache().ImmediateConsistency());
Assert.Equal(7, _cache.Count);
Assert.Equal(5, _cache.Count);
Assert.Equal(0, _cache.Hits);
Assert.Equal(2, _cache.Misses);

Assert.Equal(3, await _dailyRepository.IncrementValueAsync(logs.Select(l => l.Id).ToArray()));
Assert.Equal(4, _cache.Count);
Assert.Equal(2, _cache.Count);
Assert.Equal(0, _cache.Hits);
Assert.Equal(2, _cache.Misses);

Expand Down

0 comments on commit f82862a

Please sign in to comment.