-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Akka.Cluster.Sharding GetEntityLocation Query
#6101
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
Merged
Aaronontheweb
merged 4 commits into
akkadotnet:v1.4
from
Aaronontheweb:akkasharding-query-entity
Sep 17, 2022
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
added spec and bugfixes
- Loading branch information
commit 0222be1d1e02187a6d7e3bb85764a29cc008a74a
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
src/contrib/cluster/Akka.Cluster.Sharding.Tests/ShardRegionQueriesSpecs.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| //----------------------------------------------------------------------- | ||
| // <copyright file="ShardRegionQueriesSpecs.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com> | ||
| // Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| //----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Akka.Actor; | ||
| using Akka.Cluster.Tools.Singleton; | ||
| using Akka.Configuration; | ||
| using Akka.TestKit; | ||
| using Akka.TestKit.TestActors; | ||
| using Akka.Util; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
| using FluentAssertions; | ||
|
|
||
| namespace Akka.Cluster.Sharding.Tests | ||
| { | ||
| public class ShardRegionQueriesSpecs : AkkaSpec | ||
| { | ||
| private Cluster _cluster; | ||
| private ClusterSharding _clusterSharding; | ||
| private IActorRef _shardRegion; | ||
|
|
||
| private ActorSystem _proxySys; | ||
|
|
||
| public ShardRegionQueriesSpecs(ITestOutputHelper outputHelper) : base(GetConfig(), outputHelper) | ||
| { | ||
| _clusterSharding = ClusterSharding.Get(Sys); | ||
| _cluster = Cluster.Get(Sys); | ||
| _shardRegion = _clusterSharding.Start("entity", s => EchoActor.Props(this, true), | ||
| ClusterShardingSettings.Create(Sys).WithRole("shard"), ExtractEntityId, ExtractShardId); | ||
|
|
||
| var proxySysConfig = ConfigurationFactory.ParseString("akka.cluster.roles = [proxy]") | ||
| .WithFallback(Sys.Settings.Config); | ||
| _proxySys = ActorSystem.Create(Sys.Name, proxySysConfig); | ||
|
|
||
| _cluster.Join(_cluster.SelfAddress); | ||
| AwaitAssert(() => | ||
| { | ||
| _cluster.SelfMember.Status.ShouldBe(MemberStatus.Up); | ||
| }); | ||
|
|
||
| // form a 2-node cluster | ||
| var proxyCluster = Cluster.Get(_proxySys); | ||
| proxyCluster.Join(_cluster.SelfAddress); | ||
| AwaitAssert(() => | ||
| { | ||
| proxyCluster.SelfMember.Status.ShouldBe(MemberStatus.Up); | ||
| }); | ||
| } | ||
|
|
||
| protected override void AfterAll() | ||
| { | ||
| Shutdown(_proxySys); | ||
| base.AfterAll(); | ||
| } | ||
|
|
||
| private Option<(string, object)> ExtractEntityId(object message) | ||
| { | ||
| switch (message) | ||
| { | ||
| case int i: | ||
| return (i.ToString(), message); | ||
| } | ||
| throw new NotSupportedException(); | ||
| } | ||
|
|
||
| private string ExtractShardId(object message) | ||
| { | ||
| switch (message) | ||
| { | ||
| case int i: | ||
| return (i % 10).ToString(); | ||
| case ShardRegion.StartEntity se: | ||
| return se.EntityId; | ||
| } | ||
| throw new NotSupportedException(); | ||
| } | ||
|
|
||
| private static Config GetConfig() | ||
| { | ||
| return ConfigurationFactory.ParseString(@" | ||
| akka.loglevel = WARNING | ||
| akka.actor.provider = cluster | ||
| akka.remote.dot-netty.tcp.port = 0 | ||
| akka.cluster.roles = [shard]") | ||
|
|
||
| .WithFallback(Sharding.ClusterSharding.DefaultConfig()) | ||
| .WithFallback(DistributedData.DistributedData.DefaultConfig()) | ||
| .WithFallback(ClusterSingletonManager.DefaultConfig()); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "ShardRegion should support GetEntityLocation queries locally")] | ||
| public async Task ShardRegion_should_support_GetEntityLocation_query() | ||
| { | ||
| // arrange | ||
| await _shardRegion.Ask<int>(1, TimeSpan.FromSeconds(3)); | ||
| await _shardRegion.Ask<int>(2, TimeSpan.FromSeconds(3)); | ||
|
|
||
| // act | ||
| var q1 = await _shardRegion.Ask<EntityLocation>(new GetEntityLocation("1", TimeSpan.FromSeconds(1))); | ||
| var q2 = await _shardRegion.Ask<EntityLocation>(new GetEntityLocation("2", TimeSpan.FromSeconds(1))); | ||
| var q3 = await _shardRegion.Ask<EntityLocation>(new GetEntityLocation("3", TimeSpan.FromSeconds(1))); | ||
|
|
||
| // assert | ||
| void AssertValidEntityLocation(EntityLocation e, string entityId) | ||
| { | ||
| e.EntityId.Should().Be(entityId); | ||
| e.EntityRef.Should().NotBe(Option<IActorRef>.None); | ||
| e.ShardId.Should().NotBeNullOrEmpty(); | ||
| e.ShardRegion.Should().Be(_cluster.SelfAddress); | ||
| } | ||
|
|
||
| AssertValidEntityLocation(q1, "1"); | ||
| AssertValidEntityLocation(q2, "2"); | ||
|
|
||
| q3.EntityRef.Should().Be(Option<IActorRef>.None); | ||
| q3.ShardId.Should().NotBeNullOrEmpty(); // should still have computed a valid shard? | ||
| q3.ShardRegion.Should().Be(Address.AllSystems); | ||
| } | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -166,6 +166,9 @@ private GetCurrentRegions() | |
| /// </summary> | ||
| /// <remarks> | ||
| /// This is used primarily for testing and telemetry purposes. | ||
| /// | ||
| /// In order for this query to work, the <see cref="MessageExtractor"/> must support <see cref="ShardRegion.StartEntity"/>, | ||
| /// which is also used when remember-entities=on. | ||
| /// </remarks> | ||
| public sealed class GetEntityLocation : IShardRegionQuery | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This message never gets sent over the wire, so it doesn't require any changes to the wire type nor does it need to support |
||
| { | ||
|
|
||
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.
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.
One thing that is awkward about this query is that the
ShardExtractorhas to support theShardRegion.StartEntitytype in order for this to work - the reason being is that even though we already know what the entity id is, the extractors that resolve theShardIdare looking for an input message to generate theShardId, not theEntityId. Therefore I have to smuggle theEnttityIdback through aShardRegion.StartEntitymessage. That makes this a bit hacky but it's the best compromise I could make.