Skip to content

Commit

Permalink
add waitlist logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mhamza15 committed Jan 29, 2025
1 parent 591363c commit 71de463
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 0 additions & 3 deletions go/pools/smartconnpool/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"context"
"sync/atomic"
"time"

"vitess.io/vitess/go/vt/log"
)

type Connection interface {
Expand All @@ -47,7 +45,6 @@ func (dbc *Pooled[C]) Close() {
}

func (dbc *Pooled[C]) Recycle() {
log.Errorf("========================== IM RECYCLING THE CONNECTION\nconn:\n%+v\npool:\n%+v", dbc, dbc.pool)
switch {
case dbc.pool == nil:
dbc.Conn.Close()
Expand Down
2 changes: 2 additions & 0 deletions go/pools/smartconnpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ 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)
Expand Down
10 changes: 10 additions & 0 deletions go/pools/smartconnpool/waitlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package smartconnpool

import (
"context"
"math/rand"
"sync"

"vitess.io/vitess/go/list"
"vitess.io/vitess/go/vt/log"
)

// waiter represents a client waiting for a connection in the waitlist
Expand Down Expand Up @@ -57,10 +59,14 @@ 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
Expand Down Expand Up @@ -108,6 +114,7 @@ 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
}
// split the slow path into a separate function to enable inlining
Expand Down Expand Up @@ -146,9 +153,12 @@ 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.
Expand Down

0 comments on commit 71de463

Please sign in to comment.