Example #1
0
        protected async override Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            var client = new FabricClient();
            await client.PropertyManager.PutPropertyAsync(_serviceUri, CacheStoreProperty, CacheStorePropertyValue);

            _partitionCount = (await client.QueryManager.GetPartitionListAsync(_serviceUri)).Count;
        }
 /// <summary>
 /// This method is called when the replica is being opened and it is the final step of opening the service.
 /// Override this method to be notified that Open has completed for this replica's internal components.
 /// </summary>
 /// <param name="openMode"><see cref="T:System.Fabric.ReplicaOpenMode" /> for this service replica.</param>
 /// <param name="cancellationToken">Cancellation token to monitor for cancellation requests.</param>
 /// <returns> </returns>
 protected override async Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
 {
     foreach (var listener in this.serviceListeners)
     {
         await listener.OnOpenAsync(openMode, cancellationToken);
     }
 }
Example #3
0
 public Task <IReplicator> OpenAsync(
     ReplicaOpenMode openMode,
     IStatefulServicePartition partition,
     CancellationToken cancellationToken)
 {
     return(Task.FromResult <IReplicator>(null));
 }
 /// <summary>
 /// Initializes resources necessary for the replicator and for all the state providers.
 /// </summary>
 internal async Task OpenAsync(
     ReplicaOpenMode openMode,
     TransactionalReplicatorSettings transactionalReplicatorSettings)
 {
     this.metricManager.StartTask();
     await this.stateManager.OpenAsync(openMode, transactionalReplicatorSettings).ConfigureAwait(false);
 }
Example #5
0
        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);
        }
Example #6
0
        /// <summary>
        /// Creates or finds the log stream.
        /// If being created either initializes the log with default log records or records from backup log.
        /// </summary>
        /// <param name="openMode">Open mode of the replica.</param>
        /// <returns>Task that represents the asynchronous open operation.</returns>
        internal async Task <PhysicalLogReader> OpenAsync(ReplicaOpenMode openMode)
        {
            // TODO: Anurag: do we plumb c.token up?
            this.LogicalLog = await this.CreateLogFileAsync(openMode == ReplicaOpenMode.New, CancellationToken.None).ConfigureAwait(false);

            var logLogLength = this.LogicalLog.Length;

            if (logLogLength <= sizeof(int))
            {
                // No usable content in the log
                if (this.LogicalLog.WritePosition > 0)
                {
                    await this.LogicalLog.TruncateTail(0, CancellationToken.None).ConfigureAwait(false);

                    // Remove all contents and reset write cursor back to 0
                    Utility.Assert(this.LogicalLog.Length == 0, "this.logicalLog.Length == 0");
                    Utility.Assert(this.LogicalLog.WritePosition == 0, "this.logicalLog.WritePosition == 0");
                }

                using (
                    var logWriter = new PhysicalLogWriter(
                        this.LogicalLog,
                        this.emptyCallbackManager,
                        this.Tracer,
                        this.MaxWriteCacheSizeInMB,
                        this.IncomingBytesRateCounterWriter,
                        this.LogFlushBytesRateCounterWriter,
                        this.BytesPerFlushCounterWriter,
                        this.AvgFlushLatencyCounterWriter,
                        this.AvgSerializationLatencyCounterWriter,
                        false))
                {
                    var zeroIndexRecord = IndexingLogRecord.CreateZeroIndexingLogRecord();
                    logWriter.InsertBufferedRecord(zeroIndexRecord);
                    logWriter.InsertBufferedRecord(UpdateEpochLogRecord.CreateZeroUpdateEpochLogRecord());
                    var zeroBeginCheckpointRecord =
                        BeginCheckpointLogRecord.CreateZeroBeginCheckpointLogRecord();
                    logWriter.InsertBufferedRecord(zeroBeginCheckpointRecord);
                    logWriter.InsertBufferedRecord(BarrierLogRecord.CreateOneBarrierLogRecord());
                    var oneEndCheckpointRecord =
                        EndCheckpointLogRecord.CreateOneEndCheckpointLogRecord(
                            zeroBeginCheckpointRecord,
                            zeroIndexRecord);
                    logWriter.InsertBufferedRecord(oneEndCheckpointRecord);
                    var endCompleteCheckpointRecord =
                        new CompleteCheckpointLogRecord(
                            LogicalSequenceNumber.OneLsn,
                            zeroIndexRecord,
                            oneEndCheckpointRecord);
                    logWriter.InsertBufferedRecord(endCompleteCheckpointRecord);
                    await logWriter.FlushAsync("OpenAsync").ConfigureAwait(false);

                    // This additional await is required to ensure the log record was indeed flushed.
                    // Without this, the flushasync could succeed, but the log record flush could have failed due to a write error
                    await endCompleteCheckpointRecord.AwaitFlush().ConfigureAwait(false);
                }
            }

            return(new PhysicalLogReader(this));
        }
Example #7
0
 public Task <IReplicator> OpenAsync(ReplicaOpenMode openMode, IStatefulServicePartition partition, CancellationToken cancellationToken)
 {
     return(Task.Factory.StartNew <IReplicator>(() =>
     {
         ServiceImplementationHelper.HandleOpen(this.initParams.InitializationData, this.replicaId);
         return partition.CreateReplicator(this, null);
     }));
 }
Example #8
0
 Task <IReplicator> IStateProviderReplica.OpenAsync(
     ReplicaOpenMode openMode,
     IStatefulServicePartition partition,
     CancellationToken cancellationToken)
 {
     this.servicePartition = partition;
     return(this.stateManager.OpenAsync(openMode, partition, cancellationToken));
 }
        protected override Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            if (this.FailureMode == CrashMode.CrashInReplicaOpen)
            {
                throw new Exception("crash in replica open");
            }

            return(Task.FromResult <bool>(true));
        }
Example #10
0
 public Threading.Tasks.Task <IReplicator> OpenAsync(ReplicaOpenMode openMode, IStatefulServicePartition partition, CancellationToken cancellationToken)
 {
     return(Task.Factory.StartNew <IReplicator>(() =>
     {
         Debug.WriteLine("OnOpen");
         this.WriteEventToGlobalFile(SanityTest.OpenAsyncMarker);
         return partition.CreateReplicator(this, null);
     }));
 }
        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));
        }
 Task <IReplicator> IStateProviderReplica.OpenAsync(ReplicaOpenMode openMode, IStatefulServicePartition partition,
                                                    CancellationToken cancellationToken)
 {
     this.partition      = partition;
     isBackupInProgress  = 0;
     backupCallbackCts   = null;
     backupCallbackTask  = null;
     isClosingOrAborting = false;
     return(storeReplica.OpenAsync(openMode, partition, cancellationToken));
 }
Example #13
0
        async Task <IReplicator> IStateProviderReplica.OpenAsync(ReplicaOpenMode openMode, IStatefulServicePartition partition, CancellationToken cancellationToken)
        {
            var replicator = await((IStateProviderReplica)this._replica).OpenAsync(openMode, partition, cancellationToken);

            if (this._backupRestoreManager != null)
            {
                await this._backupRestoreManager.OpenAsync(openMode, partition, cancellationToken);
            }

            return(replicator);
        }
        /// <summary>
        /// Called during OpenAsync.
        /// The derived class should perform its initialization in this method.
        /// <param name="openMode">Replica open mode (new or existent).</param>
        /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
        /// <returns></returns>
        protected override Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            AppTrace.TraceSource.WriteNoise("StatefulServiceReplica.OnOpen", "{0}", this.ToString());

            //
            // Do nothing.
            //
            var tcs = new TaskCompletionSource <bool>();

            tcs.SetResult(true);
            return(tcs.Task);
        }
        Task <IReplicator> IStateProviderReplica.OpenAsync(
            ReplicaOpenMode openMode,
            IStatefulServicePartition partition,
            CancellationToken cancellationToken)
        {
            var fabricReplicator = partition.CreateReplicator(this, this.GetReplicatorSettings());

            this.replicator       = fabricReplicator.StateReplicator2;
            this.servicePartition = partition;

            return(Task.FromResult <IReplicator>(fabricReplicator));
        }
Example #16
0
        /// <summary>
        /// Invokes OnOpenAsync on the provided <paramref name="service"/>.
        /// </summary>
        /// <param name="service"></param>
        /// <param name="replicaOpenMode"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static Task InvokeOnOpenAsync(this StatefulServiceBase service,
                                             ReplicaOpenMode replicaOpenMode     = ReplicaOpenMode.Existing,
                                             CancellationToken?cancellationToken = null)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            //protected virtual Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
            var method = FindMethodInfo(service, "OnOpenAsync");

            return((Task)method.Invoke(service, new object[] { replicaOpenMode, cancellationToken ?? CancellationToken.None }));
        }
Example #17
0
        protected override Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            // Get the configuration settings and monitor for changes.
            this.Context.CodePackageActivationContext.ConfigurationPackageModifiedEvent += this.CodePackageActivationContext_ConfigurationPackageModifiedEvent;

            ConfigurationPackage configPackage = this.Context.CodePackageActivationContext.GetConfigurationPackageObject("Config");

            if (null != configPackage)
            {
                Interlocked.Exchange(ref this._settings, configPackage.Settings);
            }
            return(base.OnOpenAsync(openMode, cancellationToken));
        }
Example #18
0
        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));
        }
        protected override Task OnOpenAsync(ReplicaOpenMode openMode, IStatefulServicePartition partition, CancellationToken cancellationToken)
        {
            partition.ThrowIfNull("partition");
            this.partition = partition;

            Trace.WriteInfo(
                TraceType,
                "OpenAsync: serviceId = {0}, configSectionName = {1}, OpenMode = {2}",
                this.serviceId,
                this.configSectionName,
                openMode);

            return(base.OnOpenAsync(openMode, partition, cancellationToken));
        }
Example #20
0
        protected override async Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            await base.OnOpenAsync(openMode, cancellationToken);

            var services = new ServiceCollection();

            services.AddSingleton <ServiceContext>(Context);
            services.AddSingleton(Context);
            _configureServices(services);
            Container = services.BuildServiceProvider();

            // This requires the ServiceContext up a few lines, so we can't inject it in the constructor
            Container.GetService <TemporaryFiles>()?.Initialize();
        }
Example #21
0
        /// <summary>
        /// Called when the service instance is started.
        /// </summary>
        /// <param name="openMode"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            TimeSpan interval = TimeSpan.FromSeconds(30);

            //_nodeHealthTimer = new Timer(async (o) => { await ReportNodeHealthAndLoadAsync(interval); }, cancellationToken, interval, interval);

            // Create the global CPU performance counter and sample the CPU time.
            _cpuCounter      = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
            _cpuProcessTime  = Process.GetCurrentProcess().TotalProcessorTime;
            _timeOfCpuSample = DateTimeOffset.UtcNow;

            _eventSource?.OpenAsyncInvoked(Context.ServiceTypeName, Context.PartitionId, Context.ReplicaOrInstanceId);
            return(base.OnOpenAsync(openMode, cancellationToken));
        }
Example #22
0
        protected override async Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            await base.OnOpenAsync(openMode, cancellationToken);

            var services = new ServiceCollection();

            services.AddSingleton <ServiceContext>(Context);
            services.AddSingleton(Context);
            _configureServices(services);
            var builder = new ContainerBuilder();

            builder.Populate(services);
            _configureContainer(builder);
            Container = builder.Build();
        }
        /// <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));
        }
Example #24
0
        protected override Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            var services = new ServiceCollection();

            services.AddSingleton <ServiceContext>(Context);
            services.AddSingleton(Context);
            _configureServices(services);

            services.AddSingleton(StateManager);

            _container = services.BuildServiceProvider();

            // This requires the ServiceContext up a few lines, so we can't inject it in the constructor
            _container.GetService <TemporaryFiles>()?.Initialize();

            return(Task.CompletedTask);
        }
        protected override Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            var services = new ServiceCollection();

            services.AddSingleton <ServiceContext>(Context);
            services.AddSingleton(Context);
            _configureServices(services);

            services.AddSingleton(StateManager);

            var builder = new ContainerBuilder();

            builder.Populate(services);
            _configureContainer(builder);
            _container = builder.Build();
            return(Task.CompletedTask);
        }
Example #26
0
        protected override Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            this.metricReportCancellationSource = new CancellationTokenSource();
            CancellationToken token = this.metricReportCancellationSource.Token;

            this.metricReportTask = Task.Run(async() =>
            {
                while (true)
                {
                    token.ThrowIfCancellationRequested();

                    await ReportLoadMetricsAsync(token);
                    await Task.Delay(TimeSpan.FromSeconds(20), token);
                }
            }
                                             , token)
                                    .ContinueWith(t => t.Exception.Handle(e => true));

            return(base.OnOpenAsync(openMode, cancellationToken));
        }
        /// <inheritdoc/>
        protected override async Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            for (var attempt = 0; ; attempt++)
            {
                try
                {
                    await Subscribe();

                    break;
                }
                catch (BrokerNotFoundException)
                {
                    if (attempt > 10)
                    {
                        throw;
                    }

                    await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken);
                }
            }
        }
Example #28
0
        ////
        // 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));
        }
Example #29
0
        async Task <IReplicator> IStatefulServiceReplica.OpenAsync(
            ReplicaOpenMode openMode, IStatefulServicePartition partition, CancellationToken cancellationToken)
        {
            ServiceTrace.Source.WriteInfoWithId(
                TraceType,
                this.traceId,
                "OpenAsync");

            this.servicePartition             = partition;
            this.userServiceReplica.Partition = partition;

            var replicator = await this.stateProviderReplica.OpenAsync(openMode, partition, cancellationToken);

            Exception userReplicaEx = null;

            try
            {
                await this.userServiceReplica.OnOpenAsync(openMode, cancellationToken);
            }
            catch (Exception ex)
            {
                userReplicaEx = ex;

                ServiceTrace.Source.WriteWarningWithId(
                    TraceType,
                    this.traceId,
                    "Unhandled exception from userServiceReplica.OnOpenAsync() - {0}",
                    ex);
            }

            if (userReplicaEx != null)
            {
                await this.stateProviderReplica.CloseAsync(cancellationToken);

                throw userReplicaEx;
            }

            return(replicator);
        }
Example #30
0
        protected override Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            new Thread(async() => {
                while (!cancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        if (Partition != null && Partition.ReadStatus != PartitionAccessStatus.ReconfigurationPending)
                        {
                            int roomCount = await roomService.GetRoomCountAsync(cancellationToken);
                            Partition.ReportLoad(new List <LoadMetric> {
                                new LoadMetric("RoomCount", roomCount)
                            });
                        }
                    }
                    catch (Exception) { }

                    Thread.Sleep(1000);
                }
            }).Start();

            return(base.OnOpenAsync(openMode, cancellationToken));
        }
        protected override Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
        {
            this.LoadConfigPackageAndSubscribe();

            return base.OnOpenAsync(openMode, cancellationToken);
        }
 /// <summary>
 /// Executes when a replica of this service opens.
 /// Performing configuration operations here.
 /// </summary>
 /// <param name="openMode"></param>
 /// <param name="cancellationToken"></param>
 /// <returns></returns>
 protected override Task OnOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)
 {
     this.ConfigureService();
     return base.OnOpenAsync(openMode, cancellationToken);
 }