From 6f882eb354ff61d663a122c430781493cecf2467 Mon Sep 17 00:00:00 2001 From: ningfd Date: Fri, 22 Jul 2022 14:34:17 +0800 Subject: [PATCH 1/2] optimiz: master ui will render data in order --- weed/topology/data_center.go | 20 +++++++++++++++----- weed/topology/data_node.go | 25 +++++++++++++++++-------- weed/topology/rack.go | 21 ++++++++++++++++----- weed/topology/topology_map.go | 29 ++++++++++++++++++++++------- 4 files changed, 70 insertions(+), 25 deletions(-) diff --git a/weed/topology/data_center.go b/weed/topology/data_center.go index 60d91ba6d..304a5ccae 100644 --- a/weed/topology/data_center.go +++ b/weed/topology/data_center.go @@ -2,6 +2,7 @@ package topology import ( "github.com/chrislusf/seaweedfs/weed/pb/master_pb" + "golang.org/x/exp/slices" ) type DataCenter struct { @@ -30,15 +31,24 @@ func (dc *DataCenter) GetOrCreateRack(rackName string) *Rack { return rack } -func (dc *DataCenter) ToMap() interface{} { - m := make(map[string]interface{}) - m["Id"] = dc.Id() - var racks []interface{} +type DataCenterMap struct { + Id NodeId `json:"Id"` + Racks []RackMap `json:"Racks"` +} + +func (dc *DataCenter) ToMap() DataCenterMap { + m := DataCenterMap{} + m.Id = dc.Id() + var racks []RackMap for _, c := range dc.Children() { rack := c.(*Rack) racks = append(racks, rack.ToMap()) } - m["Racks"] = racks + + slices.SortFunc(racks, func(a, b RackMap) bool { + return a.Id < b.Id + }) + m.Racks = racks return m } diff --git a/weed/topology/data_node.go b/weed/topology/data_node.go index 6bdbd965f..6a24c4a49 100644 --- a/weed/topology/data_node.go +++ b/weed/topology/data_node.go @@ -217,10 +217,19 @@ func (dn *DataNode) ServerAddress() pb.ServerAddress { return pb.NewServerAddress(dn.Ip, dn.Port, dn.GrpcPort) } -func (dn *DataNode) ToMap() interface{} { - ret := make(map[string]interface{}) - ret["Url"] = dn.Url() - ret["PublicUrl"] = dn.PublicUrl +type DataNodeMap struct { + Url string `json:"Url"` + PublicUrl string `json:"PublicUrl"` + Volumes int64 `json:"Volumes"` + EcShards int64 `json:"EcShards"` + Max int64 `json:"Max"` + VolumeIds string `json:"VolumeIds"` +} + +func (dn *DataNode) ToMap() DataNodeMap { + ret := DataNodeMap{} + ret.Url = dn.Url() + ret.PublicUrl = dn.PublicUrl // aggregated volume info var volumeCount, ecShardCount, maxVolumeCount int64 @@ -236,10 +245,10 @@ func (dn *DataNode) ToMap() interface{} { volumeIds += " " + d.GetVolumeIds() } - ret["Volumes"] = volumeCount - ret["EcShards"] = ecShardCount - ret["Max"] = maxVolumeCount - ret["VolumeIds"] = volumeIds + ret.Volumes = volumeCount + ret.EcShards = ecShardCount + ret.Max = maxVolumeCount + ret.VolumeIds = volumeIds return ret } diff --git a/weed/topology/rack.go b/weed/topology/rack.go index cd09746b2..0588dfa7d 100644 --- a/weed/topology/rack.go +++ b/weed/topology/rack.go @@ -4,6 +4,7 @@ import ( "github.com/chrislusf/seaweedfs/weed/pb/master_pb" "github.com/chrislusf/seaweedfs/weed/storage/types" "github.com/chrislusf/seaweedfs/weed/util" + "golang.org/x/exp/slices" "time" ) @@ -53,15 +54,25 @@ func (r *Rack) GetOrCreateDataNode(ip string, port int, grpcPort int, publicUrl return dn } -func (r *Rack) ToMap() interface{} { - m := make(map[string]interface{}) - m["Id"] = r.Id() - var dns []interface{} +type RackMap struct { + Id NodeId `json:"Id"` + DataNodes []DataNodeMap `json:"DataNodes"` +} + +func (r *Rack) ToMap() RackMap { + m := RackMap{} + m.Id = r.Id() + var dns []DataNodeMap for _, c := range r.Children() { dn := c.(*DataNode) dns = append(dns, dn.ToMap()) } - m["DataNodes"] = dns + + slices.SortFunc(dns, func(a, b DataNodeMap) bool { + return a.Url < b.Url + }) + + m.DataNodes = dns return m } diff --git a/weed/topology/topology_map.go b/weed/topology/topology_map.go index 0fedb6221..b6585e964 100644 --- a/weed/topology/topology_map.go +++ b/weed/topology/topology_map.go @@ -1,17 +1,32 @@ package topology -import "github.com/chrislusf/seaweedfs/weed/pb/master_pb" +import ( + "github.com/chrislusf/seaweedfs/weed/pb/master_pb" + "golang.org/x/exp/slices" +) + +type TopologyMap struct { + Max int64 `json:"Max"` + Free int64 `json:"Free"` + DataCenters []DataCenterMap `json:"DataCenters"` + Layouts []interface{} `json:"Layouts"` +} func (t *Topology) ToMap() interface{} { - m := make(map[string]interface{}) - m["Max"] = t.diskUsages.GetMaxVolumeCount() - m["Free"] = t.diskUsages.FreeSpace() - var dcs []interface{} + m := TopologyMap{} + m.Max = t.diskUsages.GetMaxVolumeCount() + m.Free = t.diskUsages.FreeSpace() + var dcs []DataCenterMap for _, c := range t.Children() { dc := c.(*DataCenter) dcs = append(dcs, dc.ToMap()) } - m["DataCenters"] = dcs + + slices.SortFunc(dcs, func(a, b DataCenterMap) bool { + return a.Id < b.Id + }) + + m.DataCenters = dcs var layouts []interface{} for _, col := range t.collectionMap.Items() { c := col.(*Collection) @@ -23,7 +38,7 @@ func (t *Topology) ToMap() interface{} { } } } - m["Layouts"] = layouts + m.Layouts = layouts return m } From 58f2dd674061bb48f433abbebd604ba7a8fe1aa7 Mon Sep 17 00:00:00 2001 From: ningfd Date: Fri, 22 Jul 2022 17:18:33 +0800 Subject: [PATCH 2/2] change topology mapping definition, improve spelling --- weed/server/master_server_handlers_admin.go | 2 +- weed/server/master_server_handlers_ui.go | 4 +-- weed/topology/data_center.go | 21 ++++++----- weed/topology/data_node.go | 19 +++++----- weed/topology/rack.go | 21 ++++++----- .../{topology_map.go => topology_info.go} | 35 +++++++++---------- weed/topology/volume_layout.go | 18 ++++++---- 7 files changed, 61 insertions(+), 59 deletions(-) rename weed/topology/{topology_map.go => topology_info.go} (77%) diff --git a/weed/server/master_server_handlers_admin.go b/weed/server/master_server_handlers_admin.go index 47abfb892..0c85db791 100644 --- a/weed/server/master_server_handlers_admin.go +++ b/weed/server/master_server_handlers_admin.go @@ -48,7 +48,7 @@ func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.R func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) { m := make(map[string]interface{}) m["Version"] = util.Version() - m["Topology"] = ms.Topo.ToMap() + m["Topology"] = ms.Topo.ToInfo() writeJsonQuiet(w, r, http.StatusOK, m) } diff --git a/weed/server/master_server_handlers_ui.go b/weed/server/master_server_handlers_ui.go index d8260d8d2..f09022399 100644 --- a/weed/server/master_server_handlers_ui.go +++ b/weed/server/master_server_handlers_ui.go @@ -26,7 +26,7 @@ func (ms *MasterServer) uiStatusHandler(w http.ResponseWriter, r *http.Request) VolumeSizeLimitMB uint32 }{ util.Version(), - ms.Topo.ToMap(), + ms.Topo.ToInfo(), ms.Topo.RaftServer, infos, serverStats, @@ -43,7 +43,7 @@ func (ms *MasterServer) uiStatusHandler(w http.ResponseWriter, r *http.Request) VolumeSizeLimitMB uint32 }{ util.Version(), - ms.Topo.ToMap(), + ms.Topo.ToInfo(), ms.Topo.HashicorpRaft, infos, serverStats, diff --git a/weed/topology/data_center.go b/weed/topology/data_center.go index 304a5ccae..78c23e748 100644 --- a/weed/topology/data_center.go +++ b/weed/topology/data_center.go @@ -31,25 +31,24 @@ func (dc *DataCenter) GetOrCreateRack(rackName string) *Rack { return rack } -type DataCenterMap struct { - Id NodeId `json:"Id"` - Racks []RackMap `json:"Racks"` +type DataCenterInfo struct { + Id NodeId `json:"Id"` + Racks []RackInfo `json:"Racks"` } -func (dc *DataCenter) ToMap() DataCenterMap { - m := DataCenterMap{} - m.Id = dc.Id() - var racks []RackMap +func (dc *DataCenter) ToInfo() (info DataCenterInfo) { + info.Id = dc.Id() + var racks []RackInfo for _, c := range dc.Children() { rack := c.(*Rack) - racks = append(racks, rack.ToMap()) + racks = append(racks, rack.ToInfo()) } - slices.SortFunc(racks, func(a, b RackMap) bool { + slices.SortFunc(racks, func(a, b RackInfo) bool { return a.Id < b.Id }) - m.Racks = racks - return m + info.Racks = racks + return } func (dc *DataCenter) ToDataCenterInfo() *master_pb.DataCenterInfo { diff --git a/weed/topology/data_node.go b/weed/topology/data_node.go index 6a24c4a49..33bff2d59 100644 --- a/weed/topology/data_node.go +++ b/weed/topology/data_node.go @@ -217,7 +217,7 @@ func (dn *DataNode) ServerAddress() pb.ServerAddress { return pb.NewServerAddress(dn.Ip, dn.Port, dn.GrpcPort) } -type DataNodeMap struct { +type DataNodeInfo struct { Url string `json:"Url"` PublicUrl string `json:"PublicUrl"` Volumes int64 `json:"Volumes"` @@ -226,10 +226,9 @@ type DataNodeMap struct { VolumeIds string `json:"VolumeIds"` } -func (dn *DataNode) ToMap() DataNodeMap { - ret := DataNodeMap{} - ret.Url = dn.Url() - ret.PublicUrl = dn.PublicUrl +func (dn *DataNode) ToInfo() (info DataNodeInfo) { + info.Url = dn.Url() + info.PublicUrl = dn.PublicUrl // aggregated volume info var volumeCount, ecShardCount, maxVolumeCount int64 @@ -245,12 +244,12 @@ func (dn *DataNode) ToMap() DataNodeMap { volumeIds += " " + d.GetVolumeIds() } - ret.Volumes = volumeCount - ret.EcShards = ecShardCount - ret.Max = maxVolumeCount - ret.VolumeIds = volumeIds + info.Volumes = volumeCount + info.EcShards = ecShardCount + info.Max = maxVolumeCount + info.VolumeIds = volumeIds - return ret + return } func (dn *DataNode) ToDataNodeInfo() *master_pb.DataNodeInfo { diff --git a/weed/topology/rack.go b/weed/topology/rack.go index 0588dfa7d..7b0ed4a54 100644 --- a/weed/topology/rack.go +++ b/weed/topology/rack.go @@ -54,26 +54,25 @@ func (r *Rack) GetOrCreateDataNode(ip string, port int, grpcPort int, publicUrl return dn } -type RackMap struct { - Id NodeId `json:"Id"` - DataNodes []DataNodeMap `json:"DataNodes"` +type RackInfo struct { + Id NodeId `json:"Id"` + DataNodes []DataNodeInfo `json:"DataNodes"` } -func (r *Rack) ToMap() RackMap { - m := RackMap{} - m.Id = r.Id() - var dns []DataNodeMap +func (r *Rack) ToInfo() (info RackInfo) { + info.Id = r.Id() + var dns []DataNodeInfo for _, c := range r.Children() { dn := c.(*DataNode) - dns = append(dns, dn.ToMap()) + dns = append(dns, dn.ToInfo()) } - slices.SortFunc(dns, func(a, b DataNodeMap) bool { + slices.SortFunc(dns, func(a, b DataNodeInfo) bool { return a.Url < b.Url }) - m.DataNodes = dns - return m + info.DataNodes = dns + return } func (r *Rack) ToRackInfo() *master_pb.RackInfo { diff --git a/weed/topology/topology_map.go b/weed/topology/topology_info.go similarity index 77% rename from weed/topology/topology_map.go rename to weed/topology/topology_info.go index b6585e964..21ce77edf 100644 --- a/weed/topology/topology_map.go +++ b/weed/topology/topology_info.go @@ -5,41 +5,40 @@ import ( "golang.org/x/exp/slices" ) -type TopologyMap struct { - Max int64 `json:"Max"` - Free int64 `json:"Free"` - DataCenters []DataCenterMap `json:"DataCenters"` - Layouts []interface{} `json:"Layouts"` +type TopologyInfo struct { + Max int64 `json:"Max"` + Free int64 `json:"Free"` + DataCenters []DataCenterInfo `json:"DataCenters"` + Layouts []VolumeLayoutInfo `json:"Layouts"` } -func (t *Topology) ToMap() interface{} { - m := TopologyMap{} - m.Max = t.diskUsages.GetMaxVolumeCount() - m.Free = t.diskUsages.FreeSpace() - var dcs []DataCenterMap +func (t *Topology) ToInfo() (info TopologyInfo) { + info.Max = t.diskUsages.GetMaxVolumeCount() + info.Free = t.diskUsages.FreeSpace() + var dcs []DataCenterInfo for _, c := range t.Children() { dc := c.(*DataCenter) - dcs = append(dcs, dc.ToMap()) + dcs = append(dcs, dc.ToInfo()) } - slices.SortFunc(dcs, func(a, b DataCenterMap) bool { + slices.SortFunc(dcs, func(a, b DataCenterInfo) bool { return a.Id < b.Id }) - m.DataCenters = dcs - var layouts []interface{} + info.DataCenters = dcs + var layouts []VolumeLayoutInfo for _, col := range t.collectionMap.Items() { c := col.(*Collection) for _, layout := range c.storageType2VolumeLayout.Items() { if layout != nil { - tmp := layout.(*VolumeLayout).ToMap() - tmp["collection"] = c.Name + tmp := layout.(*VolumeLayout).ToInfo() + tmp.Collection = c.Name layouts = append(layouts, tmp) } } } - m.Layouts = layouts - return m + info.Layouts = layouts + return } func (t *Topology) ToVolumeMap() interface{} { diff --git a/weed/topology/volume_layout.go b/weed/topology/volume_layout.go index dee82762a..03c4c4adf 100644 --- a/weed/topology/volume_layout.go +++ b/weed/topology/volume_layout.go @@ -473,13 +473,19 @@ func (vl *VolumeLayout) SetVolumeCrowded(vid needle.VolumeId) { } } -func (vl *VolumeLayout) ToMap() map[string]interface{} { - m := make(map[string]interface{}) - m["replication"] = vl.rp.String() - m["ttl"] = vl.ttl.String() - m["writables"] = vl.writables +type VolumeLayoutInfo struct { + Replication string `json:"replication"` + TTL string `json:"ttl"` + Writables []needle.VolumeId `json:"writables"` + Collection string `json:"collection"` +} + +func (vl *VolumeLayout) ToInfo() (info VolumeLayoutInfo) { + info.Replication = vl.rp.String() + info.TTL = vl.ttl.String() + info.Writables = vl.writables //m["locations"] = vl.vid2location - return m + return } func (vl *VolumeLayout) Stats() *VolumeLayoutStats {