Skip to content

Commit 2f35b90

Browse files
committed
add benchmark for large scale cycles
1 parent 782625f commit 2f35b90

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

pkg/sync/expand/cycle.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package expand
22

33
import (
4+
"context"
5+
46
mapset "github.com/deckarep/golang-set/v2"
57
)
68

@@ -147,22 +149,26 @@ func (g *EntitlementGraph) removeNode(nodeID int) {
147149

148150
// FixCycles if any cycles of nodes exist, merge all nodes in that cycle into a
149151
// single node and then repeat. Iteration ends when there are no more cycles.
150-
func (g *EntitlementGraph) FixCycles() error {
152+
func (g *EntitlementGraph) FixCycles(ctx context.Context) error {
151153
if g.HasNoCycles {
152154
return nil
153155
}
154-
cycle := g.GetFirstCycle()
155-
if cycle == nil {
156-
g.HasNoCycles = true
157-
return nil
158-
}
156+
for {
157+
select {
158+
case <-ctx.Done():
159+
return ctx.Err()
160+
default:
161+
}
162+
cycle := g.GetFirstCycle()
163+
if cycle == nil {
164+
g.HasNoCycles = true
165+
return nil
166+
}
159167

160-
if err := g.fixCycle(cycle); err != nil {
161-
return err
168+
if err := g.fixCycle(cycle); err != nil {
169+
return err
170+
}
162171
}
163-
164-
// Recurse!
165-
return g.FixCycles()
166172
}
167173

168174
// fixCycle takes a list of Node IDs that form a cycle and merges them into a

pkg/sync/expand/graph_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func TestHandleCycle(t *testing.T) {
201201
}
202202
require.True(t, found)
203203

204-
err := graph.FixCycles()
204+
err := graph.FixCycles(ctx)
205205
require.NoError(t, err, graph.Str())
206206
err = graph.Validate()
207207
require.NoError(t, err)
@@ -221,7 +221,7 @@ func TestHandleComplexCycle(t *testing.T) {
221221
require.Equal(t, 4, len(graph.Edges))
222222
require.Equal(t, 3, len(graph.GetEntitlements()))
223223

224-
err := graph.FixCycles()
224+
err := graph.FixCycles(ctx)
225225
require.NoError(t, err, graph.Str())
226226
err = graph.Validate()
227227
require.NoError(t, err)
@@ -248,7 +248,7 @@ func TestHandleCliqueCycle(t *testing.T) {
248248
require.Equal(t, 6, len(graph.Edges))
249249
require.Equal(t, 3, len(graph.GetEntitlements()))
250250

251-
err := graph.FixCycles()
251+
err := graph.FixCycles(ctx)
252252
require.NoError(t, err, graph.Str())
253253
err = graph.Validate()
254254
require.NoError(t, err)

pkg/sync/syncer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ func (s *syncer) SyncGrantExpansion(ctx context.Context) error {
13731373
return fmt.Errorf("cycles detected in entitlement graph")
13741374
}
13751375

1376-
err := entitlementGraph.FixCycles()
1376+
err := entitlementGraph.FixCycles(ctx)
13771377
if err != nil {
13781378
return err
13791379
}

0 commit comments

Comments
 (0)