Search code examples
orleans

How to make sure associated grains are activated in same silo?


I am implementing a game application using orleans framework. Every game instance is a separate GameGrain and there are some associated grains with every instance of GameGrain like PlayerGrain (for every player in the game), WeaponsGrain (weapons inventory).

The primary key of these grains are:

GameGrain : <GameGuid>

PlayerGrain : <GameGuid>-<PlayerGuid>

WeaponsGrain: <GameGuid>

Now, I want to implement grains placement strategy that makes sure that GameGrain, PlayerGrain and WeaponsGrain for any game instance is activated on same silo as the GameGrain

Would appreciate if you can provide some inputs as how I should do this?


Solution

  • You can mark a grain with the attribute [PreferLocalPlacement] if you want it to be placed on the same silo as the grain that created it.

    Other placement strategies are available and you can even write your own. Read the documentation on grain placement for more information on this.

    As there is a cost associated with grain communication and you are concerned enough about this to be thinking about grain placement, then you may want to rethink what objects should be grains. If an object does not need to be accessed by multiple grains and is truly owned by a grain, then you may just want to make it part of the same grain.

    A weapon for instance could be an object in a collection of weapons in a player grain. If it needs to change ownership, the object could be sent to another player grain.