package weed_server import ( "context" "fmt" "net" "os" "testing" "time" "github.com/seaweedfs/seaweedfs/weed/cluster" "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/pb/master_pb" "github.com/seaweedfs/seaweedfs/weed/pb/remote_pb" "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb" "github.com/seaweedfs/seaweedfs/weed/remote_storage" "github.com/seaweedfs/seaweedfs/weed/wdclient" "github.com/stretchr/testify/require" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" ) // remoteCacheMaster assigns every chunk to one fake volume server. type remoteCacheMaster struct { reuseManifestMaster volume pb.ServerAddress grpcPort int } func (m *remoteCacheMaster) Assign(context.Context, *master_pb.AssignRequest) (*master_pb.AssignResponse, error) { return &master_pb.AssignResponse{ Fid: fmt.Sprintf("1,%x%08x", m.next.Add(1), 0xc0de), Count: 1, Location: &master_pb.Location{Url: m.volume.ToHttpAddress(), PublicUrl: m.volume.ToHttpAddress(), GrpcPort: uint32(m.grpcPort)}, }, nil } // remoteCacheVolume accepts every remote fetch without storing anything. type remoteCacheVolume struct { volume_server_pb.UnimplementedVolumeServerServer } func (remoteCacheVolume) FetchAndWriteNeedle(context.Context, *volume_server_pb.FetchAndWriteNeedleRequest) (*volume_server_pb.FetchAndWriteNeedleResponse, error) { return &volume_server_pb.FetchAndWriteNeedleResponse{ETag: "etag"}, nil } // startRemoteCacheCluster points fs at a fake master that assigns every chunk // to volume. func startRemoteCacheCluster(t *testing.T, fs *FilerServer, volume volume_server_pb.VolumeServerServer) { t.Helper() dialOption := grpc.WithTransportCredentials(insecure.NewCredentials()) fs.grpcDialOption = dialOption lis, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err) port := lis.Addr().(*net.TCPAddr).Port volumeAddr := pb.NewServerAddressWithGrpcPort(lis.Addr().String(), port) volumeServer := grpc.NewServer() volume_server_pb.RegisterVolumeServerServer(volumeServer, volume) go volumeServer.Serve(lis) t.Cleanup(volumeServer.Stop) master := &remoteCacheMaster{volume: volumeAddr, grpcPort: port} master.volumeHost = volumeAddr.ToHttpAddress() masterAddr := startFakeMasterServerForLeaderLookup(t, master) fs.filer.MasterClient = wdclient.NewMasterClient(dialOption, "test", cluster.FilerType, pb.ServerAddress("localhost:0"), "", "", *pb.NewServiceDiscoveryFromMap(map[string]pb.ServerAddress{"master": masterAddr})) masterCtx, cancelMaster := context.WithCancel(context.Background()) t.Cleanup(cancelMaster) go fs.filer.MasterClient.KeepConnectedToMaster(masterCtx) waitCtx, cancelWait := context.WithTimeout(context.Background(), 5*time.Second) defer cancelWait() require.NotEmpty(t, fs.filer.MasterClient.GetMaster(waitCtx), "fake master did not become available") } // With the wall clock behind the log, caching a remote object fences its // commit above the log floor and below the commit's own event. func TestCacheRemoteObjectLogTsNsAcrossClockStep(t *testing.T) { fs, store, _ := newRemoteMountTestServer(t) startRemoteCacheCluster(t, fs, remoteCacheVolume{}) obj := remoteOnlyFileEntry("/buckets/b/obj.bin") store.entries[string(obj.FullPath)] = obj floor := stepMetaLogClockBack(fs.filer, time.Hour) resp, err := fs.CacheRemoteObjectToLocalCluster(context.Background(), &filer_pb.CacheRemoteObjectToLocalClusterRequest{ Directory: "/buckets/b", Name: "obj.bin", }) require.NoError(t, err) require.NotEmpty(t, resp.GetEntry().GetChunks(), "object was not cached") require.NotNil(t, resp.MetadataEvent, "cache commit logged no event") require.Greater(t, resp.LogTsNs, floor, "fence below the log floor") require.Less(t, resp.LogTsNs, resp.MetadataEvent.TsNs, "fence not below the commit's event") } // remoteStatClient reports every object as present on the remote. type remoteStatClient struct { remote_storage.RemoteStorageClient } func (remoteStatClient) StatFile(*remote_pb.RemoteStorageLocation) (*filer_pb.RemoteEntry, error) { return &filer_pb.RemoteEntry{RemoteSize: 10, RemoteMtime: 1700000000}, nil } // A lookup that imports a remote object returns an entry whose create event // it logged itself; the lookup's fence must cover that event. func TestLookupFenceCoversRemoteImport(t *testing.T) { fs, store, _ := newRemoteMountTestServer(t) fs.filer.BuildGuardedRemoteClient = func(context.Context, *remote_pb.RemoteConf, bool) (remote_storage.RemoteStorageClient, error) { return remoteStatClient{}, nil } for _, dir := range []string{"/buckets", "/buckets/b"} { entry := newFileEntry(dir, 1) entry.Attr.Mode = os.ModeDir | 0o755 store.entries[dir] = entry } before := fs.filer.LocalMetaLogBuffer.LastTsNs.Load() resp, err := fs.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{Directory: "/buckets/b", Name: "imported.bin"}) require.NoError(t, err) require.NotNil(t, resp.Entry.GetRemoteEntry(), "lookup did not import the remote object") require.NotNil(t, store.entries["/buckets/b/imported.bin"], "import was not persisted") delivered := fs.filer.LocalMetaLogBuffer.LastTsNs.Load() require.Greater(t, delivered, before, "import logged no event") require.GreaterOrEqual(t, resp.LogTsNs, delivered, "lookup fence below the import event it reflects") } // remoteCacheVolumeHook runs onFetch while the remote object downloads. type remoteCacheVolumeHook struct { remoteCacheVolume onFetch func() } func (v remoteCacheVolumeHook) FetchAndWriteNeedle(ctx context.Context, req *volume_server_pb.FetchAndWriteNeedleRequest) (*volume_server_pb.FetchAndWriteNeedleResponse, error) { v.onFetch() return v.remoteCacheVolume.FetchAndWriteNeedle(ctx, req) } // An entry that leaves the store during the download is imported again by the // commit's re-read; the fence returned with that entry must cover the import. func TestCacheRemoteObjectFenceCoversReimport(t *testing.T) { fs, store, _ := newRemoteMountTestServer(t) for _, dir := range []string{"/buckets", "/buckets/b"} { entry := newFileEntry(dir, 1) entry.Attr.Mode = os.ModeDir | 0o755 store.entries[dir] = entry } obj := remoteOnlyFileEntry("/buckets/b/obj.bin") store.entries[string(obj.FullPath)] = obj fs.filer.BuildGuardedRemoteClient = func(context.Context, *remote_pb.RemoteConf, bool) (remote_storage.RemoteStorageClient, error) { return remoteStatClient{}, nil } startRemoteCacheCluster(t, fs, remoteCacheVolumeHook{onFetch: func() { store.mu.Lock() delete(store.entries, string(obj.FullPath)) store.mu.Unlock() }}) before := fs.filer.LocalMetaLogBuffer.LastTsNs.Load() resp, err := fs.CacheRemoteObjectToLocalCluster(context.Background(), &filer_pb.CacheRemoteObjectToLocalClusterRequest{ Directory: "/buckets/b", Name: "obj.bin", }) require.NoError(t, err) require.Nil(t, resp.MetadataEvent, "re-imported entry was committed over") require.Empty(t, resp.GetEntry().GetChunks(), "re-imported entry was cached") delivered := fs.filer.LocalMetaLogBuffer.LastTsNs.Load() require.Greater(t, delivered, before, "re-import logged no event") require.GreaterOrEqual(t, resp.LogTsNs, delivered, "fence below the re-import event it reflects") }