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

feat(rules): Revise query for optimization #1979

Merged
merged 4 commits into from
Aug 10, 2023
Merged
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
102 changes: 68 additions & 34 deletions internal/storage/sql/common/evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"context"
"database/sql"
"sort"

sq "github.com/Masterminds/squirrel"
"go.flipt.io/flipt/internal/storage"
Expand All @@ -14,37 +15,63 @@ func (s *Store) GetEvaluationRules(ctx context.Context, namespaceKey, flagKey st
namespaceKey = storage.DefaultNamespace
}

ruleMetaRows, err := s.builder.
Select("id, \"rank\", segment_operator").
From("rules").
Where(sq.And{sq.Eq{"flag_key": flagKey}, sq.Eq{"namespace_key": namespaceKey}}).
QueryContext(ctx)
if err != nil {
return nil, err
}

defer func() {
if cerr := ruleMetaRows.Close(); cerr != nil && err == nil {
err = cerr
}
}()

type RuleMeta struct {
ID string
Rank int32
SegmentOperator flipt.SegmentOperator
}

var rmMap = make(map[string]*RuleMeta)

ruleIDs := make([]string, 0)
for ruleMetaRows.Next() {
var rm RuleMeta

if err := ruleMetaRows.Scan(&rm.ID, &rm.Rank, &rm.SegmentOperator); err != nil {
return nil, err
}

rmMap[rm.ID] = &rm
ruleIDs = append(ruleIDs, rm.ID)
}

if err := ruleMetaRows.Err(); err != nil {
return nil, err
}

if err := ruleMetaRows.Close(); err != nil {
return nil, err
}

rows, err := s.builder.Select(`
r.id,
r.namespace_key,
r.flag_key,
rss.segment_key,
rss.segment_match_type,
r.segment_operator,
r."rank",
rss.constraint_id,
rss.constraint_type,
rss.constraint_property,
rss.constraint_operator,
rss.constraint_value`,
).
From("rules AS r").
LeftJoin(`(
SELECT
rs.rule_id,
rs.segment_key,
s.match_type AS segment_match_type,
c.id AS constraint_id,
c."type" AS constraint_type,
c.property AS constraint_property,
c.operator AS constraint_operator,
c.value AS constraint_value
FROM rule_segments AS rs
JOIN segments AS s ON rs.segment_key = s."key"
LEFT JOIN constraints AS c ON (s."key" = c.segment_key AND s.namespace_key = c.namespace_key)
) rss ON (r.id = rss.rule_id)`).
Where(sq.And{sq.Eq{"r.flag_key": flagKey}, sq.Eq{"r.namespace_key": namespaceKey}}).
OrderBy("r.\"rank\" ASC").
rs.rule_id,
rs.segment_key,
s.match_type AS segment_match_type,
c.id AS constraint_id,
c."type" AS constraint_type,
c.property AS constraint_property,
c.operator AS constraint_operator,
c.value AS constraint_value
`).
From("rule_segments AS rs").
Join(`segments AS s ON rs.segment_key = s."key"`).
LeftJoin(`constraints AS c ON (s."key" = c.segment_key AND s.namespace_key = c.namespace_key)`).
Where(sq.Eq{"rs.rule_id": ruleIDs}).
QueryContext(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -77,12 +104,8 @@ func (s *Store) GetEvaluationRules(ctx context.Context, namespaceKey, flagKey st

if err := rows.Scan(
&intermediateStorageRule.ID,
&intermediateStorageRule.NamespaceKey,
&intermediateStorageRule.FlagKey,
&intermediateStorageRule.SegmentKey,
&intermediateStorageRule.SegmentMatchType,
&intermediateStorageRule.SegmentOperator,
&intermediateStorageRule.Rank,
&optionalConstraint.Id,
&optionalConstraint.Type,
&optionalConstraint.Property,
Expand All @@ -91,6 +114,13 @@ func (s *Store) GetEvaluationRules(ctx context.Context, namespaceKey, flagKey st
return rules, err
}

rm := rmMap[intermediateStorageRule.ID]

intermediateStorageRule.FlagKey = flagKey
intermediateStorageRule.NamespaceKey = namespaceKey
intermediateStorageRule.Rank = rm.Rank
intermediateStorageRule.SegmentOperator = rm.SegmentOperator

if existingRule, ok := uniqueRules[intermediateStorageRule.ID]; ok {
var constraint *storage.EvaluationConstraint
if optionalConstraint.Id.Valid {
Expand Down Expand Up @@ -156,6 +186,10 @@ func (s *Store) GetEvaluationRules(ctx context.Context, namespaceKey, flagKey st
}
}

sort.Slice(rules, func(i, j int) bool {
return rules[i].Rank < rules[j].Rank
})

if err := rows.Err(); err != nil {
return rules, err
}
Expand Down