Skip to content

Commit

Permalink
add memcached
Browse files Browse the repository at this point in the history
Signed-off-by: Jiancong Zhu <[email protected]>
  • Loading branch information
Chasing1020 committed Sep 2, 2024
1 parent d383d2b commit 86276a1
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 1 deletion.
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
124 changes: 124 additions & 0 deletions db/memcached/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// 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"
"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(key)
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 {
data, err := json.Marshal(values)
if err != nil {
return err
}
item := &memcache.Item{
Key: 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 {
return db.Update(ctx, table, key, values)
}

func (db *mcDB) Delete(ctx context.Context, table string, key string) error {
return db.client.Delete(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

0 comments on commit 86276a1

Please sign in to comment.