public AtomicGroupStateProviderBroker(IStateProvider stateProvider, IAtomicGroupStateProvider atomicGroupStateProvider) :
            base(stateProvider)
        {
            Requires.Argument("atomicGroupStateProvider", atomicGroupStateProvider).NotNull();

            this.atomicGroupStateProvider = atomicGroupStateProvider;
        }
        private static StateProviderBroker CreateStateProviderBroker(IStateProvider stateProvider)
        {
            AppTrace.TraceSource.WriteNoise("StatefulPartition.CreateStateProviderBroker", "Creating broker for {0}", stateProvider);

            StateProviderBroker stateProviderBroker;

            IAtomicGroupStateProvider atomicGroupStateProvider = stateProvider as IAtomicGroupStateProvider;

            if (atomicGroupStateProvider != null)
            {
                AppTrace.TraceSource.WriteNoise("StatefulPartition.CreateStateProviderBroker", "StateProvider is IFabricAtomicGroupStateProvider");
                stateProviderBroker = new AtomicGroupStateProviderBroker(stateProvider, atomicGroupStateProvider);
            }
            else
            {
                stateProviderBroker = new StateProviderBroker(stateProvider);
            }

            return(stateProviderBroker);
        }
        /// <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);
        }