Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 43 additions & 71 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import (
"sync/atomic"
"time"

"github.com/gocql/gocql/internal/lru"
"github.com/gocql/gocql/internal/streams"
"github.com/maypok86/otter/v2"
)

var (
Expand Down Expand Up @@ -1236,78 +1236,50 @@ type inflightPrepare struct {
}

func (c *Conn) prepareStatement(ctx context.Context, stmt string, tracer Tracer) (*preparedStatment, error) {
stmtCacheKey := c.session.stmtsLRU.keyFor(c.host.HostID(), c.currentKeyspace, stmt)
flight, ok := c.session.stmtsLRU.execIfMissing(stmtCacheKey, func(lru *lru.Cache) *inflightPrepare {
flight := &inflightPrepare{
done: make(chan struct{}),
loader := otter.LoaderFunc[string, *preparedStatment](func(ctx context.Context, key string) (*preparedStatment, error) {
prep := &writePrepareFrame{
statement: stmt,
}
if c.version > protoVersion4 {
prep.keyspace = c.currentKeyspace
}
lru.Add(stmtCacheKey, flight)
return flight
})

if !ok {
go func() {
defer close(flight.done)

prep := &writePrepareFrame{
statement: stmt,
}
if c.version > protoVersion4 {
prep.keyspace = c.currentKeyspace
}

// we won the race to do the load, if our context is canceled we shouldnt
// stop the load as other callers are waiting for it but this caller should get
// their context cancelled error.
framer, err := c.exec(c.ctx, prep, tracer)
if err != nil {
flight.err = err
c.session.stmtsLRU.remove(stmtCacheKey)
return
}

frame, err := framer.parseFrame()
if err != nil {
flight.err = err
c.session.stmtsLRU.remove(stmtCacheKey)
return
}
framer, err := c.exec(c.ctx, prep, tracer)
if err != nil {
return nil, err
}

// TODO(zariel): tidy this up, simplify handling of frame parsing so its not duplicated
// everytime we need to parse a frame.
if len(framer.traceID) > 0 && tracer != nil {
tracer.Trace(framer.traceID)
}
frame, err := framer.parseFrame()
if err != nil {
return nil, err
}

switch x := frame.(type) {
case *resultPreparedFrame:
flight.preparedStatment = &preparedStatment{
// defensively copy as we will recycle the underlying buffer after we
// return.
id: copyBytes(x.preparedID),
// the type info's should _not_ have a reference to the framers read buffer,
// therefore we can just copy them directly.
request: x.reqMeta,
response: x.respMeta,
}
case error:
flight.err = x
default:
flight.err = NewErrProtocol("Unknown type in response to prepare frame: %s", x)
}
// TODO(zariel): tidy this up, simplify handling of frame parsing so its not duplicated
// everytime we need to parse a frame.
if len(framer.traceID) > 0 && tracer != nil {
tracer.Trace(framer.traceID)
}

if flight.err != nil {
c.session.stmtsLRU.remove(stmtCacheKey)
}
}()
}
switch x := frame.(type) {
case *resultPreparedFrame:
return &preparedStatment{
// defensively copy as we will recycle the underlying buffer after we
// return.
id: copyBytes(x.preparedID),
// the type info's should _not_ have a reference to the framers read buffer,
// therefore we can just copy them directly.
request: x.reqMeta,
response: x.respMeta,
}, nil
case error:
return nil, x
default:
return nil, NewErrProtocol("Unknown type in response to prepare frame: %s", x)
}
})
stmtCacheKey := keyForPreparedStatement(c.host.HostID(), c.currentKeyspace, stmt)

select {
case <-ctx.Done():
return nil, ctx.Err()
case <-flight.done:
return flight.preparedStatment, flight.err
}
return c.session.stmtsLRU.Get(ctx, stmtCacheKey, loader)
}

func marshalQueryValue(typ TypeInfo, value interface{}, dst *queryValues) error {
Expand Down Expand Up @@ -1477,8 +1449,8 @@ func (c *Conn) executeQuery(ctx context.Context, qry *Query) *Iter {
// is not consistent with regards to its schema.
return iter
case *RequestErrUnprepared:
stmtCacheKey := c.session.stmtsLRU.keyFor(c.host.HostID(), c.currentKeyspace, qry.stmt)
c.session.stmtsLRU.evictPreparedID(stmtCacheKey, x.StatementId)
stmtCacheKey := keyForPreparedStatement(c.host.HostID(), c.currentKeyspace, qry.stmt)
c.session.stmtsLRU.Invalidate(stmtCacheKey)
return c.executeQuery(ctx, qry)
case error:
return &Iter{err: x, framer: framer}
Expand Down Expand Up @@ -1623,8 +1595,8 @@ func (c *Conn) executeBatch(ctx context.Context, batch *Batch) *Iter {
case *RequestErrUnprepared:
stmt, found := stmts[string(x.StatementId)]
if found {
key := c.session.stmtsLRU.keyFor(c.host.HostID(), c.currentKeyspace, stmt)
c.session.stmtsLRU.evictPreparedID(key, x.StatementId)
key := keyForPreparedStatement(c.host.HostID(), c.currentKeyspace, stmt)
c.session.stmtsLRU.Invalidate(key)
}
return c.executeBatch(ctx, batch)
case *resultRowsFrame:
Expand Down
14 changes: 9 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
module github.com/gocql/gocql

require (
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/golang/snappy v0.0.3
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.3.0 // indirect
github.com/maypok86/otter/v2 v2.2.1
gopkg.in/inf.v0 v0.9.1
)

go 1.13
require (
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/kr/pretty v0.1.0 // indirect
golang.org/x/sys v0.34.0 // indirect
)

go 1.24.6
15 changes: 10 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 h1:mXoPYz/Ul5HYE
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8=
Expand All @@ -13,10 +13,15 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/maypok86/otter/v2 v2.2.1 h1:hnGssisMFkdisYcvQ8L019zpYQcdtPse+g0ps2i7cfI=
github.com/maypok86/otter/v2 v2.2.1/go.mod h1:1NKY9bY+kB5jwCXBJfE59u+zAwOt6C7ni1FTlFFMqVs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
73 changes: 7 additions & 66 deletions prepared_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,78 +25,19 @@
package gocql

import (
"bytes"
"sync"

"github.com/gocql/gocql/internal/lru"
"github.com/maypok86/otter/v2"
)

const defaultMaxPreparedStmts = 1000

// preparedLRU is the prepared statement cache
type preparedLRU struct {
mu sync.Mutex
lru *lru.Cache
}

func (p *preparedLRU) clear() {
p.mu.Lock()
defer p.mu.Unlock()

for p.lru.Len() > 0 {
p.lru.RemoveOldest()
}
}

func (p *preparedLRU) add(key string, val *inflightPrepare) {
p.mu.Lock()
defer p.mu.Unlock()
p.lru.Add(key, val)
}

func (p *preparedLRU) remove(key string) bool {
p.mu.Lock()
defer p.mu.Unlock()
return p.lru.Remove(key)
}

func (p *preparedLRU) execIfMissing(key string, fn func(lru *lru.Cache) *inflightPrepare) (*inflightPrepare, bool) {
p.mu.Lock()
defer p.mu.Unlock()

val, ok := p.lru.Get(key)
if ok {
return val.(*inflightPrepare), true
}

return fn(p.lru), false
func NewPreparedLRU(size int) *otter.Cache[string, *preparedStatment] {
return otter.Must(&otter.Options[string, *preparedStatment]{
InitialCapacity: size,
MaximumSize: size,
})
}

func (p *preparedLRU) keyFor(hostID, keyspace, statement string) string {
func keyForPreparedStatement(hostID, keyspace, statement string) string {
// TODO: we should just use a struct for the key in the map
return hostID + keyspace + statement
}

func (p *preparedLRU) evictPreparedID(key string, id []byte) {
p.mu.Lock()
defer p.mu.Unlock()

val, ok := p.lru.Get(key)
if !ok {
return
}

ifp, ok := val.(*inflightPrepare)
if !ok {
return
}

select {
case <-ifp.done:
if bytes.Equal(id, ifp.preparedStatment.id) {
p.lru.Remove(key)
}
default:
}

}
Loading