public static string GetPrimaryKeyStringFromGrain(IGrain grain)
 {
     //TODO: Check to make sure grain is of type IGrainWithStringKey
     string stringPrimaryKey; 
     Guid guidPrimaryKey = grain.GetPrimaryKey(out stringPrimaryKey);
     return stringPrimaryKey;
 }
        /// <summary>
        /// Returns a new <see cref="IJournal"/>, initialized with the provided actor.
        /// </summary>
        /// <param name="actor">
        /// The actor.
        /// </param>
        /// <returns>
        /// A new <see cref="IJournal"/>, initialized with the provided actor.
        /// </returns>
        public async Task<IJournal> Create(IGrain actor)
        {
            var table = Tables.GetOrAdd(
                actor.GetKind(), 
                async _ =>
                {
                    var t = this.client.GetTableReference(actor.GetKind() + TableNameSuffix);
                    await t.CreateIfNotExistsAsync();
                    return t;
                });

            return new AzureTableJournal(actor.GetIdString(), await table, this.jsonSettings);
        }
        // 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;
        }
 static string IdentityOf(IGrain grain)
 {
     return (grain as IGrainWithStringKey).GetPrimaryKeyString();
 }
Exemple #5
0
 public Task<List<IGrain>> GetMultipleGrainInterfaces_List()
 {
     IGrain[] grains = new IGrain[5];
     for (int i = 0; i < grains.Length; i++)
     {
         grains[i] = GrainFactory.GetGrain<ITestGrain>(i);
     }
     return Task.FromResult(grains.ToList());
 }
        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);
        }
Exemple #7
0
        protected void AssertIsInRange(long val, long lowerLimit, long upperLimit, IGrain grain, string reminderName, TimeSpan sleepFor)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Grain: {0} Grain PrimaryKey: {1}, Reminder: {2}, SleepFor: {3} Time now: {4}",
                grain.ToString(), grain.GetPrimaryKey(), reminderName, sleepFor, Time());
            sb.AppendFormat(
                " -- Expecting value in the range between {0} and {1}, and got value {2}.",
                lowerLimit, upperLimit, val);
            logger.Info(sb.ToString());

            bool tickCountIsInsideRange = lowerLimit <= val && val <= upperLimit;

            Skip.IfNot(tickCountIsInsideRange, string.Format("AssertIsInRange: {0}  -- WHICH IS OUTSIDE RANGE.", sb));
        }
Exemple #8
0
 /// <summary>
 /// Returns runtime identity of the given grain
 /// </summary>
 /// <param name="grain">Runtime grain instance</param>
 /// <returns>Id assigned to the grain</returns>
 public static string Of(IGrain grain)
 {
     string id;
     grain.GetPrimaryKeyLong(out id);
     return id;
 }
Exemple #9
0
 public Task<IGrain[]> GetMultipleGrainInterfaces_Array()
 {
     var grains = new IGrain[5];
     for (var i = 0; i < grains.Length; i++)
     {
         grains[i] = GrainFactory.GetGrain<ITestGrain>(i);
     }
     return Task.FromResult(grains);
 }
        // 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;
        }
 /// <summary>
 /// Returns a new <see cref="IJournal"/>, initialized with the provided <paramref name="actor"/>.
 /// </summary>
 /// <param name="actor">
 /// The actor.
 /// </param>
 /// <returns>
 /// A new <see cref="IJournal"/>, initialized for the provided <paramref name="actor"/>.
 /// </returns>
 public Task<IJournal> Create(IGrain actor)
 {
     var id = actor.GetIdString();
     var journal = Journal.GetOrAdd(id, _ => new InMemoryJournal());
     return Task.FromResult<IJournal>(journal);
 }