Skip to content

Commit

Permalink
Fix sharding entity id extractor nullability (#7059)
Browse files Browse the repository at this point in the history
* Fix sharding entity id extractor nullability

* ShardId extractor should also consider empty string
  • Loading branch information
Arkatufus authored Jan 12, 2024
1 parent 0d0d668 commit 2035d9a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/contrib/cluster/Akka.Cluster.Sharding/ClusterSharding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public abstract class HashCodeMessageExtractor : IMessageExtractor
{
private sealed class Implementation : HashCodeMessageExtractor
{
private readonly Func<Msg, string> _entityIdExtractor;
private readonly Func<Msg, string?> _entityIdExtractor;
private readonly Func<Msg, Msg>? _messageExtractor;
public Implementation(int maxNumberOfShards, Func<Msg, string> entityIdExtractor, Func<Msg, Msg>? messageExtractor = null) : base(maxNumberOfShards)
public Implementation(int maxNumberOfShards, Func<Msg, string?> entityIdExtractor, Func<Msg, Msg>? messageExtractor = null) : base(maxNumberOfShards)
{
_entityIdExtractor = entityIdExtractor ?? throw new NullReferenceException(nameof(entityIdExtractor));
_messageExtractor = messageExtractor;
Expand All @@ -124,7 +124,7 @@ public Implementation(int maxNumberOfShards, Func<Msg, string> entityIdExtractor
/// <param name="entityIdExtractor"></param>
/// <param name="messageExtractor"></param>
/// <returns></returns>
public static HashCodeMessageExtractor Create(int maxNumberOfShards, Func<Msg, string> entityIdExtractor, Func<Msg, Msg>? messageExtractor = null)
public static HashCodeMessageExtractor Create(int maxNumberOfShards, Func<Msg, string?> entityIdExtractor, Func<Msg, Msg>? messageExtractor = null)
=> new Implementation(maxNumberOfShards, entityIdExtractor, messageExtractor);

/// <summary>
Expand Down Expand Up @@ -173,7 +173,7 @@ protected HashCodeMessageExtractor(int maxNumberOfShards)
/// <param name="message">TBD</param>
/// <returns>TBD</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use ShardId(string, object?) instead")]
[Obsolete("Use ShardId(string, object?) instead. Since v1.5.15")]
public virtual ShardId? ShardId(Msg message)
{
EntityId? id;
Expand All @@ -182,7 +182,7 @@ protected HashCodeMessageExtractor(int maxNumberOfShards)
else
id = EntityId(message);

return id is null ? null : _cachedIds[(Math.Abs(MurmurHash.StringHash(id)) % MaxNumberOfShards)];
return string.IsNullOrEmpty(id) ? null : _cachedIds[(Math.Abs(MurmurHash.StringHash(id)) % MaxNumberOfShards)];
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,18 @@ namespace Akka.Cluster.Sharding
{
public readonly int MaxNumberOfShards;
protected HashCodeMessageExtractor(int maxNumberOfShards) { }
public static Akka.Cluster.Sharding.HashCodeMessageExtractor Create(int maxNumberOfShards, System.Func<object, string> entityIdExtractor, [System.Runtime.CompilerServices.NullableAttribute(new byte[] {
public static Akka.Cluster.Sharding.HashCodeMessageExtractor Create(int maxNumberOfShards, [System.Runtime.CompilerServices.NullableAttribute(new byte[] {
1,
1,
2})] System.Func<object, string> entityIdExtractor, [System.Runtime.CompilerServices.NullableAttribute(new byte[] {
2,
1,
1})] System.Func<object, object> messageExtractor = null) { }
[return: System.Runtime.CompilerServices.NullableAttribute(2)]
public abstract string EntityId(object message);
[return: System.Runtime.CompilerServices.NullableAttribute(2)]
public virtual object EntityMessage(object message) { }
[System.ObsoleteAttribute("Use ShardId(string, object?) instead")]
[System.ObsoleteAttribute("Use ShardId(string, object?) instead. Since v1.5.15")]
[return: System.Runtime.CompilerServices.NullableAttribute(2)]
public virtual string ShardId(object message) { }
public virtual string ShardId(string entityId, [System.Runtime.CompilerServices.NullableAttribute(2)] object messageHint = null) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,18 @@ namespace Akka.Cluster.Sharding
{
public readonly int MaxNumberOfShards;
protected HashCodeMessageExtractor(int maxNumberOfShards) { }
public static Akka.Cluster.Sharding.HashCodeMessageExtractor Create(int maxNumberOfShards, System.Func<object, string> entityIdExtractor, [System.Runtime.CompilerServices.NullableAttribute(new byte[] {
public static Akka.Cluster.Sharding.HashCodeMessageExtractor Create(int maxNumberOfShards, [System.Runtime.CompilerServices.NullableAttribute(new byte[] {
1,
1,
2})] System.Func<object, string> entityIdExtractor, [System.Runtime.CompilerServices.NullableAttribute(new byte[] {
2,
1,
1})] System.Func<object, object> messageExtractor = null) { }
[return: System.Runtime.CompilerServices.NullableAttribute(2)]
public abstract string EntityId(object message);
[return: System.Runtime.CompilerServices.NullableAttribute(2)]
public virtual object EntityMessage(object message) { }
[System.ObsoleteAttribute("Use ShardId(string, object?) instead")]
[System.ObsoleteAttribute("Use ShardId(string, object?) instead. Since v1.5.15")]
[return: System.Runtime.CompilerServices.NullableAttribute(2)]
public virtual string ShardId(object message) { }
public virtual string ShardId(string entityId, [System.Runtime.CompilerServices.NullableAttribute(2)] object messageHint = null) { }
Expand Down

0 comments on commit 2035d9a

Please sign in to comment.