Skip to content

Commit

Permalink
Add non-voting nodes to cluster
Browse files Browse the repository at this point in the history
This allows users to add non-voting nodes to cluster at cluster start or
join time by setting a boolean flag.

Signed-off-by: Alexander Scheel <[email protected]>
  • Loading branch information
cipherboy committed Nov 23, 2024
1 parent 6d78ab5 commit 45ed244
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
15 changes: 13 additions & 2 deletions cmd/devbao/cluster_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ func BuildClusterJoinCommand() *cli.Command {
Usage: "join a given node to the cluster",

Action: RunClusterJoinCommand,
}

c.Flags = append(c.Flags, ClusterFlags()...)
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "non-voter",
Aliases: []string{"nv"},
Value: false,
Usage: "mark new node as a non-voter",
},
},
}

return c
}
Expand All @@ -31,6 +38,8 @@ func RunClusterJoinCommand(cCtx *cli.Context) error {
clusterName := cCtx.Args().First()
nodeName := cCtx.Args().Get(1)

nonVoter := cCtx.Bool("non-voter")

cluster, err := bao.LoadCluster(clusterName)
if err != nil {
return fmt.Errorf("error loading cluster: %w", err)
Expand All @@ -45,6 +54,8 @@ func RunClusterJoinCommand(cCtx *cli.Context) error {
return fmt.Errorf("refusing to add node already in a cluster (`%v`)", node.Cluster)
}

node.NonVoter = nonVoter

if err := cluster.JoinNodeHACluster(node); err != nil {
return fmt.Errorf("failed to join node to cluster: %w", err)
}
Expand Down
30 changes: 27 additions & 3 deletions cmd/devbao/cluster_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func ClusterInfoFlags() []cli.Flag {
Value: 3,
Usage: "number of nodes to run in the cluster; suggested to use an odd number.",
},
&cli.IntFlag{
Name: "non-voter",
Value: 0,
Usage: "number of non-voter nodes to run in the cluster; less than count",
},
&cli.StringFlag{
Name: "listen",
Value: "0.0.0.0",
Expand Down Expand Up @@ -85,11 +90,16 @@ func RunClusterStartCommand(cCtx *cli.Context) error {

listen := cCtx.String("listen")

nonVoter := cCtx.Int("non-voter")
if nonVoter < 0 {
return fmt.Errorf("expected at least zero non-voter nodes; got %v", nonVoter)
}

count := cCtx.Int("count")
if count < 1 {
return fmt.Errorf("required to have at least one node in the cluster; got %v", count)
} else if (count % 2) == 0 {
fmt.Fprintf(os.Stderr, "[warning] it is suggested to have an odd number of nodes in the HA cluster")
} else if ((count - nonVoter) % 2) == 0 {
fmt.Fprintf(os.Stderr, "[warning] it is suggested to have an odd number of voting nodes in the HA cluster")
}

clusterName := cCtx.Args().First()
Expand All @@ -110,7 +120,14 @@ func RunClusterStartCommand(cCtx *cli.Context) error {
// Build nodes
var nodes []*bao.Node
for index := 0; index < count; index++ {
name := fmt.Sprintf("%v-node-%d", clusterName, index)
nonVoter := index >= (count - nonVoter)

nvSuffix := ""
if nonVoter {
nvSuffix = "-nv"
}

name := fmt.Sprintf("%v-node-%d%v", clusterName, index, nvSuffix)
fmt.Printf("starting %v...\n", name)

port := portBase + index*100
Expand Down Expand Up @@ -159,6 +176,13 @@ func RunClusterStartCommand(cCtx *cli.Context) error {
return fmt.Errorf("failed to build node %v: %w", name, err)
}

node.NonVoter = nonVoter
if nonVoter {
if err := node.SaveConfig(); err != nil {
return fmt.Errorf("failed to save non-voter config for node %v: %w", name, err)
}
}

if err := node.Start(); err != nil {
return fmt.Errorf("failed to start node %v: %w", name, err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/bao/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ func (c *Cluster) JoinNodeHACluster(node *Node) error {
resp, err := nodeClient.Sys().RaftJoin(&api.RaftJoinRequest{
LeaderAPIAddr: leaderClient.Address(),
Retry: true,
NonVoter: node.NonVoter,
})
if err != nil {
return fmt.Errorf("failed joining node %v to cluster %v / leader %v: %w", node.Name, c.Name, leaderNode.Name, err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/bao/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type Node struct {
Token string `json:"token"`
UnsealKeys []string `json:"unseal_keys,omitempty"`

Cluster string `json:"cluster,omitempty"`
Cluster string `json:"cluster,omitempty"`
NonVoter bool `json:"non_voter"`
}

func (n *Node) FromInterface(iface map[string]interface{}) error {
Expand Down

0 comments on commit 45ed244

Please sign in to comment.