public async Task <IReplicator> OpenAsync( ReplicaOpenMode openMode, IStatefulServicePartition servicePartition, CancellationToken cancellationToken) { var self = this.serviceParameters; this.logger = new Logger(self) { Prefix = () => $"[{this.unsafeRole}] " }; this.logger.Log("OpenAsync"); IReplicator result; StateProvider provider; lock (this.replicaLock) { this.partition = servicePartition; provider = this.stateProvider = new StateProvider(this.logFilePath, this.logger, this.serializer); var replicatorSettings = ReplicatorSettings.LoadFrom( self.CodePackageActivationContext, ConfigPackageName, ReplicatorConfigSectionName); replicatorSettings.BatchAcknowledgementInterval = TimeSpan.FromMilliseconds(1); result = this.fabricReplicator = servicePartition.CreateReplicator(this.stateProvider, replicatorSettings); } await provider.Initialize(); this.logger.Log("Completed OpenAsync"); return(result); }
public Task <IReplicator> OpenAsync(ReplicaOpenMode openMode, IStatefulServicePartition partition, CancellationToken cancellationToken) { LogMessage(nameof(OpenAsync)); log = new InMemoryLog(LogMessage); replicator = partition.CreateReplicator(this, new ReplicatorSettings()); return(Task.FromResult((IReplicator)replicator)); }
public Task <IReplicator> OpenAsync(ReplicaOpenMode openMode, IStatefulServicePartition partition, CancellationToken cancellationToken) { this.TraceInfo("IStatefulServiceReplica::OpenAsync invoked. Open mode: {0}.", openMode); this.partition = partition; ReplicatorSettings replicatorSettings = this.CreateReplicatorSettings(); this.replicator = partition.CreateReplicator(this, replicatorSettings); this.TraceInfo("IStatefulServiceReplica::OpenAsync completed."); return(Task.FromResult(this.replicator as IReplicator)); }
/// <inheritdoc /> public Task <IReplicator> OpenAsync(ReplicaOpenMode openMode, IStatefulServicePartition partition, CancellationToken cancellationToken) { // TODO: open a service host this.listenerUri = new Uri("tcp://127.0.0.1:1234"); var replicatorSettings = this.GetReplicatorSettings(); ServiceEventSource.Current.ServiceMessage(this.serviceContext.ServiceContext, "ReplicatorSettings: {0}", replicatorSettings.ToString()); this.replicator = partition.CreateReplicator(this, replicatorSettings); this.serviceContext.Replicator = this.replicator; return(Task.FromResult <IReplicator>(this.replicator)); }
//// // IStatefulServiceReplica //// public Task <IReplicator> OpenAsync(ReplicaOpenMode openMode, IStatefulServicePartition partition, CancellationToken cancellationToken) { if (partition == null) { throw new ArgumentNullException("partition"); } Trace.WriteInfo(TraceType, "OpenAsync: {0}, OpenMode={1}", this.serviceId, openMode); ReplicatorSettings replicatorSettings = this.CreateReplicatorSettings(this.replicatorAddress); this.replicator = partition.CreateReplicator(this, replicatorSettings); this.partition = partition; this.serviceEndpoint = this.infrastructureServiceAgent.RegisterInfrastructureService( this.initializationParameters.PartitionId, this.initializationParameters.ReplicaId, this.coordinator); return(Utility.CreateCompletedTask <IReplicator>(this.replicator)); }
/// <inheritdoc/> protected override async Task RunAsync(CancellationToken cancellationToken) { // Find a better log directory this.operationLoggerPrimary = new OperationLogger(this.Context.CodePackageActivationContext.LogDirectory); FabricReplicator replicator = null; while ((replicator = this.serviceContext.Replicator) == null) { await Task.Delay(125).ConfigureAwait(false); } this.stateReplicator = replicator.StateReplicator; this.cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); var unusedTask = Task.Run(this.ProcessIncomingRequests); ServiceEventSource.Current.ServiceMessage(this.Context, "Replicator is ready"); try { while (!cancellationToken.IsCancellationRequested) { ServiceEventSource.Current.ServiceMessage( this.Context, "RunAsync alive: {0} - pending operations {1} LSN {2}", DateTime.UtcNow, this.pendingOperations.Count, this.lastSequenceNumber); await Task.Delay(1000 * 30, cancellationToken).ConfigureAwait(false); } } catch (TaskCanceledException) { } }
/// <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); }