From dad7401371bc245e0762470c0aa589a1bfa93d2a Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Wed, 18 Dec 2024 17:40:40 +0800 Subject: [PATCH] ddl: implement backoff strategy for async event notification in tests Enhance the asyncNotifyEvent function to include a backoff mechanism when sending events to the channel during unit tests. This prevents indefinite blocking on a full channel by attempting to send the event multiple times before giving up, while also logging a warning if the notification fails. Signed-off-by: Rustin170506 --- pkg/ddl/ddl.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/ddl/ddl.go b/pkg/ddl/ddl.go index 1d23f316dd14f..57357c04f2f95 100644 --- a/pkg/ddl/ddl.go +++ b/pkg/ddl/ddl.go @@ -594,7 +594,19 @@ func asyncNotifyEvent(jobCtx *jobContext, e *notifier.SchemaChangeEvent, job *mo if intest.InTest { ch := jobCtx.oldDDLCtx.ddlEventCh if ch != nil { - ch <- e + forLoop: + // Try sending the event to the channel with a backoff strategy to avoid blocking indefinitely. + // Since most unit tests don't consume events, we make a few attempts and then give up rather + // than blocking the DDL job forever on a full channel. + for i := 0; i < 10; i++ { + select { + case ch <- e: + break forLoop + default: + time.Sleep(time.Microsecond * 10) + } + } + logutil.DDLLogger().Warn("fail to notify DDL event", zap.Stringer("event", e)) } }