Multiple activations of same grain
- Dominant language
- C#
- Stars
- 10.9k
- Forks
- 2.1k
- Avg merge
- 13h 56m
- Merged PRs (30d)
- 351
Description
Hi all,
I have an issue where multiple activations of same grain are activated even when there is only one silo in the cluster
Grain code is here
```
public interface IActivityLookupGrain : IGrainWithStringKey
{
[AlwaysInterleave]
Task GetSubscribedUser();
Task SubscribeUser(Guid userId);
Task UnsubscribeUser(Guid userId);
Task Deactivate();
}
```
```
[PreferLocalPlacement]
public class ActivityLookupGrain : Grain, IActivityLookupGrain
{
private Guid? State;
public ActivityLookupGrain()
{
State = null;
}
public override async Task OnActivateAsync()
{
Console.WriteLine($"Activating ActivityLookupGrain {this.GetPrimaryKeyString()} {DateTime.UtcNow}");
this.DelayDeactivation(TimeSpan.MaxValue);
await base.OnActivateAsync();
}
public override Task OnDeactivateAsync()
{
Console.WriteLine($"Dectivating ActivityLookupGrain {this.GetPrimaryKeyString()} {DateTime.UtcNow}");
return base.OnDeactivateAsync();
}
public Task GetSubscribedUser()
{
Console.WriteLine($"GetSubscribedUser ActivityLookupGrain {this.GetPrimaryKeyString()} {DateTime.UtcNow}");
return Task.FromResult(State);
}
public Task SubscribeUser(Guid userId)
{
Console.WriteLine($"SubscribeUser ActivityLookupGrain {this.GetPrimaryKeyString()} {DateTime.UtcNow}");
State = userId;
return Task.CompletedTask;
}
public Task UnsubscribeUser(Guid userId)
{
if (State.GetValueOrDefault() == userId)
State = null;
return Task.CompletedTask;
}
public Task Deactivate()
{
Console.WriteLine($"Deactivate ActivityLookupGrain {this.GetPrimaryKeyString()} {DateTime.UtcNow}");
this.DeactivateOnIdle();
return Task.CompletedTask;
}
}
```
it is a very simple grain that is used only to follow which user (if any) is subscribed to follow changes that are sent as notifications from other grain.
```
// UserGrain
public async Task NotificationSubscribe(Guid id1, Guid id2, Guid id3)
{
var key = KeyHelper.ConstructKey(id1, id2, id3); // $"{id1}_{id2}_{id3}
await GrainFactory.GetGrain(key).SubscribeUser(this.GetPrimaryKey());
await GrainFactory.GetGrain(key).Initialize();
}
//WorkerGrain
public async Task Initialize()
{
//...some logic...
var key = this.GetPrimaryKeyString();
string notificationUserId = (await GrainFactory.GetGrain(key).GetSubscribedUser()).ToString();
if (String.IsNullOrEmpty(notificationUserId))
{
_logger.WriteWarning("Activity lookup grain - Empty userId.");
return;
}
//...send init success notification
}
```
And this is what i get in a console
```
Activating ActivityLookupGrain 679a5ac9-d5da-4c9e-9dd2-dcf2eb07cdbc_bd4439af-29c3-2fdb-ed40-78fe07f61215_f73fd581-a23c-40b5-b9b9-ba43ddfa1818 5/17/2021 4:35:07 PM
SubscribeUser ActivityLookupGrain 679a5ac9-d5da-4c9e-9dd2-dcf2eb07cdbc_bd4439af-29c3-2fdb-ed40-78fe07f61215_f73fd581-a23c-40b5-b9b9-ba43ddfa1818 5/17/2021 4:35:07 PM
Activating ActivityLookupGrain 679a5ac9-d5da-4c9e-9dd2-dcf2eb07cdbc_bd4439af-29c3-2fdb-ed40-78fe07f61215_f73fd581-a23c-40b5-b9b9-ba43ddfa1818 5/17/2021 4:35:08 PM
GetSubscribedUser ActivityLookupGrain 679a5ac9-d5da-4c9e-9dd2-dcf2eb07cdbc_bd4439af-29c3-2fdb-ed40-78fe07f61215_f73fd581-a23c-40b5-b9b9-ba43ddfa1818 5/17/2021 4:35:08 PM
Activating ActivityLookupGrain 679a5ac9-d5da-4c9e-9dd2-dcf2eb07cdbc_bd4439af-29c3-2fdb-ed40-78fe07f61215_f73fd581-a23c-40b5-b9b9-ba43ddfa1818 5/17/2021 4:35:14 PM
GetSubscribedUser ActivityLookupGrain 679a5ac9-d5da-4c9e-9dd2-dcf2eb07cdbc_bd4439af-29c3-2fdb-ed40-78fe07f61215_f73fd581-a23c-40b5-b9b9-ba43ddfa1818 5/17/2021 4:35:14 PM
```
Also "Activity lookup grain - Empty userId" is logged
None of the grains are stateless workers and all my other grains show up correctly in orleans dashboard but ActivityLookupGrain never shows up
Orleans packages are version 3.3.0 and application is built with netCore 3.1
Any suggestions would be really helpful
Contributor guide
Assessment
This issue has not been assessed yet.