Example #1
0
        internal StatefulServiceReplicaAdapter(
            StatefulServiceContext context,
            IStatefulUserServiceReplica userServiceReplica)
        {
            this.serviceContext = context;
            this.traceId        = ServiceTrace.GetTraceIdForReplica(context.PartitionId, context.ReplicaId);
            this.serviceHelper  = new ServiceHelper(TraceType, this.traceId);

            this.servicePartition       = null;
            this.replicaListeners       = null;
            this.communicationListeners = null;
            this.endpointCollection     = new ServiceEndpointCollection();

            this.runAsynCancellationTokenSource = null;
            this.executeRunAsyncTask            = null;

            this.userServiceReplica           = userServiceReplica;
            this.userServiceReplica.Addresses = this.endpointCollection.ToReadOnlyDictionary();

            // The state provider replica should ideally be initialized
            // here (this.stateProviderReplica.Initialize()) with ServiceContext.
            // However the initialize function takes in StatefulServiceInitializationParameter
            // and resides in the DATA layer. DATA layer lies below SERVICES layer
            // and is agnostic of services and ServiceContext lies in SERVICES layer.
            // For now state provider replica is initialized when runtime calls
            // IStatefulServiceReplica.Initialize(StatefulServiceInitializationParameters initializationParameters)
            this.stateProviderReplica = this.userServiceReplica.CreateStateProviderReplica();
        }
Example #2
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);
        }
        public WindowsUpdateServiceCoordinator(IConfigStore configStore, string configSection, KeyValueStoreReplica kvsReplica, IStatefulServicePartition partition, IExceptionHandlingPolicy exceptionPolicy)
        {
            configStore.ThrowIfNull("configStore");
            configSection.ThrowIfNullOrWhiteSpace("configSectionName");
            kvsReplica.ThrowIfNull("kvsReplica");
            partition.ThrowIfNull("partition");
            exceptionPolicy.ThrowIfNull("exceptionPolicy");

            this.commandProcessor  = new FabricClientWrapper();
            this.store             = kvsReplica;
            this.partition         = partition;
            this.packageRetriever  = new UpdatePackageRetriever();
            this.serviceName       = new Uri(Constants.SystemServicePrefix + configSection);
            this.configStore       = configStore;
            this.configSectionName = configSection;
            this.exceptionPolicy   = exceptionPolicy;

            // Read all the configuration values
            string coordinatorType = configStore.ReadUnencryptedString(configSection, Constants.ConfigurationSection.CoordinatorType);

            this.testMode   = false;
            this.testSrcDir = string.Empty;
            if (coordinatorType.Equals(Constants.ConfigurationSection.WUTestCoordinator))
            {
                this.testSrcDir = configStore.ReadUnencryptedString(configSection, Constants.WUSCoordinator.TestCabFolderParam);
                this.testMode   = true;
            }

            this.waitTimeBeforePolling   = TimeSpan.FromMinutes(Constants.WUSCoordinator.PollingIntervalInMinutesDefault);
            this.windowsUpdateApiTimeout = TimeSpan.FromMinutes(Constants.WUSCoordinator.WuApiTimeoutInMinutesDefault);
        }
Example #4
0
 public Task <IReplicator> OpenAsync(
     ReplicaOpenMode openMode,
     IStatefulServicePartition partition,
     CancellationToken cancellationToken)
 {
     return(Task.FromResult <IReplicator>(null));
 }
Example #5
0
 Task <IReplicator> IStateProviderReplica.OpenAsync(
     ReplicaOpenMode openMode,
     IStatefulServicePartition partition,
     CancellationToken cancellationToken)
 {
     this.servicePartition = partition;
     return(this.stateManager.OpenAsync(openMode, partition, cancellationToken));
 }
Example #6
0
        public RestartPartitionAction(IReliableStateManager stateManager, IStatefulServicePartition partition, RestartPartitionState state, PartitionSelector partitionSelector, RestartPartitionMode restartPartitionMode, TimeSpan requestTimeout, TimeSpan operationTimeout)
            : base(stateManager, partition, state, requestTimeout, operationTimeout)
        {
            ThrowIf.Null(partitionSelector, "partitionSelector");

            this.PartitionSelector    = partitionSelector;
            this.RestartPartitionMode = restartPartitionMode;
        }
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
 public void Reuse(IStatefulServicePartition partition)
 {
     this.partition        = partition;
     this.ReplicaRole      = ReplicaRole.Unknown;
     this.DrainingStream   = DrainingStream.Invalid;
     this.ApplyRedoContext = ApplyContext.Invalid;
     this.IsClosing        = false;
 }
        public InvokeQuorumLossAction(IReliableStateManager stateManager, IStatefulServicePartition partition, InvokeQuorumLossState state, PartitionSelector partitionSelector, QuorumLossMode quorumLossMode, TimeSpan quorumLossDuration, TimeSpan requestTimeout, TimeSpan operationTimeout)
            : base(stateManager, partition, state, requestTimeout, operationTimeout)
        {
            ThrowIf.Null(partitionSelector, "partitionSelector");

            this.PartitionSelector  = partitionSelector;
            this.QuorumLossMode     = quorumLossMode;
            this.QuorumLossDuration = quorumLossDuration;
        }
Example #10
0
        internal ChaosScheduler(IReliableStateManager stateManager, IStatefulServicePartition partition)
        {
            this.StateManager = stateManager;
            this.partition    = partition;

            this.stateSemaphore = new SemaphoreSlim(1, 1);

            this.state = new SchedulerState(SchedulerState.NoChaosScheduleStopped);
            this.scheduleDescription = new ChaosScheduleDescription();
        }
 public static Task SetStoppedNodeStateAsync(Guid operationId, IStatefulServicePartition partition, IReliableStateManager stateManager, IReliableDictionary <string, bool> stoppedNodeTable, string nodeName, bool setStopped, CancellationToken cancellationToken)
 {
     return(FaultAnalysisServiceUtility.RunAndReportFaultOnRepeatedFailure(
                operationId,
                () => SetStoppedNodeStateInnerAsync(operationId, stateManager, stoppedNodeTable, nodeName, setStopped, cancellationToken),
                partition,
                "FaultAnalysisServiceUtility.SetStoppedNodeStateAsync",
                3,
                cancellationToken));
 }
        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));
        }
 /// <summary>
 /// Initialize a FabricApiCallDescription object
 /// </summary>
 /// <param name="data"></param>
 /// <param name="parameters"></param>
 /// <param name="statefulServicePartition"></param>
 public FabricApiCallDescription(
     FabricMonitoringData data,
     FabricMonitoringParameters parameters,
     IStatefulServicePartition statefulServicePartition)
 {
     this.monitoringData       = data;
     this.monitoringParameters = parameters;
     this.hasExpired           = false;
     this.partition            = statefulServicePartition;
 }
 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 #15
0
        /// <summary>
        /// Sets the partition info of the provided <paramref name="service"/>.
        /// </summary>
        /// <param name="partition">partition to set</param>
        /// <param name="service"></param>
        /// <returns></returns>
        public static void SetPartition(this StatefulServiceBase service, IStatefulServicePartition partition)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            //protected IStatefulServicePartition Partition { get; private set; }
            var propertyInfo = typeof(StatefulServiceBase).GetProperty("Partition", BindingFlags.Instance | BindingFlags.NonPublic);

            propertyInfo?.SetValue(service, partition, BindingFlags.Instance | BindingFlags.NonPublic, null, null, CultureInfo.InvariantCulture);
        }
Example #16
0
        public FabricJokesService(
            StatefulServiceContext context,
            IStatefulServicePartition partition,
            IReliableStateManager manager)
        {
            this.mapperConfiguration = new MapperConfiguration(config => config.CreateMap <JokeImportModel, JokeModel>());

            this.context   = context;
            this.partition = partition;
            this.manager   = manager;
        }
        /// <summary>
        /// Initializes an instance of the ReplicatorApiMonitor class
        /// ReplicatorApiMonitor takes ServiceContext, StatefulServicePartition and Replicator setttings as arguments.
        /// The default value of SlowApiMonitoringDuration in the replicator settings object is 120 seconds
        /// </summary>
        /// <param name="serviceContext">Current service serviceContext</param>
        /// <param name="statefulPartition">Current statefulPartition</param>
        /// <param name="settings">Current replicator settings</param>
        public void Initialize(ServiceContext serviceContext, IStatefulServicePartition statefulPartition, TransactionalReplicatorSettings settings)
        {
            this.replicatorSettings = settings;
            this.context            = serviceContext;
            this.partition          = statefulPartition;
            this.traceType          = string.Format("{0}.{1}.{2}", SR.ReplicatorApiMonitor_TraceTypeBase, this.context.PartitionId, this.context.ReplicaOrInstanceId);

            var monitorParameters = new MonitoringComponentParameters(this.replicatorSettings.PublicSettings.SlowApiMonitoringDuration.Value);

            this.fabricMonitor = new FabricApiMonitoringComponent(monitorParameters);
            this.initialized   = true;
        }
        public BackupStore(
            Uri endpoint,
            StorageCredentials credentials,
            IStatefulServicePartition servicePartition,
            StatefulServiceInitializationParameters statefulServiceInitializationParameters)
        {
            this.cloudBlobClient = new CloudBlobClient(endpoint, credentials);
            this.servicePartition = servicePartition;
            this.statefulServiceInitializationParameters = statefulServiceInitializationParameters;

            this.backupBlobContainer = this.GetBackupBlobContainer();
        }
        public PaasCoordinator(
            IConfigStore configStore,
            string configSectionName,
            KeyValueStoreReplica kvsStore,
            IStatefulServicePartition partition)
        {
            configStore.ThrowIfNull("configStore");
            configSectionName.ThrowIfNullOrWhiteSpace("configSectionName");
            partition.ThrowIfNull("partition");

            this.Initialize(configStore, configSectionName, kvsStore, partition);
        }
        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 #21
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));
        }
Example #22
0
        public ReliableFaultsEngine(ActionStore actionStore, bool isTestMode, IStatefulServicePartition partition, int commandStepRetryBackoffInSeconds)
        {
            ThrowIf.Null(actionStore, "actionStore");

            this.random = new Random();

            this.actionStore = actionStore;
            this.isTestMode  = isTestMode;

            this.partition = partition;

            this.commandStepRetryBackoffInSeconds = commandStepRetryBackoffInSeconds;
        }
        public BackupStore(
            Uri endpoint,
            StorageCredentials credentials,
            IStatefulServicePartition servicePartition,
            Guid testId,
            StatefulServiceInitializationParameters statefulServiceInitializationParameters)
        {
            this.cloudBlobClient  = new CloudBlobClient(endpoint, credentials);
            this.servicePartition = servicePartition;
            this.testId           = testId;
            this.statefulServiceInitializationParameters = statefulServiceInitializationParameters;

            this.backupBlobContainer = this.GetBackupBlobContainer();
        }
        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));
        }
        /// <summary>
        /// Constructor. Arguments break down into three groups: (1) Service Fabric objects so this library can access
        /// Service Fabric facilities, (2) Event Hub-related arguments which indicate what event hub to receive from and
        /// how to process the events, and (3) advanced, which right now consists only of the ability to replace the default
        /// reliable dictionary-based checkpoint manager with a user-provided implementation.
        /// </summary>
        /// <param name="serviceFabricServiceName">Service Fabric Uri found in StatefulServiceContext</param>
        /// <param name="serviceFabricPartitionId">Service Fabric partition id found in StatefulServiceContext</param>
        /// <param name="stateManager">Service Fabric-provided state manager, provides access to reliable dictionaries</param>
        /// <param name="partition">Service Fabric-provided partition information</param>
        /// <param name="userEventProcessor">User's event processor implementation</param>
        /// <param name="eventHubConnectionString">Connection string for user's event hub</param>
        /// <param name="eventHubConsumerGroup">Name of event hub consumer group to receive from</param>
        /// <param name="options">Optional: Options structure for ServiceFabricProcessor library</param>
        /// <param name="checkpointManager">Very advanced/optional: user-provided checkpoint manager implementation</param>
        public ServiceFabricProcessor(Uri serviceFabricServiceName, Guid serviceFabricPartitionId, IReliableStateManager stateManager, IStatefulServicePartition partition, IEventProcessor userEventProcessor,
                                      string eventHubConnectionString, string eventHubConsumerGroup,
                                      EventProcessorOptions options = null, ICheckpointMananger checkpointManager = null)
        {
            if (serviceFabricServiceName == null)
            {
                throw new ArgumentNullException("serviceFabricServiceName is null");
            }
            if (serviceFabricPartitionId == null)
            {
                throw new ArgumentNullException("serviceFabricPartitionId is null");
            }
            if (stateManager == null)
            {
                throw new ArgumentNullException("stateManager is null");
            }
            if (partition == null)
            {
                throw new ArgumentNullException("partition is null");
            }
            if (userEventProcessor == null)
            {
                throw new ArgumentNullException("userEventProcessor is null");
            }
            if (string.IsNullOrEmpty(eventHubConnectionString))
            {
                throw new ArgumentException("eventHubConnectionString is null or empty");
            }
            if (string.IsNullOrEmpty(eventHubConsumerGroup))
            {
                throw new ArgumentException("eventHubConsumerGroup is null or empty");
            }

            this.serviceFabricServiceName = serviceFabricServiceName;
            this.serviceFabricPartitionId = serviceFabricPartitionId;
            this.serviceStateManager      = stateManager;
            this.servicePartition         = partition;

            this.userEventProcessor = userEventProcessor;

            this.ehConnectionString = new EventHubsConnectionStringBuilder(eventHubConnectionString);
            this.consumerGroupName  = eventHubConsumerGroup;

            this.options           = options ?? new EventProcessorOptions();
            this.checkpointManager = checkpointManager ?? new ReliableDictionaryCheckpointMananger(this.serviceStateManager);

            this.EventHubClientFactory = new EventHubWrappers.EventHubClientFactory();
            this.TestMode = false;
            this.MockMode = null;
        }
Example #26
0
 public static async Task <ChaosStatus> GetCurrentStatusAsync(
     this IReliableDictionary <string, byte[]> statusDictionary,
     ITransaction tx,
     IStatefulServicePartition partition,
     CancellationToken cancellationToken)
 {
     return(await FaultAnalysisServiceUtility.RunAndReportFaultOnRepeatedFailure(
                ChaosOperationID,
                () => GetCurrentStatusPrivateAsync(statusDictionary, tx, cancellationToken),
                partition,
                "GetCurrentStatusPrivateAsync",
                FASConstants.MaxRetriesForReliableDictionary,
                cancellationToken).ConfigureAwait(false));
 }
Example #27
0
 public static async Task <long> GetLastStartTimeInTicksAsync(
     this IReliableDictionary <string, byte[]> startimeDictionary,
     ITransaction tx,
     IStatefulServicePartition partition,
     CancellationToken cancellationToken)
 {
     return(await FaultAnalysisServiceUtility.RunAndReportFaultOnRepeatedFailure(
                Guid.NewGuid(),
                () => GetLastStartTimeInTicksPrivateAsync(startimeDictionary, tx, cancellationToken),
                partition,
                "GetLastStartTimeInTicksPrivateAsync",
                FASConstants.MaxRetriesForReliableDictionary,
                cancellationToken).ConfigureAwait(false));
 }
Example #28
0
 public static async Task RegisterCurrentStatusAsync(
     this IReliableStateManager stateManager,
     ChaosStatus currentStatus,
     IStatefulServicePartition partition,
     CancellationToken cancellationToken)
 {
     await FaultAnalysisServiceUtility.RunAndReportFaultOnRepeatedFailure(
         ChaosOperationID,
         () => RegisterCurrentStatusPrivateAsync(stateManager, currentStatus, cancellationToken),
         partition,
         "RegisterCurrentStatusPrivateAsync",
         FASConstants.MaxRetriesForReliableDictionary,
         cancellationToken).ConfigureAwait(false);
 }
        /// <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));
        }
        public static bool GetPartitionRange(this IStatefulServicePartition partition, out long lowKey, out long highKey)
        {
            var result = false;

            lowKey  = long.MinValue;
            highKey = long.MaxValue;
            if (partition?.PartitionInfo is Int64RangePartitionInformation)
            {
                var partitionInfo = (Int64RangePartitionInformation)partition.PartitionInfo;
                lowKey  = partitionInfo.LowKey;
                highKey = partitionInfo.HighKey;
                result  = true;
            }
            return(result);
        }
Example #31
0
        public static async Task <bool> ChaosWasRunningAsync(
            this IReliableStateManager stateManager,
            IStatefulServicePartition partition,
            CancellationToken cancellationToken)
        {
            TestabilityTrace.TraceSource.WriteInfo(TraceType, "Enter ChaosWasRunningAsync");

            return(await FaultAnalysisServiceUtility.RunAndReportFaultOnRepeatedFailure(
                       ChaosOperationID,
                       () => ChaosWasRunningAsyncPrivate(stateManager, cancellationToken),
                       partition,
                       "ShouldStopAsyncPrivate",
                       FASConstants.MaxRetriesForReliableDictionary,
                       cancellationToken));
        }