Skip to content
This repository was archived by the owner on Jan 26, 2023. It is now read-only.

Commit a2eb24f

Browse files
control: Improve node classification (#37)
- Make classification in `NodeIsNew()` less overfit. - Return a clear error when the scaling values in Nomad and Consul are mismatched Resolves #36 Resolves #11
1 parent 0db713b commit a2eb24f

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

cmd/attache-control/main.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,14 @@ func (l *leader) joinOrCreateRedisCluster() error {
153153
return nil
154154
}
155155

156-
// This should never happen as long as the job and scaling opts match.
156+
clusterNodesCount := len(primaryNodesInCluster) + len(replicaNodesInCluster)
157+
if l.scalingOpts.NodesMissing(clusterNodesCount) == 0 {
158+
// This will only happen when the Nomad job is scaled without a
159+
// corresponding change to the scaling opts in Consul.
160+
return fmt.Errorf("%s Nomad group count was scaled without a corresponding change to scaling opts", l.RedisOpts.NodeAddr)
161+
}
162+
163+
// This should never happen.
157164
return fmt.Errorf("%s couldn't be added to an existing cluster", l.RedisOpts.NodeAddr)
158165
}
159166

src/redis/client/client.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ type Client struct {
2323
Client *redis.Client
2424
}
2525

26+
// IsNew returns 'true' if the contents of 'CLUSTER INFO' match those expected
27+
// of a node that has never been joined a cluster. 'cluster_state' will be
28+
// 'fail' as a minumum of 3 nodes is required. 'cluster_known_nodes' will be '1'
29+
// (self) since it's never been introduced to other nodes. 'cluster_slots_count'
30+
// and cluster_size' will both be '0' since slots are only assigned as part of
31+
// the inital clustering or during cluster rebalancing. If any of these fields
32+
// holds a different value, 'false' is returned.
2633
func (h *Client) IsNew() (bool, error) {
27-
var infoMatchingNewNodes = clusterInfo{"fail", 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}
28-
clusterInfo, err := h.GetClusterInfo()
34+
c, err := h.GetClusterInfo()
2935
if err != nil {
3036
return false, err
31-
} else if *clusterInfo == infoMatchingNewNodes {
37+
}
38+
39+
if c.State == "fail" && c.SlotsAssigned == 0 && c.KnownNodes == 1 && c.Size == 0 {
3240
return true, nil
3341
} else {
3442
return false, nil

0 commit comments

Comments
 (0)