-
Notifications
You must be signed in to change notification settings - Fork 48
Add tests to make sure that indexes being locked prevents index resets on instance restarts #4849
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5fd9e63
Initial cleanup
andreasohlund 1fe5c7c
Add tests to verify default behavior
andreasohlund c8bf6be
Add test to check that indexes are reset on setup
andreasohlund 0b97945
Add lock mode tests
andreasohlund 7764a16
Add test for non free text search
andreasohlund 0aa1060
Handle race conditions
andreasohlund 8818f4a
Use async wait
andreasohlund 943e0c5
Add test cancellation
andreasohlund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
192 changes: 99 additions & 93 deletions
192
src/ServiceControl.Audit.Persistence.RavenDB/DatabaseSetup.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,120 +1,126 @@ | ||
| namespace ServiceControl.Audit.Persistence.RavenDB | ||
| namespace ServiceControl.Audit.Persistence.RavenDB; | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Raven.Client.Documents; | ||
| using Raven.Client.Documents.Indexes; | ||
| using Raven.Client.Documents.Operations.Expiration; | ||
| using Raven.Client.Documents.Operations.Indexes; | ||
| using Raven.Client.Exceptions; | ||
| using Raven.Client.ServerWide; | ||
| using Raven.Client.ServerWide.Operations; | ||
| using Raven.Client.ServerWide.Operations.Configuration; | ||
| using Indexes; | ||
| using SagaAudit; | ||
|
|
||
| class DatabaseSetup(DatabaseConfiguration configuration) | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Raven.Client.Documents; | ||
| using Raven.Client.Documents.Indexes; | ||
| using Raven.Client.Documents.Operations.Expiration; | ||
| using Raven.Client.Documents.Operations.Indexes; | ||
| using Raven.Client.Exceptions; | ||
| using Raven.Client.ServerWide; | ||
| using Raven.Client.ServerWide.Operations; | ||
| using Raven.Client.ServerWide.Operations.Configuration; | ||
| using ServiceControl.Audit.Persistence.RavenDB.Indexes; | ||
| using ServiceControl.SagaAudit; | ||
|
|
||
| class DatabaseSetup(DatabaseConfiguration configuration) | ||
| public async Task Execute(IDocumentStore documentStore, CancellationToken cancellationToken) | ||
| { | ||
| public async Task Execute(IDocumentStore documentStore, CancellationToken cancellationToken) | ||
| { | ||
| await CreateDatabase(documentStore, configuration.Name, cancellationToken); | ||
| await UpdateDatabaseSettings(documentStore, configuration.Name, cancellationToken); | ||
| await CreateDatabase(documentStore, configuration.Name, cancellationToken); | ||
|
|
||
| await CreateIndexes(documentStore, cancellationToken); | ||
| await UpdateDatabaseSettings(documentStore, configuration.Name, cancellationToken); | ||
|
|
||
| await ConfigureExpiration(documentStore, cancellationToken); | ||
| } | ||
| await CreateIndexes(documentStore, configuration.EnableFullTextSearch, cancellationToken); | ||
|
|
||
| async Task CreateDatabase(IDocumentStore documentStore, string databaseName, CancellationToken cancellationToken) | ||
| { | ||
| var dbRecord = await documentStore.Maintenance.Server.SendAsync(new GetDatabaseRecordOperation(databaseName), cancellationToken); | ||
| await ConfigureExpiration(documentStore, cancellationToken); | ||
| } | ||
|
|
||
| if (dbRecord is null) | ||
| { | ||
| try | ||
| { | ||
| var databaseRecord = new DatabaseRecord(databaseName); | ||
| databaseRecord.Settings.Add("Indexing.Auto.SearchEngineType", "Corax"); | ||
| databaseRecord.Settings.Add("Indexing.Static.SearchEngineType", "Corax"); | ||
|
|
||
| await documentStore.Maintenance.Server.SendAsync(new CreateDatabaseOperation(databaseRecord), cancellationToken); | ||
| } | ||
| catch (ConcurrencyException) | ||
| { | ||
| // The database was already created before calling CreateDatabaseOperation | ||
| } | ||
| } | ||
| } | ||
| async Task CreateDatabase(IDocumentStore documentStore, string databaseName, CancellationToken cancellationToken) | ||
| { | ||
| var dbRecord = await documentStore.Maintenance.Server.SendAsync(new GetDatabaseRecordOperation(databaseName), cancellationToken); | ||
|
|
||
| async Task UpdateDatabaseSettings(IDocumentStore documentStore, string databaseName, CancellationToken cancellationToken) | ||
| if (dbRecord is null) | ||
| { | ||
| var dbRecord = await documentStore.Maintenance.Server.SendAsync(new GetDatabaseRecordOperation(databaseName), cancellationToken); | ||
|
|
||
| if (dbRecord is null) | ||
| try | ||
| { | ||
| throw new InvalidOperationException($"Database '{databaseName}' does not exist."); | ||
| } | ||
|
|
||
| var updated = false; | ||
| var databaseRecord = new DatabaseRecord(databaseName); | ||
|
|
||
| updated |= dbRecord.Settings.TryAdd("Indexing.Auto.SearchEngineType", "Corax"); | ||
| updated |= dbRecord.Settings.TryAdd("Indexing.Static.SearchEngineType", "Corax"); | ||
| SetSearchEngineType(databaseRecord, SearchEngineType.Corax); | ||
|
|
||
| if (updated) | ||
| await documentStore.Maintenance.Server.SendAsync(new CreateDatabaseOperation(databaseRecord), cancellationToken); | ||
| } | ||
| catch (ConcurrencyException) | ||
| { | ||
| await documentStore.Maintenance.ForDatabase(databaseName).SendAsync(new PutDatabaseSettingsOperation(databaseName, dbRecord.Settings), cancellationToken); | ||
| await documentStore.Maintenance.Server.SendAsync(new ToggleDatabasesStateOperation(databaseName, true), cancellationToken); | ||
| await documentStore.Maintenance.Server.SendAsync(new ToggleDatabasesStateOperation(databaseName, false), cancellationToken); | ||
| // The database was already created before calling CreateDatabaseOperation | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static async Task DeleteLegacySagaDetailsIndex(IDocumentStore documentStore, CancellationToken cancellationToken) | ||
| async Task UpdateDatabaseSettings(IDocumentStore documentStore, string databaseName, CancellationToken cancellationToken) | ||
| { | ||
| var databaseRecord = await documentStore.Maintenance.Server.SendAsync(new GetDatabaseRecordOperation(databaseName), cancellationToken) ?? throw new InvalidOperationException($"Database '{databaseName}' does not exist."); | ||
|
|
||
| if (!SetSearchEngineType(databaseRecord, SearchEngineType.Corax)) | ||
| { | ||
| // If the SagaDetailsIndex exists but does not have a .Take(50000), then we remove the current SagaDetailsIndex and | ||
| // create a new one. If we do not remove the current one, then RavenDB will attempt to do a side-by-side migration. | ||
| // Doing a side-by-side migration results in the index never swapping if there is constant ingestion as RavenDB will wait. | ||
| // for the index to not be stale before swapping to the new index. Constant ingestion means the index will never be not-stale. | ||
| // This needs to stay in place until the next major version as the user could upgrade from an older version of the current | ||
| // Major (v5.x.x) which might still have the incorrect index. | ||
| var sagaDetailsIndexOperation = new GetIndexOperation("SagaDetailsIndex"); | ||
| var sagaDetailsIndexDefinition = await documentStore.Maintenance.SendAsync(sagaDetailsIndexOperation, cancellationToken); | ||
| if (sagaDetailsIndexDefinition != null && !sagaDetailsIndexDefinition.Reduce.Contains("Take(50000)")) | ||
| { | ||
| await documentStore.Maintenance.SendAsync(new DeleteIndexOperation("SagaDetailsIndex"), cancellationToken); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| async Task CreateIndexes(IDocumentStore documentStore, CancellationToken cancellationToken) | ||
| await documentStore.Maintenance.ForDatabase(databaseName).SendAsync(new PutDatabaseSettingsOperation(databaseName, databaseRecord.Settings), cancellationToken); | ||
| await documentStore.Maintenance.Server.SendAsync(new ToggleDatabasesStateOperation(databaseName, true), cancellationToken); | ||
| await documentStore.Maintenance.Server.SendAsync(new ToggleDatabasesStateOperation(databaseName, false), cancellationToken); | ||
| } | ||
|
|
||
| public static async Task DeleteLegacySagaDetailsIndex(IDocumentStore documentStore, CancellationToken cancellationToken) | ||
| { | ||
| // If the SagaDetailsIndex exists but does not have a .Take(50000), then we remove the current SagaDetailsIndex and | ||
| // create a new one. If we do not remove the current one, then RavenDB will attempt to do a side-by-side migration. | ||
| // Doing a side-by-side migration results in the index never swapping if there is constant ingestion as RavenDB will wait. | ||
| // for the index to not be stale before swapping to the new index. Constant ingestion means the index will never be not-stale. | ||
| // This needs to stay in place until the next major version as the user could upgrade from an older version of the current | ||
| // Major (v5.x.x) which might still have the incorrect index. | ||
| var sagaDetailsIndexOperation = new GetIndexOperation(SagaDetailsIndexName); | ||
| var sagaDetailsIndexDefinition = await documentStore.Maintenance.SendAsync(sagaDetailsIndexOperation, cancellationToken); | ||
| if (sagaDetailsIndexDefinition != null && !sagaDetailsIndexDefinition.Reduce.Contains("Take(50000)")) | ||
| { | ||
| await DeleteLegacySagaDetailsIndex(documentStore, cancellationToken); | ||
| await documentStore.Maintenance.SendAsync(new DeleteIndexOperation(SagaDetailsIndexName), cancellationToken); | ||
| } | ||
| } | ||
|
|
||
| List<AbstractIndexCreationTask> indexList = [new FailedAuditImportIndex(), new SagaDetailsIndex()]; | ||
| internal static async Task CreateIndexes(IDocumentStore documentStore, bool enableFreeTextSearch, CancellationToken cancellationToken) | ||
| { | ||
| await DeleteLegacySagaDetailsIndex(documentStore, cancellationToken); | ||
|
|
||
| if (configuration.EnableFullTextSearch) | ||
| { | ||
| indexList.Add(new MessagesViewIndexWithFullTextSearch()); | ||
| await documentStore.Maintenance.SendAsync(new DeleteIndexOperation("MessagesViewIndex"), cancellationToken); | ||
| } | ||
| else | ||
| { | ||
| indexList.Add(new MessagesViewIndex()); | ||
| await documentStore.Maintenance.SendAsync(new DeleteIndexOperation("MessagesViewIndexWithFullTextSearch"), cancellationToken); | ||
| } | ||
| List<AbstractIndexCreationTask> indexList = [new FailedAuditImportIndex(), new SagaDetailsIndex()]; | ||
|
|
||
| await IndexCreation.CreateIndexesAsync(indexList, documentStore, null, null, cancellationToken); | ||
| if (enableFreeTextSearch) | ||
| { | ||
| indexList.Add(new MessagesViewIndexWithFullTextSearch()); | ||
| await documentStore.Maintenance.SendAsync(new DeleteIndexOperation(MessagesViewIndexName), cancellationToken); | ||
| } | ||
| else | ||
| { | ||
| indexList.Add(new MessagesViewIndex()); | ||
| await documentStore.Maintenance.SendAsync(new DeleteIndexOperation(MessagesViewIndexWithFulltextSearchName), cancellationToken); | ||
| } | ||
|
|
||
| await IndexCreation.CreateIndexesAsync(indexList, documentStore, null, null, cancellationToken); | ||
| } | ||
|
|
||
| async Task ConfigureExpiration(IDocumentStore documentStore, CancellationToken cancellationToken) | ||
| async Task ConfigureExpiration(IDocumentStore documentStore, CancellationToken cancellationToken) | ||
| { | ||
| var expirationConfig = new ExpirationConfiguration | ||
| { | ||
| var expirationConfig = new ExpirationConfiguration | ||
| { | ||
| Disabled = false, | ||
| DeleteFrequencyInSec = configuration.ExpirationProcessTimerInSeconds | ||
| }; | ||
| Disabled = false, | ||
| DeleteFrequencyInSec = configuration.ExpirationProcessTimerInSeconds | ||
| }; | ||
|
|
||
| await documentStore.Maintenance.SendAsync(new ConfigureExpirationOperation(expirationConfig), cancellationToken); | ||
| } | ||
| await documentStore.Maintenance.SendAsync(new ConfigureExpirationOperation(expirationConfig), cancellationToken); | ||
| } | ||
|
|
||
| bool SetSearchEngineType(DatabaseRecord database, SearchEngineType searchEngineType) | ||
| { | ||
| var updated = false; | ||
|
|
||
| updated |= database.Settings.TryAdd("Indexing.Auto.SearchEngineType", searchEngineType.ToString()); | ||
| updated |= database.Settings.TryAdd("Indexing.Static.SearchEngineType", searchEngineType.ToString()); | ||
|
|
||
| return updated; | ||
| } | ||
| } | ||
|
|
||
| internal const string MessagesViewIndexWithFulltextSearchName = "MessagesViewIndexWithFullTextSearch"; | ||
| internal const string SagaDetailsIndexName = "SagaDetailsIndex"; | ||
| internal const string MessagesViewIndexName = "MessagesViewIndex"; | ||
| } |
121 changes: 121 additions & 0 deletions
121
src/ServiceControl.Audit.Persistence.Tests.RavenDB/IndexSetupTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| namespace ServiceControl.Audit.Persistence.Tests; | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using NUnit.Framework; | ||
| using Persistence.RavenDB; | ||
| using Persistence.RavenDB.Indexes; | ||
| using Raven.Client.Documents.Indexes; | ||
| using Raven.Client.Documents.Operations.Indexes; | ||
| using Raven.Client.Exceptions.Documents.Indexes; | ||
|
|
||
| [TestFixture] | ||
| class IndexSetupTests : PersistenceTestFixture | ||
| { | ||
| [Test] | ||
| public async Task Corax_should_be_the_default_search_engine_type() | ||
| { | ||
| var indexes = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexesOperation(0, int.MaxValue)); | ||
|
|
||
| foreach (var index in indexes) | ||
| { | ||
| var indexStats = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(DatabaseSetup.MessagesViewIndexWithFulltextSearchName)); | ||
| Assert.That(indexStats.SearchEngineType, Is.EqualTo(SearchEngineType.Corax), $"{index.Name} is not using Corax"); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Free_text_search_index_should_be_used_by_default() | ||
| { | ||
| var freeTextIndex = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexOperation(DatabaseSetup.MessagesViewIndexWithFulltextSearchName)); | ||
| var nonFreeTextIndex = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexOperation(DatabaseSetup.MessagesViewIndexName)); | ||
|
|
||
| Assert.That(nonFreeTextIndex, Is.Null); | ||
| Assert.That(freeTextIndex, Is.Not.Null); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Free_text_search_index_can_be_opted_out_from() | ||
| { | ||
| await DatabaseSetup.CreateIndexes(configuration.DocumentStore, false, CancellationToken.None); | ||
|
|
||
| //TODO: find a better way | ||
| await Task.Delay(1000); | ||
|
|
||
| var freeTextIndex = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexOperation(DatabaseSetup.MessagesViewIndexWithFulltextSearchName)); | ||
| var nonFreeTextIndex = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexOperation(DatabaseSetup.MessagesViewIndexName)); | ||
|
|
||
| Assert.That(freeTextIndex, Is.Null); | ||
| Assert.That(nonFreeTextIndex, Is.Not.Null); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Indexes_should_be_reset_on_setup() | ||
| { | ||
| var index = new MessagesViewIndexWithFullTextSearch { Configuration = { ["Indexing.Static.SearchEngineType"] = SearchEngineType.Lucene.ToString() } }; | ||
|
|
||
| await IndexCreation.CreateIndexesAsync([index], configuration.DocumentStore); | ||
|
|
||
| //TODO: find a better way | ||
| await Task.Delay(1000); | ||
andreasohlund marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| var indexStatsBefore = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName)); | ||
|
|
||
| Assert.That(indexStatsBefore.SearchEngineType, Is.EqualTo(SearchEngineType.Lucene)); | ||
|
|
||
| await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, CancellationToken.None); | ||
|
|
||
| //TODO: find a better way | ||
| await Task.Delay(1000); | ||
|
|
||
| var indexStatsAfter = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName)); | ||
| Assert.That(indexStatsAfter.SearchEngineType, Is.EqualTo(SearchEngineType.Corax)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Indexes_should_not_be_reset_on_setup_when_locked_as_ignore() | ||
| { | ||
| var index = new MessagesViewIndexWithFullTextSearch { Configuration = { ["Indexing.Static.SearchEngineType"] = SearchEngineType.Lucene.ToString() } }; | ||
|
|
||
| await IndexCreation.CreateIndexesAsync([index], configuration.DocumentStore); | ||
|
|
||
| await configuration.DocumentStore.Maintenance.SendAsync(new SetIndexesLockOperation(new SetIndexesLockOperation.Parameters | ||
| { | ||
| IndexNames = [index.IndexName], | ||
| Mode = IndexLockMode.LockedIgnore | ||
| })); | ||
|
|
||
| //TODO: find a better way | ||
| await Task.Delay(1000); | ||
|
|
||
| var indexStatsBefore = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName)); | ||
|
|
||
| Assert.That(indexStatsBefore.SearchEngineType, Is.EqualTo(SearchEngineType.Lucene)); | ||
|
|
||
|
|
||
| await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, CancellationToken.None); | ||
|
|
||
| //TODO: find a better way | ||
| await Task.Delay(1000); | ||
|
|
||
| var indexStatsAfter = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName)); | ||
| Assert.That(indexStatsAfter.SearchEngineType, Is.EqualTo(SearchEngineType.Lucene)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Indexes_should_not_be_reset_on_setup_when_locked_as_error() | ||
| { | ||
| var index = new MessagesViewIndexWithFullTextSearch { Configuration = { ["Indexing.Static.SearchEngineType"] = SearchEngineType.Lucene.ToString() } }; | ||
|
|
||
| await IndexCreation.CreateIndexesAsync([index], configuration.DocumentStore); | ||
|
|
||
| await configuration.DocumentStore.Maintenance.SendAsync(new SetIndexesLockOperation(new SetIndexesLockOperation.Parameters | ||
| { | ||
| IndexNames = [index.IndexName], | ||
| Mode = IndexLockMode.LockedError | ||
| })); | ||
|
|
||
| Assert.ThrowsAsync<IndexCreationException>(async () => await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, CancellationToken.None)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.