Skip to content

Commit 14f1574

Browse files
authored
Make rand seeding clearer and predictable (Azure#620)
By moving out of shuffleTransfers and into startup code
1 parent 44d503c commit 14f1574

File tree

3 files changed

+4
-2
lines changed

3 files changed

+4
-2
lines changed

cmd/copyEnumeratorHelper.go

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func addTransfer(e *common.CopyJobPartOrderRequest, transfer common.CopyTransfer
5252
// this is done to avoid hitting the same partition continuously in an append only pattern
5353
// TODO this should probably be removed after the high throughput block blob feature is implemented on the service side
5454
func shuffleTransfers(transfers []common.CopyTransfer) {
55-
rand.Seed(time.Now().UnixNano())
5655
rand.Shuffle(len(transfers), func(i, j int) { transfers[i], transfers[j] = transfers[j], transfers[i] })
5756
}
5857

common/randomDataGenerator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var randomDataBytePool = NewMultiSizeSlicePool(randomSliceLength)
3636
func NewRandomDataGenerator(length int64) CloseableReaderAt {
3737
r := &randomDataGenerator{
3838
length: length,
39-
randGen: rand.New(rand.NewSource(rand.Int63())),
39+
randGen: rand.New(rand.NewSource(rand.Int63())), // create new rand source, seeded from global one, so that after seeding we never lock the global one
4040
randBytes: randomDataBytePool.RentSlice(randomSliceLength),
4141
randMu: &sync.Mutex{}}
4242

main.go

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package main
2222

2323
import (
2424
"log"
25+
"math/rand"
2526
"os"
2627
"path"
2728
"runtime"
@@ -39,6 +40,8 @@ var glcm = common.GetLifecycleMgr()
3940
func main() {
4041
pipeline.SetLogSanitizer(common.NewAzCopyLogSanitizer()) // make sure ForceLog logs get secrets redacted
4142

43+
rand.Seed(time.Now().UnixNano()) // make sure our random numbers actually are random (but remember, use crypto/rand for anything where strong/reliable randomness is required
44+
4245
// note: azcopyAppPathFolder is the default location for all AzCopy data (logs, job plans, oauth token on Windows)
4346
// but both logs and job plans can be put elsewhere as they can become very large
4447
azcopyAppPathFolder := GetAzCopyAppPath()

0 commit comments

Comments
 (0)