dotnet / orleans

Cloud Native application framework for .NET
https://docs.microsoft.com/dotnet/orleans
MIT License
9.94k stars 2.02k forks source link

Question on "GPS Tracker" Example #8190

Open Shawn-Fan opened 1 year ago

Shawn-Fan commented 1 year ago

Following the instruction of "Running the sample",

  1. dotnet run --project GPSTracker.Service Host 1 is running ...
  2. dotnet run --project GPSTracker.Service -- --InstanceId 1 Host 2 is running
  3. dotnet run --project GPSTracker.FakeDeviceGateway The client invoking the DeviceGrain->PushNotifierGrain->HubListGrain->....

My question is: In the step 3, how and where the "PushNotifierGrain" and "HubListGrain" are created in the Host 1 / Host 2 ( I noticed that there are two diagrams in the instruction but I am not sure if the second one is for the Example).

Thanks!

gfoidl commented 1 year ago

With diagrams you're refering to one from https://github.com/dotnet/orleans/tree/main/samples/GPSTracker?

The first one shows the flow of information, indepented on where the grains reside.

The second one shows where the grains reside in the hosts / silos.

Grains are created by the Orleans-runtime and placed according the Grain placement rules.

E.g. for a DeviceGrain the DeviceId is used as key, so there will be a grain for each device (to be exact: for each different DeviceId) https://github.com/dotnet/orleans/blob/f320f499726fcfc489bbff0ee03296293e3420e3/samples/GPSTracker/GPSTracker.FakeDeviceGateway/Program.cs#L104 and the grain is placed based on the strategy -- see link for more info. So it could end up being in Host 1, 2, 3. A different DeviceGrain will be placed also in one of the hosts according the placement-strategy.

The PushNotifierGrain is always refered to with a constant id (here 0) https://github.com/dotnet/orleans/blob/f320f499726fcfc489bbff0ee03296293e3420e3/samples/GPSTracker/GPSTracker.Service/Grains/DeviceGrain.cs#L17. The same placement rules apply as to all other grains, so it may end up being in Host 1, 3, 3. But as there's always the same constant id (here 0) they refer all to the same grain. Orleans knows in which host / silo is by using a grain directory.

As consumer of Orleans (and for this sample) you shouldn't care in which silo a grain is. The important thing is that a grain is somewhere and you can use / call it. For me this is a beautiful thing of the virtual actor model applied by Orleans.

If something is unclear feel free to ask.

Shawn-Fan commented 1 year ago

Thanks for your quick and detailed answer.

May be my question is not very clear, my question is on
The [second one]shows where the grains reside in the hosts / silos.

my question is which hosts/silos will the pushnotifierGrain/hublistGrain be assigned in(physically)? random assigned in the host or somewhere else?

From your description, my understanding is that the pushnotifierGrain / hublistGrain will be like globe one for the cluster and as consumer(client) shouldn't care in which silo a grain is, I am just curious to know if possible... Anyway, Thank you again.