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

Enable concurrent evaluations of matched expressions #20

Merged
merged 6 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
105 changes: 75 additions & 30 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/google/cel-go/common/operators"
"github.com/google/uuid"
"golang.org/x/sync/semaphore"
)

var (
Expand Down Expand Up @@ -70,11 +71,17 @@ func NewAggregateEvaluator(
parser TreeParser,
eval ExpressionEvaluator,
evalLoader EvaluableLoader,
concurrency int64,
) AggregateEvaluator {
if concurrency <= 0 {
concurrency = 1
}

return &aggregator{
eval: eval,
parser: parser,
loader: evalLoader,
sem: semaphore.NewWeighted(concurrency),
engines: map[EngineType]MatchingEngine{
EngineTypeStringHash: newStringEqualityMatcher(),
EngineTypeNullMatch: newNullMatcher(),
Expand All @@ -92,6 +99,8 @@ type aggregator struct {
// engines records all engines
engines map[EngineType]MatchingEngine

sem *semaphore.Weighted

// lock prevents concurrent updates of data
lock *sync.RWMutex
// len stores the current len of aggregable expressions.
Expand Down Expand Up @@ -123,6 +132,7 @@ func (a *aggregator) Evaluate(ctx context.Context, data map[string]any) ([]Evalu
err error
matched = int32(0)
result = []Evaluable{}
wg sync.WaitGroup
)

// TODO: Concurrently match constant expressions using a semaphore for capacity.
Expand All @@ -131,25 +141,42 @@ func (a *aggregator) Evaluate(ctx context.Context, data map[string]any) ([]Evalu
if err != nil {
return nil, 0, err
}
for _, expr := range constantEvals {
atomic.AddInt32(&matched, 1)

if expr.GetExpression() == "" {
result = append(result, expr)
continue
for _, item := range constantEvals {
if err := a.sem.Acquire(ctx, 1); err != nil {
return result, matched, err
}

// NOTE: We don't need to add lifted expression variables,
// because match.Parsed.Evaluable() returns the original expression
// string.
ok, evalerr := a.eval(ctx, expr, data)
if evalerr != nil {
err = errors.Join(err, evalerr)
continue
}
if ok {
result = append(result, expr)
}
expr := item
wg.Add(1)
go func() {
defer a.sem.Release(1)
defer wg.Done()
defer func() {
if r := recover(); r != nil {
err = errors.Join(err, fmt.Errorf("recovered from panic in evaluate: %v", r))
}
}()

atomic.AddInt32(&matched, 1)

if expr.GetExpression() == "" {
result = append(result, expr)
return
}

// NOTE: We don't need to add lifted expression variables,
// because match.Parsed.Evaluable() returns the original expression
// string.
ok, evalerr := a.eval(ctx, expr, data)
if evalerr != nil {
err = errors.Join(err, evalerr)
return
}
if ok {
result = append(result, expr)
}
}()
}

matches, merr := a.AggregateMatch(ctx, data)
Expand Down Expand Up @@ -178,23 +205,41 @@ func (a *aggregator) Evaluate(ctx context.Context, data map[string]any) ([]Evalu
continue
}

atomic.AddInt32(&matched, 1)
// NOTE: We don't need to add lifted expression variables,
// because match.Parsed.Evaluable() returns the original expression
// string.
ok, evalerr := a.eval(ctx, match, data)

seen[match.GetID()] = struct{}{}

if evalerr != nil {
err = errors.Join(err, evalerr)
continue
}
if ok {
result = append(result, match)
if err := a.sem.Acquire(ctx, 1); err != nil {
return result, matched, err
}

expr := match
wg.Add(1)
go func() {
defer a.sem.Release(1)
defer wg.Done()
defer func() {
if r := recover(); r != nil {
err = errors.Join(err, fmt.Errorf("recovered from panic in evaluate: %v", r))
}
}()

atomic.AddInt32(&matched, 1)
// NOTE: We don't need to add lifted expression variables,
// because match.Parsed.Evaluable() returns the original expression
// string.
ok, evalerr := a.eval(ctx, expr, data)

seen[expr.GetID()] = struct{}{}

if evalerr != nil {
err = errors.Join(err, evalerr)
return
}
if ok {
result = append(result, expr)
}
}()
}

wg.Wait()

return result, matched, err
}

Expand Down
Loading
Loading