Ejemplo n.º 1
0
 /// <summary>Ctor.</summary>
 /// <param name="currentVersion">current version and value</param>
 /// <param name="priorVersion">prior version and value</param>
 public CurrentValue(
     VersionedValue<T> currentVersion,
     VersionedValue<T> priorVersion)
 {
     this.currentVersion = currentVersion;
     this.priorVersion = priorVersion;
 }
Ejemplo n.º 2
0
        private VersionedValue CreateAndPushNewInstance()
        {
            var instance = new VersionedValue(m_stackOfValues.Count + 1, m_constructor());

            m_stackOfValues.Push(instance);
            return(instance);
        }
Ejemplo n.º 3
0
 private Task FulfillAsync()
 {
     _sumValue = _sumValue.NextVersion(_leftValue.Value + _rightValue.Value);
     _wait.SetResult(_sumValue);
     _wait = new TaskCompletionSource <VersionedValue <int> >();
     return(Task.CompletedTask);
 }
Ejemplo n.º 4
0
        private VersionedValue CreateAndPushNewInstance()
        {
            Contract.Ensures(Contract.Result <VersionedValue>().Version == CurrentVersion);

            var instance = new VersionedValue(m_stackOfValues.Count + 1, m_constructor());

            m_stackOfValues.Push(instance);
            return(instance);
        }
Ejemplo n.º 5
0
 private Task FulfillAsync()
 {
     _sumValue = _sumValue.NextVersion(_leftValue.Value + _rightValue.Value);
     _logger.LogInformation(
         "{@Time}: {@GrainType} {@GrainKey} updated sum value to {@Value} with version {@Version}",
         DateTime.Now.TimeOfDay, GrainType, GrainKey, _sumValue.Value, _sumValue.Version);
     _wait.SetResult(_sumValue);
     _wait = new TaskCompletionSource <VersionedValue <int> >();
     return(Task.CompletedTask);
 }
Ejemplo n.º 6
0
        public override Task OnActivateAsync()
        {
            // initialize the state
            _state = new VersionedValue <int>(0, 0);

            // initialize the polling wait handle
            _wait = new TaskCompletionSource <VersionedValue <int> >();

            return(base.OnActivateAsync());
        }
Ejemplo n.º 7
0
        public override async Task OnActivateAsync()
        {
            // hydrate the cache with whatever value is available right now
            _cache = await GrainFactory.GetGrain <IProducerGrain>(GrainKey).GetAsync();

            // start polling
            _pollTimer = RegisterTimer(_ => PollAsync(), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(1));

            await base.OnActivateAsync();
        }
        public override Task OnActivateAsync()
        {
            // initialize the state
            _state = VersionedValue <OrdersStats> .None.NextVersion(new OrdersStats());

            // initialize the polling wait handle
            _wait = new TaskCompletionSource <VersionedValue <OrdersStats> >();

            return(base.OnActivateAsync());
        }
Ejemplo n.º 9
0
        public override async Task OnActivateAsync()
        {
            // derive the source grains to aggregate from the grain key
            var parts = GrainKey.Split('|');

            _leftGrain  = GrainFactory.GetGrain <IProducerGrain>(parts[0]);
            _rightGrain = GrainFactory.GetGrain <IProducerGrain>(parts[1]);

            // get the starting values as they are now before starting to long poll
            _leftValue = await _leftGrain.GetAsync();

            _rightValue = await _rightGrain.GetAsync();
            await FulfillAsync();

            // start long polling the left grain
            _leftPoll = await RegisterReactivePollAsync(
                null, // dont initialize for this one
                () => _leftGrain.LongPollAsync(_leftValue.Version),
                result => result.IsValid,
                apply =>
            {
                _leftValue = apply;
                _logger.LogInformation(
                    "{@Time}: {@GrainType} {@GrainKey} updated left value to {@Value} with version {@Version}",
                    DateTime.Now.TimeOfDay, GrainType, GrainKey, _leftValue.Value, _leftValue.Version);

                return(FulfillAsync());
            },
                failed =>
            {
                _logger.LogWarning("The reactive poll timed out by returning a 'none' response before Orleans could break the promise.");
                return(Task.CompletedTask);
            });

            // start long polling the right grain
            _rightPoll = await RegisterReactivePollAsync(
                null, // dont initialize for this one
                () => _rightGrain.LongPollAsync(_rightValue.Version),
                result => result.IsValid,
                apply =>
            {
                _rightValue = apply;
                _logger.LogInformation(
                    "{@Time}: {@GrainType} {@GrainKey} updated right value to {@Value} with version {@Version}",
                    DateTime.Now.TimeOfDay, GrainType, GrainKey, _rightValue.Value, _rightValue.Version);
                return(FulfillAsync());
            },
                failed =>
            {
                _logger.LogWarning("The reactive poll timed out by returning a 'none' response before Orleans could break the promise.");
                return(Task.CompletedTask);
            });

            await base.OnActivateAsync();
        }
Ejemplo n.º 10
0
 private async Task LongPollRightAsync()
 {
     try
     {
         _rightValue = await _rightGrain.LongPollAsync(_rightValue.Version);
         await FulfillAsync();
     }
     catch (TimeoutException)
     {
     }
 }
Ejemplo n.º 11
0
 private async Task LongPollLeftAsync()
 {
     try
     {
         _leftValue = await _leftGrain.LongPollAsync(_leftValue.Version);
         await FulfillAsync();
     }
     catch (TimeoutException)
     {
     }
 }
Ejemplo n.º 12
0
 private async Task PollAsync()
 {
     try
     {
         _cache = await GrainFactory.GetGrain <IProducerGrain>(GrainKey).LongPollAsync(_cache.Version);
     }
     catch (TimeoutException error)
     {
         _logger.LogDebug(error, "{@GrainType} {@GrainKey} long polling broken. Polling again...");
     }
 }
 /// <exception cref="System.Exception"/>
 private void IncrSharedCount(SharedCount sharedCount)
 {
     while (true)
     {
         // Loop until we successfully increment the counter
         VersionedValue <int> versionedValue = sharedCount.GetVersionedValue();
         if (sharedCount.TrySetCount(versionedValue, versionedValue.GetValue() + 1))
         {
             break;
         }
     }
 }
        public Task UpdateValue(OrdersStats value)
        {
            // update the state
            _state = _state.NextVersion(value);
            _logger.LogInformation("{@GrainType} {@GrainKey} updated value to {@Value} with version {@Version}",
                                   GrainType, GrainKey, _state.Value, _state.Version);

            // fulfill waiting promises
            _wait.SetResult(_state);
            _wait = new TaskCompletionSource <VersionedValue <OrdersStats> >();

            return(Task.CompletedTask);
        }
Ejemplo n.º 15
0
        private Task IncrementAsync(int increment)
        {
            // update the state
            _state = _state.NextVersion(_state.Value + increment);
            _logger.LogInformation("{@GrainType} {@GrainKey} updated value to {@Value} with version {@Version}",
                                   GrainType, GrainKey, _state.Value, _state.Version);

            // fulfill waiting promises
            _wait.SetResult(_state);
            _wait = new TaskCompletionSource <VersionedValue <int> >();

            return(Task.CompletedTask);
        }
Ejemplo n.º 16
0
        public override async Task OnActivateAsync()
        {
            // derive the source grains to aggregate from the grain key
            var parts = GrainKey.Split('|');

            _leftGrain  = GrainFactory.GetGrain <IProducerGrain>(parts[0]);
            _rightGrain = GrainFactory.GetGrain <IProducerGrain>(parts[1]);

            // get the starting values as they are now
            _leftValue = await _leftGrain.GetAsync();

            _rightValue = await _rightGrain.GetAsync();
            await FulfillAsync();

            // start long polling
            _leftPollTimer  = RegisterTimer(_ => LongPollLeftAsync(), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(1));
            _rightPollTimer = RegisterTimer(_ => LongPollRightAsync(), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(1));

            await base.OnActivateAsync();
        }
Ejemplo n.º 17
0
        public override async Task OnActivateAsync()
        {
            // start long polling
            _poll = await RegisterReactivePollAsync(
                () => GrainFactory.GetGrain <IOrdersStatsProducer>(GrainKey).GetAsync(),
                () => GrainFactory.GetGrain <IOrdersStatsProducer>(GrainKey).LongPollStatsAsync(_cache.Version),
                result => result.IsValid,
                apply =>
            {
                _cache = apply;
                _logger.LogInformation(
                    "{@Time}: {@GrainType} {@GrainKey} updated value to {@Value}",
                    DateTime.Now.TimeOfDay, GrainType, GrainKey, _cache.Value);
                return(Task.CompletedTask);
            },
                failed =>
            {
                _logger.LogWarning("The reactive poll timed out by returning a 'none' response before Orleans could break the promise.");
                return(Task.CompletedTask);
            });

            await base.OnActivateAsync();
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="factory">logger factory</param>
        public RCService(ILoggerFactory factory)
        {
            _logger = factory.CreateLogger <RCService <T> >();

            _value = VersionedValue <T> .None.NextVersion(default);