Пример #1
0
        public async Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var key = GetKey(grainReference, grainState);
            //_logger.LogInformation($"Put {grainType} {grainReference.GetPrimaryKeyString()}");
            var bins = _aerospikeSerializer.Serialize(grainState);

            try
            {
                if (!string.IsNullOrEmpty(grainState.ETag) && _options.VerifyEtagGenerations)
                {
                    await _client.Put(
                        new WritePolicy()
                    {
                        sendKey          = true,
                        generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL,
                        generation       = int.Parse(grainState.ETag)
                    }, Task.Factory.CancellationToken, key, bins);

                    grainState.ETag = (int.Parse(grainState.ETag) + 1).ToString(); // +1
                }
                else
                {
                    await _client.Put(new WritePolicy()
                    {
                        sendKey = true
                    }, Task.Factory.CancellationToken, key, bins);

                    grainState.ETag = "1"; // initial 1
                }
            }
            catch (AerospikeException aep)
            {
                if (aep.Result == 3)
                {
                    throw new InconsistentStateException($"Generation conflict while writing Grain Type {grainType} with key {grainReference.ToKeyString()}. Error:{aep.Message}", grainState.ETag, "?");
                }

                _logger.LogError(aep, $"Failure writing state for Grain Type {grainType} with key {grainReference.ToKeyString()}.");
                throw new AerospikeOrleansException(aep.Message); // simple serializable excepction
            }
            catch (Exception exp)
            {
                _logger.LogError(exp, $"Failure writing state for Grain Type {grainType} with key {grainReference.ToKeyString()}.");
                throw;
            }
        }
        public async Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            var key = GetKey(grainReference, grainState);
            //_logger.LogInformation($"Put {grainType} {grainReference.GetPrimaryKeyString()}");
            var bins = _aerospikeSerializer.Serialize(grainState);

            try
            {
                var ops = bins.Select(p => new Operation(Operation.Type.WRITE, p.name, p.value)).ToList();
                ops.Add(new Operation(Operation.Type.READ_HEADER, "", Value.AsNull));

                if (!string.IsNullOrEmpty(grainState.ETag) && _options.VerifyEtagGenerations)
                {
                    var record = await _client.Operate(new WritePolicy(_writeStatePolicy)
                    {
                        generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL,
                        generation       = int.Parse(grainState.ETag),
                    }, Task.Factory.CancellationToken, key, ops.ToArray());

                    grainState.ETag = record.generation.ToString();
                }
                else
                {
                    var record = await _client.Operate(_writeStatePolicy, Task.Factory.CancellationToken, key, ops.ToArray());

                    grainState.ETag = record.generation.ToString();
                }

                grainState.RecordExists = true;
            }
            catch (AerospikeException aep)
            {
                if (aep.Result == 3)
                {
                    throw new InconsistentStateException($"Generation conflict while writing Grain Type {grainType} with key {grainReference.ToKeyString()}. Error:{aep.Message}", grainState.ETag, "?");
                }

                _logger.LogError(aep, $"Failure writing state for Grain Type {grainType} with key {grainReference.ToKeyString()}.");
                throw new AerospikeOrleansException(aep.Message); // simple serializable excepction
            }
            catch (Exception exp)
            {
                _logger.LogError(exp, $"Failure writing state for Grain Type {grainType} with key {grainReference.ToKeyString()}.");
                throw;
            }
        }