Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

perf(weed/topology): preallocate the heartbeat volume conversion slice (#10607)

Chris Lu3fce1a938d7b
diff --git a/weed/topology/topology.go b/weed/topology/topology.go
index e898ec7dc0..1374ed4eb4 100644
--- a/weed/topology/topology.go
+++ b/weed/topology/topology.go
@@ -587,7 +587,7 @@ func (t *Topology) ListDCAndRacks() (dcs map[NodeId][]NodeId) {
func (t *Topology) SyncDataNodeRegistration(volumes []*master_pb.VolumeInformationMessage, dn *DataNode) (newVolumes, deletedVolumes []storage.VolumeInfo) {
// convert into in memory struct storage.VolumeInfo
- var volumeInfos []storage.VolumeInfo
+ volumeInfos := make([]storage.VolumeInfo, 0, len(volumes))
for _, v := range volumes {
if vi, err := storage.NewVolumeInfo(v); err == nil {
volumeInfos = append(volumeInfos, vi)
diff --git a/weed/topology/topology_heartbeat_bench_test.go b/weed/topology/topology_heartbeat_bench_test.go
new file mode 100644
index 0000000000..dc605f6c99
--- /dev/null
+++ b/weed/topology/topology_heartbeat_bench_test.go
@@ -0,0 +1,52 @@
+package topology
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
+)
+
+func benchHeartbeatMessages(count int) []*master_pb.VolumeInformationMessage {
+ messages := make([]*master_pb.VolumeInformationMessage, 0, count)
+ for i := 0; i < count; i++ {
+ messages = append(messages, &master_pb.VolumeInformationMessage{
+ Id: uint32(i),
+ Size: 1024 * 1024,
+ Collection: "benchcollection",
+ FileCount: 100,
+ DeleteCount: 1,
+ DeletedByteCount: 1024,
+ ReplicaPlacement: 0,
+ Version: 3,
+ CompactRevision: 1,
+ ModifiedAtSecond: 1700000000,
+ })
+ }
+ return messages
+}
+
+// A volume server re-sends its whole volume list on every heartbeat, so this is
+// the master's steady-state cost per volume server every VolumePulsePeriod.
+func benchSyncDataNodeRegistration(b *testing.B, count int) {
+ topo := NewTopology("bench", nil, 32*1024*1024*1024, 5, false)
+ dn := topo.GetOrCreateDataCenter("dc1").GetOrCreateRack("rack1").
+ GetOrCreateDataNode("127.0.0.1", 8080, 18080, "", "", map[string]uint32{"": uint32(count) * 2})
+ topo.SyncDataNodeRegistration(benchHeartbeatMessages(count), dn)
+
+ messages := benchHeartbeatMessages(count)
+
+ b.ResetTimer()
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ topo.SyncDataNodeRegistration(messages, dn)
+ }
+}
+
+func BenchmarkSyncDataNodeRegistration(b *testing.B) {
+ for _, count := range []int{1000, 100000} {
+ b.Run(fmt.Sprintf("%dVolumes", count), func(b *testing.B) {
+ benchSyncDataNodeRegistration(b, count)
+ })
+ }
+}