Skip to content

Commit 37989db

Browse files
committed
write rules stat
1 parent 733a7c0 commit 37989db

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

tools/greenplum-to-pg-tests/cmd/check_pg_queries.go

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ var checkPgQueriesCmd = &cobra.Command{
123123
checkQueries(rules, &stats, dbPool, queries)
124124

125125
if checkPgQueriesConfig.writeRulesWithStat != "" {
126-
rules.UpdateFromStats(stats, checkPgQueriesConfig.sortRulesByCount)
126+
rules.UpdateFromStats(&stats, checkPgQueriesConfig.sortRulesByCount)
127127
if err := rules.WriteToFile(checkPgQueriesConfig.writeRulesWithStat); err != nil {
128128
log.Printf("Failed to update rules stat: %v", err)
129129
}
@@ -384,17 +384,28 @@ func checkQueries(rules Rules, stats *QueryStats, dbPool *internal.YdbPool, quer
384384
var itemsCounter atomic.Int64
385385
writeStatEveryItems := int64(checkPgQueriesConfig.writeStatEveryItems)
386386
var wg sync.WaitGroup
387+
var writeStatMutex sync.Mutex
387388
for range checkPgQueriesConfig.checkersCount {
388389
wg.Add(1)
389390
go func() {
390391
defer wg.Done()
391392
for q := range queries {
392393
checkQuery(stats, rules, dbPool, q)
393394
counter := itemsCounter.Add(1)
394-
if counter%writeStatEveryItems == 0 && checkPgQueriesConfig.writeStatPath != "" {
395-
if err := stats.SaveToFile(checkPgQueriesConfig.writeStatPath); err != nil {
396-
log.Printf("Stat file written failed %q: %v", checkPgQueriesConfig.writeStatPath, err)
395+
if writeStatEveryItems > 0 && counter%writeStatEveryItems == 0 {
396+
writeStatMutex.Lock()
397+
if checkPgQueriesConfig.writeStatPath != "" {
398+
if err := stats.SaveToFile(checkPgQueriesConfig.writeStatPath); err != nil {
399+
log.Printf("Stat file written failed %q: %v", checkPgQueriesConfig.writeStatPath, err)
400+
}
401+
}
402+
if checkPgQueriesConfig.writeRulesWithStat != "" {
403+
rules.UpdateFromStats(stats, checkPgQueriesConfig.sortRulesByCount)
404+
if err := rules.WriteToFile(checkPgQueriesConfig.writeRulesWithStat); err != nil {
405+
log.Printf("Failed to update rules stat: %v", err)
406+
}
397407
}
408+
writeStatMutex.Unlock()
398409
}
399410
}
400411
}()
@@ -495,13 +506,27 @@ type QueryStats struct {
495506
m sync.RWMutex
496507
writeStatMutex sync.Mutex
497508

498-
OkCount int
499-
TotalCount int
509+
okCount int
510+
totalCount int
500511

501512
MatchToRules map[string]*CounterWithExample[string] // [rule name] query example
502513
UnknownProblems map[string]*CounterWithExample[string]
503514
}
504515

516+
func (s *QueryStats) GetTotalCount() int {
517+
s.m.Lock()
518+
defer s.m.Unlock()
519+
520+
return s.totalCount
521+
}
522+
523+
func (s *QueryStats) GetOkCount() int {
524+
s.m.Lock()
525+
defer s.m.Lock()
526+
527+
return s.okCount
528+
}
529+
505530
func (s *QueryStats) GetOkPercent() float64 {
506531
s.m.RLock()
507532
defer s.m.RUnlock()
@@ -510,22 +535,22 @@ func (s *QueryStats) GetOkPercent() float64 {
510535
}
511536

512537
func (s *QueryStats) getOkPercentNeedLock() float64 {
513-
return float64(s.OkCount) / float64(s.TotalCount) * 100
538+
return float64(s.okCount) / float64(s.totalCount) * 100
514539
}
515540

516541
func (s *QueryStats) CountASOK(query string) {
517542
s.m.Lock()
518543
defer s.m.Unlock()
519544

520-
s.TotalCount++
521-
s.OkCount++
545+
s.totalCount++
546+
s.okCount++
522547
}
523548

524549
func (s *QueryStats) CountAsKnown(ruleName string, query string) {
525550
s.m.Lock()
526551
defer s.m.Unlock()
527552

528-
s.TotalCount++
553+
s.totalCount++
529554
if s.MatchToRules == nil {
530555
s.MatchToRules = make(map[string]*CounterWithExample[string])
531556
}
@@ -550,7 +575,7 @@ func (s *QueryStats) CountAsUnknown(reason string, query string) {
550575
s.m.Lock()
551576
defer s.m.Unlock()
552577

553-
s.TotalCount++
578+
s.totalCount++
554579
if s.UnknownProblems == nil {
555580
s.UnknownProblems = make(map[string]*CounterWithExample[string])
556581
}
@@ -597,7 +622,7 @@ func (s *QueryStats) PrintStats() {
597622
defer s.m.Unlock()
598623

599624
fmt.Println("Queries stat.")
600-
fmt.Println("Ok Count:", s.OkCount)
625+
fmt.Println("Ok Count:", s.okCount)
601626
fmt.Println()
602627
fmt.Println("Known issues")
603628
SessionStats_printExampleCounter(getTopCounter(s.MatchToRules, 10))
@@ -656,8 +681,8 @@ func (s *QueryStats) SaveToFile(path string) error {
656681
KnownIssues []CounterWithExample[string] `yaml:"known_issues"`
657682
}
658683

659-
statFile.TotalCount = s.TotalCount
660-
statFile.OkCount = s.OkCount
684+
statFile.TotalCount = s.totalCount
685+
statFile.OkCount = s.okCount
661686
statFile.OkPercent = s.getOkPercentNeedLock()
662687
statFile.UnknownIssues = s.getTopUnknownNeedLock(math.MaxInt)
663688
statFile.KnownIssues = s.getTopKnownNeedLock(math.MaxInt)

tools/greenplum-to-pg-tests/cmd/issue_rules.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ ydbIssue:
9696
return res, restYdbIssues
9797
}
9898

99-
func (r *Rules) UpdateFromStats(stats QueryStats, sortByCount bool) {
100-
r.TotalStat.TotalCount = stats.TotalCount
101-
r.TotalStat.TotalOk = stats.OkCount
99+
func (r *Rules) UpdateFromStats(stats *QueryStats, sortByCount bool) {
100+
r.TotalStat.TotalCount = stats.GetTotalCount()
101+
r.TotalStat.TotalOk = stats.GetOkCount()
102102
r.TotalStat.OkPercent = math.Round(stats.GetOkPercent()*100) / 100
103103

104104
okStats := stats.GetTopKnown(math.MaxInt)

0 commit comments

Comments
 (0)