Browse Source
adding etcd storage support for cluster meta data. Currently just
adding etcd storage support for cluster meta data. Currently just
sequence. More to come...pull/2/head
Chris Lu
11 years ago
12 changed files with 149 additions and 56 deletions
-
11go/metastore/backing_test.go
-
51go/metastore/etcd_backing.go
-
18go/metastore/file_backing.go
-
23go/metastore/memory_backing.go
-
24go/metastore/metastore.go
-
3go/replication/volume_growth_test.go
-
19go/sequence/memory_sequencer.go
-
35go/sequence/sequence.go
-
3go/topology/node_list_test.go
-
3go/topology/topo_test.go
-
4go/topology/topology.go
-
11go/weed/master.go
@ -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 |
||||
|
} |
@ -1,35 +1,33 @@ |
|||||
package metastore |
package metastore |
||||
|
|
||||
import ( |
import ( |
||||
"code.google.com/p/weed-fs/go/util" |
|
||||
"errors" |
"errors" |
||||
"path" |
|
||||
|
"strconv" |
||||
) |
) |
||||
|
|
||||
type MetaStoreBacking interface { |
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 { |
type MetaStore struct { |
||||
MetaStoreBacking |
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 { |
} else { |
||||
if e != nil { |
if e != nil { |
||||
return 0, e |
return 0, e |
||||
} |
} |
||||
err = errors.New("Not found value for " + path.Join(elem...)) |
|
||||
|
err = errors.New("Not found value for " + path) |
||||
} |
} |
||||
return |
return |
||||
} |
} |
@ -0,0 +1,19 @@ |
|||||
|
package sequence |
||||
|
|
||||
|
import () |
||||
|
|
||||
|
// just for testing
|
||||
|
type MemorySequencer struct { |
||||
|
counter uint64 |
||||
|
} |
||||
|
|
||||
|
func NewMemorySequencer() (m *MemorySequencer) { |
||||
|
m = &MemorySequencer{counter: 1} |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
func (m *MemorySequencer) NextFileId(count int) (uint64, int) { |
||||
|
ret := m.counter |
||||
|
m.counter += uint64(count) |
||||
|
return ret, count |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue