GrainStateStorageBridge Error with Custom Storage Provider
We have a Grain with custom storage provider. The storage provider is pretty simple. It uses blob storage provider, and makes additional post API call. From time to time, we get this error which we can't figure out it's source.
We are using Orleans 1.5.1
System.NullReferenceException: Object reference not set to an instance of an object.
Server stack trace:
at Orleans.Core.GrainStateStorageBridge.<WriteStateAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Grains.SomeGrain.<SomeAction>d__15.MoveNext() in C:\BuildAgent\work\22c1962c6b5cd168\Orleans\Grains\SomeGrain.cs:line 217
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Orleans.PublicOrleansTaskExtensions.<BoxAwait>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
This is strange. The call stack seems to point to https://github.com/dotnet/orleans/blob/03008874a5a420f7e28fbccc83be33e5e4f735da/src/Orleans/Core/GrainStateStorageBridge.cs#L90 where your 'decorator' provider should be getting invoked. Can it throw a NRE from inside? Is the above call stack from the caller's side? If so, can you find the matching exception in the target grain's silo log?
The custom storage provider's WriteStateAsync looks safe, all its extra code is surrounded with try/catch. We had a GrainCallFilter for debugging, that captures all the grain calls. We tried to remove it and it seems like the issue hasn't reproduced since then. Still not sure what threw the NRE, but it is probably related to it.
Here is its code:
public class LoggingCallFilter : IGrainCallFilter
{
private readonly Logger _logger;
public LoggingCallFilter()
{
_logger = LogManager.GetLogger("logger");
}
public async Task Invoke(IGrainCallContext context)
{
try
{
await context.Invoke();
if (context.Grain.GetType().Assembly != typeof(SomeGrain).Assembly) return;
try
{
var message = "{0}.{1}({2}) return value={3}";
var grainName = context.Grain.GetType().Name;
var methodName = context.Method.Name;
var arguments = string.Join(", ", context.Arguments ?? new object[0]);
var result = context.Result?.ToString();
_logger.Log(new LogEventInfo
{
Level = LogLevel.Info,
Message = message,
Parameters = new object[] { grainName, methodName, arguments, result },
Properties =
{
["GrainId"] = context.Grain.GetPrimaryKey().ToString(),
["Arguments"] = JsonConvert.SerializeObject(context.Arguments ?? new object[0]),
["GrainType"] = context.Grain.GetType(),
["Method"] = context.Method.Name,
["Result"] = context.Result
},
});
}
catch (Exception){}
}
catch (Exception exception)
{
if (context.Grain.GetType().Assembly != typeof(SomeGrain).Assembly) throw;
try
{
var message = "{0}.{1}({2}) threw an exception of type={3} with message={4}";
var grainName = context.Grain.GetType().Name;
var methodName = context.Method.Name;
var arguments = string.Join(", ", context.Arguments ?? new object[0]);
var exceptionType = exception.GetType().Name;
var exceptionMessage = exception.Message;
_logger.Log(new LogEventInfo
{
Level = LogLevel.Info,
Message = message,
Parameters = new object[] { grainName,methodName,arguments,exceptionType,exceptionMessage },
Exception = exception,
Properties =
{
["GrainId"] = context.Grain.GetPrimaryKey().ToString(),
["Arguments"] = JsonConvert.SerializeObject(context.Arguments ?? new object[0]),
["GrainType"] = context.Grain.GetType(),
["Method"] = context.Method.Name,
["Result"] = context.Result
},
});
}
catch (Exception){}
// If this exception is not re-thrown, it is considered to be
// handled by this filter.
throw;
}
}
}
So grain call filter is the primary suspect here, right? I don't see anything obvious in your code that could throw a NRE, but that doesn't mean, of course, it's impossible.
hmm.. Grains.SomeGrain.
I had the same issue with a custom Mongo storage provider. Turned out to be an issue where the DB had connection issues or just failed the queue wait period. Profile your DB. It could potentially be the same issue.
@sujesharukil thanks. Indeed it seems like an issue with the custom storage provider when it waits for a long time. Not sure how, but it would be much easier if the stack trace would have contained a timeout description
We've downloaded the sources, and after debugging we understood why we got the null reference exception.
We left the Log property of the custom storage provider unassigned:
public Logger Log { get; }
Any custom storage provider error resulted in a null reference exception without additional info. To save time for others, maybe Orleans can throw more suitable error? Something like 'Custom Storage Provider Log must be assigned'
Maybe Orleans should use an empty logger by default so this doesn't happen, worst case logs are not written.
@talarari
Maybe Orleans should use an empty logger by default so this doesn't happen, worst case logs are not written.
I'm not sure I follow you here. Are you suggesting that orleans runtime use the empty logger if the logger provided by IStorageProvider.Log is null?
If this is true, I'd vote no to the suggestion just as my personal opinion. For following reasons
IStorageProvideris a interface, which is a contract between its custom implementation and other code uses this interfaces. Code which build their logic around this interface assumes all its methods are implemented. If code cannot rely on the implementation to deliver what its interface promised, then these whole contract just fall apart. I believe this is not the right way to use interfaces.- I understand
Logis kind of NOT essential toIStorageProvider, which is probably why people forget to implemented them. It is our bad on a bad interface design. We refactoredIStorageProviderinterface and similar others in orleans 2.0. In 2.0IStorageProviderdoesn't have aLogmethod anymore. Each component create their own Logger from DI using ASP.NET logging abstraction. Nothing is relying onIStorageProvider, or some other component to provider a Logger. I think this partially resolve your concern.
@ZahiC
Any custom storage provider error resulted in a null reference exception without additional info. To save time for others, maybe Orleans can throw more suitable error? Something like 'Custom Storage Provider Log must be assigned'
sorry for the confusion. We should indeed provide more suitable error for stuff like this. But we are in a weird position to do so now.
As I mentioned in my previous comment, orleans 2.0 is moving to use ASP.NET logging abstraction. So each component is responsible to create its own logger and We don't have Log in IStorageProvider interface anymore. So in orleans 2.0, the situation you encountered won't happen.
But it indeed would happen in orleans 1.5, if you don't implement IStorageProvider.Log. We can consider a patch release to add more suitable exception. But I don't think this will help you guys much, right? because you guys already figured out where the bug is.
Closing this, as this is resolved in 2.0.0. Feel free to reopen if you think a patch for this for 1.5.x is critical.