Skip to content

Commit

Permalink
Merge pull request #12 from dolittle/remove-execution-context-manager
Browse files Browse the repository at this point in the history
Remove ExecutionContextManager
  • Loading branch information
joelhoisko authored Sep 21, 2020
2 parents 227be42 + f11da25 commit dfcb64b
Show file tree
Hide file tree
Showing 30 changed files with 295 additions and 299 deletions.
11 changes: 3 additions & 8 deletions Samples/Basic/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,12 @@ static void Main(string[] args)
)
)
).Build();
client.ExecutionContextManager.ForTenant("900893e7-c4cc-4873-8032-884e965e4b97");

var myEvent = new MyEvent("test string", 12345);
var commit = client.EventStore.ForTenant("900893e7-c4cc-4873-8032-884e965e4b97").Commit(myEvent, "8ac5b16a-0b88-4578-a005-e5247c611777");
Console.WriteLine(commit.Result.Events);

var myEventTask = client.EventStore.Commit(myEvent, "8ac5b16a-0b88-4578-a005-e5247c611777");
Console.WriteLine(myEventTask.Result.Events);

while (true)
{
Thread.Sleep(1000);
}
while (true) Thread.Sleep(1000);
}
}
}
8 changes: 5 additions & 3 deletions Source/Events.Filters/Internal/FilterEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Dolittle.SDK.Events.Processing;
using Dolittle.SDK.Events.Processing.Internal;
using Microsoft.Extensions.Logging;
using ExecutionContext = Dolittle.SDK.Execution.ExecutionContext;

namespace Dolittle.SDK.Events.Filters.Internal
{
Expand Down Expand Up @@ -39,19 +40,20 @@ protected FilterEventProcessor(
}

/// <inheritdoc/>
protected override Task<TResponse> Process(FilterEventRequest request, CancellationToken cancellation)
protected override Task<TResponse> Process(FilterEventRequest request, ExecutionContext executionContext, CancellationToken cancellation)
{
var comittedEvent = _converter.ToSDK(request.Event);
return Filter(comittedEvent.Content, comittedEvent.GetEventContext());
return Filter(comittedEvent.Content, comittedEvent.GetEventContext(executionContext), cancellation);
}

/// <summary>
/// Filters an event.
/// </summary>
/// <param name="event">The event to filter.</param>
/// <param name="context">The <see cref="EventContext" />.</param>
/// <param name="cancellation">The <see cref="CancellationToken" /> used to cancel the processing of the request.</param>
/// <returns>A <see cref="Task{TResult}" /> that, when resolved, returns a <typeparamref name="TResponse"/>.</returns>
protected abstract Task<TResponse> Filter(object @event, EventContext context);
protected abstract Task<TResponse> Filter(object @event, EventContext context, CancellationToken cancellation);

/// <inheritdoc/>
protected override RetryProcessingState GetRetryProcessingStateFromRequest(FilterEventRequest request)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Threading;
using System.Threading.Tasks;
using Dolittle.Runtime.Events.Processing.Contracts;
using Dolittle.SDK.Events.Processing;
Expand Down Expand Up @@ -52,7 +53,7 @@ protected override PartitionedFilterResponse CreateResponseFromFailure(Processor
=> new PartitionedFilterResponse { Failure = failure };

/// <inheritdoc/>
protected override async Task<PartitionedFilterResponse> Filter(object @event, EventContext context)
protected override async Task<PartitionedFilterResponse> Filter(object @event, EventContext context, CancellationToken cancellation)
{
var result = await _filterEventCallback(@event, context).ConfigureAwait(false);
return new PartitionedFilterResponse { IsIncluded = result.ShouldInclude, PartitionId = result.PartitionId.ToProtobuf() };
Expand Down
3 changes: 2 additions & 1 deletion Source/Events.Filters/Internal/PublicEventFilterProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Threading;
using System.Threading.Tasks;
using Dolittle.Runtime.Events.Processing.Contracts;
using Dolittle.SDK.Events.Processing;
Expand Down Expand Up @@ -47,7 +48,7 @@ protected override PartitionedFilterResponse CreateResponseFromFailure(Processor
=> new PartitionedFilterResponse { Failure = failure };

/// <inheritdoc/>
protected override async Task<PartitionedFilterResponse> Filter(object @event, EventContext context)
protected override async Task<PartitionedFilterResponse> Filter(object @event, EventContext context, CancellationToken cancellation)
{
var result = await _filterEventCallback(@event, context).ConfigureAwait(false);
return new PartitionedFilterResponse { IsIncluded = result.ShouldInclude, PartitionId = result.PartitionId.ToProtobuf() };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Threading;
using System.Threading.Tasks;
using Dolittle.Runtime.Events.Processing.Contracts;
using Dolittle.SDK.Events.Processing;
Expand Down Expand Up @@ -52,7 +53,7 @@ protected override FilterResponse CreateResponseFromFailure(ProcessorFailure fai
=> new FilterResponse { Failure = failure };

/// <inheritdoc/>
protected override async Task<FilterResponse> Filter(object @event, EventContext context)
protected override async Task<FilterResponse> Filter(object @event, EventContext context, CancellationToken cancellation)
=> new FilterResponse { IsIncluded = await _filterEventCallback(@event, context).ConfigureAwait(false) };
}
}
3 changes: 2 additions & 1 deletion Source/Events.Handling/EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Dolittle.SDK.Events.Handling.Builder;

Expand Down Expand Up @@ -49,7 +50,7 @@ public EventHandler(
public IEnumerable<EventType> HandledEvents => _eventHandlerMethods.Keys;

/// <inheritdoc/>
public async Task Handle(object @event, EventType eventType, EventContext context)
public async Task Handle(object @event, EventType eventType, EventContext context, CancellationToken cancellation)
{
if (!_eventHandlerMethods.TryGetValue(eventType, out var method)) throw new MissingEventHandlerForEventType(eventType);

Expand Down
4 changes: 3 additions & 1 deletion Source/Events.Handling/IEventHandler.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.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Dolittle.SDK.Events.Handling
Expand Down Expand Up @@ -37,7 +38,8 @@ public interface IEventHandler
/// <param name="event">The event to handle.</param>
/// <param name="eventType">The artifact representign the event type.</param>
/// <param name="context">The context in which the event is in.</param>
/// <param name="cancellation">The <see cref="CancellationToken" /> used to cancel the processing of the request.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous action.</returns>
Task Handle(object @event, EventType eventType, EventContext context);
Task Handle(object @event, EventType eventType, EventContext context, CancellationToken cancellation);
}
}
11 changes: 9 additions & 2 deletions Source/Events.Handling/Internal/EventHandlerProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Dolittle.SDK.Events.Processing.Internal;
using Dolittle.SDK.Protobuf;
using Microsoft.Extensions.Logging;
using ExecutionContext = Dolittle.SDK.Execution.ExecutionContext;

namespace Dolittle.SDK.Events.Handling.Internal
{
Expand Down Expand Up @@ -53,11 +54,17 @@ public override EventHandlerRegistrationRequest RegistrationRequest
}

/// <inheritdoc/>
protected override async Task<EventHandlerResponse> Process(HandleEventRequest request, CancellationToken cancellation)
protected override async Task<EventHandlerResponse> Process(HandleEventRequest request, ExecutionContext executionContext, CancellationToken cancellation)
{
var streamEvent = _converter.ToSDK(request.Event);
var comittedEvent = streamEvent.Event;
await _eventHandler.Handle(comittedEvent.Content, comittedEvent.EventType, comittedEvent.GetEventContext()).ConfigureAwait(false);
await _eventHandler
.Handle(
comittedEvent.Content,
comittedEvent.EventType,
comittedEvent.GetEventContext(executionContext),
cancellation)
.ConfigureAwait(false);
return new EventHandlerResponse();
}

Expand Down
8 changes: 5 additions & 3 deletions Source/Events.Processing/Internal/EventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Dolittle.SDK.Concepts;
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.Logging;
using ExecutionContext = Dolittle.SDK.Execution.ExecutionContext;

namespace Dolittle.SDK.Events.Processing.Internal
{
Expand Down Expand Up @@ -52,13 +53,13 @@ protected EventProcessor(
public abstract TRegisterArguments RegistrationRequest { get; }

/// <inheritdoc/>
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellation)
public async Task<TResponse> Handle(TRequest request, ExecutionContext executionContext, CancellationToken cancellation)
{
RetryProcessingState retryProcessingState = null;
try
{
retryProcessingState = GetRetryProcessingStateFromRequest(request);
return await Process(request, cancellation).ConfigureAwait(false);
return await Process(request, executionContext, cancellation).ConfigureAwait(false);
}
catch (Exception ex)
{
Expand All @@ -85,9 +86,10 @@ public async Task<TResponse> Handle(TRequest request, CancellationToken cancella
/// Method that will be called to process an event processing request from the server.
/// </summary>
/// <param name="request">The <typeparamref name="TRequest"/> to process.</param>
/// <param name="executionContext">The execution context to handle the request in.</param>
/// <param name="cancellation">The <see cref="CancellationToken" /> used to cancel the processing of the request.</param>
/// <returns>A <see cref="Task" /> that, when resolved, returns a <typeparamref name="TResponse"/>.</returns>
protected abstract Task<TResponse> Process(TRequest request, CancellationToken cancellation);
protected abstract Task<TResponse> Process(TRequest request, ExecutionContext executionContext, CancellationToken cancellation);

/// <summary>
/// Gets the <see cref="RetryProcessingState" /> from a <typeparamref name="TRequest"/>.
Expand Down
8 changes: 6 additions & 2 deletions Source/Events/CommittedEventExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Dolittle.SDK.Execution;

namespace Dolittle.SDK.Events
{
/// <summary>
Expand All @@ -12,12 +14,14 @@ public static class CommittedEventExtensions
/// Gets the <see cref="EventContext"/> for a <see cref="CommittedEvent"/>.
/// </summary>
/// <param name="event">The <see cref="CommittedEvent"/> to get the context for.</param>
/// <param name="currentExecutionContext">The <see cref="ExecutionContext"/> in which the event is currently being processed.</param>
/// <returns>The <see cref="EventContext"/> for a <see cref="CommittedEvent"/>.</returns>
public static EventContext GetEventContext(this CommittedEvent @event)
public static EventContext GetEventContext(this CommittedEvent @event, ExecutionContext currentExecutionContext)
=> new EventContext(
@event.EventLogSequenceNumber,
@event.EventSource,
@event.Occurred,
@event.ExecutionContext);
@event.ExecutionContext,
currentExecutionContext);
}
}
27 changes: 19 additions & 8 deletions Source/Events/EventContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ public class EventContext
/// <param name="sequenceNumber">The <see cref="EventLogSequenceNumber">sequence number</see> that uniquely identifies the event in the event log which it was committed.</param>
/// <param name="eventSourceId">The <see cref="EventSourceId"/> that the event was committed to.</param>
/// <param name="occurred">The <see cref="DateTimeOffset"/> when the event was committed to the <see cref="IEventStore"/>.</param>
/// <param name="executionContext">The <see cref="ExecutionContext"/> in which the event was committed to the <see cref="IEventStore"/>.</param>
/// <param name="committedExecutionContext">The <see cref="ExecutionContext"/> in which the event was committed to the <see cref="IEventStore"/>.</param>
/// <param name="currentExecutionContext">The <see cref="ExecutionContext"/> in which the event is currently being processed.</param>
public EventContext(
EventLogSequenceNumber sequenceNumber,
EventSourceId eventSourceId,
DateTimeOffset occurred,
ExecutionContext executionContext)
ExecutionContext committedExecutionContext,
ExecutionContext currentExecutionContext)
: this(
sequenceNumber,
eventSourceId,
occurred,
executionContext,
new EventIdentifier(executionContext.Microservice, executionContext.Tenant, sequenceNumber))
committedExecutionContext,
currentExecutionContext,
new EventIdentifier(committedExecutionContext.Microservice, committedExecutionContext.Tenant, sequenceNumber))
{
}

Expand All @@ -38,19 +41,22 @@ public EventContext(
/// <param name="sequenceNumber">The <see cref="EventLogSequenceNumber">sequence number</see> that uniquely identifies the event in the event log which it was committed.</param>
/// <param name="eventSourceId">The <see cref="EventSourceId"/> that the event was committed to.</param>
/// <param name="occurred">The <see cref="DateTimeOffset"/> when the event was committed to the <see cref="IEventStore"/>.</param>
/// <param name="executionContext">The <see cref="ExecutionContext"/> in which the event was committed to the <see cref="IEventStore"/>.</param>
/// <param name="committedExecutionContext">The <see cref="ExecutionContext"/> in which the event was committed to the <see cref="IEventStore"/>.</param>
/// <param name="currentExecutionContext">The <see cref="ExecutionContext"/> in which the event is currently being processed.</param>
/// <param name="uniqueIdentifier">The <see cref="EventIdentifier"/> that uniquely identifies the event.</param>
protected EventContext(
EventLogSequenceNumber sequenceNumber,
EventSourceId eventSourceId,
DateTimeOffset occurred,
ExecutionContext executionContext,
ExecutionContext committedExecutionContext,
ExecutionContext currentExecutionContext,
EventIdentifier uniqueIdentifier)
{
SequenceNumber = sequenceNumber;
EventSourceId = eventSourceId;
Occurred = occurred;
ExecutionContext = executionContext;
CommittedExecutionContext = committedExecutionContext;
CurrentExecutionContext = currentExecutionContext;
UniqueIdentifier = uniqueIdentifier;
}

Expand All @@ -72,7 +78,12 @@ protected EventContext(
/// <summary>
/// Gets the <see cref="ExecutionContext"/> in which the event was committed to the <see cref="IEventStore"/>.
/// </summary>
public ExecutionContext ExecutionContext { get; }
public ExecutionContext CommittedExecutionContext { get; }

/// <summary>
/// Gets the <see cref="ExecutionContext"/> in which the event was committed to the <see cref="IEventStore"/>.
/// </summary>
public ExecutionContext CurrentExecutionContext { get; }

/// <summary>
/// Gets the <see cref="EventIdentifier"/> that uniquely identifies the event.
Expand Down
14 changes: 8 additions & 6 deletions Source/Events/EventStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Dolittle.Services.Contracts;
using Microsoft.Extensions.Logging;
using Contracts = Dolittle.Runtime.Events.Contracts;
using ExecutionContext = Dolittle.SDK.Execution.ExecutionContext;

namespace Dolittle.SDK.Events
{
Expand All @@ -17,10 +18,11 @@ namespace Dolittle.SDK.Events
/// </summary>
public class EventStore : IEventStore
{
static readonly EventStoreCommitMethod _method = new EventStoreCommitMethod();

readonly IPerformMethodCalls _caller;
readonly EventStoreCommitMethod _method = new EventStoreCommitMethod();
readonly IEventConverter _eventConverter;
readonly IExecutionContextManager _executionContextManager;
readonly ExecutionContext _executionContext;
readonly IEventTypes _eventTypes;
readonly ILogger _logger;

Expand All @@ -29,19 +31,19 @@ public class EventStore : IEventStore
/// </summary>
/// <param name="caller">The caller for unary calls.</param>
/// <param name="eventConverter">The <see cref="IEventConverter" />.</param>
/// <param name="executionContextManager">An <see cref="IExecutionContextManager"/> for getting execution context from.</param>
/// <param name="executionContext">The <see cref="ExecutionContext"/> to use.</param>
/// <param name="eventTypes">The <see cref="IEventTypes"/>.</param>
/// <param name="logger">The <see cref="ILogger" />.</param>
public EventStore(
IPerformMethodCalls caller,
IEventConverter eventConverter,
IExecutionContextManager executionContextManager,
ExecutionContext executionContext,
IEventTypes eventTypes,
ILogger logger)
{
_caller = caller;
_eventConverter = eventConverter;
_executionContextManager = executionContextManager;
_executionContext = executionContext;
_eventTypes = eventTypes;
_logger = logger;
}
Expand Down Expand Up @@ -110,7 +112,7 @@ CallRequestContext GetCurrentCallContext()
{
// in the future this should be set to something more meaningfull
HeadId = HeadId.NotSet.Value.ToProtobuf(),
ExecutionContext = _executionContextManager.Current.ToProtobuf(),
ExecutionContext = _executionContext.ToProtobuf(),
};
}
}
Loading

0 comments on commit dfcb64b

Please sign in to comment.