private ReliableStateManagerReplicatorSettings LoadReplicatorSettingsFromConfigPackage(bool addChangeListener) { ReliableStateManagerReplicatorSettings settings = null; var configPackage = this.GetConfigurationPackage(); if (configPackage == null) { settings = new ReliableStateManagerReplicatorSettings(); } else { if (string.IsNullOrEmpty(this._configuration.ReplicatorSettingsSectionName) || !configPackage.Settings.Sections.Contains(this._configuration.ReplicatorSettingsSectionName)) { DataImplTrace.Source.WriteWarningWithId( TraceType, this._traceId, "Using default replicator settings, unable to load replicator settings config section named '{0}' from config package named '{1}'.", this._configuration.ReplicatorSettingsSectionName, this._configuration.ConfigPackageName); settings = new ReliableStateManagerReplicatorSettings(); } else { DataImplTrace.Source.WriteInfoWithId( TraceType, this._traceId, "Loading replicator settings using <config package name: '{0}', settings section name: '{1}'>", this._configuration.ConfigPackageName, this._configuration.ReplicatorSettingsSectionName); settings = ReliableStateManagerReplicatorSettingsUtil.LoadFrom( this.InitializationParameters.CodePackageActivationContext, this._configuration.ConfigPackageName, this._configuration.ReplicatorSettingsSectionName); } // SecurityCredentials.LoadFrom doesn't return the correct value when CredentialType is None in config. todo: find bug# // To workaround this bug, only read replicator security settings if CredentialType is present and not explicitly None. const string replicatorCredentialTypeName = "CredentialType"; if (string.IsNullOrEmpty(this._configuration.ReplicatorSecuritySectionName) || !configPackage.Settings.Sections.Contains(this._configuration.ReplicatorSecuritySectionName) || !configPackage.Settings.Sections[this._configuration.ReplicatorSecuritySectionName].Parameters.Contains(replicatorCredentialTypeName) || configPackage.Settings.Sections[this._configuration.ReplicatorSecuritySectionName].Parameters[replicatorCredentialTypeName].Value.Equals( "None", StringComparison.OrdinalIgnoreCase)) { DataImplTrace.Source.WriteWarningWithId( TraceType, this._traceId, "Using default replicator security settings, unable to load replicator security settings config section named '{0}' from config package named '{1}'.", this._configuration.ReplicatorSecuritySectionName, this._configuration.ConfigPackageName); } else { if (addChangeListener) { this.InitializationParameters.CodePackageActivationContext.ConfigurationPackageModifiedEvent += this.OnConfigurationPackageModified; } settings.SecurityCredentials = SecurityCredentials.LoadFrom( this.InitializationParameters.CodePackageActivationContext, this._configuration.ConfigPackageName, this._configuration.ReplicatorSecuritySectionName); } } if (string.IsNullOrWhiteSpace(settings.ReplicatorAddress)) { settings.ReplicatorAddress = this.TryGetDefaultReplicatorAddress(); } return(settings); }
/// <summary> /// OpenAsync is called when the replica is going to be actually used /// </summary> /// <param name="openMode">Open mode.</param> /// <param name="partitionObject">Service partition</param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns>Task that represents the asynchronous operation.</returns> async Task <IReplicator> IStatefulServiceReplica.OpenAsync( ReplicaOpenMode openMode, IStatefulServicePartition partitionObject, CancellationToken cancellationToken) { FabricEvents.Events.Lifecycle( this.tracer.Type, "OpenAsync" + " openMode: " + openMode + " replica: " + this.initializationParameters.ReplicaId); TaskScheduler.UnobservedTaskException += this.ProcessUnobservedTaskException; // Store the partitionObject this.partition = partitionObject; var statefulServiceContext = new StatefulServiceContext( FabricRuntime.GetNodeContext(), this.initializationParameters.CodePackageActivationContext, this.initializationParameters.ServiceTypeName, this.initializationParameters.ServiceName, this.initializationParameters.InitializationData, this.initializationParameters.PartitionId, this.initializationParameters.ReplicaId); this.transactionalReplicator.Initialize(statefulServiceContext, partitionObject); FabricEvents.Events.Lifecycle(this.tracer.Type, "OpenAsync: StateManager initialized"); // create the replicator // The Windows Fabric Replicator is used to actually replicate the data // The ReplicatorSettings are used for configuring the replicator - here we ask for them from the derived class // When using service groups Replicator Settings are described in the Settings.xml inside the configuration package. // So when the service is part of a service group it should use null as its Replicator Setting. FabricReplicator replicator; ReliableStateManagerReplicatorSettings replicatorSettings = null; var onOpenInvoked = false; try { if (this.Partition is IServiceGroupPartition) { replicatorSettings = new ReliableStateManagerReplicatorSettings(); ReliableStateManagerReplicatorSettingsUtil.LoadDefaultsIfNotSet(ref replicatorSettings); replicator = this.partition.CreateReplicator( this.transactionalReplicator, ReliableStateManagerReplicatorSettingsUtil.ToReplicatorSettings(replicatorSettings)); } else { replicatorSettings = await this.OnOpenAsync(cancellationToken).ConfigureAwait(false); onOpenInvoked = true; if (replicatorSettings == null) { replicatorSettings = new ReliableStateManagerReplicatorSettings(); } ReliableStateManagerReplicatorSettingsUtil.LoadDefaultsIfNotSet(ref replicatorSettings); replicator = this.partition.CreateReplicator( this.transactionalReplicator, ReliableStateManagerReplicatorSettingsUtil.ToReplicatorSettings(replicatorSettings)); } ServiceReplicaUtils.SetupLoggerPath(ref replicatorSettings, ref this.initializationParameters); var transactionalReplicatorSettings = new TransactionalReplicatorSettings { PublicSettings = replicatorSettings }; ReliableStateManagerReplicatorSettingsUtil.LoadInternalSettingsDefault(ref transactionalReplicatorSettings); this.stateReplicator = replicator.StateReplicator; this.transactionalReplicator.LoggingReplicator.FabricReplicator = this.stateReplicator; this.transactionalReplicator.LoggingReplicator.Tracer = this.tracer; // Starting local recovery await this.transactionalReplicator.OpenAsync(openMode, transactionalReplicatorSettings).ConfigureAwait(false); // Change state this.replicaState = ReplicaState.Opened; FabricEvents.Events.Lifecycle( this.tracer.Type, "OpenAsync: Finished opening replica " + this.initializationParameters.ReplicaId + " ReplicatorAddress: " + replicatorSettings.ReplicatorAddress + " ReplicatorListenAddress: " + replicatorSettings.ReplicatorListenAddress + " ReplicatorPublishAddress: " + replicatorSettings.ReplicatorPublishAddress); return(replicator); } catch (Exception e) { int innerHResult = 0; var flattenedException = Utility.FlattenException(e, out innerHResult); FabricEvents.Events.Exception_TStatefulServiceReplica( this.tracer.Type, "OpenAsync", flattenedException.GetType().ToString(), flattenedException.Message, flattenedException.HResult != 0 ? flattenedException.HResult : innerHResult, flattenedException.StackTrace); if (onOpenInvoked) { this.OnAbort(); } TaskScheduler.UnobservedTaskException -= this.ProcessUnobservedTaskException; throw; } }