GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(cosmos): support custom document IDs and partition keys by ReubenBond · Pull Request #8699 · dotnet/orleans

Conversation

@ReubenBond

@ReubenBond ReubenBond commented Nov 1, 2023 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Currently, only custom partition keys are supported. This is a compilation-breaking change, since it changes the interface used to specify the partition key. The usage of that interface was broken in previous releases, and the fix is straightforward: change your implementation of IPartitionKeyProvider to IDocumentIdProvider. Then, if you do not wish to customize the document id, inject DefaultDocumentIdProvider and call GetId to get the document id.

Microsoft Reviewers: Open in CodeFlow

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ReubenBond
ReubenBond force-pushed the fix/cosmos-doc-id-provider branch from 774631d to 5c3c5e4 Compare November 1, 2023 22:58
@OmnipotentOwl

OmnipotentOwl commented Oct 26, 2024

Copy link
Copy Markdown
Contributor

@ReubenBond, Looking ahead at the capabilities for the Microsoft.Extensions.Configuration support you implemented in #9204 should this PR include the ability to configure the type to call from DI for the IDocumentIdProvider of a given provider when configuring via IConfiguration? The wiring might not be there yet but should the configuration value be present on the CosmosGrainStorageOptions class?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ReubenBond ReubenBond changed the title Support customization of both document id and partition key for Cosmos DB grain persistence feat(cosmos)!: support custom document IDs and partition keys May 29, 2026
Copilot AI review requested due to automatic review settings August 1, 2026 15:22
Copilot started reviewing on behalf of ReubenBond August 1, 2026 15:23
View session
Copilot AI reviewed Aug 1, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Orleans Cosmos DB grain storage provider to support customizing both the document id and partition key via a new IDocumentIdProvider, replacing the prior partition-key-only abstraction (IPartitionKeyProvider) as a breaking change.

Changes:

  • Replaced IPartitionKeyProvider with IDocumentIdProvider and introduced DefaultDocumentIdProvider.
  • Updated CosmosGrainStorage to use the new provider for id/partition key generation and adjusted clear-state behavior.
  • Updated hosting extensions and Cosmos persistence tests to use the new provider.
Show a summary per file
File Description
test/Extensions/Tester.Cosmos/PersistenceProviderTests_Cosmos.cs Updates test setup to construct storage using DefaultDocumentIdProvider.
src/Azure/Orleans.Persistence.Cosmos/IPartitionKeyProvider.cs Removes the old partition-key-only provider interface and default implementation.
src/Azure/Orleans.Persistence.Cosmos/IDocumentIdProvider.cs Adds the new interface for generating document ids and partition keys.
src/Azure/Orleans.Persistence.Cosmos/HostingExtensions.cs Switches DI wiring from partition key provider to document id provider and updates extension methods.
src/Azure/Orleans.Persistence.Cosmos/DefaultDocumentIdProvider.cs Adds the default implementation for document id + partition key generation.
src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs Uses IDocumentIdProvider for read/write/clear and updates legacy /GrainType compatibility checks.

Copilot's findings

Suppressed comments (2)

src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs:369

  • The /GrainType partition key path compatibility check currently rejects any custom IDocumentIdProvider (anything other than DefaultDocumentIdProvider). That blocks custom document id support for clusters using existing containers partitioned by /GrainType, even if the custom provider returns a compatible partition key (grainType). Consider relaxing this check (or checking compatibility based on the returned partition key) so that custom document ids can be used with legacy /GrainType containers.
                if (_partitionKeyPath == GRAINTYPE_PARTITION_KEY_PATH &&
                    _documentIdProvider is not DefaultDocumentIdProvider)

src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs:370

  • This OrleansConfigurationException message still refers to a "Custom partition key provider", but the code now uses IDocumentIdProvider/DefaultDocumentIdProvider. Updating the message will make misconfiguration easier to understand.
                if (_partitionKeyPath == GRAINTYPE_PARTITION_KEY_PATH &&
                    _documentIdProvider is not DefaultDocumentIdProvider)
                    throw new OrleansConfigurationException("Custom partition key provider is not compatible with partition key path set to /GrainType");
  • Files reviewed: 6/6 changed files
  • Comments generated: 4

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/Azure/Orleans.Persistence.Cosmos/IDocumentIdProvider.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/Azure/Orleans.Persistence.Cosmos/DefaultDocumentIdProvider.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/Azure/Orleans.Persistence.Cosmos/HostingExtensions.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ReubenBond ReubenBond changed the title feat(cosmos)!: support custom document IDs and partition keys feat(cosmos): support custom document IDs and partition keys Aug 1, 2026
ReubenBond and others added 2 commits August 1, 2026 09:06
@ReubenBond
Support customization of both document id and partition key for Cosmo…
2143cdc
…s DB grain persistence
@ReubenBond
Align Cosmos storage provider semantics
7d3abdd
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: e82f060d-d09b-42f9-8b0f-34da38b386fa
Copilot AI review requested due to automatic review settings August 1, 2026 16:25
@ReubenBond
ReubenBond force-pushed the fix/cosmos-doc-id-provider branch from 7991079 to 7d3abdd Compare August 1, 2026 16:25
Copilot started reviewing on behalf of ReubenBond August 1, 2026 16:25
View session
@ReubenBond
Preserve current Cosmos storage semantics
2b0cdb2
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: e82f060d-d09b-42f9-8b0f-34da38b386fa
Copilot AI reviewed Aug 1, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

Suppressed comments (3)

src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs:178

  • After successfully deleting state (DeleteStateOnClear=true), the code clears ETag/RecordExists but does not reset grainState.State. This differs from ResetGrainState used elsewhere in the provider and can leave stale state in the activation.
                {
                    try

src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs:202

  • When DeleteStateOnClear is false, ClearStateAsync overwrites storage with an empty document and sets RecordExists=false, but it no longer resets grainState.State to a fresh instance. This can leave the activation with the pre-clear values even though the record is cleared.
                    await _executor.ExecuteOperation(static args =>
                    {
                        var (self, id, pk, requestOptions) = args;
                        return self._container.DeleteItemAsync<GrainStateEntity<T>>(id, pk, requestOptions);

src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs:168

  • When DeleteStateOnClear is enabled and the caller has no ETag, this method returns without clearing the in-memory state (and without normalizing ETag). That can leave the activation holding stale state after a successful ClearStateAsync call. Consider resetting the grain state (without touching storage) before returning.

This issue also appears in the following locations of the same file:

  • line 177
  • line 199
    public async Task ClearStateAsync<T>(string grainType, GrainId grainId, IGrainState<T> grainState)
    {
        var (id, partitionKey) = await _documentIdProvider.GetDocumentIdentifiers(grainType, grainId);

        LogTraceClearingState(grainType, id, grainId, grainState.ETag, _options.DeleteStateOnClear, _options.ContainerName, partitionKey);
  • Files reviewed: 7/7 changed files
  • Comments generated: 0 new

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Copilot AI review requested due to automatic review settings August 1, 2026 16:32
Copilot started reviewing on behalf of ReubenBond August 1, 2026 16:32
View session
Copilot AI reviewed Aug 1, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

Suppressed comments (2)

src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs:333

  • The /GrainType partition-key-path guard currently rejects any custom IDocumentIdProvider, even if it only customizes the document id while still returning partitionKey == grainType. Since Cosmos uses GrainType as the partition key when the path is /GrainType, custom document ids can still be compatible as long as the partition key remains the grain type. Consider only rejecting custom partition-key providers here (and/or validating that the returned partition key equals grainType at runtime).
                if (_partitionKeyPath == GRAINTYPE_PARTITION_KEY_PATH &&
                    (_documentIdProvider is not DefaultDocumentIdProvider defaultProvider || defaultProvider.HasCustomPartitionKeyProvider))
                    throw new OrleansConfigurationException("Custom document id or partition key providers are not compatible with partition key path set to /GrainType");

src/Azure/Orleans.Persistence.Cosmos/HostingExtensions.cs:40

  • The generic AddCosmosGrainStorage/AddCosmosGrainStorageAsDefault overloads no longer provide compile-time validation that TProvider implements IDocumentIdProvider or IPartitionKeyProvider (the constraint is now just class), so invalid types compile and fail only at runtime via AddIdentifierProvider. Consider adding a new constrained generic overload for IDocumentIdProvider and keeping legacy IPartitionKeyProvider-based overloads as [Obsolete] to preserve compile-time safety for the primary API.
    public static ISiloBuilder AddCosmosGrainStorageAsDefault<TProvider>(
        this ISiloBuilder builder,
        Action<CosmosGrainStorageOptions> configureOptions) where TProvider : class
    {
        return builder.AddCosmosGrainStorage<TProvider>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME, configureOptions);
    }

    /// <summary>
    /// Configure silo to use Azure Cosmos DB storage for grain storage using a custom document id provider
    /// or a legacy partition key provider.
    /// </summary>
    /// <typeparam name="TProvider">The document id or partition key provider.</typeparam>
    /// <param name="builder">The silo builder.</param>
    /// <param name="name">The storage provider name.</param>
    /// <param name="configureOptions">The delegate used to configure the provider.</param>
    public static ISiloBuilder AddCosmosGrainStorage<TProvider>(
        this ISiloBuilder builder,
        string name,
        Action<CosmosGrainStorageOptions> configureOptions) where TProvider : class
  • Files reviewed: 7/7 changed files
  • Comments generated: 1

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/Azure/Orleans.Persistence.Cosmos/IDocumentIdProvider.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ReubenBond
Update Cosmos API surface
07b7944
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: e82f060d-d09b-42f9-8b0f-34da38b386fa
Copilot AI review requested due to automatic review settings August 1, 2026 21:57
Copilot started reviewing on behalf of ReubenBond August 1, 2026 21:58
View session
Copilot AI reviewed Aug 1, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

Suppressed comments (2)

src/Azure/Orleans.Persistence.Cosmos/CosmosGrainStorage.cs:333

  • This check rejects any custom IDocumentIdProvider when PartitionKeyPath is "/GrainType", even though customizing only the document id can still be compatible as long as the provider returns PartitionKey == grainType (Cosmos uses the GrainType property as the partition key, independent of document id).

Either relax this restriction for custom IDocumentIdProvider implementations, or validate/enforce that the returned partition key matches grainType when PartitionKeyPath is "/GrainType" (and update the exception message accordingly).

                if (_partitionKeyPath == GRAINTYPE_PARTITION_KEY_PATH &&
                    (_documentIdProvider is not DefaultDocumentIdProvider defaultProvider || defaultProvider.HasCustomPartitionKeyProvider))
                    throw new OrleansConfigurationException("Custom document id or partition key providers are not compatible with partition key path set to /GrainType");

src/Azure/Orleans.Persistence.Cosmos/HostingExtensions.cs:26

  • The generic overloads now accept any class (no IDocumentIdProvider/IPartitionKeyProvider constraint), so mis-typed calls compile and only fail at runtime via AddIdentifierProvider’s ArgumentException. That’s a significant loss of type-safety/IDE guidance for a public API.

Consider restoring compile-time safety by splitting into two overloads (one constrained to IDocumentIdProvider, plus an [Obsolete] overload constrained to IPartitionKeyProvider for legacy), or introducing a shared marker interface. This will require regenerating the API surface file.

    public static ISiloBuilder AddCosmosGrainStorageAsDefault<TProvider>(
        this ISiloBuilder builder,
        Action<CosmosGrainStorageOptions> configureOptions) where TProvider : class
    {
        return builder.AddCosmosGrainStorage<TProvider>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME, configureOptions);
  • Files reviewed: 8/8 changed files
  • Comments generated: 0 new

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ReubenBond
ReubenBond merged commit 39e743a into dotnet:main Aug 2, 2026
121 of 124 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@ReubenBond
ReubenBond deleted the fix/cosmos-doc-id-provider branch August 2, 2026 15:39
@ReubenBond ReubenBond mentioned this pull request Aug 2, 2026
feat(cosmos): add configuration support for grain storage #10326
Merged
@ReubenBond

ReubenBond commented Aug 2, 2026

Copy link
Copy Markdown
Member Author

@OmnipotentOwl Addressed in #10326. Configuration now accepts DocumentIdProviderKey, which resolves an application-registered keyed IDocumentIdProvider through standard dependency injection and aliases it to the grain storage provider name. We intentionally keep the service key/type out of CosmosGrainStorageOptions, since service selection belongs to hosting configuration rather than runtime options.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Aug 28, 2026
Bump Microsoft.Orleans.Dashboard from 10.2.2 to 10.3.0 csrakowski/FeedManager#20
Merged
Bump Microsoft.Orleans.Persistence.AdoNet from 10.2.2 to 10.3.0 csrakowski/FeedManager#21
Merged
Bump Microsoft.Orleans.Persistence.Memory from 10.2.2 to 10.3.0 csrakowski/FeedManager#22
Closed
Bump Microsoft.Orleans.Persistence.Redis from 10.2.2 to 10.3.0 csrakowski/FeedManager#23
Merged
Bump Microsoft.Orleans.Runtime from 10.2.2 to 10.3.0 csrakowski/FeedManager#24
Merged
This was referenced Aug 28, 2026
Bump Microsoft.Orleans.Sdk from 10.2.2 to 10.3.0 csrakowski/FeedManager#25
Merged
Bump Microsoft.Orleans.Persistence.Memory and Microsoft.Orleans.Server csrakowski/FeedManager#26
Merged
Bump the dotnet-dependencies group with 1 update dills122/sandtable#67
Merged
Bump the dotnet group with 2 updates Kuestenlogik/Surgewave.Connectors#20
Merged
chore(deps): Bump the nuget-patch-and-minor group with 13 updates petesramek/microservices-vs-virtual-actors#7
Closed
chore: Bump Microsoft.Orleans.Persistence.Memory and Microsoft.Orleans.Runtime managedcode/Communication#151
Closed
chore: Bump Microsoft.Orleans.Runtime from 10.2.2 to 10.3.1 managedcode/Communication#152
Closed
chore: Bump Microsoft.Orleans.Sdk from 10.2.2 to 10.3.1 managedcode/Communication#153
Closed
chore: Bump Microsoft.Orleans.Serialization.Abstractions from 10.2.2 to 10.3.1 managedcode/Communication#154
Merged
chore: Bump Microsoft.Orleans.Persistence.Memory and 3 others managedcode/Communication#155
Closed
chore: Bump Microsoft.Orleans.TestingHost from 10.2.2 to 10.3.1 managedcode/Communication#156
Merged
Bump the orleans group with 1 update vgmello/momentum#341
Closed
Bump Microsoft.Orleans.BroadcastChannel and 5 others intochat/intochat#77
Closed
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@ReubenBond @OmnipotentOwl
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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.