Conversation
Fix RabbitMQ consuming body reusing (dotnetcore#1727)
If the name differs, we keep the old index.
… existing key patterns
|
Please explain:
|
|
There was a problem hiding this comment.
Pull Request Overview
This PR implements safe MongoDB index creation to ensure backward compatibility with previous versions that may have created indexes with different names but identical key patterns. The change prevents duplicate indexes with the same key pattern from being created.
- Replaces direct
CreateManyAsynccalls with a new helper method that checks for existing index key patterns - Removes duplicate
StatusNameindex entries from both published and received message index arrays - Adds logic to compare index key patterns rather than index names when determining if an index already exists
Comments suppressed due to low confidence (2)
src/DotNetCore.CAP.MongoDB/IStorageInitializer.MongoDB.cs:1
- The standalone
StatusNameindex entries are removed, but there's still a composite indexStatusName.Ascending(x => x.ExpiresAt)on lines 105 and 123. This removal may break existing queries that rely on the standaloneStatusNameindex for optimal performance.
// Copyright (c) .NET Core Community. All rights reserved.
src/DotNetCore.CAP.MongoDB/IStorageInitializer.MongoDB.cs:1
- The standalone
StatusNameindex entries are removed, but there's still a composite indexStatusName.Ascending(x => x.ExpiresAt)on lines 105 and 123. This removal may break existing queries that rely on the standaloneStatusNameindex for optimal performance.
// Copyright (c) .NET Core Community. All rights reserved.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| var indexes = await indexList.ToListAsync(cancellationToken); | ||
|
|
||
| var existingIndexes = new HashSet<string>( | ||
| indexes.Select(i => GetIndexKeyPattern(i["key"].AsBsonDocument)) |
There was a problem hiding this comment.
Accessing i["key"] without checking if the key exists could throw a KeyNotFoundException if an index document doesn't have a 'key' field. Add a null check or use TryGetValue to handle malformed index documents safely.
| indexes.Select(i => GetIndexKeyPattern(i["key"].AsBsonDocument)) | |
| indexes | |
| .Select(i => i.TryGetValue("key", out var keyValue) && keyValue != null | |
| ? GetIndexKeyPattern(keyValue.AsBsonDocument) | |
| : null) | |
| .Where(pattern => pattern != null) |
|
@yang-xiaodong LGTM, this may ba a problem if you are not doing any index maintenance. FYI even in case there will be a runtime exception, it will not break initialization, will just log it. |
|
@demorgi yes, agree. lets make it simple |
Description:
The change creates indexes if an index with the same key pattern does not already exist.
This ensures backward compatibility with previous versions that created indexes with different names but the same key pattern.
Affected components: