filer: gate the shared remote stat test on lookups joining the flight

The test assumed a 300ms stat was long enough for all eight lookups to
reach the singleflight; on a slow runner a late one starts a second stat.
Hold the stat until released, and release it only once the other seven
lookups are parked in singleflight.Do waiting on it: a caller there has
registered as a duplicate and can only share the result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-01 15:36:01 -08:00
1 parent 8bf659dcb6
commit 0a39e94fe5
1 file changed
+41 -6
@@ -3,6 +3,7 @@ package weed_server
import (
"context"
"errors"
"runtime"
"strings"
"sync/atomic"
"testing"
@@ -85,23 +86,47 @@ func TestLookupFenceNotAboveAConcurrentExpiryDelete(t *testing.T) {
}
}
// slowMissingRemoteClient answers every stat after a delay with not found.
type slowMissingRemoteClient struct {
// gatedMissingRemoteClient holds every stat until released, then answers not
// found.
type gatedMissingRemoteClient struct {
remote_storage.RemoteStorageClient
stats atomic.Int32
stats atomic.Int32
release chan struct{}
}
func (c *slowMissingRemoteClient) StatFile(*remote_pb.RemoteStorageLocation) (*filer_pb.RemoteEntry, error) {
func (c *gatedMissingRemoteClient) StatFile(*remote_pb.RemoteStorageLocation) (*filer_pb.RemoteEntry, error) {
c.stats.Add(1)
time.Sleep(300 * time.Millisecond)
<-c.release
return nil, remote_storage.ErrRemoteObjectNotFound
}
// joinedLookupFlights counts lookups parked in singleflight.Do waiting on
// another caller's call. Such a caller registered under the group's lock
// before waiting, so it shares that call's result and cannot start its own.
func joinedLookupFlights() int {
buf := make([]byte, 1<<20)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
buf = buf[:n]
break
}
buf = make([]byte, 2*len(buf))
}
joined := 0
for _, g := range strings.Split(string(buf), "\n\n") {
if strings.Contains(g, "LookupDirectoryEntry") && strings.Contains(g, "singleflight.(*Group).Do(") && strings.Contains(g, "sync.(*WaitGroup).Wait(") {
joined++
}
}
return joined
}
// Concurrent lookups of a key missing both locally and on the remote mount
// share one remote stat: nothing is imported, so none needs the path to itself.
func TestLookupsOfRemoteMissShareOneStat(t *testing.T) {
fs, _, _ := newRemoteMountTestServer(t)
client := &slowMissingRemoteClient{}
client := &gatedMissingRemoteClient{release: make(chan struct{})}
fs.filer.BuildGuardedRemoteClient = func(context.Context, *remote_pb.RemoteConf, bool) (remote_storage.RemoteStorageClient, error) {
return client, nil
}
@@ -113,6 +138,16 @@ func TestLookupsOfRemoteMissShareOneStat(t *testing.T) {
errs <- err
}()
}
// One lookup holds the stat; release it only once the rest have joined.
deadline := time.Now().Add(10 * time.Second)
for client.stats.Load() < 1 || joinedLookupFlights() < lookups-1 {
if time.Now().After(deadline) {
close(client.release)
t.Fatalf("stats=%d joined=%d after 10s, want 1 stat and %d joined", client.stats.Load(), joinedLookupFlights(), lookups-1)
}
time.Sleep(time.Millisecond)
}
close(client.release)
for i := 0; i < lookups; i++ {
if err := <-errs; err == nil || !strings.Contains(err.Error(), filer_pb.ErrNotFound.Error()) {
t.Fatalf("lookup: %v, want not found", err)