コード例 #1
0
        public Task ClearStateAsync(string grainType, Orleans.Runtime.GrainReference grainReference, Orleans.IGrainState grainState)
        {
            var fileInfo = this.GetFileInfo(grainType, grainReference);

            fileInfo.Delete();
            return(TaskDone.Done);
        }
コード例 #2
0
 public async Task ClearStateAsync(string grainType, Orleans.Runtime.GrainReference grainReference, IGrainState grainState)
 {
     try
     {
         var Client = new CouchbaseClient();
         await Task.Run(() => Client.Remove(GetGrainKey(grainType, grainReference)));
     }
     catch (Exception ex)
     {
         Log.Error(0, "Error in ClearStateAsync", ex);
     }
 }
コード例 #3
0
        public Task WriteStateAsync(string grainType, Orleans.Runtime.GrainReference grainReference, Orleans.IGrainState grainState)
        {
            var json     = JsonConvert.SerializeObject(grainState.AsDictionary());
            var fileInfo = this.GetFileInfo(grainType, grainReference);

            using (var stream = fileInfo.OpenWrite())
            {
                using (var writer = new StreamWriter(stream))
                {
                    return(writer.WriteAsync(json));
                }
            }
        }
コード例 #4
0
        public async Task ReadStateAsync(string grainType, Orleans.Runtime.GrainReference grainReference, Orleans.IGrainState grainState)
        {
            var fileInfo = this.GetFileInfo(grainType, grainReference);

            if (!fileInfo.Exists)
            {
                return;
            }

            using (var stream = fileInfo.OpenText())
            {
                var json = await stream.ReadToEndAsync();

                var data = JsonConvert.DeserializeObject <Dictionary <string, object> >(json);
                grainState.SetAll(data);
            }
        }
コード例 #5
0
 public async Task WriteStateAsync(string grainType, Orleans.Runtime.GrainReference grainReference, IGrainState grainState)
 {
     try
     {
         var Client   = new CouchbaseClient();
         var GrainKey = GetGrainKey(grainType, grainReference);
         var json     = JsonConvert.SerializeObject(grainState.AsDictionary());
         await Task.Run(() =>
         {
             Client.Store(StoreMode.Set, GrainKey, json);
         });
     }
     catch (Exception ex)
     {
         Log.Error(0, "Error in WriteStateAsync", ex);
     }
 }
コード例 #6
0
        public async Task ReadStateAsync(string grainType, Orleans.Runtime.GrainReference grainReference, IGrainState grainState)
        {
            var GrainKey = GetGrainKey(grainType, grainReference);

            try
            {
                var Client = new CouchbaseClient();
                if (Client.KeyExists(GrainKey))
                {
                    var json = "";
                    await Task.Run(() => { json = Client.Get(GrainKey).ToString(); });

                    var data = JsonConvert.DeserializeObject <Dictionary <String, Object> >(json);
                    grainState.SetAll(data);
                }
            }
            catch (Exception ex)
            {
                Log.Error(0, "Error in ReadStateAsync", ex);
            }
        }
コード例 #7
0
 internal static void OnStorageRead(IStorageProvider storage, string grainType, GrainReference grain, TimeSpan latency)
 {
     StorageReadTotal.Increment();
     if (latency > TimeSpan.Zero)
     {
         StorageReadLatency.AddSample(latency);
     }
 }
コード例 #8
0
 /// <summary>
 /// Converts the provided reference into a string.
 /// </summary>
 public string ToKeyString(GrainReference grainReference) => grainReference.ToKeyString();
コード例 #9
0
 internal static void OnStorageDeleteError(IStorageProvider storage, string grainType, GrainReference grain)
 {
     StorageClearErrors.Increment();
 }
コード例 #10
0
        private void SendRequestMessage(
            GrainReference target,
            Message message,
            TaskCompletionSource <object> context,
            string debugContext,
            InvokeMethodOptions options,
            string genericArguments = null)
        {
            // fill in sender
            if (message.SendingSilo == null)
            {
                message.SendingSilo = MySilo;
            }
            if (!String.IsNullOrEmpty(genericArguments))
            {
                message.GenericGrainType = genericArguments;
            }

            SchedulingContext schedulingContext = RuntimeContext.CurrentActivationContext as SchedulingContext;

            ActivationData sendingActivation = null;

            if (schedulingContext == null)
            {
                var clientAddress = this.HostedClient.ClientAddress;
                message.SendingGrain      = clientAddress.Grain;
                message.SendingActivation = clientAddress.Activation;
            }
            else
            {
                switch (schedulingContext.ContextType)
                {
                case SchedulingContextType.SystemThread:
                    throw new ArgumentException(
                              String.Format(
                                  "Trying to send a message {0} on a silo not from within grain and not from within system target (RuntimeContext is of SchedulingContextType.SystemThread type)",
                                  message),
                              "context");

                case SchedulingContextType.Activation:
                    message.SendingActivation = schedulingContext.Activation.ActivationId;
                    message.SendingGrain      = schedulingContext.Activation.Grain;
                    sendingActivation         = schedulingContext.Activation;
                    break;

                case SchedulingContextType.SystemTarget:
                    message.SendingActivation = schedulingContext.SystemTarget.ActivationId;
                    message.SendingGrain      = ((ISystemTargetBase)schedulingContext.SystemTarget).GrainId;
                    break;
                }
            }

            // fill in destination
            var targetGrainId = target.GrainId;

            message.TargetGrain = targetGrainId;
            if (targetGrainId.IsSystemTarget)
            {
                SiloAddress targetSilo = (target.SystemTargetSilo ?? MySilo);
                message.TargetSilo       = targetSilo;
                message.TargetActivation = ActivationId.GetSystemActivation(targetGrainId, targetSilo);
                message.Category         = targetGrainId.Equals(Constants.MembershipOracleId) ?
                                           Message.Categories.Ping : Message.Categories.System;
            }
            if (target.IsObserverReference)
            {
                message.TargetObserverId = target.ObserverId;
            }

            if (debugContext != null)
            {
                message.DebugContext = debugContext;
            }

            var oneWay = (options & InvokeMethodOptions.OneWay) != 0;

            if (context == null && !oneWay)
            {
                logger.Warn(ErrorCode.IGC_SendRequest_NullContext, "Null context {0}: {1}", message, Utils.GetStackTrace());
            }

            if (message.IsExpirableMessage(this.messagingOptions.DropExpiredMessages))
            {
                message.TimeToLive = this.messagingOptions.ResponseTimeout;
            }

            if (!oneWay)
            {
                var callbackData = new CallbackData(this.sharedCallbackData, context, message);
                callbacks.TryAdd(message.Id, callbackData);
            }

            if (targetGrainId.IsSystemTarget)
            {
                // Messages to system targets bypass the task system and get sent "in-line"
                this.Dispatcher.TransportMessage(message);
            }
            else
            {
                this.Dispatcher.SendMessage(message, sendingActivation);
            }
        }
コード例 #11
0
 public bool InRange(GrainReference grainReference)
 {
     return(InRange(grainReference.GetUniformHashCode()));
 }
コード例 #12
0
ファイル: GrainReference.cs プロジェクト: zhangthen/orleans
 private static string CreateMessage(GrainReference grainReference)
 {
     return($"Attempted to use a GrainReference which has not been bound to the runtime: {grainReference.ToDetailedString()}." +
            $" Use the {nameof(IGrainFactory)}.{nameof(IGrainFactory.BindGrainReference)} method to bind this reference to the runtime.");
 }
コード例 #13
0
ファイル: GrainReference.cs プロジェクト: zhangthen/orleans
 internal GrainReferenceNotBoundException(GrainReference grainReference) : base(CreateMessage(grainReference))
 {
 }
コード例 #14
0
 private bool IsUnordered(GrainReference reference)
 {
     return(this.RuntimeClient.GrainTypeResolver?.IsUnordered(reference.GrainId.TypeCode) == true);
 }
コード例 #15
0
        private SiloAddress MapGrainReferenceToSiloRing(GrainReference grainRef)
        {
            var hashCode = grainRef.GetUniformHashCode();

            return(ConsistentRingProvider.GetPrimaryTargetSilo(hashCode));
        }
コード例 #16
0
        private void SendRequestMessage(
            GrainReference target,
            Message message,
            TaskCompletionSource <object> context,
            Action <Message, TaskCompletionSource <object> > callback,
            string debugContext,
            InvokeMethodOptions options,
            string genericArguments = null)
        {
            // fill in sender
            if (message.SendingSilo == null)
            {
                message.SendingSilo = MySilo;
            }
            if (!String.IsNullOrEmpty(genericArguments))
            {
                message.GenericGrainType = genericArguments;
            }

            SchedulingContext schedulingContext = RuntimeContext.Current != null ?
                                                  RuntimeContext.Current.ActivationContext as SchedulingContext : null;

            ActivationData sendingActivation = null;

            if (schedulingContext == null)
            {
                throw new InvalidExpressionException(
                          String.Format("Trying to send a message {0} on a silo not from within grain and not from within system target (RuntimeContext is not set to SchedulingContext) "
                                        + "RuntimeContext.Current={1} TaskScheduler.Current={2}",
                                        message,
                                        RuntimeContext.Current == null ? "null" : RuntimeContext.Current.ToString(),
                                        TaskScheduler.Current));
            }
            switch (schedulingContext.ContextType)
            {
            case SchedulingContextType.SystemThread:
                throw new ArgumentException(
                          String.Format("Trying to send a message {0} on a silo not from within grain and not from within system target (RuntimeContext is of SchedulingContextType.SystemThread type)", message), "context");

            case SchedulingContextType.Activation:
                message.SendingActivation = schedulingContext.Activation.ActivationId;
                message.SendingGrain      = schedulingContext.Activation.Grain;
                sendingActivation         = schedulingContext.Activation;
                break;

            case SchedulingContextType.SystemTarget:
                message.SendingActivation = schedulingContext.SystemTarget.ActivationId;
                message.SendingGrain      = schedulingContext.SystemTarget.GrainId;
                break;
            }

            // fill in destination
            var targetGrainId = target.GrainId;

            message.TargetGrain = targetGrainId;
            if (targetGrainId.IsSystemTarget)
            {
                SiloAddress targetSilo = (target.SystemTargetSilo ?? MySilo);
                message.TargetSilo       = targetSilo;
                message.TargetActivation = ActivationId.GetSystemActivation(targetGrainId, targetSilo);
                message.Category         = targetGrainId.Equals(Constants.MembershipOracleId) ?
                                           Message.Categories.Ping : Message.Categories.System;
            }
            if (target.IsObserverReference)
            {
                message.TargetObserverId = target.ObserverId;
            }

            if (debugContext != null)
            {
                message.DebugContext = debugContext;
            }

            var oneWay = (options & InvokeMethodOptions.OneWay) != 0;

            if (context == null && !oneWay)
            {
                logger.Warn(ErrorCode.IGC_SendRequest_NullContext, "Null context {0}: {1}", message, new StackTrace());
            }

            if (message.IsExpirableMessage(Config.Globals))
            {
                message.Expiration = DateTime.UtcNow + ResponseTimeout + Constants.MAXIMUM_CLOCK_SKEW;
            }

            if (!oneWay)
            {
                var callbackData = new CallbackData(
                    callback,
                    TryResendMessage,
                    context,
                    message,
                    () => UnRegisterCallback(message.Id),
                    Config.Globals);
                callbacks.TryAdd(message.Id, callbackData);
                callbackData.StartTimer(ResponseTimeout);
            }

            if (targetGrainId.IsSystemTarget)
            {
                // Messages to system targets bypass the task system and get sent "in-line"
                dispatcher.TransportMessage(message);
            }
            else
            {
                dispatcher.SendMessage(message, sendingActivation);
            }
        }