aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2012-09-22 00:01:13 -0700
committerChris Lu <chris.lu@gmail.com>2012-09-22 00:01:13 -0700
commita34570fc5ba32535b311049f53e4e2f8a7648857 (patch)
tree4855160d55b31fe2450a45de3909cd3cbb0f90d8
parente986dcf5e084c28bed18a7596f482d53282848ed (diff)
downloadseaweedfs-a34570fc5ba32535b311049f53e4e2f8a7648857.tar.xz
seaweedfs-a34570fc5ba32535b311049f53e4e2f8a7648857.zip
read topology configuration
-rw-r--r--weed-fs/src/pkg/topology/configuration.go34
-rw-r--r--weed-fs/src/pkg/topology/configuration_test.go42
-rw-r--r--weed-fs/src/pkg/topology/topo_test.go33
3 files changed, 76 insertions, 33 deletions
diff --git a/weed-fs/src/pkg/topology/configuration.go b/weed-fs/src/pkg/topology/configuration.go
new file mode 100644
index 000000000..0114c4eef
--- /dev/null
+++ b/weed-fs/src/pkg/topology/configuration.go
@@ -0,0 +1,34 @@
+package topology
+
+import (
+ "encoding/xml"
+)
+
+type rack struct {
+ Name string `xml:"name,attr"`
+ Ips []string `xml:"Ip"`
+}
+type dataCenter struct {
+ Name string `xml:"name,attr"`
+ Racks []rack `xml:"Rack"`
+}
+type topology struct {
+ DataCenters []dataCenter `xml:"DataCenter"`
+}
+type configuration struct {
+ XMLName xml.Name `xml:"Configuration"`
+ Topo topology `xml:"Topology"`
+}
+
+func NewConfiguration(b []byte) (*configuration, error){
+ c := &configuration{}
+ err := xml.Unmarshal(b, c)
+ return c, err
+}
+
+func (c *configuration) String() string{
+ if b, e := xml.MarshalIndent(c, " ", " "); e==nil {
+ return string(b)
+ }
+ return ""
+}
diff --git a/weed-fs/src/pkg/topology/configuration_test.go b/weed-fs/src/pkg/topology/configuration_test.go
new file mode 100644
index 000000000..5542d1503
--- /dev/null
+++ b/weed-fs/src/pkg/topology/configuration_test.go
@@ -0,0 +1,42 @@
+package topology
+
+import (
+ "fmt"
+ "testing"
+)
+
+func TestLoadConfiguration(t *testing.T) {
+
+ confContent := `
+
+<?xml version="1.0" encoding="UTF-8" ?>
+<Configuration>
+ <Topology>
+ <DataCenter name="dc1">
+ <Rack name="rack1">
+ <Ip>192.168.1.1</Ip>
+ </Rack>
+ </DataCenter>
+ <DataCenter name="dc2">
+ <Rack name="rack1">
+ <Ip>192.168.1.2</Ip>
+ </Rack>
+ <Rack name="rack2">
+ <Ip>192.168.1.3</Ip>
+ <Ip>192.168.1.4</Ip>
+ </Rack>
+ </DataCenter>
+ </Topology>
+</Configuration>
+`
+ c, err := NewConfiguration([]byte(confContent))
+
+ fmt.Printf("%s\n", c)
+ if err!=nil{
+ t.Fatalf("unmarshal error:%s",err.Error())
+ }
+
+ if len(c.Topo.DataCenters) <= 0 || c.Topo.DataCenters[0].Name != "dc1" {
+ t.Fatalf("unmarshal error:%s",c)
+ }
+}
diff --git a/weed-fs/src/pkg/topology/topo_test.go b/weed-fs/src/pkg/topology/topo_test.go
index 707aa60fe..a7815acb9 100644
--- a/weed-fs/src/pkg/topology/topo_test.go
+++ b/weed-fs/src/pkg/topology/topo_test.go
@@ -76,8 +76,6 @@ func setup(topologyLayout string) *Topology {
if err != nil {
fmt.Println("error:", err)
}
- //fmt.Println("data:", data)
- //printMap(data)
//need to connect all nodes first before server adding volumes
topo := NewTopology("mynetwork","/tmp","test",234,5)
@@ -104,39 +102,9 @@ func setup(topologyLayout string) *Topology {
}
}
- fmt.Println("topology:", *topo)
-
- bytes, err := json.Marshal(topo.children)
- if err != nil {
- fmt.Println("json error:", err)
- }
- fmt.Println("json topo:", string(bytes))
-
return topo
}
-func printMap(mm interface{}) {
- m := mm.(map[string]interface{})
- for k, v := range m {
- switch vv := v.(type) {
- case string:
- fmt.Println(k, "\"", vv, "\"")
- case int, float64:
- fmt.Println(k, ":", vv)
- case []interface{}:
- fmt.Println(k, ":[")
- for _, u := range vv {
- fmt.Println(u)
- fmt.Println(",")
- }
- fmt.Println("]")
- default:
- fmt.Println(k, ":")
- printMap(vv)
- }
- }
-}
-
func TestRemoveDataCenter(t *testing.T) {
topo := setup(topologyLayout)
topo.UnlinkChildNode(NodeId("dc2"))
@@ -154,7 +122,6 @@ func TestReserveOneVolume(t *testing.T) {
rand.Seed(time.Now().UnixNano())
rand.Seed(1)
ret, node, vid := topo.RandomlyReserveOneVolume()
- fmt.Println("topology:", topo)
fmt.Println("assigned :", ret, ", node :", node,", volume id:", vid)
}