示例#1
0
        public virtual ReplicationOperation Replicate(ReplicationOperation operation)
        {
            ReplicationOperation result = DaoRepository.Save(operation);

            Task.Run(() => (ReplicationOperation)operation.Execute(this));
            return(result);
        }
        protected sealed override IEnumerable <Replicable> OnApplyReplicationOperation(StatefulServiceExecutionContext executionContext, ReplicationOperation replicationOperation)
        {
            if (this.IsDisposed)
            {
                AppTrace.TraceMsg(TraceLogEventType.Information, "StatefulServiceReplicaT.OnApplyReplicationOperation", "OnApplyReplicationOperation called after service was disposed");
                return(Enumerable.Empty <Replicable>());
            }

            if (replicationOperation.Type == ReplicationOperationType.Replicate || replicationOperation.Type == ReplicationOperationType.Copy)
            {
                List <Replicable> replicablesToCommit = new List <Replicable>();

                ItemCollection <IComparable, StatefulProgramInstance> instances;
                using (this.instanceManager.GetInstances(LockPermission.Write, out instances))
                {
                    if (replicationOperation.Type == ReplicationOperationType.Copy)
                    {
                        instances.Clear();
                    }

                    int count = replicationOperation.Replicables.Count();

                    //// Bug#221476
                    //// Debug.Assert(count <= 1, string.Format(CultureInfo.InvariantCulture, "Unexpected number of replicables {0}", count));

                    Replicable <StatefulProgramInstance> replicableToApply = (Replicable <StatefulProgramInstance>)replicationOperation.Replicables.FirstOrDefault();
                    if (replicableToApply == null)
                    {
                        return(replicablesToCommit);
                    }

                    StatefulProgramInstance instance = (StatefulProgramInstance)this.GetOrCreateInstance(replicableToApply.Value.Id, Type.GetType(replicableToApply.Value.TypeName), replicableToApply.Value.InitializationParams);
                    if (instance != null)
                    {
                        instance.SetContextInstanceId();
                        ItemCollection <string, Replicable> replicables;
                        using (instance.GetReplicables(LockPermission.Write, out replicables))
                        {
                            if (replicationOperation.Type == ReplicationOperationType.Copy)
                            {
                                // Overwrite the existing replicables
                                replicables.Fill(replicableToApply.Value.DataContractReplicables);
                                replicablesToCommit.AddRange(replicables);
                            }
                            else if (replicableToApply.IsDisposed)
                            {
                                /// If this ProgramInstance was disposed, then dispose all associated Replicables and add them
                                /// to replicablesToCommit so that the persistence logic can pick it up and delete from the store
                                /// if required.
                                ItemCollection <string, Replicable> instanceReplicables;
                                using (instance.GetReplicables(LockPermission.Read, out instanceReplicables))
                                {
                                    foreach (Replicable replicable in instanceReplicables)
                                    {
                                        replicable.Dispose();
                                        replicablesToCommit.Add(replicable);
                                    }
                                }

                                this.DeleteInstanceInternal(instance.Id, false, -2);
                            }
                            else
                            {
                                foreach (Replicable replicable in replicableToApply.Value.DataContractReplicables)
                                {
                                    if (!replicable.IsDisposed)
                                    {
                                        if (replicables.Contains(replicable.Name))
                                        {
                                            // TODO: Need setitem support on ItemCollection
                                            Replicable existingReplicable = replicables[replicable.Name];
                                            if (existingReplicable.SequenceNumber <= replicable.SequenceNumber)
                                            {
                                                replicables.Remove(replicable.Name);
                                                replicables.Add(replicable);
                                            }
                                        }
                                        else
                                        {
                                            replicables.Add(replicable);
                                        }

                                        replicablesToCommit.Add(replicable);
                                    }
                                    else
                                    {
                                        replicables.Remove(replicable.Name);
                                    }
                                }
                            }
                        }

                        instance.SetContextInstanceId(replicablesToCommit);
                    }
                }

                return(replicablesToCommit);
            }
            else if (replicationOperation.Type == ReplicationOperationType.Evict)
            {
                int count = replicationOperation.Replicables.Count();
                Debug.Assert(count <= 1, string.Format(CultureInfo.InvariantCulture, "Unexpected number of replicables {0}", count));
                Replicable <StatefulProgramInstance> replicable = (Replicable <StatefulProgramInstance>)replicationOperation.Replicables.FirstOrDefault();
                if (replicable != null)
                {
                    ItemCollection <IComparable, StatefulProgramInstance> instances;
                    using (this.instanceManager.GetInstances(LockPermission.Write, out instances))
                    {
                        foreach (Replicable <StatefulProgramInstance> instance in replicationOperation.Replicables)
                        {
                            this.DeleteInstanceInternal(instance.Value.Id, false, -2);
                        }
                    }
                }

                return(replicationOperation.Replicables);
            }

            return(base.OnApplyReplicationOperation(executionContext, replicationOperation));
        }
        protected internal sealed override IEnumerable <Replicable> ReplicationOperationComplete(StatefulServiceExecutionContext executionContext, ReplicationResult result, ReplicationOperation replicationOperation)
        {
            if (this.IsDisposed)
            {
                AppTrace.TraceMsg(TraceLogEventType.Information, "StatefulServiceReplicaT.ReplicationOperationComplete", "ReplicationOperationComplete called after service was disposed");
                return(Enumerable.Empty <Replicable>());
            }

            StatefulProgramInstance instance = null;
            Replicable <StatefulProgramInstance> replicableToCommit = replicationOperation.Replicables.FirstOrDefault() as Replicable <StatefulProgramInstance>;

            if (replicableToCommit != null)
            {
                instance = this.instanceManager.GetInstance(replicableToCommit.Value.Id) as StatefulProgramInstance;
                if (instance != null)
                {
                    instance.SetContextInstanceId();
                }
                else if (replicableToCommit.Value != null)
                {
                    Debug.Assert(replicationOperation.Type == ReplicationOperationType.Evict, "Unexpected operation type");
                    replicableToCommit.Value.SetContextInstanceId();
                }
                else
                {
                    Debug.Assert(false, "replicableToCommit.Value is null");
                }
            }

            AppTrace.TraceMsg(
                TraceLogEventType.Information,
                "StatefulComponent.ReplicationOperationComplete",
                "replicationOperation.Type={0}, result={1}, replicables.Count={2}, replicableToCommit={3}, programInstance={4}",
                replicationOperation.Type,
                result,
                replicationOperation.Replicables.Count(),
                replicableToCommit != null ? replicableToCommit.Name : "Null",
                instance != null ? instance.Id : "Null");

            IEnumerable <Replicable> replicablesToCommit = null;

            if (replicationOperation.Type == ReplicationOperationType.Replicate)
            {
                if (result == ReplicationResult.Success)
                {
                    List <Replicable> changes = new List <Replicable>();

                    if (replicableToCommit.IsDisposed)
                    {
                        foreach (Replicable replicable in replicableToCommit.Value.DataContractReplicables)
                        {
                            changes.Add(replicable);
                        }
                    }
                    else
                    {
                        ItemCollection <string, Replicable> replicables;
                        using (instance.GetReplicables(LockPermission.Write, out replicables))
                        {
                            foreach (Replicable replicable in replicableToCommit.Value.DataContractReplicables)
                            {
                                if (replicables.Contains(replicable.Name))
                                {
                                    replicables.Remove(replicable.Name);
                                    replicables.Add(replicable);
                                }
                                else
                                {
                                    replicables.Add(replicable);
                                }

                                changes.Add(replicable);
                            }
                        }
                    }

                    replicablesToCommit = changes;
                }
                else
                {
                    if (instance != null)
                    {
                        AppTrace.TraceMsg(TraceLogEventType.Error, "StatefulServiceReplicaT.ReplicationOperationComplete", "Recycling StatefulProgramInstance with id '{0}'", instance.Id);
                        instance.RecycleInstance();
                    }

                    replicablesToCommit = base.ReplicationOperationComplete(executionContext, result, replicationOperation);
                }
            }

            if (replicablesToCommit == null)
            {
                replicablesToCommit = base.ReplicationOperationComplete(executionContext, result, replicationOperation);
            }

            return(replicablesToCommit);
        }
        protected internal sealed override ReplicationOperation PrepareReplicationOperation(StatefulServiceExecutionContext executionContext, ReplicationOperationType type, IEnumerable <Replicable> replicables, IDictionary <string, string> operationMetadata)
        {
            if (this.IsDisposed)
            {
                AppTrace.TraceMsg(TraceLogEventType.Information, "StatefulServiceReplicaT.PrepareReplicationOperation", "PrepareReplicationOperation called after service was disposed");
                return(null);
            }

            ReplicationOperation replicationOperation = null;

            if (type == ReplicationOperationType.Replicate)
            {
                StatefulProgramInstance instance = this.instanceManager.GetInstance(StatefulProgramInstance.CurrentInstanceId);
                if (instance == null)
                {
                    AppTrace.TraceMsg(TraceLogEventType.Error, "StatefulServiceReplicaT.PrepareReplicationOperation", "Could not find StatefulProgramInstance with id '{0}'", StatefulProgramInstance.CurrentInstanceId);
                    throw new InvalidOperationException("Replication needs to be triggered in context of a program instance.");
                }

                IEnumerable <Replicable> attributedReplicables = instance.CollectAttributedReplicableMembers(replicables);
                if (attributedReplicables != null)
                {
                    replicables = Enumerable.Concat(replicables, attributedReplicables);
                }

                instance.SetContextInstanceId(replicables);

                IEnumerable <Replicable> invalidReplicables;
                if (!instance.ValidateReplicablesMapToTypeMembers(replicables, out invalidReplicables))
                {
                    string        missingReplicables = StatefulProgramInstance.GetReplicablesString(invalidReplicables);
                    StringBuilder error = new StringBuilder();
                    error.AppendFormat("Replication cannot be triggered as following members were not found in instance of type '{0}'.", instance.TypeInfo.ReplicableType.FullName);
                    error.AppendLine(missingReplicables.ToString());
                    AppTrace.TraceMsg(TraceLogEventType.Information, "StatefulServiceReplicaT.PrepareReplicationOperation", error.ToString());
                    throw new InvalidOperationException(error.ToString());
                }

                // TODO: When we make Replicable name to be IComparable, we can remove the ToString call from here.
                replicationOperation = new ReplicationOperation(
                    type,
                    new List <Replicable>()
                {
                    new Replicable <StatefulProgramInstance>(instance.Id.ToString(), new StatefulProgramInstance(this, instance.Id, instance.TypeInfo.ReplicableType, instance.InitializationParams, replicables, instance.Singleton))
                },
                    operationMetadata);
            }
            else if (type == ReplicationOperationType.Copy)
            {
                ItemCollection <IComparable, StatefulProgramInstance> instances;
                using (this.instanceManager.GetInstances(LockPermission.Read, out instances))
                {
                    List <Replicable <StatefulProgramInstance> > replicablesToCopy = new List <Replicable <StatefulProgramInstance> >();
                    foreach (StatefulProgramInstance instance in instances)
                    {
                        // TODO: When we make Replicable name to be IComparable, we can remove the ToString call from here.
                        replicablesToCopy.Add(new Replicable <StatefulProgramInstance>(instance.Id.ToString(), instance));
                    }

                    replicationOperation = new ReplicationOperation(
                        type,
                        replicablesToCopy,
                        operationMetadata);
                }
            }

            if (replicationOperation == null)
            {
                replicationOperation = new ReplicationOperation(type, replicables, operationMetadata);
            }

            return(replicationOperation);
        }
示例#5
0
        public virtual IEnumerable <object> NextSet(ReplicationOperation operation)
        {
            Type type = TypeResolver.ResolveType(operation);

            yield return(DaoRepository.Top(operation.BatchSize, type, QueryFilter.Where("Cuid") > operation.FromCuid));
        }
 public int IndexOf(ReplicationOperation operation)
 {
 }
 public void CopyTo(ReplicationOperation[] operations, int index)
 {
 }
 // Methods
 public bool Contains(ReplicationOperation operation)
 {
 }
 public int IndexOf(ReplicationOperation operation)
 {
 }
 // Methods
 public bool Contains(ReplicationOperation operation)
 {
 }