protected override Task OnOpenAsync(StatefulServiceExecutionContext executionContext) { TaskCompletionSource <LifeCycleState> taskProducer = new TaskCompletionSource <LifeCycleState>(); // Generate proxy type, this is static typeinfo generated once per type. ReplicableTypeInfo typeInfo = ReplicableTypeInfo.GenerateReplicableTypeInfo(typeof(TInstance)); if (typeInfo == null) { taskProducer.SetException(new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Unexpected failure. ReplicableTypeInfo could not be generated for type '{0}'.", typeof(TInstance).FullName))); } else { taskProducer.SetResult(LifeCycleState.Opened); } return(taskProducer.Task); }
public static void ClassInitialize(TestContext context) { TestStatefulServiceReplica service = new TestStatefulServiceReplica(); StatefulServiceReplicaBroker broker = new StatefulServiceReplicaBroker(service, ReplicationWithLockingTests.DefaultInitializationParameters, ReplicationWithLockingTests.DefaultReplicaId); TestNativePartition nativePartition = new TestNativePartition(); StatefulServicePartition statefulServicePartition = new StatefulServicePartition(nativePartition, ServicePartitionInformation.CreateFromNative(nativePartition.PartitionInfo)); //((IServiceCatalog)ServiceCatalog.Current).AddService(service.Name, statefulServicePartition.Id, service); ((IStatefulServiceReplica)service).OpenAsync(statefulServicePartition); ((IStatefulServiceReplica)service).ChangeRoleAsync(ReplicaRole.Primary).Wait(); ReplicationWithLockingTests.Context = new StatefulServiceExecutionContext( service, new Dictionary <string, object> { { System.Fabric.Common.Constants.PropertyNames.Service, service.Name }, { System.Fabric.Common.Constants.PropertyNames.PartitionId, service.Partition.Id } }); ReplicationWithLockingTests.TypeInfo = ReplicableTypeInfo.GenerateReplicableTypeInfo(typeof(SampleService)); ReplicationWithLockingTests.ProgramInstance = new StatefulProgramInstance(service, Guid.NewGuid(), TypeInfo, new SampleService()); ReplicationWithLockingTests.StateReplicator = nativePartition.StateReplicator; }
private StatefulProgramInstance GetOrCreateInstance(IComparable instanceId, Type instanceType, object[] initializationParameters) { Requires.Argument("instanceId", instanceId).NotNull(); Requires.Argument("instanceType", instanceType).NotNull(); StatefulServiceExecutionContext executionContext = StatefulServiceExecutionContext.Current as StatefulServiceExecutionContext; if (executionContext == null || executionContext.Partition == null) { throw new InvalidOperationException("Program instance cannot be created in context of a partition. Please ensure that StatefulServiceReplica.Invoke is called."); } ReplicableTypeInfo replicableTypeInfo = null; Dictionary <Type, ReplicableTypeInfo> replicableTypeInfos; using (ReplicableTypeInfo.GetReplicableTypeInfos(LockPermission.Read, out replicableTypeInfos)) { replicableTypeInfos.TryGetValue(instanceType, out replicableTypeInfo); if (replicableTypeInfo == null) { return(null); } } StatefulProgramInstance instance = null; ItemCollection <IComparable, StatefulProgramInstance> instances; using (this.instanceManager.GetInstances(LockPermission.Write, out instances)) { if (instances.Contains(instanceId)) { instance = instances[instanceId]; } else if (this.ReplicaRole != Fabric.ReplicaRole.Primary) { /// Since we are the secondary, create a new instance. Resurrection will only happen on the primary which /// will then replicate the state to all secondaries. instance = new StatefulProgramInstance(this, instanceId, replicableTypeInfo, initializationParameters, instanceId.CompareTo(StatefulServiceReplica <TInstance> .SingletonSessionId) == 0); instances.Add(instance); } } if (instance == null) { /// TODO: Make this async. The asynchrony needs to be plumbed all the way through. WCF interfaces don't support /// asynchrony when obtaining service instances, so we will need to live with this there but can do better for the /// rest. StatefulComponentStateManager stateManager = (StatefulComponentStateManager)this.StateManager; Task <IEnumerable <Replicable> > task = stateManager.ResurrectProgramInstanceAsync(executionContext, instanceId); Task.WaitAll(task); IEnumerable <Replicable> replicables = !task.IsFaulted ? task.Result : Enumerable.Empty <Replicable>(); using (this.instanceManager.GetInstances(LockPermission.Write, out instances)) { if (instances.Contains(instanceId)) { instance = instances[instanceId]; } else { StatefulProgramInstance statefulProgramInstance = new StatefulProgramInstance(this, instanceId, replicableTypeInfo, initializationParameters, instanceId.CompareTo(StatefulServiceReplica <TInstance> .SingletonSessionId) == 0); instance = statefulProgramInstance; instances.Add(instance); ItemCollection <string, Replicable> typeReplicables; using (statefulProgramInstance.GetReplicables(LockPermission.Write, out typeReplicables)) { foreach (Replicable replicable in replicables) { typeReplicables.Remove(replicable); typeReplicables.Add(replicable); } } } } } return(instance); }