orleans

What happens when a grain instance is obtained with a new Guid?


I have an Orleans grain that performs an async database operation. I would rather not have the calling code wait for that to finish because it may negatively affect the overall response time and the results of the database op aren't used in the response.

Grains instances are being obtains in the client like this:
var grain = grainFactory.GetGrain<IExample>(Guid.NewGuid());

Will new instances of the grain class be created each time because the key is unique?

If the client doesn't wait for the grain method to finish, will the grain class instance eventually be garbage collected after some period of inactivity?


Solution

  • Will new instances of the grain class be created each time because the key is unique?

    Actually this line var grain = grainFactory.GetGrain<IExample>(Guid.NewGuid()); doesn't activate a new grain because it doesn't call a grain's method. This line await grainFactory.GetGrain<IExample>(Guid.NewGuid()).DoSomethingAsync() does activate. And in the second example orleans runtime ensures that the grain you're calling exists and is ready to use. So if you're calling a grain every time with a new key - you're activating a new grain for each unique key.

    If the client doesn't wait for the grain method to finish, will the grain class instance eventually be garbage collected after some period of inactivity?

    Grain's life is prolonged every time you call it, whether you're waiting for the task completion or not. After 2 hours (it's configurable) of inactivity the grain will be deactivated:enter image description here