Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resource.UniqueId to be properly ordered #10152

Merged
merged 1 commit into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 15 additions & 29 deletions helper/resource/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,34 @@ package resource

import (
"crypto/rand"
"encoding/base32"
"fmt"
"strings"
"time"
"math/big"
"sync"
)

const UniqueIdPrefix = `terraform-`

// idCounter is a randomly seeded monotonic counter for generating ordered
// unique ids. It uses a big.Int so we can easily increment a long numeric
// string. The max possible hex value here with 12 random bytes is
// "01000000000000000000000000", so there's no chance of rollover during
// operation.
var idMutex sync.Mutex
var idCounter = big.NewInt(0).SetBytes(randomBytes(12))

// Helper for a resource to generate a unique identifier w/ default prefix
func UniqueId() string {
return PrefixedUniqueId(UniqueIdPrefix)
}

// Helper for a resource to generate a unique identifier w/ given prefix
//
// After the prefix, the ID consists of a timestamp and 12 random base32
// characters. The timestamp means that multiple IDs created with the same
// prefix will sort in the order of their creation.
// After the prefix, the ID consists of an incrementing 26 digit value (to match
// previous timestamp output).
func PrefixedUniqueId(prefix string) string {
// Be precise to the level nanoseconds, but remove the dot before the
// nanosecond. We assume that the randomCharacters call takes at least a
// nanosecond, so that multiple calls to this function from the same goroutine
// will have distinct ordered timestamps.
timestamp := strings.Replace(
time.Now().UTC().Format("20060102150405.000000000"),
".",
"", 1)
// This uses 3 characters, so that the length of the unique ID is the same as
// it was before we added the timestamp prefix, which happened to be 23
// characters.
return fmt.Sprintf("%s%s%s", prefix, timestamp, randomCharacters(3))
}

func randomCharacters(n int) string {
// Base32 has 5 bits of information per character.
b := randomBytes(n * 8 / 5)
chars := strings.ToLower(
strings.Replace(
base32.StdEncoding.EncodeToString(b),
"=", "", -1))
// Trim extra characters.
return chars[:n]
idMutex.Lock()
defer idMutex.Unlock()
return fmt.Sprintf("%s%026x", prefix, idCounter.Add(idCounter, big.NewInt(1)))
}

func randomBytes(n int) []byte {
Expand Down
14 changes: 3 additions & 11 deletions helper/resource/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"testing"
)

var allDigits = regexp.MustCompile(`^\d+$`)
var allBase32 = regexp.MustCompile(`^[a-z234567]+$`)
var allHex = regexp.MustCompile(`^[a-f0-9]+$`)

func TestUniqueId(t *testing.T) {
iterations := 10000
Expand All @@ -30,15 +29,8 @@ func TestUniqueId(t *testing.T) {
t.Fatalf("Post-prefix part has wrong length! %s", rest)
}

timestamp := rest[:23]
random := rest[23:]

if !allDigits.MatchString(timestamp) {
t.Fatalf("Timestamp not all digits! %s", timestamp)
}

if !allBase32.MatchString(random) {
t.Fatalf("Random part not all base32! %s", random)
if !allHex.MatchString(rest) {
t.Fatalf("Random part not all hex! %s", rest)
}

if lastId != "" && lastId >= id {
Expand Down