Skip to content

Commit

Permalink
refactor backend client and metarepo api.
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar committed Aug 26, 2016
1 parent b8a3a40 commit 76910dd
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 182 deletions.
15 changes: 7 additions & 8 deletions backends/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ import (
// The StoreClient interface is implemented by objects that can retrieve
// key/value pairs from a backend store.
type StoreClient interface {
GetValues(nodePath string) (map[string]interface{}, error)
GetValue(nodePath string) (string, error)
Sync(store store.Store, stopChan chan bool)
SetValues(nodePath string, values map[string]interface{}, replace bool) error
SetValue(nodePath string, value string) error
Set(nodePath string, value interface{}, replace bool) error
Get(nodePath string, dir bool) (interface{}, error)
Put(nodePath string, value interface{}, replace bool) error
// Delete
// if the 'key' represent a dir, 'dir' should be true.
Delete(nodePath string, dir bool) error
SyncMapping(mapping store.Store, stopChan chan bool)
UpdateMapping(nodePath string, mapping interface{}, replace bool) error
Sync(store store.Store, stopChan chan bool)

GetMapping(nodePath string, dir bool) (interface{}, error)
PutMapping(nodePath string, mapping interface{}, replace bool) error
DeleteMapping(nodePath string, dir bool) error
SyncMapping(mapping store.Store, stopChan chan bool)
}

// New is used to create a storage client based on our configuration.
Expand Down
88 changes: 65 additions & 23 deletions backends/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {
rand.Seed(int64(time.Now().Nanosecond()))
}

func TestClientGetSet(t *testing.T) {
func TestClientGetPut(t *testing.T) {
for _, backend := range backendNodes {
println("Test backend: ", backend)

Expand All @@ -42,23 +42,23 @@ func TestClientGetSet(t *testing.T) {

storeClient.Delete("/", true)

err = storeClient.SetValue("testkey", "testvalue")
err = storeClient.Put("testkey", "testvalue", false)
assert.NoError(t, err)

val, getErr := storeClient.GetValue("testkey")
val, getErr := storeClient.Get("testkey", false)
assert.NoError(t, getErr)
assert.Equal(t, "testvalue", val)

// test no exist key
val, getErr = storeClient.GetValue("noexistkey")
val, getErr = storeClient.Get("noexistkey", false)
assert.NoError(t, getErr)
assert.Equal(t, "", val)

storeClient.Delete("/", true)
}
}

func TestClientGetsSets(t *testing.T) {
func TestClientGetsPuts(t *testing.T) {
for _, backend := range backendNodes {
println("Test backend: ", backend)

Expand All @@ -83,10 +83,10 @@ func TestClientGetsSets(t *testing.T) {
},
}

err = storeClient.SetValues("testkey", values, true)
err = storeClient.Put("testkey", values, true)
assert.NoError(t, err)

val, getErr := storeClient.GetValues("testkey")
val, getErr := storeClient.Get("testkey", true)
assert.NoError(t, getErr)
assert.Equal(t, values, val)

Expand All @@ -98,7 +98,7 @@ func TestClientGetsSets(t *testing.T) {
},
}

err = storeClient.SetValues("testkey", values2, false)
err = storeClient.Put("testkey", values2, false)
assert.NoError(t, err)

values3 := map[string]interface{}{
Expand All @@ -109,24 +109,24 @@ func TestClientGetsSets(t *testing.T) {
},
}

val, getErr = storeClient.GetValues("testkey")
val, getErr = storeClient.Get("testkey", true)
assert.NoError(t, getErr)
assert.Equal(t, values3, val)

//test replace

err = storeClient.SetValues("testkey", values2, true)
err = storeClient.Put("testkey", values2, true)
assert.NoError(t, err)

val, getErr = storeClient.GetValues("testkey")
val, getErr = storeClient.Get("testkey", true)
assert.NoError(t, getErr)
assert.Equal(t, values2, val)

assert.NoError(t, storeClient.Delete("/", true))
}
}

func TestClientSet(t *testing.T) {
func TestClientPutJSON(t *testing.T) {
for _, backend := range backendNodes {
println("Test backend: ", backend)

Expand Down Expand Up @@ -156,10 +156,10 @@ func TestClientSet(t *testing.T) {
err = json.Unmarshal(jsonVal, &values)
assert.NoError(t, err)

err = storeClient.Set("testkey", values, true)
err = storeClient.Put("testkey", values, true)
assert.NoError(t, err)

val, getErr := storeClient.GetValues("testkey")
val, getErr := storeClient.Get("testkey", true)
assert.NoError(t, getErr)
assert.Equal(t, values, val)

Expand All @@ -177,7 +177,7 @@ func TestClientSet(t *testing.T) {
err = json.Unmarshal(jsonVal2, &values2)
assert.NoError(t, err)

err = storeClient.Set("testkey", values2, false)
err = storeClient.Put("testkey", values2, false)
assert.NoError(t, err)

values3 := map[string]interface{}{
Expand All @@ -188,16 +188,16 @@ func TestClientSet(t *testing.T) {
},
}

val, getErr = storeClient.GetValues("testkey")
val, getErr = storeClient.Get("testkey", true)
assert.NoError(t, getErr)
assert.Equal(t, values3, val)

//test replace

err = storeClient.Set("testkey", values2, true)
err = storeClient.Put("testkey", values2, true)
assert.NoError(t, err)

val, getErr = storeClient.GetValues("testkey")
val, getErr = storeClient.Get("testkey", true)
assert.NoError(t, getErr)
assert.Equal(t, values2, val)

Expand Down Expand Up @@ -261,6 +261,48 @@ func TestMapping(t *testing.T) {

prefix := fmt.Sprintf("/prefix%v", rand.Intn(1000))

for _, backend := range backendNodes {
println("Test backend: ", backend)
nodes := GetDefaultBackends(backend)

config := Config{
Backend: backend,
BackendNodes: nodes,
Prefix: prefix,
}
storeClient, err := New(config)
assert.NoError(t, err)
mappings := make(map[string]interface{})
for i := 0; i < 10; i++ {
ip := fmt.Sprintf("192.168.1.%v", i)
mapping := map[string]string{
"instance": fmt.Sprintf("/instances/%v", i),
"config": fmt.Sprintf("/configs/%v", i),
}
mappings[ip] = mapping
}
storeClient.PutMapping("/", mappings, true)

val, err := storeClient.GetMapping("/", true)
assert.NoError(t, err)
m, mok := val.(map[string]interface{})
assert.True(t, mok)
assert.True(t, m["192.168.1.0"] != nil)

ip := fmt.Sprintf("192.168.1.%v", 1)
nodePath := "/" + ip + "/" + "instance"
storeClient.PutMapping(nodePath, "/instances/new1", true)
time.Sleep(1000 * time.Millisecond)
val, err = storeClient.GetMapping(nodePath, false)
assert.NoError(t, err)
assert.Equal(t, "/instances/new1", val)
}
}

func TestMappingSync(t *testing.T) {

prefix := fmt.Sprintf("/prefix%v", rand.Intn(1000))

for _, backend := range backendNodes {
println("Test backend: ", backend)
stopChan := make(chan bool)
Expand All @@ -287,7 +329,7 @@ func TestMapping(t *testing.T) {
"instance": fmt.Sprintf("/instances/%v", i),
"config": fmt.Sprintf("/configs/%v", i),
}
storeClient.UpdateMapping(ip, mapping, true)
storeClient.PutMapping(ip, mapping, true)
}
time.Sleep(1000 * time.Millisecond)
storeClient.SyncMapping(metastore, stopChan)
Expand All @@ -309,7 +351,7 @@ func TestMapping(t *testing.T) {
"instance": fmt.Sprintf("/instances/%v", i),
"config": fmt.Sprintf("/configs/%v", i),
}
storeClient.UpdateMapping(ip, mapping, true)
storeClient.PutMapping(ip, mapping, true)
}
time.Sleep(1000 * time.Millisecond)
for i := 10; i < 20; i++ {
Expand All @@ -323,7 +365,7 @@ func TestMapping(t *testing.T) {
}
ip := fmt.Sprintf("192.168.1.%v", 1)
nodePath := ip + "/" + "instance"
storeClient.UpdateMapping(nodePath, "/instances/new1", true)
storeClient.PutMapping(nodePath, "/instances/new1", true)
time.Sleep(1000 * time.Millisecond)
val, ok := metastore.Get(nodePath)
assert.True(t, ok)
Expand All @@ -340,7 +382,7 @@ func FillTestData(storeClient StoreClient) map[string]string {
}
testData[fmt.Sprintf("%v", i)] = ci
}
err := storeClient.SetValues("/", testData, true)
err := storeClient.Put("/", testData, true)
if err != nil {
log.Error("SetValues error", err.Error())
}
Expand All @@ -358,7 +400,7 @@ func RandomUpdate(testData map[string]string, storeClient StoreClient, times int
key := keys[idx]
val := testData[key]
newVal := fmt.Sprintf("%s-%v", val, 0)
storeClient.SetValue(key, newVal)
storeClient.Put(key, newVal, true)
testData[key] = newVal
}
}
Expand Down
Loading

0 comments on commit 76910dd

Please sign in to comment.