Skip to content

Commit

Permalink
Use a single implementation of GenerateUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Sep 7, 2015
1 parent 9c4285c commit 8a02dbc
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 146 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (c *Client) setupNode() error {
node.Resources = &structs.Resources{}
}
if node.ID == "" {
node.ID = generateUUID()
node.ID = structs.GenerateUUID()
}
if node.Datacenter == "" {
node.Datacenter = "dc1"
Expand Down
16 changes: 0 additions & 16 deletions client/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
crand "crypto/rand"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -75,21 +74,6 @@ func diffAllocs(existing, updated []*structs.Allocation) *diffResult {
return result
}

// generateUUID is used to generate a random UUID
func generateUUID() string {
buf := make([]byte, 16)
if _, err := crand.Read(buf); err != nil {
panic(fmt.Errorf("failed to read random bytes: %v", err))
}

return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
buf[0:4],
buf[4:6],
buf[6:8],
buf[8:10],
buf[10:16])
}

// Returns a random stagger interval between 0 and the duration
func randomStagger(intv time.Duration) time.Duration {
return time.Duration(uint64(rand.Int63()) % uint64(intv))
Expand Down
19 changes: 1 addition & 18 deletions client/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"os"
"reflect"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -50,22 +49,6 @@ func TestDiffAllocs(t *testing.T) {
}
}

func TestGenerateUUID(t *testing.T) {
prev := generateUUID()
for i := 0; i < 100; i++ {
id := generateUUID()
if prev == id {
t.Fatalf("Should get a new ID!")
}

matched, err := regexp.MatchString(
"[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}", id)
if !matched || err != nil {
t.Fatalf("expected match %s %v %s", id, matched, err)
}
}
}

func TestRandomStagger(t *testing.T) {
intv := time.Minute
for i := 0; i < 10; i++ {
Expand All @@ -80,7 +63,7 @@ func TestShuffleStrings(t *testing.T) {
// Generate input
inp := make([]string, 10)
for idx := range inp {
inp[idx] = generateUUID()
inp[idx] = structs.GenerateUUID()
}

// Copy the input
Expand Down
2 changes: 1 addition & 1 deletion nomad/eval_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (b *EvalBroker) dequeueForSched(sched string) (*structs.Evaluation, string,
eval := raw.(*structs.Evaluation)

// Generate a UUID for the token
token := generateUUID()
token := structs.GenerateUUID()

// Setup Nack timer
nackTimer := time.AfterFunc(b.nackTimeout, func() {
Expand Down
2 changes: 1 addition & 1 deletion nomad/eval_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestEvalEndpoint_GetEval(t *testing.T) {
}

// Lookup non-existing node
get.EvalID = generateUUID()
get.EvalID = structs.GenerateUUID()
if err := msgpackrpc.CallWithCodec(codec, "Eval.GetEval", get, &resp); err != nil {
t.Fatalf("err: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions nomad/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (j *Job) Register(args *structs.JobRegisterRequest, reply *structs.JobRegis

// Create a new evaluation
eval := &structs.Evaluation{
ID: generateUUID(),
ID: structs.GenerateUUID(),
Priority: args.Job.Priority,
Type: args.Job.Type,
TriggeredBy: structs.EvalTriggerJobRegister,
Expand Down Expand Up @@ -110,7 +110,7 @@ func (j *Job) Evaluate(args *structs.JobEvaluateRequest, reply *structs.JobRegis

// Create a new evaluation
eval := &structs.Evaluation{
ID: generateUUID(),
ID: structs.GenerateUUID(),
Priority: job.Priority,
Type: job.Type,
TriggeredBy: structs.EvalTriggerJobRegister,
Expand Down Expand Up @@ -157,7 +157,7 @@ func (j *Job) Deregister(args *structs.JobDeregisterRequest, reply *structs.JobD
// priority even if the job was. The scheduler itself also doesn't matter,
// since all should be able to handle deregistration in the same way.
eval := &structs.Evaluation{
ID: generateUUID(),
ID: structs.GenerateUUID(),
Priority: structs.JobDefaultPriority,
Type: structs.JobTypeService,
TriggeredBy: structs.EvalTriggerJobDeregister,
Expand Down
2 changes: 1 addition & 1 deletion nomad/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (s *Server) schedulePeriodic(stopCh chan struct{}) {
// coreJobEval returns an evaluation for a core job
func (s *Server) coreJobEval(job string) *structs.Evaluation {
return &structs.Evaluation{
ID: generateUUID(),
ID: structs.GenerateUUID(),
Priority: structs.CoreJobPriority,
Type: structs.JobTypeCore,
TriggeredBy: structs.EvalTriggerScheduled,
Expand Down
33 changes: 7 additions & 26 deletions nomad/mock/mock.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
package mock

import (
crand "crypto/rand"
"fmt"

"github.com/hashicorp/nomad/nomad/structs"
)

func GenerateUUID() string {
buf := make([]byte, 16)
if _, err := crand.Read(buf); err != nil {
panic(fmt.Errorf("failed to read random bytes: %v", err))
}

return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
buf[0:4],
buf[4:6],
buf[6:8],
buf[8:10],
buf[10:16])
}
import "github.com/hashicorp/nomad/nomad/structs"

func Node() *structs.Node {
node := &structs.Node{
ID: GenerateUUID(),
ID: structs.GenerateUUID(),
Datacenter: "dc1",
Name: "foobar",
Attributes: map[string]string{
Expand Down Expand Up @@ -65,7 +46,7 @@ func Node() *structs.Node {

func Job() *structs.Job {
job := &structs.Job{
ID: GenerateUUID(),
ID: structs.GenerateUUID(),
Name: "my-job",
Type: structs.JobTypeService,
Priority: 50,
Expand Down Expand Up @@ -116,19 +97,19 @@ func Job() *structs.Job {

func Eval() *structs.Evaluation {
eval := &structs.Evaluation{
ID: GenerateUUID(),
ID: structs.GenerateUUID(),
Priority: 50,
Type: structs.JobTypeService,
JobID: GenerateUUID(),
JobID: structs.GenerateUUID(),
Status: structs.EvalStatusPending,
}
return eval
}

func Alloc() *structs.Allocation {
alloc := &structs.Allocation{
ID: GenerateUUID(),
EvalID: GenerateUUID(),
ID: structs.GenerateUUID(),
EvalID: structs.GenerateUUID(),
NodeID: "foo",
TaskGroup: "web",
Resources: &structs.Resources{
Expand Down
2 changes: 1 addition & 1 deletion nomad/node_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func (n *Node) createNodeEvals(nodeID string, nodeIndex uint64) ([]string, uint6

// Create a new eval
eval := &structs.Evaluation{
ID: generateUUID(),
ID: structs.GenerateUUID(),
Priority: alloc.Job.Priority,
Type: alloc.Job.Type,
TriggeredBy: structs.EvalTriggerNodeUpdate,
Expand Down
16 changes: 0 additions & 16 deletions nomad/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nomad

import (
crand "crypto/rand"
"fmt"
"math/rand"
"net"
Expand Down Expand Up @@ -98,21 +97,6 @@ func isNomadServer(m serf.Member) (bool, *serverParts) {
return true, parts
}

// generateUUID is used to generate a random UUID
func generateUUID() string {
buf := make([]byte, 16)
if _, err := crand.Read(buf); err != nil {
panic(fmt.Errorf("failed to read random bytes: %v", err))
}

return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
buf[0:4],
buf[4:6],
buf[6:8],
buf[8:10],
buf[10:16])
}

// Returns a random stagger interval between 0 and the duration
func randomStagger(intv time.Duration) time.Duration {
return time.Duration(uint64(rand.Int63()) % uint64(intv))
Expand Down
20 changes: 2 additions & 18 deletions nomad/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package nomad
import (
"net"
"reflect"
"regexp"
"testing"
"time"

"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/serf/serf"
)

Expand Down Expand Up @@ -57,22 +57,6 @@ func TestIsNomadServer(t *testing.T) {
}
}

func TestGenerateUUID(t *testing.T) {
prev := generateUUID()
for i := 0; i < 100; i++ {
id := generateUUID()
if prev == id {
t.Fatalf("Should get a new ID!")
}

matched, err := regexp.MatchString(
"[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}", id)
if !matched || err != nil {
t.Fatalf("expected match %s %v %s", id, matched, err)
}
}
}

func TestRandomStagger(t *testing.T) {
intv := time.Minute
for i := 0; i < 10; i++ {
Expand All @@ -87,7 +71,7 @@ func TestShuffleStrings(t *testing.T) {
// Generate input
inp := make([]string, 10)
for idx := range inp {
inp[idx] = generateUUID()
inp[idx] = structs.GenerateUUID()
}

// Copy the input
Expand Down
2 changes: 1 addition & 1 deletion nomad/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func TestWorker_invokeScheduler(t *testing.T) {
eval := mock.Eval()
eval.Type = "noop"

err := w.invokeScheduler(eval, generateUUID())
err := w.invokeScheduler(eval, structs.GenerateUUID())
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down
17 changes: 8 additions & 9 deletions scheduler/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"testing"

"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/state"
"github.com/hashicorp/nomad/nomad/structs"
)
Expand All @@ -32,7 +31,7 @@ func TestEvalContext_ProposedAlloc(t *testing.T) {
&RankedNode{
Node: &structs.Node{
// Perfect fit
ID: mock.GenerateUUID(),
ID: structs.GenerateUUID(),
Resources: &structs.Resources{
CPU: 2048,
MemoryMB: 2048,
Expand All @@ -42,7 +41,7 @@ func TestEvalContext_ProposedAlloc(t *testing.T) {
&RankedNode{
Node: &structs.Node{
// Perfect fit
ID: mock.GenerateUUID(),
ID: structs.GenerateUUID(),
Resources: &structs.Resources{
CPU: 2048,
MemoryMB: 2048,
Expand All @@ -53,21 +52,21 @@ func TestEvalContext_ProposedAlloc(t *testing.T) {

// Add existing allocations
alloc1 := &structs.Allocation{
ID: mock.GenerateUUID(),
EvalID: mock.GenerateUUID(),
ID: structs.GenerateUUID(),
EvalID: structs.GenerateUUID(),
NodeID: nodes[0].Node.ID,
JobID: mock.GenerateUUID(),
JobID: structs.GenerateUUID(),
Resources: &structs.Resources{
CPU: 2048,
MemoryMB: 2048,
},
DesiredStatus: structs.AllocDesiredStatusRun,
}
alloc2 := &structs.Allocation{
ID: mock.GenerateUUID(),
EvalID: mock.GenerateUUID(),
ID: structs.GenerateUUID(),
EvalID: structs.GenerateUUID(),
NodeID: nodes[1].Node.ID,
JobID: mock.GenerateUUID(),
JobID: structs.GenerateUUID(),
Resources: &structs.Resources{
CPU: 1024,
MemoryMB: 1024,
Expand Down
3 changes: 1 addition & 2 deletions scheduler/generic_sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"

"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
)

Expand Down Expand Up @@ -376,7 +375,7 @@ func (s *GenericScheduler) computePlacements(place []allocTuple) error {

// Create an allocation for this
alloc := &structs.Allocation{
ID: mock.GenerateUUID(),
ID: structs.GenerateUUID(),
EvalID: s.eval.ID,
Name: missing.Name,
NodeID: nodeID,
Expand Down
Loading

0 comments on commit 8a02dbc

Please sign in to comment.