mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-08-24 14:35:35 +08:00
loading from json setup in tests
This commit is contained in:
parent
f5d981ab4d
commit
8d0ea07f5f
@ -7,3 +7,9 @@ type DataCenter struct {
|
|||||||
Node
|
Node
|
||||||
ipRange IpRange
|
ipRange IpRange
|
||||||
}
|
}
|
||||||
|
func NewDataCenter(id NodeId) *DataCenter{
|
||||||
|
dc := &DataCenter{}
|
||||||
|
dc.Node = *NewNode()
|
||||||
|
dc.Node.Id = id
|
||||||
|
return dc
|
||||||
|
}
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
package topology
|
package topology
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"pkg/storage"
|
"pkg/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NodeId string
|
type NodeId string
|
||||||
type Node struct {
|
type Node struct {
|
||||||
id NodeId
|
Id NodeId
|
||||||
countVolumeCount int
|
countVolumeCount int
|
||||||
reservedVolumeCount int
|
reservedVolumeCount int
|
||||||
maxVolumeCount int
|
maxVolumeCount int
|
||||||
@ -15,6 +16,12 @@ type Node struct {
|
|||||||
maxVolumeId storage.VolumeId
|
maxVolumeId storage.VolumeId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewNode() *Node {
|
||||||
|
n := &Node{}
|
||||||
|
n.children = make(map[NodeId]*Node)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
func (n *Node) ReserveOneVolume(r int, vid storage.VolumeId) bool {
|
func (n *Node) ReserveOneVolume(r int, vid storage.VolumeId) bool {
|
||||||
for _, node := range n.children {
|
for _, node := range n.children {
|
||||||
freeSpace := node.maxVolumeCount - node.countVolumeCount - node.reservedVolumeCount
|
freeSpace := node.maxVolumeCount - node.countVolumeCount - node.reservedVolumeCount
|
||||||
@ -37,6 +44,7 @@ func (n *Node) AddVolume(v *storage.VolumeInfo) {
|
|||||||
n.maxVolumeId = v.Id
|
n.maxVolumeId = v.Id
|
||||||
}
|
}
|
||||||
n.countVolumeCount++
|
n.countVolumeCount++
|
||||||
|
fmt.Println(n.Id, "adds 1, volumeCount =", n.countVolumeCount)
|
||||||
if n.reservedVolumeCount > 0 { //if reserved
|
if n.reservedVolumeCount > 0 { //if reserved
|
||||||
n.reservedVolumeCount--
|
n.reservedVolumeCount--
|
||||||
}
|
}
|
||||||
@ -50,19 +58,19 @@ func (n *Node) GetMaxVolumeId() storage.VolumeId {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) AddNode(node *Node) {
|
func (n *Node) AddNode(node *Node) {
|
||||||
n.children[node.id] = node
|
if n.children[node.Id] == nil {
|
||||||
|
n.children[node.Id] = node
|
||||||
n.countVolumeCount += node.countVolumeCount
|
n.countVolumeCount += node.countVolumeCount
|
||||||
n.maxVolumeCount += node.maxVolumeCount
|
n.maxVolumeCount += node.maxVolumeCount
|
||||||
if n.parent != nil {
|
fmt.Println(n.Id, "adds", node.Id, "volumeCount =", n.countVolumeCount)
|
||||||
n.parent.AddNode(node)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) RemoveNode(node *Node) {
|
func (n *Node) RemoveNode(node *Node) {
|
||||||
delete(n.children, node.id)
|
if n.children[node.Id] != nil {
|
||||||
|
delete(n.children, node.Id)
|
||||||
n.countVolumeCount -= node.countVolumeCount
|
n.countVolumeCount -= node.countVolumeCount
|
||||||
n.maxVolumeCount -= node.maxVolumeCount
|
n.maxVolumeCount -= node.maxVolumeCount
|
||||||
if n.parent != nil {
|
fmt.Println(n.Id, "removes", node.Id, "volumeCount =", n.countVolumeCount)
|
||||||
n.parent.RemoveNode(node)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,3 +7,10 @@ type Rack struct {
|
|||||||
Node
|
Node
|
||||||
ipRange IpRange
|
ipRange IpRange
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewRack(id NodeId) *Rack {
|
||||||
|
r := &Rack{}
|
||||||
|
r.Node = *NewNode()
|
||||||
|
r.Node.Id = id
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
@ -2,6 +2,7 @@ package topology
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"pkg/storage"
|
"pkg/storage"
|
||||||
|
_ "fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
@ -11,6 +12,12 @@ type Server struct {
|
|||||||
Port int
|
Port int
|
||||||
PublicUrl string
|
PublicUrl string
|
||||||
}
|
}
|
||||||
|
func NewServer(id NodeId) *Server{
|
||||||
|
s := &Server{}
|
||||||
|
s.Node.Id = id
|
||||||
|
s.volumes = make(map[storage.VolumeId]*storage.VolumeInfo)
|
||||||
|
return s
|
||||||
|
}
|
||||||
func (s *Server) CreateOneVolume(r int, vid storage.VolumeId) storage.VolumeId {
|
func (s *Server) CreateOneVolume(r int, vid storage.VolumeId) storage.VolumeId {
|
||||||
s.AddVolume(&storage.VolumeInfo{Id:vid, Size: 32*1024*1024*1024})
|
s.AddVolume(&storage.VolumeInfo{Id:vid, Size: 32*1024*1024*1024})
|
||||||
return vid
|
return vid
|
||||||
|
@ -1,11 +1,146 @@
|
|||||||
package topology
|
package topology
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"pkg/storage"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAddVolume(t *testing.T) {
|
var topologyLayout = `
|
||||||
|
{
|
||||||
|
"dc1":{
|
||||||
|
"rack1":{
|
||||||
|
"server1":{
|
||||||
|
"volumes":[
|
||||||
|
{"id":1, "size":12312},
|
||||||
|
{"id":2, "size":12312},
|
||||||
|
{"id":3, "size":12312}
|
||||||
|
],
|
||||||
|
"limit":3
|
||||||
|
},
|
||||||
|
"server2":{
|
||||||
|
"volumes":[
|
||||||
|
{"id":4, "size":12312},
|
||||||
|
{"id":5, "size":12312},
|
||||||
|
{"id":6, "size":12312}
|
||||||
|
],
|
||||||
|
"limit":10
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"rack2":{
|
||||||
|
"server1":{
|
||||||
|
"volumes":[
|
||||||
|
{"id":4, "size":12312},
|
||||||
|
{"id":5, "size":12312},
|
||||||
|
{"id":6, "size":12312}
|
||||||
|
],
|
||||||
|
"limit":4
|
||||||
|
},
|
||||||
|
"server2":{
|
||||||
|
"volumes":[],
|
||||||
|
"limit":4
|
||||||
|
},
|
||||||
|
"server3":{
|
||||||
|
"volumes":[
|
||||||
|
{"id":2, "size":12312},
|
||||||
|
{"id":3, "size":12312},
|
||||||
|
{"id":4, "size":12312}
|
||||||
|
],
|
||||||
|
"limit":2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"dc2":{
|
||||||
|
},
|
||||||
|
"dc3":{
|
||||||
|
"rack2":{
|
||||||
|
"server1":{
|
||||||
|
"volumes":[
|
||||||
|
{"id":1, "size":12312},
|
||||||
|
{"id":3, "size":12312},
|
||||||
|
{"id":5, "size":12312}
|
||||||
|
],
|
||||||
|
"limit":4
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
func setup() *Topology {
|
||||||
|
var data interface{}
|
||||||
|
err := json.Unmarshal([]byte(topologyLayout), &data)
|
||||||
|
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(NodeId("mynetwork"))
|
||||||
|
mTopology := data.(map[string]interface{})
|
||||||
|
for dcKey, dcValue := range mTopology {
|
||||||
|
dc := NewDataCenter(NodeId(dcKey))
|
||||||
|
dc.Node.parent = &topo.Node
|
||||||
|
dcMap := dcValue.(map[string]interface{})
|
||||||
|
topo.Node.AddNode(&dc.Node)
|
||||||
|
for rackKey, rackValue := range dcMap {
|
||||||
|
rack := NewRack(NodeId(rackKey))
|
||||||
|
rack.Node.parent = &dc.Node
|
||||||
|
rackMap := rackValue.(map[string]interface{})
|
||||||
|
dc.Node.AddNode(&rack.Node)
|
||||||
|
for serverKey, serverValue := range rackMap {
|
||||||
|
server := NewServer(NodeId(serverKey))
|
||||||
|
server.Node.parent = &rack.Node
|
||||||
|
serverMap := serverValue.(map[string]interface{})
|
||||||
|
rack.Node.AddNode(&server.Node)
|
||||||
|
for _, v := range serverMap["volumes"].([]interface{}) {
|
||||||
|
m := v.(map[string]interface{})
|
||||||
|
vi := &storage.VolumeInfo{Id: storage.VolumeId(int64(m["id"].(float64))), Size: int64(m["size"].(float64))}
|
||||||
|
server.AddVolume(vi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("topology:", *topo)
|
||||||
|
|
||||||
|
bytes, err := json.Marshal(topo.Node.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 TestAddVolume(t *testing.T) {
|
||||||
|
topo := setup()
|
||||||
|
v := &storage.VolumeInfo{}
|
||||||
|
topo.AddVolume(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddServer(t *testing.T) {
|
func TestAddServer(t *testing.T) {
|
||||||
|
@ -8,6 +8,12 @@ import (
|
|||||||
type Topology struct {
|
type Topology struct {
|
||||||
Node
|
Node
|
||||||
}
|
}
|
||||||
|
func NewTopology(id NodeId) *Topology{
|
||||||
|
t := &Topology{}
|
||||||
|
t.Node = *NewNode()
|
||||||
|
t.Node.Id = id
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Topology) RandomlyReserveOneVolume() (bool,storage.VolumeId) {
|
func (t *Topology) RandomlyReserveOneVolume() (bool,storage.VolumeId) {
|
||||||
r := rand.Intn(t.Node.maxVolumeCount-t.Node.countVolumeCount-t.Node.reservedVolumeCount)
|
r := rand.Intn(t.Node.maxVolumeCount-t.Node.countVolumeCount-t.Node.reservedVolumeCount)
|
||||||
|
Loading…
Reference in New Issue
Block a user