-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements compute node heartbeats to requester node(s)
Adds pkg/node/heartbeat which contains a client and a server for sending heartbeat messages over NATS PubSub.
- Loading branch information
Showing
9 changed files
with
208 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package heartbeat | ||
|
||
import ( | ||
"context" | ||
|
||
natsPubSub "github.com/bacalhau-project/bacalhau/pkg/nats/pubsub" | ||
"github.com/bacalhau-project/bacalhau/pkg/pubsub" | ||
"github.com/rs/zerolog/log" | ||
|
||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
type HeartbeatClient struct { | ||
publisher *natsPubSub.PubSub[Heartbeat] | ||
nodeID string | ||
} | ||
|
||
func NewClient(conn *nats.Conn, nodeID string) (*HeartbeatClient, error) { | ||
subParams := natsPubSub.PubSubParams{ | ||
Subject: heartbeatTopic, | ||
Conn: conn, | ||
} | ||
|
||
publisher, err := natsPubSub.NewPubSub[Heartbeat](subParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &HeartbeatClient{publisher: publisher, nodeID: nodeID}, nil | ||
} | ||
|
||
func (h *HeartbeatClient) Start(ctx context.Context) error { | ||
// Waits until we are cancelled and then closes the publisher | ||
<-ctx.Done() | ||
return h.publisher.Close(ctx) | ||
} | ||
|
||
func (h *HeartbeatClient) SendHeartbeat(ctx context.Context, sequence uint64) error { | ||
log.Ctx(ctx).Trace().Msgf("sending heartbeat seq: %d", sequence) | ||
return h.Publish(ctx, Heartbeat{NodeID: h.nodeID, Sequence: sequence}) | ||
} | ||
|
||
func (h *HeartbeatClient) Publish(ctx context.Context, message Heartbeat) error { | ||
return h.publisher.Publish(ctx, message) | ||
} | ||
|
||
var _ pubsub.Publisher[Heartbeat] = (*HeartbeatClient)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package heartbeat | ||
|
||
import ( | ||
"context" | ||
|
||
natsPubSub "github.com/bacalhau-project/bacalhau/pkg/nats/pubsub" | ||
"github.com/bacalhau-project/bacalhau/pkg/pubsub" | ||
|
||
"github.com/nats-io/nats.go" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type HeartbeatServer struct { | ||
subscription *natsPubSub.PubSub[Heartbeat] | ||
} | ||
|
||
func NewServer(conn *nats.Conn) (*HeartbeatServer, error) { | ||
subParams := natsPubSub.PubSubParams{ | ||
Subject: heartbeatTopic, | ||
Conn: conn, | ||
} | ||
|
||
subscription, err := natsPubSub.NewPubSub[Heartbeat](subParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &HeartbeatServer{subscription: subscription}, nil | ||
} | ||
|
||
func (h *HeartbeatServer) Start(ctx context.Context) error { | ||
if err := h.subscription.Subscribe(ctx, h); err != nil { | ||
return err | ||
} | ||
|
||
go func(ctx context.Context) { | ||
log.Ctx(ctx).Info().Msg("Heartbeat server started") | ||
<-ctx.Done() | ||
_ = h.subscription.Close(ctx) | ||
log.Ctx(ctx).Info().Msg("Heartbeat server shutdown") | ||
}(ctx) | ||
|
||
return nil | ||
} | ||
|
||
func (h *HeartbeatServer) Handle(ctx context.Context, message Heartbeat) error { | ||
log.Ctx(ctx).Trace().Msgf("heartbeat received from %s", message.NodeID) | ||
|
||
// TODO: Process the heartbeat (e.g. update the node's last seen time) | ||
|
||
return nil | ||
} | ||
|
||
var _ pubsub.Subscriber[Heartbeat] = (*HeartbeatServer)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package heartbeat | ||
|
||
const heartbeatTopic = "heartbeat" | ||
|
||
// Heartbeat represents a heartbeat message from a specific node. | ||
// It contains the node ID and the sequence number of the heartbeat | ||
// which is monotonically increasing (reboots aside). We do not | ||
// use timestamps on the client, we rely solely on the server-side | ||
// time to avoid clock drift issues. | ||
type Heartbeat struct { | ||
NodeID string | ||
Sequence uint64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.