private FabricReplicatorEx CreateFabricReplicatorHelperEx(IAtomicGroupStateProviderEx stateProvider, ReplicatorSettings replicatorSettings)
        {
            Requires.Argument <IAtomicGroupStateProviderEx>("stateProvider", stateProvider).NotNull();

            // Initialize replicator
            if (replicatorSettings == null)
            {
                AppTrace.TraceSource.WriteNoise("StatefulServicePartitionEx.CreateFabricReplicatorEx", "Using default replicator settings");
            }
            else
            {
                AppTrace.TraceSource.WriteNoise("StatefulServicePartitionEx.CreateFabricReplicatorEx", "{0}", replicatorSettings);
            }

            NativeRuntime.IFabricReplicator nativeReplicator;
            NativeRuntime.IFabricAtomicGroupStateReplicatorEx nativeStateReplicator;

            AppTrace.TraceSource.WriteNoise("StatefulServicePartitionEx.CreateFabricReplicatorEx", "Creating state provider borker");
            AtomicGroupStateProviderExBroker stateProviderBroker = new AtomicGroupStateProviderExBroker(stateProvider);

            using (var pin = new PinCollection())
            {
                unsafe
                {
                    IntPtr replicatorSettingsNative = IntPtr.Zero;
                    IntPtr logSettingsNative        = IntPtr.Zero;

                    if (replicatorSettings != null)
                    {
                        replicatorSettingsNative = replicatorSettings.ToNative(pin);
                    }

                    nativeStateReplicator = this.nativePartition.CreateReplicatorEx(
                        stateProviderBroker,
                        replicatorSettingsNative,
                        logSettingsNative,
                        out nativeReplicator);
                }
            }

            NativeRuntime.IOperationDataFactory nativeOperationDataFactory = (NativeRuntime.IOperationDataFactory)nativeReplicator;
            if (nativeStateReplicator == null || nativeReplicator == null || nativeOperationDataFactory == null)
            {
                AppTrace.TraceSource.WriteError("StatefulServicePartitionEx.Create", "Native replicators could not be initialized.");
                throw new InvalidOperationException(StringResources.Error_StatefulServicePartition_Native_Replicator_Init_Failed);
            }

            stateProviderBroker.OperationDataFactory = new OperationDataFactoryWrapper(nativeOperationDataFactory);

            return(new FabricReplicatorEx(nativeReplicator, nativeStateReplicator, nativeOperationDataFactory));
        }
        /// <summary>
        /// Opens the replica.
        /// </summary>
        /// <param name="openMode">Replica open mode (new or existent).</param>
        /// <param name="partition">Stateful partition object.</param>
        /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
        /// <returns></returns>
        public virtual async Task <IReplicator> OpenAsync(ReplicaOpenMode openMode, IStatefulServicePartition partition, CancellationToken cancellationToken)
        {
            //
            // Check arguments.
            //
            if (null == partition)
            {
                AppTrace.TraceSource.WriteError("StatefulServiceReplica.Open", "{0}", this.ToString());
                throw new ArgumentNullException("partition");
            }

            //
            // Perform local open functionality.
            //
            AppTrace.TraceSource.WriteNoise("StatefulServiceReplica.Open", "{0}", this.ToString());

            //
            // Set partition related members.
            //
            this.servicePartition      = partition;
            this.serviceGroupPartition = partition as IServiceGroupPartition;
            this.servicePartitionEx    = partition as IStatefulServicePartitionEx;

            //
            // Create an implementation of the state provider broker.
            //
            this.stateProviderBroker = this.CreateStateProviderBroker();
            if (null == this.stateProviderBroker)
            {
                AppTrace.TraceSource.WriteError("StatefulServiceReplica.Open", "{0} invalid state provider broker", this.ToString());
                throw new InvalidOperationException();
            }

            //
            // Extract state providers.
            //
            this.stateProvider              = this.stateProviderBroker as IStateProvider;
            this.atomicGroupStateProvider   = this.stateProviderBroker as IAtomicGroupStateProvider;
            this.atomicGroupStateProviderEx = this.stateProviderBroker as IAtomicGroupStateProviderEx;
            if (null == this.stateProvider && null == this.atomicGroupStateProvider && null == this.atomicGroupStateProviderEx)
            {
                AppTrace.TraceSource.WriteError("StatefulServiceReplica.Open", "{0} invalid state providers", this.ToString());
                throw new InvalidOperationException();
            }

            //
            // Create replicator settings (replication and log).
            // For service groups, these settings are specified in the service manifest.
            //
            ReplicatorSettings replicatorSettings = null;

            if (null != this.serviceGroupPartition)
            {
                replicatorSettings = this.ReplicatorSettings;
            }

            ReplicatorLogSettings replicatorLogSettings = null;

            if (null != this.atomicGroupStateProviderEx && null != this.serviceGroupPartition)
            {
                replicatorLogSettings = this.ReplicatorLogSettings;
            }

            //
            // Create replicator.
            //
            FabricReplicator   replicator   = null;
            FabricReplicatorEx replicatorEx = null;

            if (null == this.atomicGroupStateProviderEx)
            {
                AppTrace.TraceSource.WriteInfo("StatefulServiceReplica.Open", "{0} creating replicator", this.ToString());
                //
                // v1 replicator.
                //
                replicator                      = this.servicePartition.CreateReplicator(this.stateProvider, replicatorSettings);
                this.stateReplicator            = replicator.StateReplicator;
                this.atomicGroupStateReplicator = this.stateReplicator as IAtomicGroupStateReplicator;
            }
            else
            {
                AppTrace.TraceSource.WriteInfo("StatefulServiceReplica.Open", "{0} creating atomic group replicator", this.ToString());
                //
                // v2 replicator.
                //
                replicatorEx = this.servicePartitionEx.CreateReplicatorEx(this.atomicGroupStateProviderEx, replicatorSettings, replicatorLogSettings);
                this.atomicGroupStateReplicatorEx = replicatorEx.StateReplicator;
            }

            //
            // Perform local open functionality. Initialize and open state provider broker.
            //
            this.stateProviderBroker.Initialize(this.initializationParameters);
            await this.stateProviderBroker.OpenAsync(
                openMode,
                this.servicePartitionEx,
                this.atomicGroupStateReplicatorEx,
                cancellationToken);

            //
            // Perform custom open functionality.
            //
            await this.OnOpenAsync(openMode, cancellationToken);

            //
            // Change current replica state.
            //
            this.replicaState = ReplicaState.Opened;
            AppTrace.TraceSource.WriteNoise("StatefulServiceReplica.State", "{0} is {1}", this.ToString(), this.replicaState);

            //
            // Done.
            //
            return((null != replicator) ? replicator as IReplicator : replicatorEx as IReplicator);
        }
 public FabricReplicatorEx CreateReplicatorEx(IAtomicGroupStateProviderEx stateProvider, ReplicatorSettings replicatorSettings, ReplicatorLogSettings logSettings)
 {
     return(Utility.WrapNativeSyncInvoke(
                () => this.CreateFabricReplicatorHelperEx(stateProvider, replicatorSettings),
                "StatefulServicePartitionEx.CreateFabricReplicator"));
 }
Exemplo n.º 4
0
        public AtomicGroupStateProviderExBroker(IAtomicGroupStateProviderEx stateProvider)
        {
            Requires.Argument <IAtomicGroupStateProviderEx>("stateProvider", stateProvider).NotNull();

            this.stateProvider = stateProvider;
        }