Skip to content

Commit

Permalink
server: Rename functions and use iterator function for clarity (hashi…
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanadelacuesta authored Oct 11, 2023
1 parent 635afee commit 70b020e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
3 changes: 1 addition & 2 deletions nomad/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,10 @@ func (s *Server) establishLeadership(stopCh chan struct{}) error {
schedulerConfig := s.getOrCreateSchedulerConfig()

// Initialize the Cluster metadata
clusterMetadata, err := s.ClusterMetaData()
clusterMetadata, err := s.ClusterMetadata()
if err != nil {
return err
}
// todo: use cluster ID for stuff, later!

// Enable the plan queue, since we are now the leader
s.planQueue.SetEnabled(true)
Expand Down
8 changes: 4 additions & 4 deletions nomad/leader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ func TestLeader_ClusterID(t *testing.T) {
defer cleanupS1()
testutil.WaitForLeader(t, s1.RPC)

clusterMD, err := s1.ClusterMetaData()
clusterMD, err := s1.ClusterMetadata()

require.NoError(t, err)
require.True(t, helper.IsUUID(clusterMD.ClusterID))
Expand Down Expand Up @@ -915,15 +915,15 @@ func agreeClusterID(t *testing.T, servers []*Server) {
must.Len(t, 3, servers)

f := func() error {
id1, err1 := servers[0].ClusterMetaData()
id1, err1 := servers[0].ClusterMetadata()
if err1 != nil {
return err1
}
id2, err2 := servers[1].ClusterMetaData()
id2, err2 := servers[1].ClusterMetadata()
if err2 != nil {
return err2
}
id3, err3 := servers[2].ClusterMetaData()
id3, err3 := servers[2].ClusterMetadata()
if err3 != nil {
return err3
}
Expand Down
2 changes: 1 addition & 1 deletion nomad/node_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,7 @@ func (n *Node) DeriveSIToken(args *structs.DeriveSITokenRequest, reply *structs.
}

// Get the ClusterID
clusterMD, err := n.srv.ClusterMetaData()
clusterMD, err := n.srv.ClusterMetadata()
if err != nil {
setError(err, false)
return nil
Expand Down
18 changes: 5 additions & 13 deletions nomad/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/helper/codec"
"github.com/hashicorp/nomad/helper/goruntime"
"github.com/hashicorp/nomad/helper/iterator"
"github.com/hashicorp/nomad/helper/pool"
"github.com/hashicorp/nomad/helper/tlsutil"
"github.com/hashicorp/nomad/lib/auth/oidc"
Expand Down Expand Up @@ -2059,16 +2060,16 @@ func (s *Server) ReplicationToken() string {
return s.config.ReplicationToken
}

// ClusterMetaData returns the unique ID for this cluster composed of a
// uuid and a timestamp.
// ClusterMetadata returns the metadata for this cluster composed of a
// UUID and a timestamp.
//
// Any Nomad server agent may call this method to get at the ID.
// If we are the leader and the ID has not yet been created, it will
// be created now. Otherwise an error is returned.
//
// The ID will not be created until all participating servers have reached
// a minimum version (0.10.4).
func (s *Server) ClusterMetaData() (structs.ClusterMetadata, error) {
func (s *Server) ClusterMetadata() (structs.ClusterMetadata, error) {
s.clusterIDLock.Lock()
defer s.clusterIDLock.Unlock()

Expand Down Expand Up @@ -2109,21 +2110,12 @@ func (s *Server) GetClientNodesCount() (int, error) {
return 0, err
}

var count int
iter, err := stateSnapshot.Nodes(nil)
if err != nil {
return 0, err
}

for {
raw := iter.Next()
if raw == nil {
break
}
count++
}

return count, nil
return iterator.Len(iter), nil
}

// peersInfoContent is used to help operators understand what happened to the
Expand Down

0 comments on commit 70b020e

Please sign in to comment.