/// <inheritdoc />
        public CommittedEventStream Commit(UncommittedEventStream uncommittedEvents)
        {
            var commit = uncommittedEvents.AsBsonCommit();

            return(Do(() =>
            {
                try
                {
                    var result = ExecuteCommit(commit);
                    if (result.IsSuccessfulCommit())
                    {
                        var sequence_number = result[Constants.ID].ToUlong();
                        return uncommittedEvents.ToCommitted(sequence_number);
                    }
                    else if (result.IsKnownError())
                    {
                        if (result.IsPreviousVersion())
                        {
                            throw new EventSourceConcurrencyConflict(result.ToEventSourceVersion(), uncommittedEvents.Source.Version);
                        }
                        else if (IsDuplicateCommit(uncommittedEvents.Id))
                        {
                            throw new CommitIsADuplicate();
                        }
                        else
                        {
                            throw new EventSourceConcurrencyConflict(result[Constants.ERROR].AsBsonDocument.ToEventSourceVersion(), uncommittedEvents.Source.Version);
                        }
                    }
                    else
                    {
                        throw new UnknownCommitError(result?.ToString() ?? "[NULL]");
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "Exception committing event stream");
                    throw;
                }
            }));
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public CommittedEventStream Commit(UncommittedEventStream uncommittedEvents)
        {
            var commit = uncommittedEvents.AsBsonCommit();

            return(Do <CommittedEventStream>(() => {
                try
                {
                    var result = ExecuteCommit(commit);
                    if (result.IsSuccessfulCommit())
                    {
                        var sequence_number = result[Constants.ID].ToUlong();
                        var committed = uncommittedEvents.ToCommitted(sequence_number);
                        return committed;
                    }
                    else if (result.IsKnownError())
                    {
                        if (result.IsPreviousVersion())
                        {
                            throw new EventSourceConcurrencyConflict($"Current Version is {result["version"]}, tried to commit {uncommittedEvents.Source.Version.Commit}");
                        }
                        else if (IsDuplicateCommit(uncommittedEvents.Id))
                        {
                            throw new CommitIsADuplicate();
                        }
                        else
                        {
                            throw new EventSourceConcurrencyConflict();
                        }
                    }
                    else
                    {
                        throw new Exception("Unknown error type");
                    }
                } catch (Exception ex)
                {
                    _logger.Error(ex, "Exception committing event stream");
                    throw;
                }
            }));
        }
Exemplo n.º 3
0
        async Task <CommittedEventStream> CommitAsync(UncommittedEventStream uncommittedEvents)
        {
            var commit = Persistence.Commit.From(uncommittedEvents, _serializer, _config.BasePartitionKey);

            try
            {
                var result = await _config.Client.ExecuteStoredProcedureAsync <dynamic>(
                    _config.CommitStoredProcedure,
                    new RequestOptions
                {
                    PartitionKey     = new PartitionKey(_config.BasePartitionKey),
                    ConsistencyLevel = ConsistencyLevel.Session,
                    //EnableScriptLogging = true
                },
                    commit);

                //_logger.Debug(result.ScriptLog);
                ulong commitSequenceNumber = Convert.ToUInt64(result.Response);

                //_logger.Debug(ResponseMetadata.FromCommit("Commit", result)?.ToString());
                return(uncommittedEvents.ToCommitted(commitSequenceNumber));
            }
            catch (DocumentClientException ex)
            {
                //_logger.Debug(ex.ScriptLog);
                if (ex.Message.Contains(CONCURRENCY_CONFLICT_DUPLICATE_KEY))
                {
                    throw new CommitIsADuplicate();
                }
                if (ex.Message.Contains(CONCURRENCY_CONFLICT_PREVIOUS_VERSION_KEY))
                {
                    throw new EventSourceConcurrencyConflict(ex.Error.Message, ex);
                }

                throw new EventStorePersistenceError("Unknown error", ex);
            }
        }