Skip to content
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

RavenDB-22036 Added tests and throwing exception on side-by-side reset of replacement index #18459

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Raven.Server/Documents/Indexes/IndexStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,9 @@ private Index ResetIndexInternal(Index index, bool start = true)

private Index ResetIndexSideBySideInternal(Index index)
{
if (index.Name.StartsWith(Constants.Documents.Indexing.SideBySideIndexNamePrefix))
throw new InvalidOperationException($"Index {index.Name} is already a side-by-side running index.");

try
{
var definitionClone = new IndexDefinition();
Expand Down
156 changes: 156 additions & 0 deletions test/SlowTests/Issues/RavenDB-22036.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using FastTests;
using Raven.Client;
using Raven.Client.Documents.Indexes;
using Raven.Client.Documents.Operations.Indexes;
using Raven.Client.Exceptions;
using Raven.Server.Config;
using Tests.Infrastructure;
using Tests.Infrastructure.Extensions;
Expand Down Expand Up @@ -242,4 +244,158 @@ public void TestResetIndexOperationWithConfigurationOption(Options options)
});
}
}

[RavenTheory(RavenTestCategory.Indexes)]
[RavenData(DatabaseMode = RavenDatabaseMode.All)]
public void TestInPlaceResetOnIndexWithRunningSideBySide(Options options)
{
using (var store = GetDocumentStore(options))
{
const string indexName = "Users/ByName";

store.Maintenance.Send(new PutIndexesOperation(new IndexDefinition
{
Maps = { "from user in docs.Users select new { user.FirstName }" },
Type = IndexType.Map,
Name = indexName
}));

store.Maintenance.ForTesting(() => new StopIndexingOperation()).ExecuteOnAll();

store.Maintenance.ForTesting(() => new ResetIndexOperation(indexName, IndexResetMode.SideBySide)).ExecuteOnAll();

store.Maintenance.ForTesting(() => new GetIndexOperation($"{Constants.Documents.Indexing.SideBySideIndexNamePrefix}{indexName}")).AssertAll((key, stats) =>
{
Assert.NotNull(stats);
});

store.Maintenance.ForTesting(() => new GetIndexNamesOperation(0, int.MaxValue)).AssertAll((key, stats) =>
{
Assert.Equal(2, stats.Length);
});

store.Maintenance.ForTesting(() => new ResetIndexOperation(indexName)).ExecuteOnAll();

store.Maintenance.ForTesting(() => new GetIndexOperation($"{Constants.Documents.Indexing.SideBySideIndexNamePrefix}{indexName}")).AssertAll((key, stats) =>
{
Assert.NotNull(stats);
});

store.Maintenance.ForTesting(() => new GetIndexNamesOperation(0, int.MaxValue)).AssertAll((key, stats) =>
{
Assert.Equal(2, stats.Length);
});

store.Maintenance.ForTesting(() => new StartIndexingOperation()).ExecuteOnAll();

Indexes.WaitForIndexing(store);

store.Maintenance.ForTesting(() => new GetIndexOperation($"{Constants.Documents.Indexing.SideBySideIndexNamePrefix}{indexName}")).AssertAll((key, stats) =>
{
Assert.Null(stats);
});

store.Maintenance.ForTesting(() => new GetIndexNamesOperation(0, int.MaxValue)).AssertAll((key, stats) =>
{
Assert.Equal(1, stats.Length);
});
}
}

[RavenTheory(RavenTestCategory.Indexes)]
[RavenData(DatabaseMode = RavenDatabaseMode.All)]
public void TestInPlaceResetOfRunningSideBySideIndex(Options options)
{
using (var store = GetDocumentStore(options))
{
const string indexName = "Users/ByName";

store.Maintenance.Send(new PutIndexesOperation(new IndexDefinition
{
Maps = { "from user in docs.Users select new { user.FirstName }" },
Type = IndexType.Map,
Name = indexName
}));

store.Maintenance.ForTesting(() => new StopIndexingOperation()).ExecuteOnAll();

store.Maintenance.ForTesting(() => new ResetIndexOperation(indexName, IndexResetMode.SideBySide)).ExecuteOnAll();

store.Maintenance.ForTesting(() => new GetIndexOperation($"{Constants.Documents.Indexing.SideBySideIndexNamePrefix}{indexName}")).AssertAll((key, stats) =>
{
Assert.NotNull(stats);
});

store.Maintenance.ForTesting(() => new GetIndexNamesOperation(0, int.MaxValue)).AssertAll((key, stats) =>
{
Assert.Equal(2, stats.Length);
});

store.Maintenance.ForTesting(() => new ResetIndexOperation(Constants.Documents.Indexing.SideBySideIndexNamePrefix + indexName)).ExecuteOnAll();

store.Maintenance.ForTesting(() => new GetIndexOperation($"{Constants.Documents.Indexing.SideBySideIndexNamePrefix}{indexName}")).AssertAll((key, stats) =>
{
Assert.NotNull(stats);
});

store.Maintenance.ForTesting(() => new GetIndexNamesOperation(0, int.MaxValue)).AssertAll((key, stats) =>
{
Assert.Equal(2, stats.Length);
});

store.Maintenance.ForTesting(() => new StartIndexingOperation()).ExecuteOnAll();

Indexes.WaitForIndexing(store);

store.Maintenance.ForTesting(() => new GetIndexOperation($"{Constants.Documents.Indexing.SideBySideIndexNamePrefix}{indexName}")).AssertAll((key, stats) =>
{
Assert.Null(stats);
});

store.Maintenance.ForTesting(() => new GetIndexNamesOperation(0, int.MaxValue)).AssertAll((key, stats) =>
{
Assert.Equal(1, stats.Length);
});
}
}

[RavenTheory(RavenTestCategory.Indexes)]
[RavenData(DatabaseMode = RavenDatabaseMode.All)]
public void TestSideBySideResetOfRunningSideBySideIndex(Options options)
{
using (var store = GetDocumentStore(options))
{
const string indexName = "Users/ByName";
const string replacementIndexName = $"{Constants.Documents.Indexing.SideBySideIndexNamePrefix}{indexName}";

store.Maintenance.Send(new PutIndexesOperation(new IndexDefinition
{
Maps = { "from user in docs.Users select new { user.FirstName }" },
Type = IndexType.Map,
Name = indexName
}));

store.Maintenance.ForTesting(() => new StopIndexingOperation()).ExecuteOnAll();

store.Maintenance.ForTesting(() => new ResetIndexOperation(indexName, IndexResetMode.SideBySide)).ExecuteOnAll();

store.Maintenance.ForTesting(() => new GetIndexOperation(replacementIndexName)).AssertAll((key, stats) =>
{
Assert.NotNull(stats);
});

store.Maintenance.ForTesting(() => new GetIndexNamesOperation(0, int.MaxValue)).AssertAll((key, stats) =>
{
Assert.Equal(2, stats.Length);
});

var ex = Assert.Throws<RavenException>(() =>
store.Maintenance.ForTesting(() => new ResetIndexOperation(replacementIndexName, IndexResetMode.SideBySide)).ExecuteOnAll()
);

Assert.IsType<InvalidOperationException>(ex.InnerException);

Assert.Contains($"Index {replacementIndexName} is already a side-by-side running index.", ex.InnerException.Message);
}
}
}