diff --git a/pkg/domain/resourcegroup/runaway.go b/pkg/domain/resourcegroup/runaway.go index 87c26bf5ff813..266927af8f767 100644 --- a/pkg/domain/resourcegroup/runaway.go +++ b/pkg/domain/resourcegroup/runaway.go @@ -255,6 +255,11 @@ func (rm *RunawayManager) MarkSyncerInitialized() { rm.syncerInitialized.Store(true) } +// IsSyncerInitialized is used to check whether the syncer is initialized. +func (rm *RunawayManager) IsSyncerInitialized() bool { + return rm.syncerInitialized.Load() +} + // DeriveChecker derives a RunawayChecker from the given resource group func (rm *RunawayManager) DeriveChecker(resourceGroupName, originalSQL, sqlDigest, planDigest string) *RunawayChecker { group, err := rm.resourceGroupCtl.GetResourceGroup(resourceGroupName) @@ -262,6 +267,10 @@ func (rm *RunawayManager) DeriveChecker(resourceGroupName, originalSQL, sqlDiges logutil.BgLogger().Warn("cannot setup up runaway checker", zap.Error(err)) return nil } + // Only check the normal statement. + if len(planDigest) == 0 { + return nil + } rm.activeLock.RLock() defer rm.activeLock.RUnlock() if group.RunawaySettings == nil && rm.activeGroup[resourceGroupName] == 0 { diff --git a/pkg/executor/internal/querywatch/BUILD.bazel b/pkg/executor/internal/querywatch/BUILD.bazel index a562d80683d32..b94749fbdc27e 100644 --- a/pkg/executor/internal/querywatch/BUILD.bazel +++ b/pkg/executor/internal/querywatch/BUILD.bazel @@ -38,6 +38,7 @@ go_test( "//pkg/meta/autoid", "//pkg/testkit", "//pkg/testkit/testsetup", + "@com_github_pingcap_failpoint//:failpoint", "@com_github_stretchr_testify//require", "@com_github_tikv_client_go_v2//tikv", "@org_uber_go_goleak//:goleak", diff --git a/pkg/executor/internal/querywatch/query_watch_test.go b/pkg/executor/internal/querywatch/query_watch_test.go index f47160c27c397..adb64091a19f5 100644 --- a/pkg/executor/internal/querywatch/query_watch_test.go +++ b/pkg/executor/internal/querywatch/query_watch_test.go @@ -19,6 +19,7 @@ import ( "testing" "time" + "github.com/pingcap/failpoint" mysql "github.com/pingcap/tidb/pkg/errno" "github.com/pingcap/tidb/pkg/kv" "github.com/pingcap/tidb/pkg/testkit" @@ -132,3 +133,22 @@ func TestQueryWatch(t *testing.T) { time.Sleep(1 * time.Second) tk.MustGetErrCode("select * from test.t1", mysql.ErrResourceGroupQueryRunawayQuarantine) } + +func TestQueryWatchIssue56897(t *testing.T) { + require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/resourcegroup/runaway/FastRunawayGC", `return(true)`)) + defer func() { + require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/resourcegroup/runaway/FastRunawayGC")) + }() + store, dom := testkit.CreateMockStoreAndDomain(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + require.Eventually(t, func() bool { + return dom.RunawayManager().IsSyncerInitialized() + }, 20*time.Second, 300*time.Millisecond) + tk.MustQuery("QUERY WATCH ADD ACTION KILL SQL TEXT SIMILAR TO 'use test';").Check((testkit.Rows("1"))) + time.Sleep(1 * time.Second) + _, err := tk.Exec("use test") + require.Nil(t, err) + _, err = tk.Exec("use mysql") + require.Nil(t, err) +}