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

Activity: fix for token streams clearing delay #29

Merged
merged 1 commit into from
Jul 11, 2024
Merged
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
91 changes: 43 additions & 48 deletions Core/Stateflows/Activities/Engine/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,38 +544,54 @@ public async Task DoHandleNodeAsync(Node node, NodeScope nodeScope, IEnumerable<
return;
}

IEnumerable<Stream> streams = Array.Empty<Stream>();
var activated = false;

IEnumerable<TokenHolder> inputTokens = Array.Empty<TokenHolder>();

lock (node)
{
var streams = !Context.NodesToExecute.Contains(node)
? Context.GetActivatedStreams(node, nodeScope.ThreadId)
: Array.Empty<Stream>();

activated =
( // initial node case
node.Type == NodeType.Initial
) ||
( // input node case - has to have input
node.Type == NodeType.Input &&
(input?.Any() ?? false)
) ||
( // no implicit join node - has any incoming streams
streams.Any() &&
!node.Options.HasFlag(NodeOptions.ImplicitJoin)
) ||
( // implicit join node - has incoming streams on all edges
node.IncomingEdges.Count == streams.Count() &&
node.Options.HasFlag(NodeOptions.ImplicitJoin)
) ||
(
Context.NodesToExecute.Contains(node)
);

if (!Context.NodesToExecute.Contains(node))
{
lock (node)
inputTokens = input ?? streams.SelectMany(stream => stream.Tokens).Distinct().ToArray();

if (activated)
{
streams = Context.GetActivatedStreams(node, nodeScope.ThreadId);
foreach (var stream in streams)
{
if (!stream.IsPersistent)
{
Context.ClearStream(stream.EdgeIdentifier, nodeScope.ThreadId);
}
}
}
else
{
ReportNodeAttemptedExecution(node, streams);
}
}

var activated =
( // initial node case
node.Type == NodeType.Initial
) ||
( // input node case - has to have input
node.Type == NodeType.Input &&
(input?.Any() ?? false)
) ||
( // no implicit join node - has any incoming streams
streams.Any() &&
!node.Options.HasFlag(NodeOptions.ImplicitJoin)
) ||
( // implicit join node - has incoming streams on all edges
node.IncomingEdges.Count == streams.Count() &&
node.Options.HasFlag(NodeOptions.ImplicitJoin)
) ||
(
Context.NodesToExecute.Contains(node)
);

var inputTokens = input ?? streams.SelectMany(stream => stream.Tokens).Distinct().ToArray();

nodeScope = nodeScope.CreateChildScope(node);

var actionContext = new ActionContext(Context, nodeScope, node, inputTokens, selectionTokens);
Expand All @@ -602,14 +618,8 @@ public async Task DoHandleNodeAsync(Node node, NodeScope nodeScope, IEnumerable<
return;
}

//var inputTokens = input ?? streams.SelectMany(stream => stream.Tokens).Distinct().ToArray();

ReportNodeExecuting(node, inputTokens);

//nodeScope = nodeScope.CreateChildScope(node);

//var actionContext = new ActionContext(Context, nodeScope, node, inputTokens, selectionTokens);

await node.Action.WhenAll(actionContext);

if (node.Type == NodeType.AcceptEventAction)
Expand Down Expand Up @@ -677,21 +687,6 @@ public async Task DoHandleNodeAsync(Node node, NodeScope nodeScope, IEnumerable<
await Task.WhenAll(nodes.Select(n => DoHandleNodeAsync(n, nodeScope)).ToArray());
}
}

lock (node)
{
foreach (var stream in streams)
{
if (!stream.IsPersistent)
{
Context.ClearStream(stream.EdgeIdentifier, nodeScope.ThreadId);
}
}
}
}
else
{
ReportNodeAttemptedExecution(node, streams);
}

await Inspector.AfterNodeActivateAsync(null);
Expand Down
61 changes: 61 additions & 0 deletions Tests/Activity.IntegrationTests/Tests/Decision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,53 @@ protected override void InitializeStateflows(IStateflowsBuilder builder)
TokenCount2 += c.GetTokensOfType<int>().Count();
})
)
.AddActivity("multipleTokensDecision", b => b
.AddInitial(b => b
.AddControlFlow("generate")
)
.AddAction(
"generate",
async c =>
{
c.OutputRange(Enumerable.Range(0, 10));
c.Output("test");
},
b => b
.AddFlow<int>("main")
.AddFlow<string>("main")
)
.AddStructuredActivity("main", b => b
.AddInput(b => b
.AddFlow<int, DecisionNode<int>>()
.AddFlow<string>("final1")
.AddFlow<string>("final2")
)
.AddDecision<int>(b => b
.AddFlow("final1", b => b.AddGuard(async c => c.Token % 2 == 0))
.AddElseFlow("final2")
)
.AddAction(
"final1",
async c =>
{
ExecutionCount1++;
TokenCount1 += c.GetTokensOfType<int>().Count();
await Task.Delay(1);
},
b => b.AddFlow<int, OutputNode>()
)
.AddAction(
"final2",
async c =>
{
ExecutionCount2++;
TokenCount2 += c.GetTokensOfType<int>().Count();
},
b => b.AddFlow<int, OutputNode>()
)
.AddOutput()
)
)
.AddActivity("controlDecision", b => b
.AddInitial(b => b
.AddControlFlow("setup")
Expand Down Expand Up @@ -85,6 +132,20 @@ public async Task TokenDecision()
Assert.AreEqual(5, TokenCount2);
}

[TestMethod]
public async Task MultipleTokenDecision()
{
if (ActivityLocator.TryLocateActivity(new ActivityId("multipleTokensDecision", "x"), out var a))
{
await a.InitializeAsync();
}

Assert.AreEqual(1, ExecutionCount1);
Assert.AreEqual(5, TokenCount1);
Assert.AreEqual(1, ExecutionCount2);
Assert.AreEqual(5, TokenCount2);
}

[TestMethod]
public async Task ControlDecision()
{
Expand Down
Loading