Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add memcached #302

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ ifndef CROSS_COMPILE

ifeq ($(SQLITE_CHECK), 0)
TAGS += libsqlite3
ifeq ($(shell uname -s), Darwin)
ifeq ($(shell uname -m), arm64)
DYNAMIC_TAGS = -tags dynamic
endif
endif
endif

ifeq ($(ROCKSDB_CHECK), 0)
Expand All @@ -36,9 +41,12 @@ build:
ifeq ($(TAGS),)
$(CGO_FLAGS) go build -o bin/go-ycsb cmd/go-ycsb/*
else
$(CGO_FLAGS) go build -tags "$(TAGS)" -o bin/go-ycsb cmd/go-ycsb/*
$(CGO_FLAGS) go build -tags "$(TAGS)" $(DYNAMIC_TAGS) -o bin/go-ycsb cmd/go-ycsb/*
endif

check:
golint -set_exit_status db/... cmd/... pkg/...

clean:
rm -rf bin

7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Available Commands:
- BoltDB
- etcd
- DynamoDB
- Memcached

## Output configuration

Expand Down Expand Up @@ -336,7 +337,13 @@ Common configurations:
|dynamodb.consistent.reads|false|Reads on DynamoDB provide an eventually consistent read by default. If your benchmark/use-case requires a strongly consistent read, set this option to true|
|dynamodb.delete.after.run.stage|false|Detele the database table after the run stage|

### Memcached

|field|default value|description|
|-|-|-|
|memcached.hosts|"localhost:11211"|The server hosts lists, splited by comma|
|memcached.timeout|"500ms"|The socket read/write timeout|
|memcached.maxIdleConns|2|The maximum number of idle connections that will be maintained per address|

## TODO

Expand Down
2 changes: 2 additions & 0 deletions cmd/go-ycsb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ import (
_ "github.com/pingcap/go-ycsb/db/etcd"
// Register dynamodb
_ "github.com/pingcap/go-ycsb/db/dynamodb"
// Register memcached
_ "github.com/pingcap/go-ycsb/db/memcached"
)

var (
Expand Down
155 changes: 155 additions & 0 deletions db/memcached/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* Copyright (c) 2010-2016 Yahoo! Inc., 2017 YCSB contributors All rights reserved.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License. See accompanying
* LICENSE file.
*/

package memcached

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/bradfitz/gomemcache/memcache"
"github.com/magiconair/properties"
"github.com/pingcap/go-ycsb/pkg/ycsb"
"strings"
)

// properties
const (
memcachedHosts = "memcached.hosts"
memcachedTimeout = "memcached.timeout"
memcachedMaxIdleConns = "memcached.maxIdleConns"
)

type mcCreator struct{}

type mcDB struct {
p *properties.Properties
client *memcache.Client
}

func init() {
ycsb.RegisterDBCreator("memcached", mcCreator{})
}

func (c mcCreator) Create(p *properties.Properties) (ycsb.DB, error) {
hosts := p.GetString(memcachedHosts, "localhost:11211")
timeout := p.GetDuration(memcachedTimeout, memcache.DefaultTimeout)
maxIdleConns := p.GetInt(memcachedMaxIdleConns, memcache.DefaultMaxIdleConns)

client := memcache.New(strings.Split(hosts, ",")...)
client.Timeout = timeout
client.MaxIdleConns = maxIdleConns

return &mcDB{
p: p,
client: client,
}, nil
}

func (db *mcDB) Close() error {
return db.client.Close()
}

func (db *mcDB) InitThread(ctx context.Context, _ int, _ int) context.Context {
return ctx
}

func (db *mcDB) CleanupThread(_ context.Context) {}

func (db *mcDB) Read(ctx context.Context, table string, key string, fields []string) (map[string][]byte, error) {
item, err := db.client.Get(getRowKey(table, key))
if errors.Is(err, memcache.ErrCacheMiss) {
return nil, nil
} else if err != nil {
return nil, err
}
var r map[string][]byte
err = json.NewDecoder(bytes.NewReader(item.Value)).Decode(&r)
if err != nil {
return nil, err
}
return r, nil
}

func (db *mcDB) Scan(ctx context.Context, table string, startKey string, count int, fields []string) ([]map[string][]byte, error) {
return nil, fmt.Errorf("scan is not supported")
}

func (db *mcDB) Update(ctx context.Context, table string, key string, values map[string][]byte) error {
item, err := db.client.Get(getRowKey(table, key))
if errors.Is(err, memcache.ErrCacheMiss) {
return db.Insert(ctx, table, key, values)
}

var m map[string][]byte
err = json.NewDecoder(bytes.NewReader(item.Value)).Decode(&m)
for field, value := range values {
m[field] = value
}

data, err := json.Marshal(m)
if err != nil {
return err
}

item = &memcache.Item{
Key: getRowKey(table, key),
Value: data,
}
err = db.client.Set(item)
if err != nil {
return err
}
return nil
}

func (db *mcDB) Insert(ctx context.Context, table string, key string, values map[string][]byte) error {
data, err := json.Marshal(values)
if err != nil {
return err
}
item := &memcache.Item{
Key: getRowKey(table, key),
Value: data,
}
err = db.client.Add(item)
if err != nil {
return err
}
return nil
}

func (db *mcDB) Delete(ctx context.Context, table string, key string) error {
return db.client.Delete(getRowKey(table, key))
}

func getRowKey(table, key string) string {
return fmt.Sprintf("%s:%s", table, key)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ require (
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.10.0
github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression v1.4.26
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.17.1
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874
go.etcd.io/etcd/client/pkg/v3 v3.5.2
go.etcd.io/etcd/client/v3 v3.5.2
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4Yn
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874 h1:N7oVaKyGp8bttX0bfZGmcGkjz7DLQXhAn3DNd3T0ous=
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874/go.mod h1:r5xuitiExdLAJ09PR7vBVENGvp4ZuTBeWTGtxuX3K+c=
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down