Skip to content

Commit

Permalink
Use local random generator (#83)
Browse files Browse the repository at this point in the history
Golang prior to 1.20 has a fixed seed for `math/rand.globalRand` and backup/snapshots have the same suffix in a name.
  • Loading branch information
kayrus authored Jul 5, 2023
1 parent 3043314 commit 646121c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/cinder/block_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cinder

import (
"fmt"
"math/rand"
"os"
"strconv"

Expand Down Expand Up @@ -86,7 +85,7 @@ func (b *BlockStore) CreateVolumeFromSnapshot(snapshotID, volumeType, volumeAZ s
})
logWithFields.Info("BlockStore.CreateVolumeFromSnapshot called")

volumeName := fmt.Sprintf("%s.backup.%s", snapshotID, strconv.FormatUint(rand.Uint64(), 10))
volumeName := fmt.Sprintf("%s.backup.%s", snapshotID, strconv.FormatUint(utils.Rand.Uint64(), 10))
// Make sure snapshot is in ready state
// Possible values for snapshot state:
// https://github.com/openstack/cinder/blob/master/api-ref/source/v3/volumes-v3-snapshots.inc#volume-snapshots-snapshots
Expand Down Expand Up @@ -168,7 +167,7 @@ func (b *BlockStore) IsVolumeReady(volumeID, volumeAZ string) (ready bool, err e
// CreateSnapshot creates a snapshot of the specified volume, and applies any provided
// set of tags to the snapshot.
func (b *BlockStore) CreateSnapshot(volumeID, volumeAZ string, tags map[string]string) (string, error) {
snapshotName := fmt.Sprintf("%s.snap.%s", volumeID, strconv.FormatUint(rand.Uint64(), 10))
snapshotName := fmt.Sprintf("%s.snap.%s", volumeID, strconv.FormatUint(utils.Rand.Uint64(), 10))
logWithFields := b.log.WithFields(logrus.Fields{
"snapshotName": snapshotName,
"volumeID": volumeID,
Expand Down
5 changes: 5 additions & 0 deletions src/utils/helpers.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package utils

import (
"math/rand"
"os"
"strings"
"time"
)

// Rand is used for a random generator exclusively for this go module
var Rand = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))

// GetEnv gets value from environment variable or fallbacks to default value
// This snippet is from https://stackoverflow.com/a/40326580/3323419
func GetEnv(key, fallback string) string {
Expand Down
21 changes: 21 additions & 0 deletions src/utils/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"testing"
"time"
)

func TestReplaceAccount(t *testing.T) {
Expand Down Expand Up @@ -41,3 +42,23 @@ func TestReplaceAccount(t *testing.T) {
}
}
}

func TestRand(t *testing.T) {
Rand.Seed(time.Now().UTC().UnixNano())
a := Rand.Uint64()
Rand.Seed(time.Now().UTC().UnixNano())
b := Rand.Uint64()

if a == b {
t.Errorf("failed to verify random seed generator %v == %v", a, b)
}

Rand.Seed(int64(time.Now().UTC().Second()))
a = Rand.Uint64()
Rand.Seed(int64(time.Now().UTC().Second()))
b = Rand.Uint64()

if a != b {
t.Errorf("failed to verify random seed generator %v != %v", a, b)
}
}

0 comments on commit 646121c

Please sign in to comment.