internal static IActorStateProvider CreateDefaultStateProvider(ActorTypeInformation actorTypeInfo)
        {
            // KvsActorStateProvider is used only when:
            //    1. Actor's [StatePersistenceAttribute] attribute has StatePersistence.Persisted.
            // VolatileActorStateProvider is used when:
            //    1. Actor's [StatePersistenceAttribute] attribute has StatePersistence.Volatile
            // NullActorStateProvider is used when:
            //    2. Actor's [StatePersistenceAttribute] attribute has StatePersistence.None OR
            //    3. Actor doesn't have [StatePersistenceAttribute] attribute.

            IActorStateProvider stateProvider = new NullActorStateProvider();

            if (actorTypeInfo.StatePersistence.Equals(StatePersistence.Persisted))
            {
                stateProvider = new KvsActorStateProvider();
            }
            else if (actorTypeInfo.StatePersistence.Equals(StatePersistence.Volatile))
            {
                stateProvider = new VolatileActorStateProvider();
            }

            // Get state provider override from settings if specified, used by tests to override state providers.
            var stateProviderOverride = GetActorStateProviderOverride();

            if (stateProviderOverride != null)
            {
                stateProvider = stateProviderOverride;
            }

            return(stateProvider);
        }
Example #2
0
 internal MockActorEventManager(ActorTypeInformation actorTypeInformation)
 {
     this.eventIdToEventTypeMap =
         actorTypeInformation.EventInterfaceTypes.ToDictionary(IdUtil.ComputeId, t => t);
     this.actorIdToEventProxyMap =
         new ConcurrentDictionary <ActorId, ConcurrentDictionary <Type, ActorEventProxy> >();
 }
 public ActorServiceFactory(
     ActorTypeInformation actorTypeInformation,
     ActorMethodFriendlyNameBuilder methodFriendlyNameBuilder,
     Func <StatefulServiceContext, ActorTypeInformation, ActorService> actorServiceFactory)
 {
     this.actorTypeInformation      = actorTypeInformation;
     this.methodFriendlyNameBuilder = methodFriendlyNameBuilder;
     this.actorServiceFactory       = actorServiceFactory;
 }
 /// <summary>
 /// Creates <see cref="ActorTypeInformation"/> from actorType.
 /// </summary>
 /// <param name="actorType">Type of class implementing the actor to create ActorTypeInforamtion for.</param>
 /// <param name="actorTypeInformation">When this method returns, contains ActorTypeInformation, if the creation of
 /// ActorTypeInformation from actorType succeeded, or null if the creation failed.
 /// The creation fails if the actorType parameter is null or it does not implement an actor.</param>
 /// <returns>true if ActorTypeInformation was successfully created for actorType; otherwise, false.</returns>
 /// <remarks>
 /// <para>Creation of ActorTypeInformation from actorType will fail when </para>
 /// <para>1. <see cref="System.Type.BaseType"/> for actorType is not of type <see cref="Actor"/>.</para>
 /// <para>2. actorType does not implement an interface deriving from <see cref="IActor"/> and is not marked as abstract.</para>
 /// <para>3. actorType implements more than one interface which derives from <see cref="IActor"/>
 /// but doesn't have <see cref="ActorServiceAttribute"/>.</para>
 /// </remarks>
 public static bool TryGet(Type actorType, out ActorTypeInformation actorTypeInformation)
 {
     try
     {
         actorTypeInformation = Get(actorType);
         return(true);
     }
     catch (ArgumentException)
     {
         actorTypeInformation = null;
         return(false);
     }
 }
Example #5
0
        /// <summary>
        /// Registers an actor service with Service Fabric runtime. This allows the runtime to create instances of the replicas for the actor service.
        /// </summary>
        /// <typeparam name="TActor">Type implementing actor.</typeparam>
        /// <param name="actorServiceFactory">Delegate that create new actor service.</param>
        /// <param name="timeout">A timeout period after which the registration operation will be canceled.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <returns>A task that represents the asynchronous operation to register actor service with Service Fabric runtime.</returns>
        public static async Task RegisterActorAsync <TActor>(
            Func <StatefulServiceContext, ActorTypeInformation, ActorService> actorServiceFactory,
            TimeSpan timeout = default(TimeSpan),
            CancellationToken cancellationToken = default(CancellationToken))
            where TActor : ActorBase
        {
            actorServiceFactory.ThrowIfNull("actorServiceFactory");

            var actorType            = typeof(TActor);
            var actorServiceType     = actorServiceFactory.GetMethodInfo().ReturnType;
            var actorTypeInformation = ActorTypeInformation.Get(actorType);
            var serviceTypeName      = ActorNameFormat.GetFabricServiceTypeName(actorTypeInformation.ImplementationType);

            try
            {
                var customActorServiceFactory = new ActorServiceFactory(
                    actorTypeInformation,
                    new ActorMethodFriendlyNameBuilder(actorTypeInformation),
                    actorServiceFactory,
                    new ActorMethodDispatcherMap(actorTypeInformation));

                await ServiceRuntime.RegisterServiceAsync(
                    serviceTypeName,
                    context => customActorServiceFactory.CreateActorService(context),
                    timeout,
                    cancellationToken);

                ActorFrameworkEventSource.Writer.ActorTypeRegistered(
                    actorType.ToString(),
                    actorServiceType.ToString(),
                    NodeName);
            }
            catch (Exception e)
            {
                ActorFrameworkEventSource.Writer.ActorTypeRegistrationFailed(
                    e.ToString(),
                    actorType.ToString(),
                    actorServiceType.ToString(),
                    NodeName);
                throw;
            }
        }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ActorService"/> class.
        /// </summary>
        /// <param name="context">Service context the actor service is operating under.</param>
        /// <param name="actorTypeInfo">The type information of the Actor.</param>
        /// <param name="actorFactory">The factory method to create Actor objects.</param>
        /// <param name="stateManagerFactory">The factory method to create <see cref="IActorStateManager"/></param>
        /// <param name="stateProvider">The state provider to store and access the state of the Actor objects.</param>
        /// <param name="settings">The settings used to configure the behavior of the Actor service.</param>
        public ActorService(
            StatefulServiceContext context,
            ActorTypeInformation actorTypeInfo,
            Func <ActorService, ActorId, ActorBase> actorFactory = null,
            Func <ActorBase, IActorStateProvider, IActorStateManager> stateManagerFactory = null,
            IActorStateProvider stateProvider = null,
            ActorServiceSettings settings     = null)
            : base(
                context,
                stateProvider ?? ActorStateProviderHelper.CreateDefaultStateProvider(actorTypeInfo))
        {
            this.actorTypeInformation = actorTypeInfo;
            this.stateProvider        = (IActorStateProvider)this.StateProviderReplica;
            this.settings             = ActorServiceSettings.DeepCopyFromOrDefaultOnNull(settings);

            // Set internal components
            this.actorActivator      = new ActorActivator(actorFactory ?? this.DefaultActorFactory);
            this.stateManagerFactory = stateManagerFactory ?? DefaultActorStateManagerFactory;
            this.actorManagerAdapter = new ActorManagerAdapter {
                ActorManager = new MockActorManager(this)
            };
            this.replicaRole = ReplicaRole.Unknown;
        }
Example #7
0
 void IActorStateProvider.Initialize(ActorTypeInformation actorTypeInformation)
 {
     this.actorTypeInformation = actorTypeInformation;
 }