From a4b40398da73985a1d7e4cee591c3dc674c20e8f Mon Sep 17 00:00:00 2001 From: Mohamed Hamza Date: Wed, 29 Jan 2025 16:45:14 -0500 Subject: [PATCH] add tiny sleep --- go/pools/smartconnpool/pool.go | 2 -- go/pools/smartconnpool/waitlist.go | 17 +++++++---------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/go/pools/smartconnpool/pool.go b/go/pools/smartconnpool/pool.go index acfb859a42d..7eead7fe264 100644 --- a/go/pools/smartconnpool/pool.go +++ b/go/pools/smartconnpool/pool.go @@ -396,9 +396,7 @@ func (pool *ConnPool[C]) put(conn *Pooled[C]) { } } - log.Error("========================== I'M TRYING TO RETURN A CONNECTION HERE") if !pool.wait.tryReturnConn(conn) { - log.Error("============================= tryReturnConn returned false, we're in that branch") connSetting := conn.Conn.Setting() if connSetting == nil { pool.clean.Push(conn) diff --git a/go/pools/smartconnpool/waitlist.go b/go/pools/smartconnpool/waitlist.go index 9ba2f3aedfe..42e49a78e8d 100644 --- a/go/pools/smartconnpool/waitlist.go +++ b/go/pools/smartconnpool/waitlist.go @@ -18,8 +18,8 @@ package smartconnpool import ( "context" - "math/rand" "sync" + "time" "vitess.io/vitess/go/list" "vitess.io/vitess/go/vt/log" @@ -59,14 +59,10 @@ func (wl *waitlist[C]) waitForConn(ctx context.Context, setting *Setting) (*Pool wl.mu.Lock() // add ourselves as a waiter at the end of the waitlist wl.list.PushBackValue(elem) - log.Errorf("========================== THIS IS WHAT THE WAITLIST LOOKS LIKE BEFORE I START WAITING:\n%+v", wl) wl.mu.Unlock() // block on our waiter's semaphore until somebody can hand over a connection to us - i := rand.Int() - log.Errorf("======================= %d: I'M WAITING", i) elem.Value.sema.wait() - log.Error("======================== %d: I'M DONE WAITING", i) // we're awake -- the conn in our waiter contains the connection that was handed // over to us, or nothing if we've been waken up forcefully. save the conn before @@ -114,8 +110,12 @@ func (wl *waitlist[C]) expire(force bool) { func (wl *waitlist[D]) tryReturnConn(conn *Pooled[D]) bool { // fast path: if there's nobody waiting there's nothing to do if wl.list.Len() == 0 { - log.Error("======================= WE HIT THE FAST PATH, WE'RE RETURNING") - return false + // HACK: we're gonna sleep for a bit and try again, to see if there are still no waiters + time.Sleep(250 * time.Millisecond) + if wl.list.Len() == 0 { + log.Error("======================= WE HIT THE FAST PATH, WE'RE RETURNING") + return false + } } // split the slow path into a separate function to enable inlining return wl.tryReturnConnSlow(conn) @@ -153,12 +153,9 @@ func (wl *waitlist[D]) tryReturnConnSlow(conn *Pooled[D]) bool { // maybe there isn't anybody to hand over the connection to, because we've // raced with another client returning another connection if target == nil { - log.Error("====================== target was nil, we couldn't find someone to give the connection to") return false } - log.Errorf("===================== WE'VE FOUND A TARGET: %+v", target) - // if we have a target to return the connection to, simply write the connection // into the waiter and signal their semaphore. they'll wake up to pick up the // connection.