21139: Reduce lock contention.
authorTom Clegg <tom@curii.com>
Fri, 3 Nov 2023 15:04:18 +0000 (11:04 -0400)
committerTom Clegg <tom@curii.com>
Fri, 3 Nov 2023 15:04:18 +0000 (11:04 -0400)
refs #21139

Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

tilelib.go

index f5741233eeee3ac8de8afb346925856f8d379934..7d4599d4e254bda48a7c0b6436e6ffcaf1d44f30 100644 (file)
@@ -11,6 +11,7 @@ import (
        "encoding/gob"
        "fmt"
        "io"
+       "math/rand"
        "os"
        "regexp"
        "runtime"
@@ -645,7 +646,16 @@ readall:
                log.Infof("%s %s getting %d librefs", filelabel, seqlabel, len(found))
                path = path[:len(found)]
                var lowquality int64
-               for i, f := range found {
+               // Visit each element of found, but start at a random
+               // index, to reduce the likelihood of lock contention
+               // when importing many samples concurrently.
+               startpoint := rand.Int() % len(found)
+               for offset := range found {
+                       i := startpoint + offset
+                       if i >= len(found) {
+                               i -= len(found)
+                       }
+                       f := found[i]
                        var startpos, endpos int
                        if i == 0 {
                                startpos = 0