Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add special EntityMessageEvent #984

Draft
wants to merge 1 commit into
base: feature/core-entities
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -908,16 +908,17 @@ TraceContextBase GetParentTraceContext(OrchestrationSession session)
}

break;
}
} else
}
}
else
{

// In this case, we set the parentTraceContext later in this method
if (message.TaskMessage.Event is EventRaisedEvent)
{
foundEventRaised = true;
}
}
}
}

// When EventRaisedEvent is present, it will not, out of the box, share the same operation
Expand Down
9 changes: 9 additions & 0 deletions src/DurableTask.Core/Common/Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ public static bool AutoStart(string instanceId, IList<TaskMessage> newMessages)
OrchestrationInstance = orchestrationInstance,
Event = startedEvent
};

if (newMessages[0].Event is EntityMessageEvent)
{
startedEvent.Tags = new Dictionary<string, string>()
{
[OrchestrationTags.Entity] = ""
};
}

newMessages.Insert(0, taskMessage);
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/DurableTask.Core/Entities/EntityMessageEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public readonly struct EntityMessageEvent
{
readonly string eventName;
readonly EntityMessage message;
readonly OrchestrationInstance target;
readonly EntityInstance target;

internal EntityMessageEvent(string eventName, EntityMessage message, OrchestrationInstance target)
{
this.eventName = eventName;
this.message = message;
this.target = target;
this.target = target as EntityInstance ?? EntityInstance.FromBase(target);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -78,7 +78,7 @@ public TaskMessage AsTaskMessage()
return new TaskMessage
{
OrchestrationInstance = this.target,
Event = new History.EventRaisedEvent(-1, this.ContentAsString())
Event = new History.EntityMessageEvent(-1, this.ContentAsString())
{
Name = this.eventName
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public IEnumerable<EntityMessageEvent> EmitLockReleaseMessages()

foreach (var entityId in this.criticalSectionLocks!)
{
var instance = new OrchestrationInstance() { InstanceId = entityId.ToString() };
var instance = new EntityInstance() { InstanceId = entityId.ToString() };
yield return new EntityMessageEvent(EntityMessageEventNames.ReleaseMessageEventName, message, instance);
}

Expand Down
31 changes: 31 additions & 0 deletions src/DurableTask.Core/History/EntityOperationEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace DurableTask.Core.History
{
/// <summary>
/// A special case of <see cref="EventRaisedEvent"/> for entities-as-orchestrations.
/// </summary>
public class EntityMessageEvent : EventRaisedEvent
{
/// <summary>
/// Creates a new <see cref="EntityMessageEvent"/> with the supplied event id and input.
/// </summary>
/// <param name="eventId">The ID of the event.</param>
/// <param name="input">The serialized operation payload.</param>
public EntityMessageEvent(int eventId, string input)
: base(eventId, input)
{
}
}
}
24 changes: 23 additions & 1 deletion src/DurableTask.Core/History/EventRaisedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

namespace DurableTask.Core.History
{
using DurableTask.Core.Command;
using DurableTask.Core.Tracing;
using System.Diagnostics;
using System;
using System.Runtime.Serialization;

/// <summary>
Expand Down Expand Up @@ -56,5 +57,26 @@ public EventRaisedEvent(int eventId, string input)
/// </summary>
[DataMember]
public DistributedTraceContext ParentTraceContext { get; set; }

internal static EventRaisedEvent FromAction(SendEventOrchestratorAction action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}

if (action.Instance is EntityInstance)
{
return new EntityMessageEvent(-1, action.EventData)
{
Name = action.EventName,
};
}

return new EventRaisedEvent(-1, action.EventData)
{
Name = action.EventName,
};
}
}
}
22 changes: 22 additions & 0 deletions src/DurableTask.Core/OrchestrationInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace DurableTask.Core
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;

Expand Down Expand Up @@ -71,4 +72,25 @@ public override string ToString()
/// </summary>
public ExtensionDataObject ExtensionData { get; set; }
}

/// <summary>
/// A marker for entity instances.
/// </summary>
internal class EntityInstance : OrchestrationInstance
{
internal static EntityInstance FromBase(OrchestrationInstance instance)
{
if (instance is null)
{
throw new ArgumentNullException(nameof(instance));
}

return new EntityInstance
{
InstanceId = instance.InstanceId,
ExecutionId = instance.ExecutionId,
ExtensionData = instance.ExtensionData,
};
}
}
}
17 changes: 15 additions & 2 deletions src/DurableTask.Core/OrchestrationTags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DurableTask.Core
{
Expand All @@ -36,6 +34,11 @@ public static class OrchestrationTags
/// </remarks>
public const string FireAndForget = "FireAndForget";

/// <summary>
/// Used for entities-as-orchestrations.
/// </summary>
public const string Entity = "__Entity__";

/// <summary>
/// Check whether the given tags contain the fire and forget tag
/// </summary>
Expand All @@ -46,6 +49,16 @@ internal static bool IsTaggedAsFireAndForget(IDictionary<string, string> tags)
return tags != null && tags.ContainsKey(FireAndForget);
}

/// <summary>
/// Check whether the given tags contain the entity tag.
/// </summary>
/// <param name="tags"></param>
/// <returns></returns>
internal static bool IsEntity(IDictionary<string, string> tags)
{
return tags != null && tags.ContainsKey(Entity);
}

internal static IDictionary<string, string> MergeTags(
IDictionary<string, string> newTags,
IDictionary<string, string> existingTags)
Expand Down
5 changes: 1 addition & 4 deletions src/DurableTask.Core/TaskOrchestrationDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,10 +1148,7 @@ TaskMessage ProcessSendEventDecision(

runtimeState.AddEvent(historyEvent);

EventRaisedEvent eventRaisedEvent = new EventRaisedEvent(-1, sendEventAction.EventData)
{
Name = sendEventAction.EventName
};
EventRaisedEvent eventRaisedEvent = EventRaisedEvent.FromAction(sendEventAction);

// Distributed Tracing: start a new trace activity derived from the orchestration
// for an EventRaisedEvent (external event)
Expand Down