orleans

How does PreferLocalPlacement works in Orleans


The doc does not explain clearly. I suppose PreferLocalPlacement works in this way:

When client(out of cluster) sends a request to a grain marked as PreferLocalPlacement, it chooses a random silo server it knows, and sends the request there.

When the silo server receives the request, it determines if grain with the specified identity was already activated. If not, the grain is activated automatically in this silo.

Thus, each silo server can have max 1 activation of this grain type per an identity.

Do I get it right? Thanks in advance


Solution

  • Your understanding is close but missing a key aspect of how Orleans finds and places grain activations.

    The key is that most placement providers in Orleans rely on a grain directory. That directory stores the current mapping of which grain is activated on which server. Like placement, the directory is configurable. It uses an in-cluster directory by default, where responsibility for storing the mapping is shared among the servers.

    Orleans will check the directory first, to see if the grain is currently activated somewhere in the cluster and it will only run the logic in PreferLocalPlacement when the grain does not currently have a valid entry in the directory.

    Therefore, you will have one instance of your grain active in the cluster, not one instance per server. If there are no currently active instances of your grain then PreferLocalPlacement will always pick the current, local server (or the server which the request first lands on) as long as it is compatible.

    Extra detail

    "As long as it is compatible"

    Clusters can have a mix of servers each having a different set of grain assemblies loaded. Therefore, placement providers need to take into account whether a given server is compatible with the requested grain type and if it has a compatible version of the interface which is being requested. If it does not support that grain, then a random compatible server will be chosen instead. For more information, see the documentation on Heterogeneous Clusters.

    "Most placement providers rely on a grain directory"

    You might ask which placement providers do not rely on a grain directory. The only answer today is [StatelessWorker] placement, since [StatelessWorker] grains can have multiple instances, i.e, each server can have a configurable number of local instances of any given [StatelessWorker] grain.