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

[Proposal] New, generic overload of IPartitionKeyProvider for Orleans.Persistence.Cosmos #9295

Open
ScottArbeit opened this issue Jan 27, 2025 · 0 comments

Comments

@ScottArbeit
Copy link

Hi!

I was wondering if Orleans would entertain the idea of adding a new overload to IPartitionKeyProvider to enable generating the PartitionKey from something other than the GrainId.

For example, in \src\Azure\Orleans.Persistence.Cosmos\IPartitionKeyProvider.cs:

namespace Orleans.Persistence.Cosmos;

/// <summary>
/// Creates a partition key for the provided grain.
/// </summary>
public interface IPartitionKeyProvider
{
    /// <summary>
    /// Creates a partition key for the provided grain.
    /// </summary>
    /// <param name="grainType">The grain type.</param>
    /// <param name="grainId">The grain identifier.</param>
    /// <returns>The partition key.</returns>
    ValueTask<string> GetPartitionKey(string grainType, GrainId grainId);

    // ↓ Add this overload. ↓

    /// <summary>
    /// Creates a partition key for the provided grain using a generic type.
    /// </summary>
    /// <typeparam name="T">The type of the object to use to derive the partition key.</typeparam>
    /// <param name="grainType">The grain type.</param>
    /// <param name="obj">The object to use to derive the partition key.</param>
    /// <returns>The partition key.</returns>
    ValueTask<string> GetPartitionKey<T>(string grainType, T obj) => new(CosmosIdSanitizer.Sanitize(grainType));
}

internal class DefaultPartitionKeyProvider : IPartitionKeyProvider
{
    public ValueTask<string> GetPartitionKey(string grainType, GrainId grainId) => new(CosmosIdSanitizer.Sanitize(grainType));

}

Here's my use case:

I'm writing a version control system, and in Cosmos DB, I'd like to group all of the metadata for a repository (branches, references, directory versions, etc.) in the same Partition. This will enable me to include WHERE c.partitionKey = @repositoryId when I'm running queries, which will
tell Cosmos DB to only run the query against that partition, saving me time and money (in the form of fewer Request Units).

Without this change, the GrainId is too granular as a PartitionKey, and all of my queries would be cross-partition, which would take longer and cost me more.

I'm happy to send a PR, of course, but wanted to discuss this as an approach before doing anything rash. 😉

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant