mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
master, admin: report the real last heartbeat time of volume servers (#13074)
* master: report each volume server's last heartbeat time DataNode.LastSeen only moves when a volume server registers, so nothing recorded when the master last heard from it. Stamp every processed heartbeat and carry it in DataNodeInfo.last_heartbeat_ns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * admin: show the master's last heartbeat time for volume servers The volume server details reported the admin's own clock as the last heartbeat. Use the time the master reports, and show unknown when the master does not report one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * admin: carry the last heartbeat time into the volume servers page GetClusterVolumeServers builds its records from the master's volume list without the topology conversion, so the details dialog showed unknown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
df98e9df3e
commit
ec34160a1a
14 files changed
+184
-16
No files matched your search
@@ -436,6 +436,7 @@ message DataNodeInfo {
|
||||
string id = 1;
|
||||
map<string, DiskInfo> diskInfos = 2;
|
||||
uint32 grpc_port = 3;
|
||||
int64 last_heartbeat_ns = 5;
|
||||
string address = 4; // ip:port for connecting to the volume server
|
||||
}
|
||||
message RackInfo {
|
||||
|
||||
@@ -225,7 +225,7 @@ func (s *AdminServer) getTopologyViaGRPC(topology *ClusterTopology) error {
|
||||
MaxVolumes: int(totalMaxVolumes),
|
||||
DiskUsage: diskUsage,
|
||||
DiskCapacity: diskCapacity,
|
||||
LastHeartbeat: time.Now(),
|
||||
LastHeartbeat: dataNodeLastHeartbeat(node),
|
||||
RemoteSize: remoteSize,
|
||||
}
|
||||
|
||||
@@ -268,3 +268,10 @@ func (s *AdminServer) InvalidateCache() {
|
||||
s.lastCollectionStatsUpdate = time.Now().Add(-s.collectionStatsCacheThreshold)
|
||||
s.collectionStatsCache = nil
|
||||
}
|
||||
|
||||
func dataNodeLastHeartbeat(node *master_pb.DataNodeInfo) time.Time {
|
||||
if node.LastHeartbeatNs <= 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
return time.Unix(0, node.LastHeartbeatNs)
|
||||
}
|
||||
@@ -522,6 +522,7 @@ func (s *AdminServer) GetClusterVolumeServers() (*ClusterVolumeServersData, erro
|
||||
EcVolumes: 0,
|
||||
EcShards: 0,
|
||||
EcShardDetails: []VolumeServerEcInfo{},
|
||||
LastHeartbeat: dataNodeLastHeartbeat(node),
|
||||
}
|
||||
}
|
||||
vs := volumeServerMap[node.Id]
|
||||
|
||||
@@ -314,3 +314,26 @@ func TestClusterTopologyDiskTotals(t *testing.T) {
|
||||
require.Equal(t, int64(1_000_000_000_000+10*30000*1024*1024), topology.DiskCapacity)
|
||||
require.Equal(t, int64(590_000_000_000+30_000_000_000+5_000_000_000), topology.TotalSize)
|
||||
}
|
||||
|
||||
func TestVolumeServerLastHeartbeat(t *testing.T) {
|
||||
reported := time.Date(2026, 11, 27, 12, 0, 5, 0, time.UTC)
|
||||
topology := volumeAccessTestTopology(
|
||||
&master_pb.DataNodeInfo{Id: "node-a", LastHeartbeatNs: reported.UnixNano()},
|
||||
&master_pb.DataNodeInfo{Id: "node-b"},
|
||||
)
|
||||
want := map[string]time.Time{"node-a": reported, "node-b": {}}
|
||||
|
||||
servers, err := newVolumeServersTestAdmin(t, topology).GetClusterVolumeServers()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, servers.VolumeServers, 2)
|
||||
for _, vs := range servers.VolumeServers {
|
||||
require.True(t, want[vs.Address].Equal(vs.LastHeartbeat), "volume servers page %s: %v", vs.Address, vs.LastHeartbeat)
|
||||
}
|
||||
|
||||
clusterTopology, err := newVolumeServersTestAdmin(t, topology).GetClusterTopology()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, clusterTopology.VolumeServers, 2)
|
||||
for _, vs := range clusterTopology.VolumeServers {
|
||||
require.True(t, want[vs.Address].Equal(vs.LastHeartbeat), "topology %s: %v", vs.Address, vs.LastHeartbeat)
|
||||
}
|
||||
}
|
||||
@@ -207,7 +207,7 @@ templ ClusterVolumeServers(data dash.ClusterVolumeServersData) {
|
||||
data-remote-size={fmt.Sprintf("%d", host.RemoteSize)}
|
||||
data-ec-volumes={fmt.Sprintf("%d", host.EcVolumes)}
|
||||
data-ec-shards={fmt.Sprintf("%d", host.EcShards)}
|
||||
data-last-heartbeat={host.LastHeartbeat.Format("2006-01-02 15:04:05")}>
|
||||
data-last-heartbeat={formatLastHeartbeat(host.LastHeartbeat)}>
|
||||
<i class="bi bi-eye"></i>
|
||||
</button>
|
||||
} else {
|
||||
@@ -227,7 +227,7 @@ templ ClusterVolumeServers(data dash.ClusterVolumeServersData) {
|
||||
data-remote-size={fmt.Sprintf("%d", host.RemoteSize)}
|
||||
data-ec-volumes={fmt.Sprintf("%d", host.EcVolumes)}
|
||||
data-ec-shards={fmt.Sprintf("%d", host.EcShards)}
|
||||
data-last-heartbeat={host.LastHeartbeat.Format("2006-01-02 15:04:05")}>
|
||||
data-last-heartbeat={formatLastHeartbeat(host.LastHeartbeat)}>
|
||||
<i class="bi bi-eye"></i>
|
||||
</button>
|
||||
}
|
||||
|
||||
@@ -585,9 +585,9 @@ func ClusterVolumeServers(data dash.ClusterVolumeServersData) templ.Component {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var38 string
|
||||
templ_7745c5c3_Var38, templ_7745c5c3_Err = templruntime.NewAttributeExpression(host.LastHeartbeat.Format("2006-01-02 15:04:05")).Resolve(ctx)
|
||||
templ_7745c5c3_Var38, templ_7745c5c3_Err = templruntime.NewAttributeExpression(formatLastHeartbeat(host.LastHeartbeat)).Resolve(ctx)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `view/app/cluster_volume_servers.templ`, Line: 210, Col: 125}
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `view/app/cluster_volume_servers.templ`, Line: 210, Col: 116}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var38)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
@@ -759,9 +759,9 @@ func ClusterVolumeServers(data dash.ClusterVolumeServersData) templ.Component {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var51 string
|
||||
templ_7745c5c3_Var51, templ_7745c5c3_Err = templruntime.NewAttributeExpression(host.LastHeartbeat.Format("2006-01-02 15:04:05")).Resolve(ctx)
|
||||
templ_7745c5c3_Var51, templ_7745c5c3_Err = templruntime.NewAttributeExpression(formatLastHeartbeat(host.LastHeartbeat)).Resolve(ctx)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `view/app/cluster_volume_servers.templ`, Line: 230, Col: 125}
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `view/app/cluster_volume_servers.templ`, Line: 230, Col: 116}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var51)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// formatBytes converts bytes to human readable format
|
||||
@@ -98,3 +99,10 @@ func pluginLaneDescription(lane string) string {
|
||||
return "Default workers, scheduler state, queue, and execution flows."
|
||||
}
|
||||
}
|
||||
|
||||
func formatLastHeartbeat(t time.Time) string {
|
||||
if t.IsZero() {
|
||||
return "unknown"
|
||||
}
|
||||
return t.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFormatLastHeartbeat(t *testing.T) {
|
||||
if got := formatLastHeartbeat(time.Time{}); got != "unknown" {
|
||||
t.Fatalf("zero time = %q, want unknown", got)
|
||||
}
|
||||
beat := time.Date(2026, 1, 2, 3, 4, 5, 0, time.Local)
|
||||
if got := formatLastHeartbeat(beat); got != "2026-01-02 03:04:05" {
|
||||
t.Fatalf("formatLastHeartbeat = %q", got)
|
||||
}
|
||||
}
|
||||
@@ -436,6 +436,7 @@ message DataNodeInfo {
|
||||
string id = 1;
|
||||
map<string, DiskInfo> diskInfos = 2;
|
||||
uint32 grpc_port = 3;
|
||||
int64 last_heartbeat_ns = 5;
|
||||
string address = 4; // ip:port for connecting to the volume server
|
||||
}
|
||||
message RackInfo {
|
||||
|
||||
@@ -2774,13 +2774,14 @@ func (x *DiskInfo) GetDiskFreeBytes() uint64 {
|
||||
}
|
||||
|
||||
type DataNodeInfo struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
DiskInfos map[string]*DiskInfo `protobuf:"bytes,2,rep,name=diskInfos,proto3" json:"diskInfos,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
|
||||
GrpcPort uint32 `protobuf:"varint,3,opt,name=grpc_port,json=grpcPort,proto3" json:"grpc_port,omitempty"`
|
||||
Address string `protobuf:"bytes,4,opt,name=address,proto3" json:"address,omitempty"` // ip:port for connecting to the volume server
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
DiskInfos map[string]*DiskInfo `protobuf:"bytes,2,rep,name=diskInfos,proto3" json:"diskInfos,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
|
||||
GrpcPort uint32 `protobuf:"varint,3,opt,name=grpc_port,json=grpcPort,proto3" json:"grpc_port,omitempty"`
|
||||
LastHeartbeatNs int64 `protobuf:"varint,5,opt,name=last_heartbeat_ns,json=lastHeartbeatNs,proto3" json:"last_heartbeat_ns,omitempty"`
|
||||
Address string `protobuf:"bytes,4,opt,name=address,proto3" json:"address,omitempty"` // ip:port for connecting to the volume server
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *DataNodeInfo) Reset() {
|
||||
@@ -2834,6 +2835,13 @@ func (x *DataNodeInfo) GetGrpcPort() uint32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *DataNodeInfo) GetLastHeartbeatNs() int64 {
|
||||
if x != nil {
|
||||
return x.LastHeartbeatNs
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *DataNodeInfo) GetAddress() string {
|
||||
if x != nil {
|
||||
return x.Address
|
||||
@@ -5529,11 +5537,12 @@ const file_master_proto_rawDesc = "" +
|
||||
"\x0fdisk_free_bytes\x18\r \x01(\x04R\rdiskFreeBytes\x1aG\n" +
|
||||
"\x19MaxVolumeCountByDiskEntry\x12\x10\n" +
|
||||
"\x03key\x18\x01 \x01(\rR\x03key\x12\x14\n" +
|
||||
"\x05value\x18\x02 \x01(\x03R\x05value:\x028\x01\"\xee\x01\n" +
|
||||
"\x05value\x18\x02 \x01(\x03R\x05value:\x028\x01\"\x9a\x02\n" +
|
||||
"\fDataNodeInfo\x12\x0e\n" +
|
||||
"\x02id\x18\x01 \x01(\tR\x02id\x12D\n" +
|
||||
"\tdiskInfos\x18\x02 \x03(\v2&.master_pb.DataNodeInfo.DiskInfosEntryR\tdiskInfos\x12\x1b\n" +
|
||||
"\tgrpc_port\x18\x03 \x01(\rR\bgrpcPort\x12\x18\n" +
|
||||
"\tgrpc_port\x18\x03 \x01(\rR\bgrpcPort\x12*\n" +
|
||||
"\x11last_heartbeat_ns\x18\x05 \x01(\x03R\x0flastHeartbeatNs\x12\x18\n" +
|
||||
"\aaddress\x18\x04 \x01(\tR\aaddress\x1aQ\n" +
|
||||
"\x0eDiskInfosEntry\x12\x10\n" +
|
||||
"\x03key\x18\x01 \x01(\tR\x03key\x12)\n" +
|
||||
|
||||
@@ -345,6 +345,7 @@ func (ms *MasterServer) SendHeartbeat(stream master_pb.Seaweed_SendHeartbeatServ
|
||||
|
||||
glog.V(4).Infof("master received heartbeat %s", heartbeat.String())
|
||||
stats.MasterReceivedHeartbeatCounter.WithLabelValues("total").Inc()
|
||||
dn.SetLastHeartbeat(time.Now())
|
||||
|
||||
// Every heartbeat may carry the state, so a master that just took over
|
||||
// learns about a server already in maintenance from the first one.
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package weed_server
|
||||
|
||||
import (
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/topology"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type chanHeartbeatStream struct {
|
||||
grpc.ServerStream
|
||||
in chan *master_pb.Heartbeat
|
||||
}
|
||||
|
||||
func (s *chanHeartbeatStream) Recv() (*master_pb.Heartbeat, error) {
|
||||
hb, ok := <-s.in
|
||||
if !ok {
|
||||
return nil, io.EOF
|
||||
}
|
||||
return hb, nil
|
||||
}
|
||||
|
||||
func (s *chanHeartbeatStream) Send(*master_pb.HeartbeatResponse) error { return nil }
|
||||
|
||||
func reportedLastHeartbeatNs(ms *MasterServer) int64 {
|
||||
for _, dc := range ms.Topo.ToTopologyInfo(topology.VolumeFilter{}).DataCenterInfos {
|
||||
for _, rack := range dc.RackInfos {
|
||||
for _, dn := range rack.DataNodeInfos {
|
||||
return dn.LastHeartbeatNs
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func TestSendHeartbeatReportsEachHeartbeatTime(t *testing.T) {
|
||||
ms := newLeaderMaster()
|
||||
stream := &chanHeartbeatStream{in: make(chan *master_pb.Heartbeat)}
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
_ = ms.SendHeartbeat(stream)
|
||||
}()
|
||||
|
||||
before := time.Now().UnixNano()
|
||||
stream.in <- &master_pb.Heartbeat{Ip: "10.0.0.1", Port: 8080, HasNoVolumes: true}
|
||||
var first int64
|
||||
require.Eventually(t, func() bool {
|
||||
first = reportedLastHeartbeatNs(ms)
|
||||
return first >= before
|
||||
}, 5*time.Second, time.Millisecond, "first heartbeat time not reported")
|
||||
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
stream.in <- &master_pb.Heartbeat{HasNoVolumes: true}
|
||||
require.Eventually(t, func() bool {
|
||||
return reportedLastHeartbeatNs(ms) > first
|
||||
}, 5*time.Second, time.Millisecond, "a later heartbeat must move the reported time")
|
||||
|
||||
close(stream.in)
|
||||
<-done
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package topology
|
||||
import (
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
@@ -31,6 +32,9 @@ type DataNode struct {
|
||||
// lookupDigest covers the volumes reachable through this node in the volume
|
||||
// layouts, for comparison against what its disks actually hold.
|
||||
lookupDigest atomic.Uint64
|
||||
// lastHeartbeatNs is when the master last processed a heartbeat from the
|
||||
// node, unlike LastSeen which only moves when the node registers.
|
||||
lastHeartbeatNs atomic.Int64
|
||||
// duplicateVolumeIds records that the node last reported one volume id more
|
||||
// than once, which the master cannot represent.
|
||||
duplicateVolumeIds atomic.Bool
|
||||
@@ -423,6 +427,18 @@ func (dn *DataNode) ToInfo() (info DataNodeInfo) {
|
||||
return
|
||||
}
|
||||
|
||||
func (dn *DataNode) SetLastHeartbeat(t time.Time) {
|
||||
dn.lastHeartbeatNs.Store(t.UnixNano())
|
||||
}
|
||||
|
||||
func (dn *DataNode) LastHeartbeat() time.Time {
|
||||
ns := dn.lastHeartbeatNs.Load()
|
||||
if ns == 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
return time.Unix(0, ns)
|
||||
}
|
||||
|
||||
func (dn *DataNode) ToDataNodeInfo(filter VolumeFilter) *master_pb.DataNodeInfo {
|
||||
m := &master_pb.DataNodeInfo{
|
||||
Id: string(dn.Id()),
|
||||
@@ -432,6 +448,7 @@ func (dn *DataNode) ToDataNodeInfo(filter VolumeFilter) *master_pb.DataNodeInfo
|
||||
GrpcPort: uint32(dn.GrpcPort),
|
||||
Address: dn.Url(), // ip:port for connecting to the volume server
|
||||
}
|
||||
m.LastHeartbeatNs = dn.lastHeartbeatNs.Load()
|
||||
if m.DiskInfos == nil {
|
||||
m.DiskInfos = make(map[string]*master_pb.DiskInfo)
|
||||
}
|
||||
|
||||
@@ -288,6 +288,25 @@ func TestDataNodeToDataNodeInfo_IncludeEmptyDiskFromUsage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDataNodeToDataNodeInfoLastHeartbeat(t *testing.T) {
|
||||
dn := NewDataNode("node-1")
|
||||
if got := dn.ToDataNodeInfo(VolumeFilter{}).LastHeartbeatNs; got != 0 {
|
||||
t.Fatalf("LastHeartbeatNs before any heartbeat = %d, want 0", got)
|
||||
}
|
||||
if !dn.LastHeartbeat().IsZero() {
|
||||
t.Fatalf("LastHeartbeat before any heartbeat = %v, want zero", dn.LastHeartbeat())
|
||||
}
|
||||
|
||||
beat := time.Unix(1700000000, 123456789)
|
||||
dn.SetLastHeartbeat(beat)
|
||||
if got := dn.ToDataNodeInfo(VolumeFilter{}).LastHeartbeatNs; got != beat.UnixNano() {
|
||||
t.Fatalf("LastHeartbeatNs = %d, want %d", got, beat.UnixNano())
|
||||
}
|
||||
if !dn.LastHeartbeat().Equal(beat) {
|
||||
t.Fatalf("LastHeartbeat = %v, want %v", dn.LastHeartbeat(), beat)
|
||||
}
|
||||
}
|
||||
|
||||
func assert(t *testing.T, message string, actual, expected int) {
|
||||
if actual != expected {
|
||||
t.Fatalf("unexpected %s: %d, expected: %d", message, actual, expected)
|
||||
|
||||
Reference in new issue
Block a user