Skip to content

Commit

Permalink
switch to using UUIDs directly
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Mar 16, 2017
1 parent c19dad6 commit 21f5957
Show file tree
Hide file tree
Showing 35 changed files with 143 additions and 2,699 deletions.
2 changes: 1 addition & 1 deletion LICENSE_OF_DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Dependencies
* github.com/dgrijalva/jwt-go [MIT](https://github.com/dgrijalva/jwt-go/blob/master/LICENSE)
* github.com/dustin/go-humanize [MIT](https://github.com/dustin/go-humanize/blob/master/LICENSE)
* github.com/golang/protobuf [BSD](https://github.com/golang/protobuf/blob/master/LICENSE)
* github.com/google/uuid [BSD](https://github.com/google/uuid/blob/master/LICENSE)
* github.com/gorhill/cronexpr [APLv2](https://github.com/gorhill/cronexpr/blob/master/APLv2)
* github.com/k-sone/snmpgo [MIT](https://github.com/k-sone/snmpgo/blob/master/LICENSE)
* github.com/kimor79/gollectd [BSD](https://github.com/kimor79/gollectd/blob/master/LICENSE)
Expand All @@ -20,5 +21,4 @@ Dependencies
* github.com/shurcooL/markdownfmt [MIT](https://github.com/shurcooL/markdownfmt/blob/master/README.md)
* github.com/shurcooL/sanitized\_anchor\_name [MIT](https://github.com/shurcooL/sanitized_anchor_name/blob/master/LICENSE)
* github.com/stretchr/testify [MIT](https://github.com/stretchr/testify/blob/master/LICENSE)
* github.com/twinj/uuid [MIT](https://github.com/twinj/uuid/blob/master/LICENSE)
* gopkg.in/gomail.v2 [MIT](https://github.com/go-gomail/gomail/blob/v2/LICENSE)
34 changes: 34 additions & 0 deletions expvar/expvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strconv"
"sync"
"sync/atomic"

"github.com/influxdata/kapacitor/uuid"
)

type IntVar interface {
Expand Down Expand Up @@ -290,3 +292,35 @@ func (v *String) StringValue() string {
defer v.mu.RUnlock()
return v.s
}

// UUID is a string variable that contain an UUID and satisfies the expvar.Var interface.
type UUID struct {
mu sync.RWMutex
id uuid.UUID
s string
}

func (v *UUID) String() string {
v.mu.RLock()
defer v.mu.RUnlock()
return strconv.Quote(v.s)
}

func (v *UUID) Set(value uuid.UUID) {
v.mu.Lock()
defer v.mu.Unlock()
v.id = value
v.s = value.String()
}

func (v *UUID) StringValue() string {
v.mu.RLock()
defer v.mu.RUnlock()
return v.s
}

func (v *UUID) UUIDValue() uuid.UUID {
v.mu.RLock()
defer v.mu.RUnlock()
return v.id
}
46 changes: 31 additions & 15 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"sync"

"github.com/influxdata/influxdb/influxql"
"github.com/influxdata/influxdb/services/collectd"
Expand Down Expand Up @@ -48,9 +48,9 @@ import (
"github.com/influxdata/kapacitor/services/udf"
"github.com/influxdata/kapacitor/services/udp"
"github.com/influxdata/kapacitor/services/victorops"
"github.com/influxdata/kapacitor/uuid"
"github.com/influxdata/kapacitor/vars"
"github.com/pkg/errors"
"github.com/twinj/uuid"
)

const clusterIDFilename = "cluster.id"
Expand Down Expand Up @@ -103,9 +103,10 @@ type Server struct {
// Channel of incoming configuration updates.
configUpdates chan config.ConfigUpdate

BuildInfo BuildInfo
ClusterID string
ServerID string
BuildInfo BuildInfo
clusterIDMu sync.Mutex
ClusterID uuid.UUID
ServerID uuid.UUID

// Profiling
CPUProfile string
Expand Down Expand Up @@ -747,8 +748,8 @@ func (s *Server) setupIDs() error {
if err != nil && !os.IsNotExist(err) {
return err
}
if clusterID == "" {
clusterID = uuid.NewV4().String()
if clusterID == uuid.Nil {
clusterID = uuid.New()
if err := s.writeID(clusterIDPath, clusterID); err != nil {
return errors.Wrap(err, "failed to save cluster ID")
}
Expand All @@ -760,8 +761,8 @@ func (s *Server) setupIDs() error {
if err != nil && !os.IsNotExist(err) {
return err
}
if serverID == "" {
serverID = uuid.NewV4().String()
if serverID == uuid.Nil {
serverID = uuid.New()
if err := s.writeID(serverIDPath, serverID); err != nil {
return errors.Wrap(err, "failed to save server ID")
}
Expand All @@ -771,32 +772,47 @@ func (s *Server) setupIDs() error {
return nil
}

func (s *Server) readID(file string) (string, error) {
func (s *Server) readID(file string) (uuid.UUID, error) {
f, err := os.Open(file)
if err != nil {
return "", err
return uuid.Nil, err
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return "", err
return uuid.Nil, err
}
return strings.TrimSpace(string(b)), nil
return uuid.ParseBytes(b)
}

func (s *Server) writeID(file, id string) error {
func (s *Server) writeID(file string, id uuid.UUID) error {
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write([]byte(id))
_, err = f.Write([]byte(id.String()))
if err != nil {
return err
}
return nil
}

func (s *Server) SetClusterID(clusterID uuid.UUID) error {
s.clusterIDMu.Lock()
defer s.clusterIDMu.Unlock()
if s.ClusterID == clusterID {
return nil
}
clusterIDPath := filepath.Join(s.dataDir, clusterIDFilename)
if err := s.writeID(clusterIDPath, clusterID); err != nil {
return errors.Wrap(err, "failed to save cluster ID")
}
s.ClusterID = clusterID
vars.ClusterIDVar.Set(s.ClusterID)
return nil
}

// Service represents a service attached to the server.
type Service interface {
Open() error
Expand Down
9 changes: 6 additions & 3 deletions services/influxdb/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ import (
influxcli "github.com/influxdata/kapacitor/influxdb"
"github.com/influxdata/kapacitor/services/httpd"
"github.com/influxdata/kapacitor/services/influxdb"
"github.com/influxdata/kapacitor/uuid"
"github.com/influxdata/kapacitor/vars"
)

var ls = logSerivce{}

const (
testKapacitorClusterID = "test-kclusterid"
testSubName = "kapacitor-" + testKapacitorClusterID

randomTokenData = "test random data that is 64 bytes long xxxxxxxxxxxxxxxxxxxxxxxxx"
testClusterName = "testcluster0"
randomToken = testClusterName + ";" + randomTokenData
Expand All @@ -36,6 +34,11 @@ const (
tokenSize = 64
)

var (
testKapacitorClusterID = uuid.New()
testSubName = "kapacitor-" + testKapacitorClusterID.String()
)

func init() {
vars.ClusterIDVar.Set(testKapacitorClusterID)
if len(randomTokenData) != tokenSize {
Expand Down
14 changes: 7 additions & 7 deletions services/replay/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/services/httpd"
"github.com/influxdata/kapacitor/services/storage"
"github.com/influxdata/kapacitor/uuid"
"github.com/pkg/errors"
"github.com/twinj/uuid"
)

const streamEXT = ".srpl"
Expand Down Expand Up @@ -581,7 +581,7 @@ func (s *Service) handleRecordStream(w http.ResponseWriter, r *http.Request) {
return
}
if opt.ID == "" {
opt.ID = uuid.NewV4().String()
opt.ID = uuid.New().String()
}
if !validID.MatchString(opt.ID) {
httpd.HttpError(w, fmt.Sprintf("recording ID must contain only letters, numbers, '-', '.' and '_'. %q", opt.ID), true, http.StatusBadRequest)
Expand Down Expand Up @@ -627,7 +627,7 @@ func (s *Service) handleRecordBatch(w http.ResponseWriter, req *http.Request) {
return
}
if opt.ID == "" {
opt.ID = uuid.NewV4().String()
opt.ID = uuid.New().String()
}
if !validID.MatchString(opt.ID) {
httpd.HttpError(w, fmt.Sprintf("recording ID must contain only letters, numbers, '-', '.' and '_'. %q", opt.ID), true, http.StatusBadRequest)
Expand Down Expand Up @@ -681,7 +681,7 @@ func (s *Service) handleRecordQuery(w http.ResponseWriter, req *http.Request) {
return
}
if opt.ID == "" {
opt.ID = uuid.NewV4().String()
opt.ID = uuid.New().String()
}
if !validID.MatchString(opt.ID) {
httpd.HttpError(w, fmt.Sprintf("recording ID must contain only letters, numbers, '-', '.' and '_'. %q", opt.ID), true, http.StatusBadRequest)
Expand Down Expand Up @@ -899,7 +899,7 @@ func (s *Service) handleCreateReplay(w http.ResponseWriter, req *http.Request) {
return
}
if opt.ID == "" {
opt.ID = uuid.NewV4().String()
opt.ID = uuid.New().String()
}
if !validID.MatchString(opt.ID) {
httpd.HttpError(w, fmt.Sprintf("replay ID must contain only letters, numbers, '-', '.' and '_'. %q", opt.ID), true, http.StatusBadRequest)
Expand Down Expand Up @@ -963,7 +963,7 @@ func (s *Service) handleReplayBatch(w http.ResponseWriter, req *http.Request) {
return
}
if opt.ID == "" {
opt.ID = uuid.NewV4().String()
opt.ID = uuid.New().String()
}
if !validID.MatchString(opt.ID) {
httpd.HttpError(w, fmt.Sprintf("replay ID must match %v %q", validID, opt.ID), true, http.StatusBadRequest)
Expand Down Expand Up @@ -1027,7 +1027,7 @@ func (r *Service) handleReplayQuery(w http.ResponseWriter, req *http.Request) {
return
}
if opt.ID == "" {
opt.ID = uuid.NewV4().String()
opt.ID = uuid.New().String()
}
if !validID.MatchString(opt.ID) {
httpd.HttpError(w, fmt.Sprintf("recording ID must match %v %q", validID, opt.ID), true, http.StatusBadRequest)
Expand Down
6 changes: 3 additions & 3 deletions services/task_store/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"github.com/influxdata/kapacitor/services/storage"
"github.com/influxdata/kapacitor/tick"
"github.com/influxdata/kapacitor/tick/ast"
"github.com/influxdata/kapacitor/uuid"
"github.com/influxdata/kapacitor/vars"
"github.com/pkg/errors"
"github.com/twinj/uuid"
)

const (
Expand Down Expand Up @@ -656,7 +656,7 @@ func (ts *Service) handleCreateTask(w http.ResponseWriter, r *http.Request) {
return
}
if task.ID == "" {
task.ID = uuid.NewV4().String()
task.ID = uuid.New().String()
}
if !validTaskID.MatchString(task.ID) {
httpd.HttpError(w, fmt.Sprintf("task ID must contain only letters, numbers, '-', '.' and '_'. %q", task.ID), true, http.StatusBadRequest)
Expand Down Expand Up @@ -1547,7 +1547,7 @@ func (ts *Service) handleCreateTemplate(w http.ResponseWriter, r *http.Request)
return
}
if template.ID == "" {
template.ID = uuid.NewV4().String()
template.ID = uuid.New().String()
}
if !validTemplateID.MatchString(template.ID) {
httpd.HttpError(w, fmt.Sprintf("template ID must contain only letters, numbers, '-', '.' and '_'. %q", template.ID), true, http.StatusBadRequest)
Expand Down
56 changes: 56 additions & 0 deletions uuid/uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Package uuid generates and parses UUIDs.
// A simple API is exposed while the implemenation is provided by github.com/google/uuid
package uuid

import "github.com/google/uuid"

// UUID is a 16 byte (128 bit) id, and can be used as a map key and in direct comparisons.
type UUID uuid.UUID

// Nil represents an invalid or empty UUID.
var Nil = UUID(uuid.Nil)

func New() UUID {
return UUID(uuid.New())
}

// Must returns u or panics if err is not nil.
func Must(u UUID, err error) UUID {
if err != nil {
panic(err)
}
return u
}

// Parse an UUID of the forms "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" and
// "urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".
func Parse(s string) (UUID, error) {
u, err := uuid.Parse(s)
return UUID(u), err
}

// Parse an UUID of the forms "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" and
// "urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" represented as a byte slice.
func ParseBytes(b []byte) (UUID, error) {
u, err := uuid.ParseBytes(b)
return UUID(u), err
}

// String represents the UUID in the form "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".
func (u UUID) String() string {
return uuid.UUID(u).String()
}

func (u UUID) MarshalBinary() ([]byte, error) {
return uuid.UUID(u).MarshalBinary()
}
func (u *UUID) UnmarshalBinary(data []byte) error {
return (*uuid.UUID)(u).UnmarshalBinary(data)
}

func (u UUID) MarshalText() ([]byte, error) {
return uuid.UUID(u).MarshalText()
}
func (u *UUID) UnmarshalText(data []byte) error {
return (*uuid.UUID)(u).UnmarshalText(data)
}
4 changes: 2 additions & 2 deletions vars/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"runtime"

kexpvar "github.com/influxdata/kapacitor/expvar"
"github.com/twinj/uuid"
"github.com/influxdata/kapacitor/uuid"
)

var (
Expand All @@ -23,7 +23,7 @@ func init() {

// NewStatistic creates a new statistic in the published expvar map.
func NewStatistic(name string, tags map[string]string) (string, *kexpvar.Map) {
key := uuid.NewV4().String()
key := uuid.New().String()

m := &kexpvar.Map{}
m.Init()
Expand Down
4 changes: 2 additions & 2 deletions vars/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ var (
NumEnabledTasksVar = &kexpvar.Int{}
NumSubscriptionsVar = kexpvar.NewIntSum()

ClusterIDVar = &kexpvar.String{}
ServerIDVar = &kexpvar.String{}
ClusterIDVar = &kexpvar.UUID{}
ServerIDVar = &kexpvar.UUID{}
HostVar = &kexpvar.String{}
ProductVar = &kexpvar.String{}
VersionVar = &kexpvar.String{}
Expand Down
2 changes: 1 addition & 1 deletion vendor.list
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ github.com/evanphx/json-patch
github.com/ghodss/yaml
github.com/gogo/protobuf
github.com/golang/protobuf
github.com/google/uuid
github.com/gorhill/cronexpr
github.com/influxdata/influxdb master
github.com/influxdata/usage-client
Expand All @@ -23,7 +24,6 @@ github.com/serenize/snaker
github.com/shurcooL/go
github.com/shurcooL/markdownfmt
github.com/shurcooL/sanitized_anchor_name
github.com/twinj/uuid
golang.org/x/crypto master
gopkg.in/gomail.v2
gopkg.in/yaml.v2
Expand Down
4 changes: 0 additions & 4 deletions vendor/github.com/twinj/uuid/.gitignore

This file was deleted.

Loading

0 comments on commit 21f5957

Please sign in to comment.