-
Notifications
You must be signed in to change notification settings - Fork 6.2k
pkg/util/topsql/reporter: stabilize flaky TestTopRUPipelineInProcessIntegration #67600
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -178,6 +178,116 @@ func TestDoReportSendsMetaWhenRURecordsEmpty(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestReportWorkerWaitsForInFlightSQLMetaRegistration(t *testing.T) { | ||
| origReportHook := reportWorkerBeforeBuildReportDataHook | ||
| origRegisterHook := normalizedMetaRegisterAfterLoadHook | ||
|
|
||
| registerLoadedOldMap := make(chan struct{}) | ||
| releaseRegister := make(chan struct{}) | ||
| reportWorkerReceivedData := make(chan struct{}) | ||
| takeDone := make(chan struct{}) | ||
|
|
||
| reportWorkerBeforeBuildReportDataHook = func() { | ||
| select { | ||
| case <-reportWorkerReceivedData: | ||
| default: | ||
| close(reportWorkerReceivedData) | ||
| } | ||
| } | ||
|
|
||
| tsr := NewRemoteTopSQLReporter(mockPlanBinaryDecoderFunc, mockPlanBinaryCompressFunc) | ||
| tsr.BindKeyspaceName([]byte("ks-race")) | ||
| t.Cleanup(func() { | ||
| select { | ||
| case <-releaseRegister: | ||
| default: | ||
| close(releaseRegister) | ||
| } | ||
| normalizedMetaRegisterAfterLoadHook = origRegisterHook | ||
| reportWorkerBeforeBuildReportDataHook = origReportHook | ||
| }) | ||
|
Comment on lines
+198
to
+208
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait for
Track the worker with a Also applies to: 210-212 🤖 Prompt for AI Agents |
||
| t.Cleanup(tsr.Close) | ||
|
|
||
| ch := make(chan *ReportData, 1) | ||
| require.NoError(t, tsr.Register(newMockDataSink(ch))) | ||
| go tsr.reportWorker() | ||
|
|
||
| seedDigest := []byte("sql-seed") | ||
| racingDigest := []byte("sql-racing") | ||
| tsr.RegisterSQL(seedDigest, "select 1", false) | ||
| normalizedMetaRegisterAfterLoadHook = func() { | ||
| select { | ||
| case <-registerLoadedOldMap: | ||
| default: | ||
| close(registerLoadedOldMap) | ||
| } | ||
| <-releaseRegister | ||
| } | ||
|
|
||
| registerDone := make(chan struct{}) | ||
| go func() { | ||
| tsr.RegisterSQL(racingDigest, "select 2", false) | ||
| close(registerDone) | ||
| }() | ||
|
|
||
| select { | ||
| case <-registerLoadedOldMap: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("timed out waiting for RegisterSQL to snapshot the old SQL meta map") | ||
| } | ||
|
|
||
| go func() { | ||
| tsr.takeDataAndSendToReportChan(60) | ||
| close(takeDone) | ||
| }() | ||
|
|
||
| require.Never(t, func() bool { | ||
| select { | ||
| case <-takeDone: | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| }, 300*time.Millisecond, 5*time.Millisecond) | ||
|
|
||
| require.Never(t, func() bool { | ||
| select { | ||
| case <-ch: | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| }, 300*time.Millisecond, 5*time.Millisecond) | ||
|
|
||
| close(releaseRegister) | ||
| select { | ||
| case <-registerDone: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("timed out waiting for the in-flight SQL meta registration to finish") | ||
| } | ||
| select { | ||
| case <-takeDone: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("timed out waiting for takeDataAndSendToReportChan to return") | ||
| } | ||
| select { | ||
| case <-reportWorkerReceivedData: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("timed out waiting for reportWorker to dequeue the payload") | ||
| } | ||
|
|
||
| select { | ||
| case payload := <-ch: | ||
| require.Len(t, payload.SQLMetas, 2) | ||
| _, ok := findSQLMeta(payload.SQLMetas, seedDigest) | ||
| require.True(t, ok, "missing seed SQL meta") | ||
| _, ok = findSQLMeta(payload.SQLMetas, racingDigest) | ||
| require.True(t, ok, "missing in-flight SQL meta") | ||
| case <-time.After(time.Second): | ||
| t.Fatal("timeout waiting for the report payload") | ||
| } | ||
| } | ||
|
|
||
| func TestCollectAndSendBatch(t *testing.T) { | ||
| tsr, ds := setupRemoteTopSQLReporter(t, maxSQLNum, 1) | ||
| populateCache(tsr, 0, maxSQLNum, 1) | ||
|
|
@@ -1026,9 +1136,32 @@ func TestTopRUPipelineInProcessIntegration(t *testing.T) { | |
| }, rmclient.DefaultRUVersion) | ||
|
|
||
| require.Eventually(t, func() bool { | ||
| // Wait until collectWorker has actually merged both hot-key batches. | ||
| // Channel drain is not enough because receive and addBatch are separate steps. | ||
| tsr.ruAggregator.mu.Lock() | ||
| defer tsr.ruAggregator.mu.Unlock() | ||
| return len(tsr.ruAggregator.buckets) > 0 | ||
|
|
||
| for _, bucket := range tsr.ruAggregator.buckets { | ||
| if bucket == nil || bucket.collecting == nil { | ||
| continue | ||
| } | ||
| userCollecting, ok := bucket.collecting.users["user-hot"] | ||
| if !ok { | ||
| continue | ||
| } | ||
| hotRecord, ok := userCollecting.records[makeKey(stmtstats.BinaryDigest("sql-hot"), stmtstats.BinaryDigest("plan-hot"))] | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| hotTotalRU, hotExecCount := 0.0, uint64(0) | ||
| for _, item := range hotRecord.items { | ||
| hotTotalRU += item.totalRU | ||
| hotExecCount += item.execCount | ||
| } | ||
| return hotTotalRU >= 17.0 && hotExecCount >= 3 | ||
| } | ||
| return false | ||
| }, time.Second, 10*time.Millisecond) | ||
|
|
||
| tsr.takeDataAndSendToReportChan(60) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.