Move to fork github.com/grafana/gomemcache (#7936)

pull/7937/head
Irina 2 years ago committed by GitHub
parent 8fe386fbb3
commit 10b869c82e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      go.mod
  2. 4
      go.sum
  3. 4
      pkg/storage/chunk/cache/memcached.go
  4. 2
      pkg/storage/chunk/cache/memcached_client.go
  5. 4
      pkg/storage/chunk/cache/memcached_client_selector.go
  6. 2
      pkg/storage/chunk/cache/memcached_client_selector_test.go
  7. 2
      pkg/storage/chunk/cache/memcached_client_test.go
  8. 2
      pkg/storage/chunk/cache/memcached_test.go
  9. 0
      vendor/github.com/grafana/gomemcache/LICENSE
  10. 150
      vendor/github.com/grafana/gomemcache/memcache/memcache.go
  11. 0
      vendor/github.com/grafana/gomemcache/memcache/selector.go
  12. 7
      vendor/modules.txt

@ -18,7 +18,6 @@ require (
github.com/aws/aws-sdk-go v1.44.128
github.com/baidubce/bce-sdk-go v0.9.111
github.com/bmatcuk/doublestar v1.2.2
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/buger/jsonparser v1.1.1
github.com/c2h5oh/datasize v0.0.0-20200112174442-28bbd4740fee
github.com/cespare/xxhash v1.1.0
@ -51,6 +50,7 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/grafana/dskit v0.0.0-20221212120341-3e308a49441b
github.com/grafana/go-gelf/v2 v2.0.1
github.com/grafana/gomemcache v0.0.0-20221213170046-b5da8a745d41
github.com/grafana/regexp v0.0.0-20221005093135-b4c2bcb0a4b6
github.com/grafana/tail v0.0.0-20221214082743-3a1c242a4d7b
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
@ -318,10 +318,6 @@ replace github.com/hashicorp/consul => github.com/hashicorp/consul v1.5.1
// Use fork of gocql that has gokit logs and Prometheus metrics.
replace github.com/gocql/gocql => github.com/grafana/gocql v0.0.0-20200605141915-ba5dc39ece85
// Same as Cortex
// Using a 3rd-party branch for custom dialer - see https://github.com/bradfitz/gomemcache/pull/86
replace github.com/bradfitz/gomemcache => github.com/owen-d/gomemcache v0.0.0-20220719101501-ce4268ea75ae
replace github.com/cloudflare/cloudflare-go => github.com/cyriltovena/cloudflare-go v0.27.1-0.20211118103540-ff77400bcb93
exclude k8s.io/client-go v8.0.0+incompatible

@ -732,6 +732,8 @@ github.com/grafana/go-gelf/v2 v2.0.1 h1:BOChP0h/jLeD+7F9mL7tq10xVkDG15he3T1zHuQa
github.com/grafana/go-gelf/v2 v2.0.1/go.mod h1:lexHie0xzYGwCgiRGcvZ723bSNyNI8ZRD4s0CLobh90=
github.com/grafana/gocql v0.0.0-20200605141915-ba5dc39ece85 h1:xLuzPoOzdfNb/RF/IENCw+oLVdZB4G21VPhkHBgwSHY=
github.com/grafana/gocql v0.0.0-20200605141915-ba5dc39ece85/go.mod h1:crI9WX6p0IhrqB+DqIUHulRW853PaNFf7o4UprV//3I=
github.com/grafana/gomemcache v0.0.0-20221213170046-b5da8a745d41 h1:YxVdHh0Erfya/wb4mzy/JkTxtmefBICE6gAwSkS+61I=
github.com/grafana/gomemcache v0.0.0-20221213170046-b5da8a745d41/go.mod h1:6fkC8bkriadatJOc7Pvjcvqr2xh9C79BYRRfE3WWoo0=
github.com/grafana/memberlist v0.3.1-0.20220714140823-09ffed8adbbe h1:yIXAAbLswn7VNWBIvM71O2QsgfgW9fRXZNR0DXe6pDU=
github.com/grafana/memberlist v0.3.1-0.20220714140823-09ffed8adbbe/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE=
github.com/grafana/regexp v0.0.0-20221005093135-b4c2bcb0a4b6 h1:A3dhViTeFDSQcGOXuUi6ukCQSMyDtDISBp2z6OOo2YM=
@ -1127,8 +1129,6 @@ github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh
github.com/openzipkin/zipkin-go-opentracing v0.3.4/go.mod h1:js2AbwmHW0YD9DwIw2JhQWmbfFi/UnWyYwdVhqbCDOE=
github.com/ory/dockertest v3.3.4+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs=
github.com/ovh/go-ovh v1.1.0 h1:bHXZmw8nTgZin4Nv7JuaLs0KG5x54EQR7migYTd1zrk=
github.com/owen-d/gomemcache v0.0.0-20220719101501-ce4268ea75ae h1:NkG2GIrREfX6FfB8D1QCLcFqpE5xHL7rbUGwXB9pvwM=
github.com/owen-d/gomemcache v0.0.0-20220719101501-ce4268ea75ae/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
github.com/packethost/packngo v0.1.1-0.20180711074735-b9cb5096f54c/go.mod h1:otzZQXgoO96RTzDB/Hycg0qZcXZsWJGJRSXbmEIJ+4M=
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=

@ -8,8 +8,8 @@ import (
"sync"
"time"
"github.com/bradfitz/gomemcache/memcache"
"github.com/go-kit/log"
"github.com/grafana/gomemcache/memcache"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
instr "github.com/weaveworks/common/instrument"
@ -113,7 +113,7 @@ type result struct {
}
func memcacheStatusCode(err error) string {
// See https://godoc.org/github.com/bradfitz/gomemcache/memcache#pkg-variables
// See https://godoc.org/github.com/grafana/gomemcache/memcache#pkg-variables
switch err {
case nil:
return "200"

@ -10,9 +10,9 @@ import (
"sync"
"time"
"github.com/bradfitz/gomemcache/memcache"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/gomemcache/memcache"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

@ -5,9 +5,9 @@ import (
"strings"
"sync"
"github.com/bradfitz/gomemcache/memcache"
"github.com/cespare/xxhash"
"github.com/facette/natsort"
"github.com/grafana/gomemcache/memcache"
)
// MemcachedJumpHashSelector implements the memcache.ServerSelector
@ -47,7 +47,7 @@ func DefaultMemcachedJumpHashSelector() *MemcachedJumpHashSelector {
// staticAddr caches the Network() and String() values from
// any net.Addr.
//
// Copied from github.com/bradfitz/gomemcache/selector.go.
// Copied from github.com/grafana/gomemcache/selector.go.
type staticAddr struct {
network, str string
}

@ -5,8 +5,8 @@ import (
"net"
"testing"
"github.com/bradfitz/gomemcache/memcache"
"github.com/facette/natsort"
"github.com/grafana/gomemcache/memcache"
"github.com/stretchr/testify/require"
"github.com/grafana/loki/pkg/storage/chunk/cache"

@ -3,7 +3,7 @@ package cache_test
import (
"sync"
"github.com/bradfitz/gomemcache/memcache"
"github.com/grafana/gomemcache/memcache"
)
type mockMemcache struct {

@ -7,8 +7,8 @@ import (
"sync"
"testing"
"github.com/bradfitz/gomemcache/memcache"
"github.com/go-kit/log"
"github.com/grafana/gomemcache/memcache"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"

@ -22,7 +22,6 @@ import (
"bytes"
"errors"
"fmt"
"hash/fnv"
"io"
"net"
@ -127,24 +126,14 @@ func New(server ...string) *Client {
// NewFromSelector returns a new Client using the provided ServerSelector.
func NewFromSelector(ss ServerSelector) *Client {
c := Client{selector: ss}
// TODO: make configurable
shards := 128
c.connMaps = make([]*connMap, 0, shards)
for i := 0; i < shards; i++ {
c.connMaps = append(c.connMaps, &connMap{})
}
return &c
return &Client{selector: ss}
}
// Client is a memcache client.
// It is safe for unlocked use by multiple concurrent goroutines.
type Client struct {
// Dialer specifies a custom dialer used to dial new connections to a server.
// DialTimeout specifies a custom dialer used to dial new connections to a server.
DialTimeout func(network, address string, timeout time.Duration) (net.Conn, error)
// Timeout specifies the socket read/write timeout.
// If zero, DefaultTimeout is used.
Timeout time.Duration
@ -157,12 +146,10 @@ type Client struct {
// be set to a number higher than your peak parallel requests.
MaxIdleConns int
selector ServerSelector
Pool BytesPool
connMaps []*connMap
}
selector ServerSelector
type connMap struct {
lk sync.Mutex
freeconn map[string][]*conn
}
@ -196,6 +183,15 @@ type conn struct {
c *Client
}
// BytesPool is a pool of bytes that can be reused.
type BytesPool interface {
// Get returns a new byte slice that has a capacity at least the same as the
// requested size.
Get(sz int) (*[]byte, error)
// Put returns a byte slice to the pool.
Put(b *[]byte)
}
// release returns this connection back to the client's free pool
func (cn *conn) release() {
cn.c.putFreeConn(cn.addr, cn)
@ -217,47 +213,32 @@ func (cn *conn) condRelease(err *error) {
}
}
func (c *Client) cmFor(addr string) *connMap {
return c.connMaps[int(hash(addr))%len(c.connMaps)]
}
func (c *Client) putFreeConn(addr net.Addr, cn *conn) {
cm := c.cmFor(addr.String())
cm.lk.Lock()
defer cm.lk.Unlock()
if cm.freeconn == nil {
cm.freeconn = make(map[string][]*conn)
c.lk.Lock()
defer c.lk.Unlock()
if c.freeconn == nil {
c.freeconn = make(map[string][]*conn)
}
freelist := cm.freeconn[addr.String()]
freelist := c.freeconn[addr.String()]
if len(freelist) >= c.maxIdleConns() {
cn.nc.Close()
return
}
cm.freeconn[addr.String()] = append(freelist, cn)
}
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
c.freeconn[addr.String()] = append(freelist, cn)
}
func (c *Client) getFreeConn(addr net.Addr) (cn *conn, ok bool) {
cm := c.cmFor(addr.String())
cm.lk.Lock()
defer cm.lk.Unlock()
if cm.freeconn == nil {
c.lk.Lock()
defer c.lk.Unlock()
if c.freeconn == nil {
return nil, false
}
freelist, ok := cm.freeconn[addr.String()]
freelist, ok := c.freeconn[addr.String()]
if !ok || len(freelist) == 0 {
return nil, false
}
cn = freelist[len(freelist)-1]
cm.freeconn[addr.String()] = freelist[:len(freelist)-1]
c.freeconn[addr.String()] = freelist[:len(freelist)-1]
return cn, true
}
@ -287,11 +268,11 @@ func (cte *ConnectTimeoutError) Error() string {
}
func (c *Client) dial(addr net.Addr) (net.Conn, error) {
if c.DialTimeout == nil {
c.DialTimeout = net.DialTimeout
dialTimeout := c.DialTimeout
if dialTimeout == nil {
dialTimeout = net.DialTimeout
}
nc, err := net.DialTimeout(addr.Network(), addr.String(), c.netTimeout())
nc, err := dialTimeout(addr.Network(), addr.String(), c.netTimeout())
if err == nil {
return nc, nil
}
@ -400,7 +381,7 @@ func (c *Client) getFromAddr(addr net.Addr, keys []string, cb func(*Item)) error
if err := rw.Flush(); err != nil {
return err
}
if err := parseGetResponse(rw.Reader, cb); err != nil {
if err := c.parseGetResponse(rw.Reader, cb); err != nil {
return err
}
return nil
@ -513,7 +494,7 @@ func (c *Client) GetMulti(keys []string) (map[string]*Item, error) {
}
var err error
for range keyMap {
for _ = range keyMap {
if ge := <-ch; ge != nil {
err = ge
}
@ -523,7 +504,7 @@ func (c *Client) GetMulti(keys []string) (map[string]*Item, error) {
// parseGetResponse reads a GET response from r and calls cb for each
// read and allocated Item
func parseGetResponse(r *bufio.Reader, cb func(*Item)) error {
func (c *Client) parseGetResponse(r *bufio.Reader, cb func(*Item)) error {
for {
line, err := r.ReadSlice('\n')
if err != nil {
@ -537,14 +518,27 @@ func parseGetResponse(r *bufio.Reader, cb func(*Item)) error {
if err != nil {
return err
}
it.Value = make([]byte, size+2)
buffSize := size + 2
if c.Pool != nil {
v, err := c.Pool.Get(buffSize)
if err != nil {
return err
}
it.Value = (*v)[:buffSize]
} else {
it.Value = make([]byte, buffSize)
}
_, err = io.ReadFull(r, it.Value)
if err != nil {
it.Value = nil
if c.Pool != nil {
c.Pool.Put(&it.Value)
}
return err
}
if !bytes.HasSuffix(it.Value, crlf) {
it.Value = nil
if c.Pool != nil {
c.Pool.Put(&it.Value)
}
return fmt.Errorf("memcache: corrupt get result read")
}
it.Value = it.Value[:size]
@ -555,17 +549,49 @@ func parseGetResponse(r *bufio.Reader, cb func(*Item)) error {
// scanGetResponseLine populates it and returns the declared size of the item.
// It does not read the bytes of the item.
func scanGetResponseLine(line []byte, it *Item) (size int, err error) {
pattern := "VALUE %s %d %d %d\r\n"
dest := []interface{}{&it.Key, &it.Flags, &size, &it.casid}
if bytes.Count(line, space) == 3 {
pattern = "VALUE %s %d %d\r\n"
dest = dest[:3]
}
n, err := fmt.Sscanf(string(line), pattern, dest...)
if err != nil || n != len(dest) {
errf := func(line []byte) (int, error) {
return -1, fmt.Errorf("memcache: unexpected line in get response: %q", line)
}
return size, nil
if !bytes.HasPrefix(line, []byte("VALUE ")) || !bytes.HasSuffix(line, []byte("\r\n")) {
return errf(line)
}
s := string(line[6 : len(line)-2])
var rest string
var found bool
it.Key, rest, found = cut(s, ' ')
if !found {
return errf(line)
}
val, rest, found := cut(rest, ' ')
if !found {
return errf(line)
}
flags64, err := strconv.ParseUint(val, 10, 32)
if err != nil {
return errf(line)
}
it.Flags = uint32(flags64)
val, rest, found = cut(rest, ' ')
size64, err := strconv.ParseUint(val, 10, 32)
if err != nil {
return errf(line)
}
if !found { // final CAS ID is optional.
return int(size64), nil
}
it.casid, err = strconv.ParseUint(rest, 10, 64)
if err != nil {
return errf(line)
}
return int(size64), nil
}
// Similar to strings.Cut in Go 1.18, but sep can only be 1 byte.
func cut(s string, sep byte) (before, after string, found bool) {
if i := strings.IndexByte(s, sep); i >= 0 {
return s[:i], s[i+1:], true
}
return s, "", false
}
// Set writes the given item, unconditionally.

@ -280,9 +280,6 @@ github.com/beorn7/perks/quantile
# github.com/bmatcuk/doublestar v1.2.2
## explicit; go 1.12
github.com/bmatcuk/doublestar
# github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b => github.com/owen-d/gomemcache v0.0.0-20220719101501-ce4268ea75ae
## explicit; go 1.12
github.com/bradfitz/gomemcache/memcache
# github.com/buger/jsonparser v1.1.1
## explicit; go 1.13
github.com/buger/jsonparser
@ -711,6 +708,9 @@ github.com/grafana/dskit/tenant
# github.com/grafana/go-gelf/v2 v2.0.1
## explicit; go 1.17
github.com/grafana/go-gelf/v2/gelf
# github.com/grafana/gomemcache v0.0.0-20221213170046-b5da8a745d41
## explicit; go 1.12
github.com/grafana/gomemcache/memcache
# github.com/grafana/regexp v0.0.0-20221005093135-b4c2bcb0a4b6
## explicit; go 1.17
github.com/grafana/regexp
@ -1953,7 +1953,6 @@ sigs.k8s.io/yaml
# github.com/Azure/azure-storage-blob-go => github.com/MasslessParticle/azure-storage-blob-go v0.14.1-0.20220216145902-b5e698eff68e
# github.com/hashicorp/consul => github.com/hashicorp/consul v1.5.1
# github.com/gocql/gocql => github.com/grafana/gocql v0.0.0-20200605141915-ba5dc39ece85
# github.com/bradfitz/gomemcache => github.com/owen-d/gomemcache v0.0.0-20220719101501-ce4268ea75ae
# github.com/cloudflare/cloudflare-go => github.com/cyriltovena/cloudflare-go v0.27.1-0.20211118103540-ff77400bcb93
# google.golang.org/grpc => google.golang.org/grpc v1.45.0
# github.com/hashicorp/memberlist => github.com/grafana/memberlist v0.3.1-0.20220714140823-09ffed8adbbe

Loading…
Cancel
Save