adding etcd storage support for cluster meta data. Currently just
sequence. More to come...
This commit is contained in:
@@ -14,13 +14,18 @@ func TestFileBacking(t *testing.T) {
|
||||
verifySetGet(t, ms)
|
||||
}
|
||||
|
||||
func TestEtcdBacking(t *testing.T) {
|
||||
ms := &MetaStore{NewMetaStoreEtcdBacking("http://localhost:4001")}
|
||||
verifySetGet(t, ms)
|
||||
}
|
||||
|
||||
func verifySetGet(t *testing.T, ms *MetaStore) {
|
||||
data := uint64(234234)
|
||||
ms.SetUint64(data, "/tmp", "sequence")
|
||||
if !ms.Has("/tmp", "sequence") {
|
||||
ms.SetUint64("/tmp/sequence", data)
|
||||
if !ms.Has("/tmp/sequence") {
|
||||
t.Errorf("Failed to set data")
|
||||
}
|
||||
if val, err := ms.GetUint64("/tmp", "sequence"); err == nil {
|
||||
if val, err := ms.GetUint64("/tmp/sequence"); err == nil {
|
||||
if val != data {
|
||||
t.Errorf("Set %d, but read back %d", data, val)
|
||||
}
|
||||
|
||||
51
go/metastore/etcd_backing.go
Normal file
51
go/metastore/etcd_backing.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package metastore
|
||||
|
||||
import (
|
||||
"code.google.com/p/weed-fs/go/glog"
|
||||
"errors"
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// store data on etcd
|
||||
|
||||
type MetaStoreEtcdBacking struct {
|
||||
client *etcd.Client
|
||||
}
|
||||
|
||||
func NewMetaStoreEtcdBacking(etcdCluster string) *MetaStoreEtcdBacking {
|
||||
m := &MetaStoreEtcdBacking{}
|
||||
m.client = etcd.NewClient(strings.Split(etcdCluster, ","))
|
||||
return m
|
||||
}
|
||||
|
||||
func (m MetaStoreEtcdBacking) Set(path, val string) error {
|
||||
res, e := m.client.Set(path, val, 0)
|
||||
glog.V(0).Infof("etcd set response: %+v\n", res)
|
||||
return e
|
||||
}
|
||||
|
||||
func (m MetaStoreEtcdBacking) Get(path string) (string, error) {
|
||||
results, err := m.client.Get(path)
|
||||
for i, res := range results {
|
||||
glog.V(0).Infof("[%d] get response: %+v\n", i, res)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if results[0].Key != path {
|
||||
return "", errors.New("Key Not Found:" + path)
|
||||
}
|
||||
return results[0].Value, nil
|
||||
}
|
||||
|
||||
func (m MetaStoreEtcdBacking) Has(path string) (ok bool) {
|
||||
results, err := m.client.Get(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if results[0].Key != path {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package metastore
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
// store data on disk, enough for most cases
|
||||
@@ -11,21 +10,22 @@ import (
|
||||
type MetaStoreFileBacking struct {
|
||||
}
|
||||
|
||||
func NewMetaStoreFileBacking() MetaStoreFileBacking {
|
||||
mms := MetaStoreFileBacking{}
|
||||
func NewMetaStoreFileBacking() *MetaStoreFileBacking {
|
||||
mms := &MetaStoreFileBacking{}
|
||||
return mms
|
||||
}
|
||||
|
||||
func (mms MetaStoreFileBacking) Set(val []byte, elem ...string) error {
|
||||
return ioutil.WriteFile(path.Join(elem...), val, 0644)
|
||||
func (mms *MetaStoreFileBacking) Set(path, val string) error {
|
||||
return ioutil.WriteFile(path, []byte(val), 0644)
|
||||
}
|
||||
|
||||
func (mms MetaStoreFileBacking) Get(elem ...string) (val []byte, err error) {
|
||||
return ioutil.ReadFile(path.Join(elem...))
|
||||
func (mms *MetaStoreFileBacking) Get(path string) (string, error) {
|
||||
val, e := ioutil.ReadFile(path)
|
||||
return string(val), e
|
||||
}
|
||||
|
||||
func (mms MetaStoreFileBacking) Has(elem ...string) (ok bool) {
|
||||
seqFile, se := os.OpenFile(path.Join(elem...), os.O_RDONLY, 0644)
|
||||
func (mms *MetaStoreFileBacking) Has(path string) (ok bool) {
|
||||
seqFile, se := os.OpenFile(path, os.O_RDONLY, 0644)
|
||||
if se != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -2,36 +2,35 @@ package metastore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
)
|
||||
|
||||
//this is for testing only
|
||||
|
||||
type MetaStoreMemoryBacking struct {
|
||||
m map[string][]byte
|
||||
m map[string]string
|
||||
}
|
||||
|
||||
func NewMetaStoreMemoryBacking() MetaStoreMemoryBacking {
|
||||
mms := MetaStoreMemoryBacking{}
|
||||
mms.m = make(map[string][]byte)
|
||||
func NewMetaStoreMemoryBacking() *MetaStoreMemoryBacking {
|
||||
mms := &MetaStoreMemoryBacking{}
|
||||
mms.m = make(map[string]string)
|
||||
return mms
|
||||
}
|
||||
|
||||
func (mms MetaStoreMemoryBacking) Set(val []byte, elem ...string) error {
|
||||
mms.m[path.Join(elem...)] = val
|
||||
func (mms MetaStoreMemoryBacking) Set(path, val string) error {
|
||||
mms.m[path] = val
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mms MetaStoreMemoryBacking) Get(elem ...string) (val []byte, err error) {
|
||||
func (mms MetaStoreMemoryBacking) Get(path string) (val string, err error) {
|
||||
var ok bool
|
||||
val, ok = mms.m[path.Join(elem...)]
|
||||
val, ok = mms.m[path]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Missing value for %s", path.Join(elem...))
|
||||
return "", fmt.Errorf("Missing value for %s", path)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (mms MetaStoreMemoryBacking) Has(elem ...string) (ok bool) {
|
||||
_, ok = mms.m[path.Join(elem...)]
|
||||
func (mms MetaStoreMemoryBacking) Has(path string) (ok bool) {
|
||||
_, ok = mms.m[path]
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,35 +1,33 @@
|
||||
package metastore
|
||||
|
||||
import (
|
||||
"code.google.com/p/weed-fs/go/util"
|
||||
"errors"
|
||||
"path"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type MetaStoreBacking interface {
|
||||
Get(elem ...string) ([]byte, error)
|
||||
Set(val []byte, elem ...string) error
|
||||
Has(elem ...string) bool
|
||||
Get(path string) (string, error)
|
||||
Set(path, val string) error
|
||||
Has(path string) bool
|
||||
}
|
||||
|
||||
type MetaStore struct {
|
||||
MetaStoreBacking
|
||||
}
|
||||
|
||||
func (m *MetaStore) SetUint64(val uint64, elem ...string) error {
|
||||
b := make([]byte, 8)
|
||||
util.Uint64toBytes(b, val)
|
||||
return m.Set(b, elem...)
|
||||
func (m *MetaStore) SetUint64(path string, val uint64) error {
|
||||
return m.Set(path, strconv.FormatUint(val, 10))
|
||||
}
|
||||
|
||||
func (m *MetaStore) GetUint64(elem ...string) (val uint64, err error) {
|
||||
if b, e := m.Get(elem...); e == nil && len(b) == 8 {
|
||||
val = util.BytesToUint64(b)
|
||||
func (m *MetaStore) GetUint64(path string) (val uint64, err error) {
|
||||
if b, e := m.Get(path); e == nil {
|
||||
val, err = strconv.ParseUint(b, 10, 64)
|
||||
return
|
||||
} else {
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
err = errors.New("Not found value for " + path.Join(elem...))
|
||||
err = errors.New("Not found value for " + path)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user