Skip to content

Commit

Permalink
Merge pull request #440 from dolittle/add-logging-for-aggregates
Browse files Browse the repository at this point in the history
Add more logging for aggregates
  • Loading branch information
joelhoisko authored Oct 27, 2020
2 parents 7dc3977 + 7a0e1ca commit 2aaace0
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 22 deletions.
14 changes: 7 additions & 7 deletions Source/Events.Store.MongoDB/Aggregates/AggregateRoots.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Task<AggregateRoot> IncrementVersionFor(
CancellationToken cancellationToken)
{
_logger.Trace("Incrementing version for Aggregate: {AggregateRoot} and Event Source Id: {EventSourceId}", aggregateRoot, eventSource);
ThrowIfNextVersionIsNotGreaterThanExpectedVersion(expectedVersion, nextVersion);
ThrowIfNextVersionIsNotGreaterThanExpectedVersion(eventSource, aggregateRoot, expectedVersion, nextVersion);

if (expectedVersion == AggregateRootVersion.Initial)
{
Expand Down Expand Up @@ -110,7 +110,7 @@ await _aggregates.Aggregates.InsertOneAsync(
eventSource,
aggregateRoot,
cancellationToken).ConfigureAwait(false);
throw new AggregateRootConcurrencyConflict(currentVersion, expectedVersion);
throw new AggregateRootConcurrencyConflict(eventSource, aggregateRoot, currentVersion, expectedVersion);
}
catch (MongoWriteException exception)
{
Expand All @@ -121,7 +121,7 @@ await _aggregates.Aggregates.InsertOneAsync(
eventSource,
aggregateRoot,
cancellationToken).ConfigureAwait(false);
throw new AggregateRootConcurrencyConflict(currentVersion, expectedVersion);
throw new AggregateRootConcurrencyConflict(eventSource, aggregateRoot, currentVersion, expectedVersion);
}

throw;
Expand All @@ -137,7 +137,7 @@ await _aggregates.Aggregates.InsertOneAsync(
eventSource,
aggregateRoot,
cancellationToken).ConfigureAwait(false);
throw new AggregateRootConcurrencyConflict(currentVersion, expectedVersion);
throw new AggregateRootConcurrencyConflict(eventSource, aggregateRoot, currentVersion, expectedVersion);
}
}

Expand Down Expand Up @@ -173,18 +173,18 @@ async Task<AggregateRoot> UpdateExistingAggregateRootDocument(
eventSource,
aggregateRoot,
cancellationToken).ConfigureAwait(false);
throw new AggregateRootConcurrencyConflict(currentVersion, expectedVersion);
throw new AggregateRootConcurrencyConflict(eventSource, aggregateRoot, currentVersion, expectedVersion);
}

if (result.ModifiedCount > 1) throw new MultipleAggregateInstancesFound(eventSource, aggregateRoot);
return new AggregateRoot(eventSource, aggregateRoot, nextVersion);
}

void ThrowIfNextVersionIsNotGreaterThanExpectedVersion(AggregateRootVersion expectedVersion, AggregateRootVersion nextVersion)
void ThrowIfNextVersionIsNotGreaterThanExpectedVersion(EventSourceId eventSource, ArtifactId aggregateRoot, AggregateRootVersion expectedVersion, AggregateRootVersion nextVersion)
{
if (nextVersion <= expectedVersion)
{
throw new NextAggregateRootVersionMustBeGreaterThanCurrentVersion(expectedVersion, nextVersion);
throw new NextAggregateRootVersionMustBeGreaterThanCurrentVersion(eventSource, aggregateRoot, expectedVersion, nextVersion);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using Dolittle.Artifacts;

namespace Dolittle.Runtime.Events.Store.MongoDB
namespace Dolittle.Runtime.Events.Store.MongoDB.Aggregates
{
/// <summary>
/// Exception that gets thrown when multiple versions of a single aggregate root instance is found in the MongoDB.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Dolittle.Artifacts;

namespace Dolittle.Runtime.Events.Store.MongoDB
namespace Dolittle.Runtime.Events.Store.MongoDB.Aggregates
{
/// <summary>
/// Exception that gets thrown when attempting to decrease the version of an aggregate root instance.
Expand All @@ -13,10 +14,12 @@ public class NextAggregateRootVersionMustBeGreaterThanCurrentVersion : Exception
/// <summary>
/// Initializes a new instance of the <see cref="NextAggregateRootVersionMustBeGreaterThanCurrentVersion"/> class.
/// </summary>
/// <param name="eventSource">The <see cref="EventSourceId" />.</param>
/// <param name="aggregateRoot">The aggregate root id.</param>
/// <param name="currentVersion">The current <see cref="AggregateRootVersion"/> of the aggregate root instance.</param>
/// <param name="nextVersion">The <see cref="AggregateRootVersion"/> that was attempted to set on the aggregate root instance.</param>
public NextAggregateRootVersionMustBeGreaterThanCurrentVersion(AggregateRootVersion currentVersion, AggregateRootVersion nextVersion)
: base($"Next Aggregate Root instance version must be greater than '{currentVersion}', got '{nextVersion}'.")
public NextAggregateRootVersionMustBeGreaterThanCurrentVersion(EventSourceId eventSource, ArtifactId aggregateRoot, AggregateRootVersion currentVersion, AggregateRootVersion nextVersion)
: base($"Next aggregate root version of aggregate root {aggregateRoot} with event source {eventSource} must be greater than '{currentVersion}', but got '{nextVersion}'.")
{
}
}
Expand Down
7 changes: 5 additions & 2 deletions Source/Events.Store/AggregateRootConcurrencyConflict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Dolittle.Artifacts;

namespace Dolittle.Runtime.Events.Store
{
Expand All @@ -13,10 +14,12 @@ public class AggregateRootConcurrencyConflict : Exception
/// <summary>
/// Initializes a new instance of the <see cref="AggregateRootConcurrencyConflict"/> class.
/// </summary>
/// <param name="eventSource">The <see cref="EventSourceId" />.</param>
/// <param name="aggregateRoot">The aggregate root id.</param>
/// <param name="currentVersion">The current version before commit.</param>
/// <param name="commitVersion">The version of the commit that causes a concurrency conflict.</param>
public AggregateRootConcurrencyConflict(AggregateRootVersion currentVersion, AggregateRootVersion commitVersion)
: base($"Current Version is {currentVersion}, tried to commit {commitVersion}")
public AggregateRootConcurrencyConflict(EventSourceId eventSource, ArtifactId aggregateRoot, AggregateRootVersion currentVersion, AggregateRootVersion commitVersion)
: base($"Tried to commit events to event source {eventSource} on aggregate root {aggregateRoot} with expected aggregate root version {commitVersion}, but current aggregate root version was {currentVersion}. The expected aggregate root version needs to be the same as the current aggregate root version")
{
}
}
Expand Down
7 changes: 5 additions & 2 deletions Source/Events.Store/AggregateRootVersionIsOutOfOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Dolittle.Artifacts;

namespace Dolittle.Runtime.Events.Store
{
Expand All @@ -13,10 +14,12 @@ public class AggregateRootVersionIsOutOfOrder : ArgumentException
/// <summary>
/// Initializes a new instance of the <see cref="AggregateRootVersionIsOutOfOrder"/> class.
/// </summary>
/// <param name="eventSource">The <see cref="EventSourceId" />.</param>
/// <param name="aggregateRoot">The aggregate root id.</param>
/// <param name="eventVersion">The <see cref="AggregateRootVersion"/> the Event was applied by.</param>
/// <param name="expectedVersion">Expected <see cref="AggregateRootVersion"/>.</param>
public AggregateRootVersionIsOutOfOrder(AggregateRootVersion eventVersion, AggregateRootVersion expectedVersion)
: base($"Aggregate Root version is out of order. Version '{eventVersion}' from event does not match '{expectedVersion}'")
public AggregateRootVersionIsOutOfOrder(EventSourceId eventSource, ArtifactId aggregateRoot, AggregateRootVersion eventVersion, AggregateRootVersion expectedVersion)
: base($"Aggregate Root version for event source {eventSource} on aggregate root {aggregateRoot} is out of order. Version '{eventVersion}' from event does not match '{expectedVersion}'")
{
}
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Events.Store/CommittedAggregateEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ public CommittedAggregateEvents(EventSourceId eventSource, ArtifactId aggregateR

void ThrowIfEventWasAppliedToOtherEventSource(CommittedAggregateEvent @event)
{
if (@event.EventSource != EventSource) throw new EventWasAppliedToOtherEventSource(@event.EventSource, EventSource);
if (@event.EventSource != EventSource) throw new EventWasAppliedToOtherEventSource(AggregateRoot, @event.EventSource, EventSource);
}

void ThrowIfEventWasAppliedByOtherAggregateRoot(CommittedAggregateEvent @event)
{
if (@event.AggregateRoot.Id != AggregateRoot) throw new EventWasAppliedByOtherAggregateRoot(@event.AggregateRoot.Id, AggregateRoot);
if (@event.AggregateRoot.Id != AggregateRoot) throw new EventWasAppliedByOtherAggregateRoot(EventSource, @event.AggregateRoot.Id, AggregateRoot);
}

void ThrowIfAggreggateRootVersionIsOutOfOrder(CommittedAggregateEvent @event)
{
if (@event.AggregateRootVersion != _currentCheckedVersion) throw new AggregateRootVersionIsOutOfOrder(@event.AggregateRootVersion, _currentCheckedVersion);
if (@event.AggregateRootVersion != _currentCheckedVersion) throw new AggregateRootVersionIsOutOfOrder(EventSource, AggregateRoot, @event.AggregateRootVersion, _currentCheckedVersion);
}
}
}
5 changes: 3 additions & 2 deletions Source/Events.Store/EventWasAppliedByOtherAggregateRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ public class EventWasAppliedByOtherAggregateRoot : ArgumentException
/// <summary>
/// Initializes a new instance of the <see cref="EventWasAppliedByOtherAggregateRoot"/> class.
/// </summary>
/// <param name="eventSource">The <see cref="EventSourceId" />.</param>
/// <param name="eventAggregateRoot">Type <see cref="ArtifactId"/> the Event was applied by.</param>
/// <param name="aggregateRoot"><see cref="ArtifactId"/> of the Aggregate Root.</param>
public EventWasAppliedByOtherAggregateRoot(ArtifactId eventAggregateRoot, ArtifactId aggregateRoot)
: base($"Aggregate Root '{eventAggregateRoot}' from event does not match with '{aggregateRoot}'.")
public EventWasAppliedByOtherAggregateRoot(EventSourceId eventSource, ArtifactId eventAggregateRoot, ArtifactId aggregateRoot)
: base($"Tried to apply events to event source {eventSource} on aggregate Root {eventAggregateRoot} but expected events to be applied to aggregate root {aggregateRoot}")
{
}
}
Expand Down
6 changes: 4 additions & 2 deletions Source/Events.Store/EventWasAppliedToOtherEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Dolittle.Artifacts;

namespace Dolittle.Runtime.Events.Store
{
Expand All @@ -13,10 +14,11 @@ public class EventWasAppliedToOtherEventSource : ArgumentException
/// <summary>
/// Initializes a new instance of the <see cref="EventWasAppliedToOtherEventSource"/> class.
/// </summary>
/// <param name="aggregateRoot">The aggregate root id.</param>
/// <param name="eventEventSource">The <see cref="EventSourceId"/> the Event was applied to.</param>
/// <param name="eventSource"><see cref="EventSourceId"/> of the Event Source.</param>
public EventWasAppliedToOtherEventSource(EventSourceId eventEventSource, EventSourceId eventSource)
: base($"Event Source '{eventEventSource}' from event does not match with '{eventSource}'.")
public EventWasAppliedToOtherEventSource(ArtifactId aggregateRoot, EventSourceId eventEventSource, EventSourceId eventSource)
: base($"Failed to commit events to event source {eventSource} on aggregate root {aggregateRoot} because the committed events was applied to another event source {eventEventSource}")
{
}
}
Expand Down

0 comments on commit 2aaace0

Please sign in to comment.