diff --git a/driver/memcached/alloc_guard_test.go b/driver/memcached/alloc_guard_test.go index a5f307150..15f9d198e 100644 --- a/driver/memcached/alloc_guard_test.go +++ b/driver/memcached/alloc_guard_test.go @@ -65,6 +65,11 @@ func TestAllocBudgets(t *testing.T) { // per-request slab allocation that replaces the per-value copies // — tracked for v1.4.1. {name: "GetMulti_10", budget: 26, bench: benchGetMulti10}, + // GET with a context that can be cancelled (WithCancel, WithTimeout, an + // HTTP/2 stream's): on the direct path the blocked read is woken when the + // context ends, which costs context.AfterFunc's two allocations on top of + // the 3 of GET_small (celeris#1218). The wake's own state lives on the conn. + {name: "GET_small_cancellable", budget: 5, bench: benchGETSmallCancellable}, {name: "PoolAcquireRelease", budget: 0, bench: benchPoolAcquireRelease}, } @@ -102,6 +107,28 @@ func benchGETSmall(b *testing.B) { } } +func benchGETSmallCancellable(b *testing.B) { + fake := startFakeB(b) + fake.store.kv["k"] = memItem{value: []byte("v"), cas: 1} + c, err := NewClient(fake.Addr()) + if err != nil { + b.Fatal(err) + } + defer func() { _ = c.Close() }() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + if _, err := c.Get(ctx, "k"); err != nil { + b.Fatal(err) + } + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + if _, err := c.Get(ctx, "k"); err != nil { + b.Fatal(err) + } + } +} + func benchSETSmall(b *testing.B) { fake := startFakeB(b) c, err := NewClient(fake.Addr()) diff --git a/driver/memcached/cluster.go b/driver/memcached/cluster.go index 12af1430a..3e46649b1 100644 --- a/driver/memcached/cluster.go +++ b/driver/memcached/cluster.go @@ -49,7 +49,8 @@ type ClusterConfig struct { // [Config.MaxOpen]; zero means NumWorkers * 4 per node. MaxOpen int - // Timeout is the advisory per-op deadline forwarded to each node client. + // Timeout is the per-operation deadline forwarded to each node client: see + // [Config.Timeout]. Timeout time.Duration // DialTimeout is forwarded to each node client: see [Config.DialTimeout] diff --git a/driver/memcached/commands.go b/driver/memcached/commands.go index 9caec7267..80e12d8a8 100644 --- a/driver/memcached/commands.go +++ b/driver/memcached/commands.go @@ -107,15 +107,66 @@ func asBytes(v any) ([]byte, error) { } } -// run acquires a conn, runs fn with it, and releases the conn. On ctx -// cancellation or context deadline the conn is discarded because the server -// may still send a stale reply. +// opTimeout is the per-operation bound of the client: Config.Timeout, the default +// (3 s) when it is zero, none when it is negative. +func (c *Client) opTimeout() time.Duration { + t := c.cfg.Timeout + if t == 0 { + t = defaultTimeout + if h := testHookDefaultOpTimeout.Load(); h != nil { + t = *h + } + } + return max(t, 0) +} + +// opLimit works out the per-operation limit of one command (celeris#1218), as a +// value: no context is built, so a command pays no allocation for it. It returns +// the timeout and the time it ends, or zero values when the command is not limited +// by it: the timeout is off, or the caller's deadline is not later than it (the +// caller's deadline then ends the command, and its error is the caller's). +func (c *Client) opLimit(ctx context.Context) (timeout time.Duration, deadline time.Time) { + t := c.opTimeout() + if t == 0 { + return 0, time.Time{} + } + dl := time.Now().Add(t) + if cdl, ok := ctx.Deadline(); ok && !cdl.After(dl) { + return 0, time.Time{} + } + return t, dl +} + +// beginOp puts the per-operation limit on conn for one command and, for a direct +// conn (which waits in a socket read that does not look at a context), arms the wake +// of its read on the end of a cancellable ctx (armWake). A ctx that cannot be +// cancelled costs nothing; a cancellable one costs context.AfterFunc's two +// allocations. Pair it with endOp, after opError. +func (c *Client) beginOp(ctx context.Context, conn *memcachedConn) { + conn.opTimeout, conn.opDeadline = c.opLimit(ctx) + if conn.useDirect && ctx.Done() != nil { + conn.armWake(ctx) + } +} + +// endOp ends the limit of the command that ran on conn and disarms the wake. +func endOp(conn *memcachedConn) { + conn.disarmWake() + conn.opTimeout, conn.opDeadline = 0, time.Time{} +} + +// run acquires a conn, runs fn with it, and releases the conn. fn runs under the +// per-operation timeout (beginOp); acquiring the conn does not (a new conn has its +// own DialTimeout). On ctx cancellation or context deadline the conn is discarded +// because the server may still send a stale reply. func (c *Client) run(ctx context.Context, fn func(ctx context.Context, conn *memcachedConn) error) error { conn, err := c.pool.acquire(ctx, workerFromCtx(ctx)) if err != nil { return err } - ferr := fn(ctx, conn) + c.beginOp(ctx, conn) + ferr := conn.opError(ctx, fn(ctx, conn)) + endOp(conn) if ferr != nil && (errors.Is(ferr, context.Canceled) || errors.Is(ferr, context.DeadlineExceeded)) { c.pool.discard(conn) return ferr @@ -851,7 +902,9 @@ func (c *Client) Ping(ctx context.Context) error { if err != nil { return err } - perr := conn.Ping(ctx) + c.beginOp(ctx, conn) + perr := conn.opError(ctx, conn.Ping(ctx)) + endOp(conn) if perr != nil { if errors.Is(perr, context.Canceled) || errors.Is(perr, context.DeadlineExceeded) { c.pool.discard(conn) diff --git a/driver/memcached/conn.go b/driver/memcached/conn.go index 89c541187..ebe24ca5e 100644 --- a/driver/memcached/conn.go +++ b/driver/memcached/conn.go @@ -61,6 +61,28 @@ type syncMultiRoundTripper interface { type memcachedConn struct { useDirect bool + // opTimeout and opDeadline are the per-operation limit of the command that is + // running on this conn (celeris#1218): both zero when it is not limited by + // Config.Timeout. They are set by Client.beginOp and cleared by endOp, on the + // goroutine that owns the conn for the command. opTimer is the timer a loop + // conn parks on for it, created on first use and reused. + opTimeout time.Duration + opDeadline time.Time + opTimer *time.Timer + + // The wake of a direct conn's blocked read on the end of the caller's context + // (armWake). The state is on the conn so that a command allocates none of it. + wakeMu sync.Mutex + wakeFired bool + wakeDone bool + wakeFn func() + wakeStop func() bool + // wakeRan has capacity 1: the callback signals it when it has run, and + // disarmWake waits on it whenever stopping the wake failed, so that a callback + // of one command never lands in the next one. + wakeRan chan struct{} + wakeTCP *net.TCPConn + // Direct-mode fields. tcp *net.TCPConn // tcpFd caches the raw fd for MSG_DONTWAIT peeks. Pre-netpoll @@ -211,6 +233,11 @@ func (c *memcachedConn) pollIsDone() bool { // A test sets it to avoid waiting 5 s, and resets it to nil when it is done. var testHookDefaultHandshakeTimeout atomic.Pointer[time.Duration] +// testHookWakeDelay, when set, makes the wake callback of armWake sleep that long +// before it acts, so that a test can hold a callback in flight across the end of +// its command (celeris#1218). It is nil in production. +var testHookWakeDelay atomic.Pointer[time.Duration] + // handshake runs per-protocol validation. Text protocol has no handshake; // for binary we issue a VERSION opcode to confirm the server speaks binary. // (Some older stacks accept the TCP connection but send back a text ERROR @@ -247,14 +274,11 @@ func (c *memcachedConn) handshake(ctx context.Context) error { // A direct conn waits in a socket read that does not look at a context (only // at the deadline execBinaryDirect set from it), so the end of the caller's // context, a cancellation too, wakes the read. - var stopWake func() if c.useDirect { - stopWake = c.wakeReadOnDone(hctx) + c.armWake(hctx) } err := c.runHandshake(hctx) - if stopWake != nil { - stopWake() - } + c.disarmWake() if err != nil && errors.Is(err, os.ErrDeadlineExceeded) && (ctx.Err() != nil || callerFirst) { // The caller's context ended the handshake (its deadline can fire on the // socket a hair before the context reports it): its error, not a bare @@ -277,32 +301,59 @@ func (c *memcachedConn) handshake(ctx context.Context) error { return err } -// wakeReadOnDone arranges that, when ctx ends, the socket of a direct conn gets a -// deadline of "now", which wakes a read blocked on a silent server (celeris#1213; -// a direct conn does not watch its context's cancellation, only its deadline). The -// returned func disarms it; it also undoes a wake that raced with a handshake that -// had just succeeded, so the conn is not left with a deadline in the past. -func (c *memcachedConn) wakeReadOnDone(ctx context.Context) (disarm func()) { - var mu sync.Mutex - fired, done := false, false - tcp := c.tcp // Close sets c.tcp to nil - stop := context.AfterFunc(ctx, func() { - mu.Lock() - defer mu.Unlock() - if !done { - fired = true - _ = tcp.SetDeadline(time.Now()) - } - }) - return func() { - stop() - mu.Lock() - defer mu.Unlock() - done = true - if fired && !c.closed.Load() { - _ = tcp.SetDeadline(time.Time{}) +// armWake arranges that, when ctx ends, the socket of a direct conn gets a deadline +// of "now", which wakes a read blocked on a silent server (celeris#1213; a direct +// conn does not watch its context's cancellation, only its deadline). Its state +// lives on the conn and the callback is built once per conn, so what a command pays +// for it is context.AfterFunc's own two allocations and nothing else (celeris#1218). +// disarmWake ends it; it also undoes a wake that raced with a command that had +// just succeeded, so the conn is not left with a deadline in the past. A callback +// that has started when the command ends is waited for there (wakeRan), so it +// cannot run during the next command on the same conn and fail it. +func (c *memcachedConn) armWake(ctx context.Context) { + if c.wakeFn == nil { + tcp := c.tcp // Close sets c.tcp to nil + c.wakeTCP = tcp + c.wakeRan = make(chan struct{}, 1) + c.wakeFn = func() { + if d := testHookWakeDelay.Load(); d != nil { + time.Sleep(*d) + } + c.wakeMu.Lock() + if !c.wakeDone { + c.wakeFired = true + _ = tcp.SetDeadline(time.Now()) + } + c.wakeMu.Unlock() + select { + case c.wakeRan <- struct{}{}: + default: + } } } + c.wakeMu.Lock() + c.wakeFired, c.wakeDone = false, false + c.wakeMu.Unlock() + c.wakeStop = context.AfterFunc(ctx, c.wakeFn) +} + +// disarmWake ends the wake armed by armWake; it does nothing if none is armed. +func (c *memcachedConn) disarmWake() { + if c.wakeStop == nil { + return + } + if !c.wakeStop() { + // The context has ended and the callback has been started on its own + // goroutine (stop is called once per arm): it signals wakeRan when done. + <-c.wakeRan + } + c.wakeStop = nil + c.wakeMu.Lock() + defer c.wakeMu.Unlock() + c.wakeDone = true + if c.wakeFired && !c.closed.Load() { + _ = c.wakeTCP.SetDeadline(time.Time{}) + } } // runHandshake is the binary handshake without the bound: a VERSION round trip. @@ -599,13 +650,99 @@ func (c *memcachedConn) waitTransport(ctx context.Context, req *mcRequest) error default: } } + tc := c.armOpTimer() + defer c.disarmOpTimer() select { case <-req.doneCh: return nil case <-ctx.Done(): _ = c.closeWithErr(ctx.Err()) return ctx.Err() + case <-tc: + return c.opTimedOut(nil) + } +} + +// opTimeoutError is the error of a command that Config.Timeout ended. It matches +// context.DeadlineExceeded (and says "timed out"); cause is the socket's own +// timeout on a direct conn, nil on a loop conn. +type opTimeoutError struct { + d time.Duration + cause error +} + +func (e *opTimeoutError) Error() string { + s := fmt.Sprintf("celeris-memcached: operation timed out after %v: %v", e.d, context.DeadlineExceeded) + if e.cause != nil { + s += ": " + e.cause.Error() + } + return s +} +func (e *opTimeoutError) Is(target error) bool { return target == context.DeadlineExceeded } +func (e *opTimeoutError) Unwrap() error { return e.cause } +func (e *opTimeoutError) Timeout() bool { return true } + +// directDeadline is the deadline a direct conn's socket gets for the running +// command: the earlier of the caller's and the per-operation one. +func (c *memcachedConn) directDeadline(ctx context.Context) (time.Time, bool) { + dl, ok := ctx.Deadline() + if !c.opDeadline.IsZero() && (!ok || c.opDeadline.Before(dl)) { + return c.opDeadline, true } + return dl, ok +} + +// armOpTimer arms the conn's timer for what is left of the per-operation limit and +// returns its channel; nil (a channel that never fires) if the command has no +// limit. Called only when a loop conn is about to park: a reply that comes within +// the spin window costs nothing. +func (c *memcachedConn) armOpTimer() <-chan time.Time { + if c.opDeadline.IsZero() { + return nil + } + d := time.Until(c.opDeadline) + if c.opTimer == nil { + c.opTimer = time.NewTimer(d) + } else { + c.opTimer.Reset(d) + } + return c.opTimer.C +} + +func (c *memcachedConn) disarmOpTimer() { + if c.opTimer != nil && !c.opDeadline.IsZero() { + c.opTimer.Stop() + } +} + +// opTimedOut ends the running command because its per-operation limit ran out: the +// conn is closed (the server may still send a reply) and the error says so. +func (c *memcachedConn) opTimedOut(cause error) error { + err := &opTimeoutError{d: c.opTimeout, cause: cause} + _ = c.closeWithErr(err) + return err +} + +// opError turns the error of a command into what the caller should see. A +// deadline that fired on a direct conn's socket is reported as what ended it: the +// caller's context if that has ended (its error, wrapped around the socket's), the +// per-operation timeout if that was the limit, the caller's deadline if that has +// been reached. An application answer is never relabelled, whatever the time. +// Call it before endOp (commands.go): it reads the limit beginOp put on the conn. +func (c *memcachedConn) opError(ctx context.Context, err error) error { + if err == nil || !errors.Is(err, os.ErrDeadlineExceeded) { + return err + } + if cerr := ctx.Err(); cerr != nil { + return fmt.Errorf("%w: %w", cerr, err) + } + if c.opTimeout != 0 { + return &opTimeoutError{d: c.opTimeout, cause: err} + } + if dl, ok := ctx.Deadline(); ok && !time.Now().Before(dl) { + return fmt.Errorf("%w: %w", context.DeadlineExceeded, err) + } + return err } // closeWithErr records err as the close cause and closes the conn. Safe to @@ -643,6 +780,8 @@ func (c *memcachedConn) wait(ctx context.Context, req *mcRequest) error { default: } } + tc := c.armOpTimer() + defer c.disarmOpTimer() select { case <-req.doneCh: return req.resultErr @@ -653,6 +792,8 @@ func (c *memcachedConn) wait(ctx context.Context, req *mcRequest) error { // is poisoned — close it so the pool discards it. _ = c.closeWithErr(ctx.Err()) return ctx.Err() + case <-tc: + return c.opTimedOut(nil) } } @@ -737,7 +878,7 @@ func (c *memcachedConn) execTextDirect(ctx context.Context, req *mcRequest, enco c.syncBuf = make([]byte, 16<<10) } - if dl, ok := ctx.Deadline(); ok { + if dl, ok := c.directDeadline(ctx); ok { _ = c.tcp.SetDeadline(dl) defer func() { if c.tcp != nil { @@ -785,7 +926,7 @@ func (c *memcachedConn) execBinaryDirect(ctx context.Context, req *mcRequest, en c.syncBuf = make([]byte, 16<<10) } - if dl, ok := ctx.Deadline(); ok { + if dl, ok := c.directDeadline(ctx); ok { _ = c.tcp.SetDeadline(dl) defer func() { if c.tcp != nil { @@ -834,7 +975,7 @@ func (c *memcachedConn) execBinaryMultiDirect(ctx context.Context, req *mcReques c.syncBuf = make([]byte, 16<<10) } - if dl, ok := ctx.Deadline(); ok { + if dl, ok := c.directDeadline(ctx); ok { _ = c.tcp.SetDeadline(dl) defer func() { if c.tcp != nil { diff --git a/driver/memcached/op_alloc_1218_test.go b/driver/memcached/op_alloc_1218_test.go new file mode 100644 index 000000000..089b97c8e --- /dev/null +++ b/driver/memcached/op_alloc_1218_test.go @@ -0,0 +1,46 @@ +package memcached + +import ( + "context" + "testing" + "time" +) + +// The per-operation limit costs a command no allocation: a client with Timeout +// (the 3 s default or a set one) allocates exactly what one with the limit off does, +// on the direct and the loop paths, with a context that cannot be cancelled. (A +// context built per command cost 4 allocations and broke the strict budgets of +// TestAllocBudgets; the CI step that runs them skips -race; the comparison below +// holds under -race too, both sides being inflated alike.) +// +// A context that CAN be cancelled (WithCancel, WithTimeout, an HTTP/2 stream's) +// costs a direct conn context.AfterFunc's two allocations, which arm the wake of +// its blocked read (the loop paths select on the context and cost nothing). That +// price is budgeted in TestAllocBudgets (GET_small_cancellable, which CI runs +// without -race): AllocsPerRun is too noisy under -race to hold it here. +func TestOperationLimitDoesNotAllocate1218(t *testing.T) { + f := startFake(t) + f.store.kv["k"] = memItem{value: []byte("v"), cas: 1} + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + allocs := func(timeout time.Duration, ctx context.Context) float64 { + c := cl.new(t, Config{Addr: f.Addr(), Timeout: timeout}) + if _, err := c.Get(ctx, "k"); err != nil { // warm the pool + t.Fatal(err) + } + return testing.AllocsPerRun(200, func() { + if _, err := c.Get(ctx, "k"); err != nil { + t.Fatal(err) + } + }) + } + bg := context.Background() + on, off := allocs(3*time.Second, bg), allocs(-1, bg) + t.Logf("Get: %.1f allocs/op with the limit, %.1f without", on, off) + if on > off { + t.Errorf("the per-operation limit costs %.1f allocations per command, want 0", on-off) + } + + }) + } +} diff --git a/driver/memcached/op_timeout_1218_test.go b/driver/memcached/op_timeout_1218_test.go new file mode 100644 index 000000000..d7ac24950 --- /dev/null +++ b/driver/memcached/op_timeout_1218_test.go @@ -0,0 +1,543 @@ +package memcached + +// celeris#1218: Config.Timeout, documented as the per-operation default (3 s), was +// never applied. A command on a server that accepts TCP and never answers it held +// the caller until its context ended: for ever under context.Background(). + +import ( + "bufio" + "bytes" + "context" + "encoding/binary" + "errors" + "io" + "net" + "os" + "strings" + "sync" + "testing" + "time" + + "github.com/goceleris/celeris/driver/internal/async" + "github.com/goceleris/celeris/driver/internal/eventloop" + "github.com/goceleris/celeris/internal/driver/memcached/protocol" +) + +// startMuteServer accepts TCP conns and reads what they send. For the binary +// protocol it answers the first request of a conn (the VERSION handshake) and +// nothing after it; for the text protocol it never answers. It counts the conns +// the client closed. +func startMuteServer(t *testing.T, binaryProto bool) (addr string, closed *int32Counter) { + t.Helper() + closed = &int32Counter{} + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + var mu sync.Mutex + var conns []net.Conn + t.Cleanup(func() { + _ = ln.Close() + mu.Lock() + defer mu.Unlock() + for _, c := range conns { + _ = c.Close() + } + }) + go func() { + for { + c, err := ln.Accept() + if err != nil { + return + } + mu.Lock() + conns = append(conns, c) + mu.Unlock() + go func() { + defer closed.add() + if binaryProto { + var hdr [protocol.BinHeaderLen]byte + if _, err := io.ReadFull(c, hdr[:]); err != nil { + return + } + body := make([]byte, binary.BigEndian.Uint32(hdr[8:12])) + if _, err := io.ReadFull(c, body); err != nil { + return + } + var out bytes.Buffer + writeResp(&out, hdr[1], 0, 0, binary.BigEndian.Uint32(hdr[12:16]), nil, nil, []byte("1.6.0")) + if _, err := c.Write(out.Bytes()); err != nil { + return + } + } + _, _ = io.Copy(io.Discard, c) + }() + } + }() + return ln.Addr().String(), closed +} + +type int32Counter struct { + mu sync.Mutex + n int +} + +func (c *int32Counter) add() { c.mu.Lock(); c.n++; c.mu.Unlock() } + +// opClients are the three ways a Client reaches its conns: direct (what a client +// without an engine uses), the unix event loop, and the net.Conn loop. +var opClients = []struct { + name string + new func(t *testing.T, cfg Config) *Client +}{ + {"direct", func(t *testing.T, cfg Config) *Client { + c := &Client{cfg: cfg, pool: newDirectPool(cfg)} + t.Cleanup(func() { _ = c.Close() }) + return c + }}, + {"eventloop", func(t *testing.T, cfg Config) *Client { + prov, err := eventloop.Resolve(nil) + if err != nil { + t.Fatal(err) + } + c := &Client{cfg: cfg, pool: newPool(cfg, prov, true)} + t.Cleanup(func() { _ = c.Close() }) + return c + }}, + {"netconn", func(t *testing.T, cfg Config) *Client { + prov := netConnProvider(t) + p := &memcachedPool{cfg: cfg, provider: prov} + p.pool = async.NewPool[*memcachedConn](async.PoolConfig{ + MaxOpen: 4, MaxIdlePerWorker: 2, MaxLifetime: time.Hour, MaxIdleTime: time.Hour, NumWorkers: 2, + }, func(ctx context.Context, w int) (*memcachedConn, error) { + return dialMemcachedConnNetConn(ctx, prov, cfg, w) + }) + c := &Client{cfg: cfg, pool: p} + t.Cleanup(func() { _ = c.Close() }) + return c + }}, +} + +// opWithin runs op and fails the test (not the test binary's timeout) if it is +// still blocked after limit. +func opWithin(t *testing.T, limit time.Duration, op func() error) (time.Duration, error) { + t.Helper() + done := make(chan error, 1) + start := time.Now() + go func() { done <- op() }() + select { + case err := <-done: + return time.Since(start), err + case <-time.After(limit): + t.Fatalf("the operation is still blocked after %v: Config.Timeout is not applied", limit) + return 0, nil + } +} + +// A command on a server that never answers: it fails after about Config.Timeout, +// with a context that has no deadline and with one that is much later, for the text +// and the binary protocol, on the three paths, for a command (Get) and for Ping and +// Version. The conn it used is dropped from the pool. +func TestOperationIsBoundedByTimeout1218(t *testing.T) { + const bound = 300 * time.Millisecond + for _, proto := range []Protocol{ProtocolText, ProtocolBinary} { + for _, cl := range opClients { + for _, op := range []string{"Get", "Ping", "Version"} { + for _, cx := range []string{"background", "later-deadline"} { + t.Run(proto.String()+"/"+cl.name+"/"+op+"/"+cx, func(t *testing.T) { + t.Parallel() + addr, _ := startMuteServer(t, proto == ProtocolBinary) + c := cl.new(t, Config{Addr: addr, Protocol: proto, Timeout: bound, DialTimeout: 5 * time.Second}) + ctx := context.Background() + if cx == "later-deadline" { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, 30*time.Second) + defer cancel() + } + elapsed, err := opWithin(t, 2*time.Second, func() error { + switch op { + case "Get": + _, err := c.Get(ctx, "k") + return err + case "Ping": + return c.Ping(ctx) + } + _, err := c.Version(ctx) + return err + }) + t.Logf("%s: %v after %v", t.Name(), err, elapsed.Round(10*time.Millisecond)) + if err == nil { + t.Fatal("no error") + } + if !errors.Is(err, context.DeadlineExceeded) { + t.Errorf("error %v does not match context.DeadlineExceeded", err) + } + if !strings.Contains(err.Error(), "timed out") { + t.Errorf("error %q does not say that the operation timed out", err) + } + if elapsed < bound/2 { + t.Errorf("returned after %v, before the bound of %v", elapsed, bound) + } + if n := c.PoolStats().Open; n != 0 { + t.Errorf("the pool holds %d conns after the timeout, want 0 (the conn is poisoned)", n) + } + }) + } + } + } + } +} + +// A caller's shorter deadline still wins, and its error is not relabelled as the +// driver's. (Passes on main by design: it guards the "no earlier one" rule.) +func TestOperationKeepsTheCallersShorterDeadline1218(t *testing.T) { + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + t.Parallel() + addr, _ := startMuteServer(t, false) + c := cl.new(t, Config{Addr: addr, Timeout: 30 * time.Second}) + ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) + defer cancel() + elapsed, err := opWithin(t, 2*time.Second, func() error { + _, err := c.Get(ctx, "k") + return err + }) + if !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("error %v, want one matching context.DeadlineExceeded (also on the direct path)", err) + } + if strings.Contains(err.Error(), "operation timed out") { + t.Errorf("error %q: the caller's own deadline is reported as Config.Timeout", err) + } + if elapsed > time.Second { + t.Errorf("returned after %v, want about the caller's 200 ms", elapsed) + } + }) + } +} + +// A client that answers within the bound is not affected, and the bound does not +// stay on the conn: the conn is used again after the time it would have fired. +// (Passes on main by design.) +func TestTimeoutDoesNotTouchAnAnsweredOperation1218(t *testing.T) { + f := startFake(t) + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + c := cl.new(t, Config{Addr: f.Addr(), Timeout: 200 * time.Millisecond}) + ctx := context.Background() + if err := c.Set(ctx, "k"+cl.name, "v", 0); err != nil { + t.Fatal(err) + } + time.Sleep(300 * time.Millisecond) // past Timeout + if v, err := c.Get(ctx, "k"+cl.name); err != nil || v != "v" { + t.Fatalf("Get after Timeout passed = %q, %v", v, err) + } + }) + } +} + +// A client whose Timeout is unset gets the default (3 s). The test lowers the +// default through the test hook, so it does not wait 3 s. +func TestOperationIsBoundedByTheDefaultTimeout1218(t *testing.T) { + const def = 300 * time.Millisecond + d := def + testHookDefaultOpTimeout.Store(&d) + t.Cleanup(func() { testHookDefaultOpTimeout.Store(nil) }) + for _, proto := range []Protocol{ProtocolText, ProtocolBinary} { + for _, cl := range opClients { + t.Run(proto.String()+"/"+cl.name, func(t *testing.T) { + t.Parallel() + addr, _ := startMuteServer(t, proto == ProtocolBinary) + c := cl.new(t, Config{Addr: addr, Protocol: proto}) // no Timeout + elapsed, err := opWithin(t, 2*time.Second, func() error { + ctx := context.Background() + _, err := c.Get(ctx, "k") + return err + }) + if !errors.Is(err, context.DeadlineExceeded) || !strings.Contains(err.Error(), "after "+def.String()) { + t.Errorf("error %v, want the operation timeout of the default (%v)", err, def) + } + if elapsed < def/2 { + t.Errorf("returned after %v, before the default of %v", elapsed, def) + } + }) + } + } +} + +// A negative Timeout turns the per-operation deadline off: only the caller's +// context ends the operation (here a deadline of 900 ms, three times the lowered +// default), and its error is not relabelled. +func TestNegativeTimeoutTurnsTheOperationBoundOff1218(t *testing.T) { + d := 300 * time.Millisecond + testHookDefaultOpTimeout.Store(&d) + t.Cleanup(func() { testHookDefaultOpTimeout.Store(nil) }) + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + t.Parallel() + addr, _ := startMuteServer(t, false) + c := cl.new(t, Config{Addr: addr, Timeout: -1}) + ctx, cancel := context.WithTimeout(context.Background(), 900*time.Millisecond) + defer cancel() + elapsed, err := opWithin(t, 3*time.Second, func() error { + _, err := c.Get(ctx, "k") + return err + }) + if err == nil || strings.Contains(err.Error(), "operation timed out") || elapsed < 800*time.Millisecond { + t.Errorf("returned after %v with %v: the bound was not off", elapsed, err) + } + }) + } +} + +// A caller that cancels its context gets its own cancellation back, not relabelled +// as the driver's timeout, and the conn is dropped. This includes the direct path, +// the one a client without an engine uses: its blocked read is woken by the cancel, +// it does not wait for Config.Timeout, and the error matches context.Canceled, not a +// bare i/o timeout. +func TestOperationKeepsTheCallersCancellation1218(t *testing.T) { + if defaultTimeout != 3*time.Second { + t.Errorf("defaultTimeout = %v, want the documented 3 s", defaultTimeout) + } + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + t.Parallel() + addr, _ := startMuteServer(t, false) + c := cl.new(t, Config{Addr: addr, Timeout: 30 * time.Second}) + ctx, cancel := context.WithCancel(context.Background()) + time.AfterFunc(200*time.Millisecond, cancel) + elapsed, err := opWithin(t, 2*time.Second, func() error { + _, err := c.Get(ctx, "k") + return err + }) + if !errors.Is(err, context.Canceled) || strings.Contains(err.Error(), "operation timed out") { + t.Errorf("error %v, want the caller's context.Canceled as it is", err) + } + if elapsed > time.Second { + t.Errorf("returned after %v, want about the cancel at 200 ms", elapsed) + } + if n := c.PoolStats().Open; n != 0 { + t.Errorf("the pool holds %d conns after the cancel, want 0", n) + } + }) + } +} + +// opError never relabels an application answer, whatever the time: only a deadline +// error (the socket's, on a direct conn) is turned into the per-operation timeout, +// and a caller's ended context wins over it. +func TestOpErrorRelabelsOnlyDeadlineErrors1218(t *testing.T) { + c := &memcachedConn{opTimeout: 300 * time.Millisecond} + if err := c.opError(context.Background(), ErrCacheMiss); err != ErrCacheMiss { + t.Errorf("an application answer was relabelled: %v", err) + } + sock := os.ErrDeadlineExceeded + err := c.opError(context.Background(), sock) + if !errors.Is(err, context.DeadlineExceeded) || !errors.Is(err, os.ErrDeadlineExceeded) || !strings.Contains(err.Error(), "operation timed out after 300ms") { + t.Errorf("the socket's deadline error became %v", err) + } + ctx, cancel := context.WithCancel(context.Background()) + cancel() + err = c.opError(ctx, sock) + if !errors.Is(err, context.Canceled) || strings.Contains(err.Error(), "operation timed out") { + t.Errorf("a caller's cancel became %v", err) + } + // No limit on the command: the socket's error is left alone while the caller's + // deadline has not been reached, and is the caller's once it has. + c.opTimeout = 0 + if err := c.opError(context.Background(), sock); err != sock { + t.Errorf("an error of an unlimited command was changed: %v", err) + } + dctx, dcancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Millisecond)) + defer dcancel() + if err := c.opError(dctx, sock); !errors.Is(err, context.DeadlineExceeded) { + t.Errorf("a caller's deadline became %v", err) + } +} + +// The pool's health check pings an idle conn with conn.Ping, outside any Client +// command, after the command that last used it. The limit of that command must have +// been cleared: a stale deadline in the past would fail every ping at once, and the +// check would close every idle conn. +func TestHealthCheckPingAfterALimitedCommand1218(t *testing.T) { + f := startFake(t) + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + c := cl.new(t, Config{Addr: f.Addr(), Timeout: 40 * time.Millisecond}) + ctx := context.Background() + if err := c.Set(ctx, "k"+cl.name, "v", 0); err != nil { + t.Fatal(err) + } + time.Sleep(80 * time.Millisecond) // past that command's limit + conn, err := c.pool.acquire(ctx, -1) + if err != nil { + t.Fatal(err) + } + defer c.pool.release(conn) + for i := 0; i < 5; i++ { + if err := conn.Ping(ctx); err != nil { + t.Fatalf("health-style Ping %d after a limited command: %v", i, err) + } + } + }) + } +} + +// startSlowFirstServer answers the first command of each conn after a delay (the +// caller parks for it) and none after it (it stalls): the second command on a +// reused conn is the one the limit must end. +func startSlowFirstServer(t *testing.T, delay time.Duration) string { + t.Helper() + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + var mu sync.Mutex + var conns []net.Conn + t.Cleanup(func() { + _ = ln.Close() + mu.Lock() + defer mu.Unlock() + for _, c := range conns { + _ = c.Close() + } + }) + go func() { + for { + c, err := ln.Accept() + if err != nil { + return + } + mu.Lock() + conns = append(conns, c) + mu.Unlock() + go func() { + r := bufio.NewReader(c) + if _, err := r.ReadString('\n'); err != nil { + return + } + time.Sleep(delay) + if _, err := c.Write([]byte("VERSION 1.6.0\r\n")); err != nil { + return + } + _, _ = io.Copy(io.Discard, r) // the next command is never answered + }() + } + }() + return ln.Addr().String() +} + +// The limit is armed again on a conn that is used again: the first command parks and +// is answered, the second on the same conn stalls and must end at about Timeout, not +// run unbounded. (Every other bounded test uses a fresh conn, whose first park builds +// the timer; a loop conn that does not re-arm its timer on reuse leaves every command +// after the first unbounded.) +func TestLimitIsArmedAgainOnAReusedConn1218(t *testing.T) { + const bound = 150 * time.Millisecond + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + t.Parallel() + addr := startSlowFirstServer(t, 40*time.Millisecond) + c := cl.new(t, Config{Addr: addr, Timeout: bound}) + ctx := context.Background() + if _, err := c.Version(ctx); err != nil { + t.Fatalf("the first command: %v", err) + } + elapsed, err := opWithin(t, 2*time.Second, func() error { + _, err := c.Version(ctx) + return err + }) + if !errors.Is(err, context.DeadlineExceeded) || !strings.Contains(err.Error(), "timed out") { + t.Errorf("second command error %v, want the operation timeout", err) + } + if elapsed < bound/2 { + t.Errorf("second command returned after %v, before the bound of %v", elapsed, bound) + } + }) + } +} + +// The wake callback of a command that ended as its context was cancelled can still +// be in flight when the next command starts on the same direct conn; it must not +// give that command, whose context is alive, a deadline of "now" (a false "operation +// timed out" and a lost healthy conn). The hook holds the callback of the first +// command for 100 ms so that it lands after the second one has armed its own wake. +func TestLateWakeDoesNotFailTheNextCommand1218(t *testing.T) { + d := 100 * time.Millisecond + testHookWakeDelay.Store(&d) + t.Cleanup(func() { testHookWakeDelay.Store(nil) }) + for _, proto := range []struct { + name string + p Protocol + start func(t *testing.T) string + }{ + {"text", ProtocolText, func(t *testing.T) string { return startFake(t).Addr() }}, + {"binary", ProtocolBinary, func(t *testing.T) string { return startFakeBinary(t).Addr() }}, + } { + t.Run(proto.name, func(t *testing.T) { + c := opClients[0].new(t, Config{Addr: proto.start(t), Protocol: proto.p, Timeout: 30 * time.Second}) + conn, err := c.pool.acquire(context.Background(), -1) + if err != nil { + t.Fatal(err) + } + defer c.pool.release(conn) + // First command: its context ends as it finishes, the stop of its wake + // fails (the callback has started) and the callback is held by the hook. + ctx1, cancel1 := context.WithCancel(context.Background()) + conn.armWake(ctx1) + cancel1() + conn.disarmWake() + // Second command, on a live context. + ctx2, cancel2 := context.WithCancel(context.Background()) + defer cancel2() + conn.armWake(ctx2) + defer conn.disarmWake() + time.Sleep(3 * d) // the held callback of the first command runs now, if it still can + if err := conn.Ping(ctx2); err != nil { + t.Fatalf("a live command on the conn after a cancelled one: %v", err) + } + }) + } +} + +// A cancellation reaches a command on a conn that has already served one: the wake +// is armed again for every command, so the cancel ends it at once instead of at +// Config.Timeout. (Every other cancellation test uses a fresh conn; one whose wake +// state is not reset per command has it spent by the first command's disarm, and +// every cancel after it waits for the limit. For the binary protocol the handshake +// is that first use.) The first command runs under a cancellable context, since a +// context that cannot be cancelled arms no wake. +func TestCancelOnAReusedDirectConn1218(t *testing.T) { + for _, proto := range []struct { + name string + p Protocol + start func(t *testing.T) string + first func(ctx context.Context, c *Client) error + }{ + {"text", ProtocolText, func(t *testing.T) string { return startSlowFirstServer(t, 10*time.Millisecond) }, + func(ctx context.Context, c *Client) error { _, err := c.Version(ctx); return err }}, + {"binary", ProtocolBinary, func(t *testing.T) string { a, _ := startMuteServer(t, true); return a }, + func(ctx context.Context, c *Client) error { return nil }}, // the handshake was the first use + } { + t.Run(proto.name, func(t *testing.T) { + t.Parallel() + c := opClients[0].new(t, Config{Addr: proto.start(t), Protocol: proto.p, Timeout: 30 * time.Second}) + ctx1, cancel1 := context.WithCancel(context.Background()) + defer cancel1() + if err := proto.first(ctx1, c); err != nil { + t.Fatalf("the first command: %v", err) + } + ctx2, cancel2 := context.WithCancel(context.Background()) + time.AfterFunc(150*time.Millisecond, cancel2) + elapsed, err := opWithin(t, 3*time.Second, func() error { + _, err := c.Get(ctx2, "k") + return err + }) + if !errors.Is(err, context.Canceled) || strings.Contains(err.Error(), "operation timed out") { + t.Errorf("error %v, want the caller's context.Canceled", err) + } + if elapsed > time.Second { + t.Errorf("returned after %v, want about the cancel at 150 ms and not the 30 s Timeout", elapsed) + } + }) + } +} diff --git a/driver/memcached/options.go b/driver/memcached/options.go index b41f1141b..8687056bf 100644 --- a/driver/memcached/options.go +++ b/driver/memcached/options.go @@ -1,9 +1,15 @@ package memcached import ( + "sync/atomic" "time" ) +// testHookDefaultOpTimeout, when set, replaces the default per-operation timeout +// (defaultTimeout, 3 s) of a client whose Config.Timeout is unset. A test sets it +// to avoid waiting 3 s, and resets it to nil when it is done. +var testHookDefaultOpTimeout atomic.Pointer[time.Duration] + // Protocol selects the memcached wire dialect spoken by a Client. Every // memcached deployment supports text; binary was deprecated upstream but // remains broadly available and is sometimes preferred for its CAS semantics @@ -57,8 +63,16 @@ type Config struct { // followed by a silent server takes up to about twice DialTimeout. A shorter // deadline of the caller's context wins. The text protocol has no handshake. DialTimeout time.Duration - // Timeout is the advisory per-op deadline used when the caller's context - // carries no deadline. Default 3s. + // Timeout is the per-operation deadline: a command (and Ping, Version, + // Stats, ...) that has not completed after Timeout fails with an error that + // matches [context.DeadlineExceeded], and the conn it used is dropped. It + // applies whether or not the caller's context has a deadline; a deadline of the + // caller's that is not later than Timeout wins and its error is returned as it + // is. A multi-key get, Stats and Flush are one operation each: Timeout covers + // the whole command, not each key or each read. It does not cover waiting for a pool slot or dialling a new conn (that is + // [Config.DialTimeout]'s). Zero means the default, 3s; a negative value turns + // the per-operation deadline off, and the caller's context is then the only + // bound. Timeout time.Duration // MaxLifetime is the max age of a pooled conn; default 30m. MaxLifetime time.Duration @@ -88,7 +102,8 @@ func WithMaxIdlePerWorker(n int) Option { return func(c *Config) { c.MaxIdlePerW // binary-protocol handshake. func WithDialTimeout(d time.Duration) Option { return func(c *Config) { c.DialTimeout = d } } -// WithTimeout sets the default per-op deadline when the caller's ctx has none. +// WithTimeout sets [Config.Timeout], the per-operation deadline (3s when unset, +// negative for none). func WithTimeout(d time.Duration) Option { return func(c *Config) { c.Timeout = d } } // WithMaxLifetime sets the pooled conn lifetime.