Skip to content

Commit

Permalink
gossip: add gossip interface
Browse files Browse the repository at this point in the history
  • Loading branch information
andydunstall committed Apr 6, 2024
1 parent 92dc0a6 commit 4e4ed7f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cli/server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/andydunstall/pico/pkg/log"
"github.com/andydunstall/pico/server"
"github.com/andydunstall/pico/server/config"
"github.com/andydunstall/pico/server/gossip"
"github.com/andydunstall/pico/server/netmap"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cobra"
"go.uber.org/zap"
Expand Down Expand Up @@ -183,6 +185,11 @@ func run(conf *config.Config, logger *log.Logger) {
logger,
)

netmap := netmap.NewNetworkMap()
// TODO(andydunstall): Should wait for gossip to join and sync before
// the server becomes ready.
gossip := gossip.NewGossip(netmap, logger)

ctx, cancel := signal.NotifyContext(
context.Background(), syscall.SIGINT, syscall.SIGTERM,
)
Expand Down Expand Up @@ -211,6 +218,12 @@ func run(conf *config.Config, logger *log.Logger) {
}
return nil
})
g.Go(func() error {
if err := gossip.Run(ctx); err != nil {
return fmt.Errorf("gossip: %w", err)
}
return nil
})

if err := g.Wait(); err != nil {
logger.Error("failed to run server", zap.Error(err))
Expand Down
29 changes: 29 additions & 0 deletions server/gossip/gossip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gossip

import (
"context"

"github.com/andydunstall/pico/pkg/log"
"github.com/andydunstall/pico/server/netmap"
)

// Gossip is responsible for maintaining the nodes local NetworkMap and
// propagating the state of the local node to the rest of the cluster.
type Gossip struct {
netmap *netmap.NetworkMap
logger *log.Logger
}

// NewGossip initializes gossip to maintain the given network map.
func NewGossip(netmap *netmap.NetworkMap, logger *log.Logger) *Gossip {
return &Gossip{
netmap: netmap,
logger: logger.WithSubsystem("gossip"),
}
}

// Run gossips with the other nodes in the cluster until cancelled.
func (g *Gossip) Run(_ context.Context) error {
g.logger.Info("starting gossip")
return nil
}
13 changes: 13 additions & 0 deletions server/netmap/netmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package netmap

// NetworkMap represents the known state of the cluster as seen by the local
// node.
//
// This map is eventually consistent. The state is propagated among the nodes
// in the cluster using gossip.
type NetworkMap struct {
}

func NewNetworkMap() *NetworkMap {
return &NetworkMap{}
}
17 changes: 17 additions & 0 deletions server/netmap/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package netmap

// Node represents the known state about a node in the cluster.
type Node struct {
// ID is a unique identifier for the node in the cluster.
ID string

// HTTPAddr is the advertised HTTP address
HTTPAddr string

// GossipAddr is the advertised gossip address.
GossipAddr string

// Endpoints contains the active endpoints on the node. This maps the
// active endpoint ID to the number of listeners for that endpoint.
Endpoints map[string]int
}

0 comments on commit 4e4ed7f

Please sign in to comment.