-
-
Notifications
You must be signed in to change notification settings - Fork 506
Include ref.parent matches in events by-reference navigation
#2280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
f09562e
8f5e641
ae3cc52
311c5d5
6f152ed
775c14f
0e94773
5f0dd46
6942014
a507d66
85e61ce
3e5f263
d7a0b37
2991ef0
5d986d8
11f79ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| using System.Diagnostics; | ||
| using Elastic.Clients.Elasticsearch; | ||
| using Exceptionless.Core.Models; | ||
| using Exceptionless.Core.Repositories.Configuration; | ||
| using Foundatio.Repositories.Elasticsearch.Extensions; | ||
| using Foundatio.Repositories.Migrations; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Exceptionless.Core.Migrations; | ||
|
|
||
| public sealed class BackfillParentReferences : MigrationBase | ||
| { | ||
| private readonly ElasticsearchClient _client; | ||
| private readonly ExceptionlessElasticConfiguration _config; | ||
| private readonly TimeProvider _timeProvider; | ||
|
|
||
| public BackfillParentReferences(ExceptionlessElasticConfiguration configuration, TimeProvider timeProvider, ILoggerFactory loggerFactory) : base(loggerFactory) | ||
| { | ||
| _config = configuration; | ||
| _client = configuration.Client; | ||
| _timeProvider = timeProvider; | ||
|
|
||
| MigrationType = MigrationType.VersionedAndResumable; | ||
| Version = 3; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When this ships to an environment that has already run the existing AGENTS.md reference: AGENTS.md:L72-L74 Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| public override async Task RunAsync(MigrationContext context) | ||
| { | ||
| string referenceKey = $"@ref:{Event.KnownReferenceNames.Parent}"; | ||
| string indexKey = $"{Event.KnownReferenceNames.Parent}-r"; | ||
| string script = $"if (ctx._source.data != null && ctx._source.data.containsKey('{referenceKey}') && ctx._source.data['{referenceKey}'] != null) {{ if (ctx._source.idx == null) ctx._source.idx = [:]; ctx._source.idx['{indexKey}'] = ctx._source.data['{referenceKey}']; }} else {{ ctx.op = 'noop'; }}"; | ||
|
niemyjski marked this conversation as resolved.
Outdated
|
||
|
|
||
| _logger.LogInformation("Backfilling retained event parent references"); | ||
| var stopwatch = Stopwatch.StartNew(); | ||
| var response = await _client.UpdateByQueryAsync<PersistentEvent>(request => request | ||
| .Indices($"{_config.Events.VersionedName}-*") | ||
| .Query(query => query.Bool(filter => filter.MustNot(mustNot => mustNot.Exists(exists => exists.Field($"idx.{indexKey}"))))) | ||
| .Script(value => value.Source(script).Lang(ScriptLanguage.Painless)) | ||
| .Conflicts(Conflicts.Proceed) | ||
| .WaitForCompletion(false)); | ||
| _logger.LogRequest(response, LogLevel.Information); | ||
|
|
||
| if (!response.IsValidResponse || response.Task is null) | ||
| throw new ApplicationException($"Unable to start parent-reference backfill: {response.DebugInformation}"); | ||
|
|
||
| int attempts = 0; | ||
| while (!context.CancellationToken.IsCancellationRequested) | ||
| { | ||
| var taskStatus = await _client.Tasks.GetAsync(response.Task.FullyQualifiedId, context.CancellationToken); | ||
| if (!taskStatus.IsValidResponse) | ||
| throw new ApplicationException($"Unable to monitor parent-reference backfill: {taskStatus.DebugInformation}"); | ||
|
|
||
| if (taskStatus.Completed) | ||
| { | ||
| _logger.LogInformation("Finished parent-reference backfill: Duration={Duration}", stopwatch.Elapsed); | ||
| return; | ||
| } | ||
|
|
||
| attempts++; | ||
| await context.Lock.RenewAsync(); | ||
| await Task.Delay(TimeSpan.FromSeconds(attempts <= 5 ? 1 : 5), _timeProvider, context.CancellationToken); | ||
| } | ||
|
|
||
| context.CancellationToken.ThrowIfCancellationRequested(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| using Exceptionless.Core.Migrations; | ||
| using Exceptionless.Core.Models; | ||
| using Exceptionless.Core.Repositories; | ||
| using Exceptionless.Tests.Utility; | ||
| using Foundatio.Lock; | ||
| using Foundatio.Repositories; | ||
| using Foundatio.Repositories.Migrations; | ||
| using Foundatio.Utility; | ||
| using Xunit; | ||
|
|
||
| namespace Exceptionless.Tests.Migrations; | ||
|
|
||
| public sealed class BackfillParentReferencesMigrationTests : IntegrationTestsBase | ||
| { | ||
| private readonly EventData _eventData; | ||
| private readonly IEventRepository _eventRepository; | ||
|
|
||
| public BackfillParentReferencesMigrationTests(ITestOutputHelper output, AppWebHostFactory factory) : base(output, factory) | ||
| { | ||
| _eventData = GetService<EventData>(); | ||
| _eventRepository = GetService<IEventRepository>(); | ||
| } | ||
|
|
||
| protected override void RegisterServices(IServiceCollection services) | ||
| { | ||
| services.AddTransient<BackfillParentReferences>(); | ||
| services.AddSingleton<ILock>(EmptyLock.Empty); | ||
| base.RegisterServices(services); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WillBackfillRetainedParentReference() | ||
| { | ||
| var ev = _eventData.GenerateEvent(organizationId: TestConstants.OrganizationId, projectId: TestConstants.ProjectId, stackId: TestConstants.StackId, generateData: false, occurrenceDate: TimeProvider.GetUtcNow()); | ||
| ev.Data = new() { [$"@ref:{Event.KnownReferenceNames.Parent}"] = "parent-reference" }; | ||
| ev.Idx = null; | ||
| await _eventRepository.AddAsync(ev, options => options.ImmediateConsistency()); | ||
|
|
||
| var before = await _eventRepository.FindAsync(query => query.FieldEquals("idx.parent-r", "parent-reference")); | ||
| Assert.Empty(before.Documents); | ||
|
|
||
| var migration = GetService<BackfillParentReferences>(); | ||
| var context = new MigrationContext(GetService<ILock>(), _logger, TestCancellationToken); | ||
| await migration.RunAsync(context); | ||
| await RefreshDataAsync(); | ||
|
|
||
| var after = await _eventRepository.FindAsync(query => query.FieldEquals("idx.parent-r", "parent-reference")); | ||
| Assert.Equal(ev.Id, Assert.Single(after.Documents).Id); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.