Example #1
0
        private void PromoteToStarted()
        {
            if (StoppedCancellationTokenSource.IsCancellationRequested)
            {
                return;
            }

            // Logger.Info(ErrorCode.RS_ServiceStarted, "Reminder system target started OK on: {0} x{1,8:X8}, with range {2}", this.Silo, this.Silo.GetConsistentHashCode(), this.myRange);

            var random  = new SafeRandom();
            var dueTime = random.NextTimeSpan(Constants.RefreshReminderList);

            if (listRefreshTimer != null)
            {
                listRefreshTimer.Dispose();
            }
            listRefreshTimer = GrainTimer.FromTaskCallback(
                this.RuntimeClient.Scheduler,
                _ => ReadAndUpdateReminders(),
                null,
                dueTime,
                Constants.RefreshReminderList,
                name: "ReminderService.ReminderListRefresher");
            listRefreshTimer.Start();
            Status = GrainServiceStatus.Started;
            startedTask.TrySetResult(true);
        }
        private void InitializeInternal()
        {
            logger.Info(ErrorCode.PersistentStreamPullingAgent_02, "Init of {0} {1} on silo {2} for queue {3}.",
                        GetType().Name, ((ISystemTargetBase)this).GrainId.ToString(), Silo, QueueId.ToStringWithHashCode());

            lastTimeCleanedPubSubCache = DateTime.UtcNow;

            try
            {
                receiverInitTask = OrleansTaskExtentions.SafeExecute(() => receiver.Initialize(this.options.InitQueueTimeout))
                                   .LogException(logger, ErrorCode.PersistentStreamPullingAgent_03, $"QueueAdapterReceiver {QueueId.ToStringWithHashCode()} failed to Initialize.");
                receiverInitTask.Ignore();
            }
            catch
            {
                // Just ignore this exception and proceed as if Initialize has succeeded.
                // We already logged individual exceptions for individual calls to Initialize. No need to log again.
            }

            // Setup a reader for a new receiver.
            // Even if the receiver failed to initialise, treat it as OK and start pumping it. It's receiver responsibility to retry initialization.
            var randomTimerOffset = ThreadSafeRandom.NextTimeSpan(this.options.GetQueueMsgsTimerPeriod);

            timer = RegisterGrainTimer(AsyncTimerCallback, QueueId, randomTimerOffset, this.options.GetQueueMsgsTimerPeriod);

            IntValueStatistic.FindOrCreate(new StatisticName(StatisticNames.STREAMS_PERSISTENT_STREAM_PUBSUB_CACHE_SIZE, StatisticUniquePostfix), () => pubSubCache.Count);

            logger.Info((int)ErrorCode.PersistentStreamPullingAgent_04, "Taking queue {0} under my responsibility.", QueueId.ToStringWithHashCode());
        }
Example #3
0
 public void OnTimerDisposed(IGrainTimer orleansTimerInsideGrain)
 {
     lock (this) // need to lock since dispose can be called on finalizer thread, outside grain context (not single threaded).
     {
         timers.Remove(orleansTimerInsideGrain);
     }
 }
Example #4
0
            public void StopReminder(Logger Logger)
            {
                if (Timer != null)
                {
                    Timer.Dispose();
                }

                Timer = null;
            }
Example #5
0
 internal void AddTimer(IGrainTimer timer)
 {
     lock (this)
     {
         if (timers == null)
         {
             timers = new HashSet <IGrainTimer>();
         }
         timers.Add(timer);
     }
 }
Example #6
0
            public void StartTimer(OrleansTaskScheduler scheduler, Func <object, Task> asyncCallback, Logger Logger)
            {
                StopReminder(Logger); // just to make sure.
                var dueTimeSpan = CalculateDueTime();

                Timer = GrainTimer.FromTaskCallback(scheduler, asyncCallback, this, dueTimeSpan, period, name: ReminderName);
                if (Logger.IsVerbose)
                {
                    Logger.Verbose("Reminder {0}, Due time{1}", this, dueTimeSpan);
                }
                Timer.Start();
            }
 private void Start()
 {
     var random = new SafeRandom();
     var randomOffset = random.NextTimeSpan(orleansConfig.Globals.ClientRegistrationRefresh);
     clientRefreshTimer = GrainTimer.FromTaskCallback(
             OnClientRefreshTimer, 
             null, 
             randomOffset, 
             orleansConfig.Globals.ClientRegistrationRefresh, 
             "ClientObserverRegistrar.ClientRefreshTimer");
     clientRefreshTimer.Start();
     if (logger.IsVerbose) { logger.Verbose("Client registrar service started successfully."); }
 }
Example #8
0
        private void StartTimer()
        {
            if (timer != null)
                timer.Dispose();

            timer = GrainTimer.FromTimerCallback(
                this.RuntimeClient.Scheduler,
                this.OnGossipTimerTick,
                null,
                this.backgroundGossipInterval,
                this.backgroundGossipInterval,
                "MultiCluster.GossipTimer");

            timer.Start();
        }
Example #9
0
        protected override async Task StartInBackground()
        {
            await DoInitialReadAndUpdateReminders();

            if (Status == GrainServiceStatus.Booting)
            {
                var random = new SafeRandom();
                listRefreshTimer = GrainTimer.FromTaskCallback(
                    _ => DoInitialReadAndUpdateReminders(),
                    null,
                    random.NextTimeSpan(InitialReadRetryPeriod),
                    InitialReadRetryPeriod,
                    name: "ReminderService.ReminderListInitialRead");
                listRefreshTimer.Start();
            }
        }
Example #10
0
        public async override Task Stop()
        {
            await base.Stop();

            if (listRefreshTimer != null)
            {
                listRefreshTimer.Dispose();
                listRefreshTimer = null;
            }
            foreach (LocalReminderData r in localReminders.Values)
            {
                r.StopReminder(Logger);
            }

            // for a graceful shutdown, also handover reminder responsibilities to new owner, and update the ReminderTable
            // currently, this is taken care of by periodically reading the reminder table
        }
        private void Start()
        {
            var random       = new SafeRandom();
            var randomOffset = random.NextTimeSpan(orleansConfig.Globals.ClientRegistrationRefresh);

            clientRefreshTimer = GrainTimer.FromTaskCallback(
                OnClientRefreshTimer,
                null,
                randomOffset,
                orleansConfig.Globals.ClientRegistrationRefresh,
                "ClientObserverRegistrar.ClientRefreshTimer");
            clientRefreshTimer.Start();
            if (logger.IsVerbose)
            {
                logger.Verbose("Client registrar service started successfully.");
            }
        }
Example #12
0
        public Task Stop()
        {
            stoppedCancellationTokenSource.Cancel();

            logger.Info(ErrorCode.RS_ServiceStopping, "Stopping reminder system target");
            status = ReminderServiceStatus.Stopped;

            ring.UnSubscribeFromRangeChangeEvents(this);

            if (listRefreshTimer != null)
            {
                listRefreshTimer.Dispose();
                listRefreshTimer = null;
            }
            foreach (LocalReminderData r in localReminders.Values)
            {
                r.StopReminder(logger);
            }

            // for a graceful shutdown, also handover reminder responsibilities to new owner, and update the ReminderTable
            // currently, this is taken care of by periodically reading the reminder table
            return(TaskDone.Done);
        }
Example #13
0
 public void OnTimerCreated(IGrainTimer timer)
 {
     AddTimer(timer);
 }
Example #14
0
 public Task Start()
 {
     requestProcessor = GrainTimer.FromTaskCallback(this.RuntimeClient.Scheduler, this.loggerFactory.CreateLogger <GrainTimer>(), ProcessRequests, null, TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(10), "TransactionAgent");
     requestProcessor.Start();
     return(Task.CompletedTask);
 }
Example #15
0
 public void OnTimerDisposed(IGrainTimer orleansTimerInsideGrain)
 {
     lock (this) // need to lock since dispose can be called on finalizer thread, outside garin context (not single threaded).
     {
         timers.Remove(orleansTimerInsideGrain);
     }
 }
        public async Task Shutdown()
        {
            // Stop pulling from queues that are not in my range anymore.
            logger.Info(ErrorCode.PersistentStreamPullingAgent_05, "Shutdown of {0} responsible for queue: {1}", GetType().Name, QueueId.ToStringWithHashCode());

            if (timer != null)
            {
                var tmp = timer;
                timer = null;
                Utils.SafeExecute(tmp.Dispose, this.logger);

                try
                {
                    await tmp.GetCurrentlyExecutingTickTask().WithTimeout(TimeSpan.FromSeconds(5));
                }
                catch (Exception ex)
                {
                    this.logger.LogWarning(ex, "Waiting for the last timer tick failed");
                }
            }

            this.queueCache = null;

            Task localReceiverInitTask = receiverInitTask;

            if (localReceiverInitTask != null)
            {
                try
                {
                    await localReceiverInitTask;
                    receiverInitTask = null;
                }
                catch (Exception)
                {
                    receiverInitTask = null;
                    // squelch
                }
            }

            try
            {
                IQueueAdapterReceiver localReceiver = this.receiver;
                this.receiver = null;
                if (localReceiver != null)
                {
                    var task = OrleansTaskExtentions.SafeExecute(() => localReceiver.Shutdown(this.options.InitQueueTimeout));
                    task = task.LogException(logger, ErrorCode.PersistentStreamPullingAgent_07,
                                             $"QueueAdapterReceiver {QueueId} failed to Shutdown.");
                    await task;
                }
            }
            catch
            {
                // Just ignore this exception and proceed as if Shutdown has succeeded.
                // We already logged individual exceptions for individual calls to Shutdown. No need to log again.
            }

            var unregisterTasks    = new List <Task>();
            var meAsStreamProducer = this.AsReference <IStreamProducerExtension>();

            foreach (var tuple in pubSubCache)
            {
                tuple.Value.DisposeAll(logger);
                var streamId = tuple.Key;
                logger.Info(ErrorCode.PersistentStreamPullingAgent_06, "Unregister PersistentStreamPullingAgent Producer for stream {0}.", streamId);
                unregisterTasks.Add(pubSub.UnregisterProducer(streamId, meAsStreamProducer));
            }

            try
            {
                await Task.WhenAll(unregisterTasks);
            }
            catch (Exception exc)
            {
                logger.Warn(ErrorCode.PersistentStreamPullingAgent_08,
                            "Failed to unregister myself as stream producer to some streams that used to be in my responsibility.", exc);
            }
            pubSubCache.Clear();
            IntValueStatistic.Delete(new StatisticName(StatisticNames.STREAMS_PERSISTENT_STREAM_PUBSUB_CACHE_SIZE, StatisticUniquePostfix));
            //IntValueStatistic.Delete(new StatisticName(StatisticNames.STREAMS_PERSISTENT_STREAM_QUEUE_CACHE_SIZE, StatisticUniquePostfix));
        }
Example #17
0
 internal void AddTimer(IGrainTimer timer)
 {
     lock(this)
     {
         if (timers == null)
         {
             timers = new HashSet<IGrainTimer>();
         }
         timers.Add(timer);
     }
 }