public static string LoggingIdentifier(this IGrain grain) { var type = grain.GetType(); try { string temp = null; if (typeof(IGrainWithIntegerKey).IsAssignableFrom(type)) { return($"[{grain.GetPrimaryKeyLong()}] "); } else if (typeof(IGrainWithIntegerCompoundKey).IsAssignableFrom(type)) { var key = grain.GetPrimaryKeyLong(out temp); return($"[{key} {temp}] "); } else if (typeof(IGrainWithGuidKey).IsAssignableFrom(type)) { return($"[{grain.GetPrimaryKey()}] "); } else if (typeof(IGrainWithGuidCompoundKey).IsAssignableFrom(type)) { var key = grain.GetPrimaryKey(out temp); return($"[{key} {temp}] "); } else if (typeof(IGrainWithStringKey).IsAssignableFrom(type)) { return($"[{grain.GetPrimaryKeyString()}] "); } } catch { } // Don't throw for logging methods. return(string.Empty); }
public static Type GetConcreteGrainType(this IGrain @this) { if (@this is GrainProxy) { return(((GrainProxy)@this).GrainType); } if (@this is Grain) { return(@this.GetType()); } throw new NotImplementedException("Not handling current type at mo!"); }
public void OnInvoke(InvokeMethodRequest request, IGrain grain) { // (NOT YET AVAILABLE) Interface name is available from: <c>grainReference.InterfaceName</c> // (NOT YET AVAILABLE) Method name is available from: <c>grainReference.GetMethodName(request.InterfaceId, request.MethodId)</c> // GrainId is available from: <c>grainReference.GrainId</c> // PrimaryKey is availabe from: <c>grainReference.GrainId.GetPrimaryKeyLong()</c> or <c>grainReference.GrainId.GetPrimaryKey()</c> depending on key type. // Call arguments are available from: <c>request.Arguments</c> array TotalCalls++; output.WriteLine("OnInvoke TotalCalls={0}", TotalCalls); try { output.WriteLine("OnInvoke called for Grain={0} PrimaryKey={1} GrainId={2} with {3} arguments", grain.GetType().FullName, ((GrainReference)grain).GrainId.GetPrimaryKeyLong(), ((GrainReference)grain).GrainId, request.Arguments != null ? request.Arguments.Length : 0); } catch (Exception exc) { output.WriteLine("**** Error OnInvoke for Grain={0} GrainId={1} with {2} arguments. Exception = {3}", grain.GetType().FullName, ((GrainReference)grain).GrainId, request.Arguments != null ? request.Arguments.Length : 0, exc); } if (setActivityId != Guid.Empty) { RequestContextTestUtils.SetActivityId(setActivityId); output.WriteLine("OnInvoke Set ActivityId={0}", setActivityId); } output.WriteLine("OnInvoke Current ActivityId={0}", RequestContextTestUtils.GetActivityId()); }
// capture stats async Task <object> InvokeInterceptor(MethodInfo targetMethod, InvokeMethodRequest request, IGrain grain, IGrainMethodInvoker invoker) { // round down to nearest 10 seconds to group results var grainName = grain.GetType().FullName; var stopwatch = Stopwatch.StartNew(); // invoke grain object result = null; if (this.innerInterceptor != null) { result = await this.innerInterceptor(targetMethod, request, grain, invoker); } else { result = await invoker.Invoke(grain, request); } stopwatch.Stop(); var elapsedMs = (double)stopwatch.ElapsedTicks / TimeSpan.TicksPerMillisecond; var key = $"{grainName}.{targetMethod?.Name ?? "Unknown"}"; grainTrace.AddOrUpdate(key, _ => { return(new GrainTraceEntry { Count = 1, SiloAddress = siloAddress, ElapsedTime = elapsedMs, Grain = grainName, Method = targetMethod?.Name ?? "Unknown", Period = DateTime.UtcNow }); }, (_, last) => { last.Count += 1; last.ElapsedTime += elapsedMs; return(last); }); return(result); }
// capture stats async Task<object> InvokeInterceptor(MethodInfo targetMethod, InvokeMethodRequest request, IGrain grain, IGrainMethodInvoker invoker) { // round down to nearest 10 seconds to group results var grainName = grain.GetType().FullName; var stopwatch = Stopwatch.StartNew(); // invoke grain object result = null; if (this.innerInterceptor != null) { result = await this.innerInterceptor(targetMethod, request, grain, invoker); } else { result = await invoker.Invoke(grain, request); } stopwatch.Stop(); var elapsedMs = (double)stopwatch.ElapsedTicks / TimeSpan.TicksPerMillisecond; var key = $"{grainName}.{targetMethod?.Name ?? "Unknown"}"; grainTrace.AddOrUpdate(key, _ => { return new GrainTraceEntry { Count = 1, SiloAddress = siloAddress, ElapsedTime = elapsedMs, Grain = grainName, Method = targetMethod?.Name ?? "Unknown", Period = DateTime.UtcNow }; }, (_, last) => { last.Count += 1; last.ElapsedTime += elapsedMs; return last; }); return result; }
private Func <object, Task> GetTimerFunc(IProviderRuntime providerRuntime, IGrain grain) { var grainName = grain.GetType().FullName; return(async o => { var filterableGrain = grain.AsReference <IFilterableGrain>(); var result = await filterableGrain.GetFilters(); if (result != null) { var filterGrain = providerRuntime.GrainFactory.GetGrain <ITypeFilterGrain>(grain.GetType().FullName); await filterGrain.RegisterFilter(grainName, filterableGrain.GetPrimaryKey().ToString(), result); var filterString = string.Join(",", result.Select(p => $"{p.FilterName} : {p.Value}")); _logger.Verbose($"Filters for grain [Type : {grainName}] [Id : {grain.GetPrimaryKey()}][Filter : {filterString}]"); } else { _logger.Verbose("Filter was not set yet"); } }); }
// capture stats async Task <object> InvokeInterceptor(MethodInfo targetMethod, InvokeMethodRequest request, IGrain grain, IGrainMethodInvoker invoker) { var grainName = grain.GetType().FullName; var stopwatch = Stopwatch.StartNew(); // invoke grain object result = null; var isException = false; try { if (this.innerInterceptor != null) { result = await this.innerInterceptor(targetMethod, request, grain, invoker).ConfigureAwait(false); } else { result = await invoker.Invoke(grain, request).ConfigureAwait(false); } } catch (Exception) { isException = true; throw; } finally { try { stopwatch.Stop(); var elapsedMs = (double)stopwatch.ElapsedTicks / TimeSpan.TicksPerMillisecond; var key = string.Format("{0}.{1}", grainName, formatMethodName(targetMethod, request, grain)); grainTrace.AddOrUpdate(key, _ => { return(new GrainTraceEntry { Count = 1, ExceptionCount = (isException ? 1 : 0), SiloAddress = siloAddress, ElapsedTime = elapsedMs, Grain = grainName, Method = formatMethodName(targetMethod, request, grain), Period = DateTime.UtcNow }); }, (_, last) => { last.Count += 1; last.ElapsedTime += elapsedMs; if (isException) { last.ExceptionCount += 1; } return(last); }); } catch (Exception ex) { this.Logger.Error(100002, "error recording results for grain", ex); } } return(result); }
public void OnInvoke(InvokeMethodRequest request, IGrain grain) { // (NOT YET AVAILABLE) Interface name is available from: <c>grainReference.InterfaceName</c> // (NOT YET AVAILABLE) Method name is available from: <c>grainReference.GetMethodName(request.InterfaceId, request.MethodId)</c> // GrainId is available from: <c>grainReference.GrainId</c> // PrimaryKey is availabe from: <c>grainReference.GrainId.GetPrimaryKeyLong()</c> or <c>grainReference.GrainId.GetPrimaryKey()</c> depending on key type. // Call arguments are available from: <c>request.Arguments</c> array TotalCalls++; Console.WriteLine("OnInvoke TotalCalls={0}", TotalCalls); try { Console.WriteLine("OnInvoke called for Grain={0} PrimaryKey={1} GrainId={2} with {3} arguments", grain.GetType().FullName, ((GrainReference) grain).GrainId.GetPrimaryKeyLong(), ((GrainReference) grain).GrainId, request.Arguments != null ? request.Arguments.Length : 0); } catch (Exception exc) { Console.WriteLine("**** Error OnInvoke for Grain={0} GrainId={1} with {2} arguments. Exception = {3}", grain.GetType().FullName, ((GrainReference)grain).GrainId, request.Arguments != null ? request.Arguments.Length : 0, exc); } if (setActivityId != Guid.Empty) { Trace.CorrelationManager.ActivityId = setActivityId; Console.WriteLine("OnInvoke Set ActivityId={0}", setActivityId); } Console.WriteLine("OnInvoke Current ActivityId={0}", Trace.CorrelationManager.ActivityId); }
/// <summary> /// The get kind. /// </summary> /// <param name="actor"> /// The actor. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public static string GetKind(this IGrain actor) { return(GetKind(actor.GetType())); }
internal static IGrainIdentity GetGrainIdentity(IGrain grain) { var grainBase = grain as Grain; if (grainBase != null) { if (grainBase.Identity == null) { throw new ArgumentException(WRONG_GRAIN_ERROR_MSG, "grain"); } return(grainBase.Identity); } var grainReference = grain as GrainReference; if (grainReference != null) { if (grainReference.GrainId == null) { throw new ArgumentException(WRONG_GRAIN_ERROR_MSG, "grain"); } return(grainReference.GrainId); } throw new ArgumentException(String.Format("GetGrainIdentity has been called on an unexpected type: {0}.", grain.GetType().FullName), "grain"); }
public static GrainId GetGrainIdentity(this IGrain grain) { switch (grain) { case Grain grainBase: if (grainBase.Identity.IsDefault) { throw new ArgumentException(WRONG_GRAIN_ERROR_MSG, "grain"); } return(grainBase.Identity); case GrainReference grainReference: if (grainReference.GrainId.IsDefault) { throw new ArgumentException(WRONG_GRAIN_ERROR_MSG, "grain"); } return(grainReference.GrainId); default: throw new ArgumentException(String.Format("GetGrainIdentity has been called on an unexpected type: {0}.", grain.GetType().FullName), "grain"); } }
async Task <object> SiloInterceptor(MethodInfo method, InvokeMethodRequest request, IGrain grain, IGrainMethodInvoker invoker) { try { object result = PreviousInterceptor != null ? await PreviousInterceptor(method, request, grain, invoker) : await invoker.Invoke(grain, request); //if (PreviousInterceptor != null) // result = await PreviousInterceptor(method, request, grain, invoker); //// Invoke the request and return the result back to the caller. //var result = await invoker.Invoke(grain, request); if (Configuration.TrackMethodGrainCalls) { // Would be nice if we could figure out if this is a local or remote call, // and perhaps caller / calling silo... Unless I've got something backwards logger.IncrementMetric($"GrainMethodCall:{grain.GetType().Name}:{method.Name}"); } return(result); } catch (TimeoutException ex) // Not sure if this is going to be an innerException here or if everything gets unrolled... Fingers crossed for now! { if (Configuration.TrackExceptionCounters) { logger.IncrementMetric($"GrainInvokeTimeout:{grain.GetType().Name}:{method.Name}"); } if (Configuration.LogExceptions) { logger.TrackException(ex, new Dictionary <string, string> { { "GrainType", grain.GetType().Name }, { "MethodName", method.Name }, }); } throw; } catch (Exception ex) { if (Configuration.TrackExceptionCounters) { logger.IncrementMetric($"GrainException:{grain.GetType().Name}:{method.Name}"); } if (Configuration.LogExceptions) { logger.TrackException(ex, new Dictionary <string, string> { { "GrainType", grain.GetType().Name }, { "MethodName", method.Name }, }); } throw; } }
// capture stats async Task<object> InvokeInterceptor(MethodInfo targetMethod, InvokeMethodRequest request, IGrain grain, IGrainMethodInvoker invoker) { var grainName = grain.GetType().FullName; var stopwatch = Stopwatch.StartNew(); // invoke grain object result = null; var isException = false; try { if (this.innerInterceptor != null) { result = await this.innerInterceptor(targetMethod, request, grain, invoker); } else { result = await invoker.Invoke(grain, request); } } catch (Exception ex) { isException = true; throw; } finally { try { stopwatch.Stop(); var elapsedMs = (double)stopwatch.ElapsedTicks / TimeSpan.TicksPerMillisecond; var key = string.Format("{0}.{1}", grainName, targetMethod?.Name ?? "Unknown"); grainTrace.AddOrUpdate(key, _ => { return new GrainTraceEntry { Count = 1, ExceptionCount = (isException ? 1 : 0), SiloAddress = siloAddress, ElapsedTime = elapsedMs, Grain = grainName , Method = targetMethod?.Name ?? "Unknown", Period = DateTime.UtcNow }; }, (_, last) => { last.Count += 1; last.ElapsedTime += elapsedMs; if (isException) last.ExceptionCount += 1; return last; }); } catch (Exception ex) { this.Logger.Error(100002, "error recording results for grain", ex); } } return result; }
internal static IGrainIdentity GetGrainIdentity(IGrain grain) { var grainBase = grain as Grain; if (grainBase != null) { return(grainBase.Identity); } var grainReference = grain as GrainReference; if (grainReference != null) { return(grainReference.GrainId); } throw new OrleansException(String.Format("GetGrainIdentity has been called on an unexpected type: {0}.", grain.GetType().FullName)); }