Skip to content

Commit 709ae7f

Browse files
committed
loops
1 parent da42eb0 commit 709ae7f

File tree

8 files changed

+28
-14
lines changed

8 files changed

+28
-14
lines changed

ManagedCode.Orleans.Graph.Tests/Cluster/TestSiloConfigurations.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public void Configure(ISiloBuilder siloBuilder)
1111
siloBuilder.AddOrleansGraph()
1212
.CreateGraph(graph =>
1313
{
14+
graph.AddGrain<IGrainA>()
15+
.WithReentrancy();
16+
1417
graph.AddGrainTransition<IGrainA, IGrainB>().AllMethods().WithReentrancy();
1518
graph.AddGrainTransition<IGrainB, IGrainC>().AllMethods().WithReentrancy();
1619
graph.AddGrainTransition<IGrainC, IGrainD>().AllMethods().WithReentrancy();

ManagedCode.Orleans.Graph.Tests/GrainGraphManagerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace ManagedCode.Orleans.Graph.Tests;
77

8-
public class GrainGraphManagerTests
8+
public class GrainTransitionManagerTests
99
{
1010

1111
[Fact]

ManagedCode.Orleans.Graph/Builder/GrainCallsBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ internal void AddReentrancy(string source, string target)
6262
_graph.AddTransition(source, target, new GrainTransition("*", "*", IsReentrant: true));
6363
}
6464

65-
public GrainGraphManager Build()
65+
public GrainTransitionManager Build()
6666
{
6767
ValidateCycles();
68-
return new GrainGraphManager(_graph);
68+
return new GrainTransitionManager(_graph);
6969
}
7070

7171
private void ValidateCycles()

ManagedCode.Orleans.Graph/Builder/Interfaces/IGrainCallsBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ public interface IGrainCallsBuilder
5050
/// <summary>
5151
/// Builds the grain graph manager.
5252
/// </summary>
53-
/// <returns>An instance of <see cref="GrainGraphManager"/>.</returns>
54-
GrainGraphManager Build();
53+
/// <returns>An instance of <see cref="GrainTransitionManager"/>.</returns>
54+
GrainTransitionManager Build();
5555
}

ManagedCode.Orleans.Graph/Common/DirectedGraph.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ private bool IsCyclicUtil(string vertex, HashSet<string> visited, HashSet<string
9292
{
9393
foreach (var neighbor in _adjacencyList[vertex].Keys)
9494
{
95+
if (_allowSelfLoops && vertex.Equals(neighbor))
96+
continue;
97+
9598
if (!visited.Contains(neighbor) && IsCyclicUtil(neighbor, visited, recursionStack))
9699
return true;
97100
else if (recursionStack.Contains(neighbor))

ManagedCode.Orleans.Graph/Filters/GraphIncomingGrainCallFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace ManagedCode.Orleans.Graph.Filters;
99

1010
public class GraphIncomingGrainCallFilter(IServiceProvider serviceProvider, GraphCallFilterConfig graphCallFilterConfig) : IIncomingGrainCallFilter
1111
{
12-
private GrainGraphManager? GraphManager => serviceProvider.GetService<GrainGraphManager>();
12+
private GrainTransitionManager? GraphManager => serviceProvider.GetService<GrainTransitionManager>();
1313

1414
public Task Invoke(IIncomingGrainCallContext context)
1515
{

ManagedCode.Orleans.Graph/Filters/GraphOutgoingGrainCallFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace ManagedCode.Orleans.Graph.Filters;
99

1010
public class GraphOutgoingGrainCallFilter(IServiceProvider serviceProvider, GraphCallFilterConfig graphCallFilterConfig) : IOutgoingGrainCallFilter
1111
{
12-
private GrainGraphManager? GraphManager => serviceProvider.GetService<GrainGraphManager>();
12+
private GrainTransitionManager? GraphManager => serviceProvider.GetService<GrainTransitionManager>();
1313

1414
public Task Invoke(IOutgoingGrainCallContext context)
1515
{

ManagedCode.Orleans.Graph/GrainGraphManager.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
namespace ManagedCode.Orleans.Graph;
77

8-
public class GrainGraphManager
8+
public class GrainTransitionManager
99
{
1010
private readonly DirectedGraph _grainGraph;
1111

12-
public GrainGraphManager(DirectedGraph grainGraph)
12+
public GrainTransitionManager(DirectedGraph grainGraph)
1313
{
1414
_grainGraph = grainGraph ?? throw new ArgumentNullException(nameof(grainGraph));
1515
}
@@ -18,19 +18,27 @@ public bool IsTransitionAllowed(CallHistory callHistory)
1818
{
1919
if (callHistory.IsEmpty())
2020
return false;
21-
22-
//TODO: check this code
21+
2322
var calls = callHistory.History.Reverse().ToArray();
23+
var visited = new HashSet<string>();
2424

2525
for (var i = 0; i < calls.Length - 1; i++)
2626
{
2727
var currentCall = calls[i];
2828
var nextCall = calls[i + 1];
2929

30-
if (currentCall.Direction == Direction.Out && nextCall.Direction == Direction.In)
30+
// Check for cycles
31+
var transitionKey = $"{currentCall.Interface}->{nextCall.Interface}";
32+
if (visited.Contains(transitionKey))
33+
{
34+
return false;
35+
}
36+
visited.Add(transitionKey);
37+
38+
// Check if the transition is allowed
39+
if (!_grainGraph.IsTransitionAllowed(currentCall.Interface, nextCall.Interface, currentCall.Method, nextCall.Method))
3140
{
32-
if (!_grainGraph.IsTransitionAllowed(currentCall.Interface, nextCall.Interface, currentCall.Method, nextCall.Method))
33-
return false;
41+
return false;
3442
}
3543
}
3644

0 commit comments

Comments
 (0)