From 0e0c2402c149d554a6c1020d9a0fef254d7cfa62 Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Fri, 6 Dec 2024 15:11:57 +0100 Subject: [PATCH] Add VirtualApply(ApplyOpenReadFrozen) Right now the only way to read the contents of a pool backed file is to upload it into an REv2 CAS. Let's expose the ability to open files for reading through VirtualApply(), so that it's possible to use this logic outside the context of REv2. --- pkg/filesystem/virtual/node.go | 18 ++++++++++++++++++ .../virtual/pool_backed_file_allocator.go | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/pkg/filesystem/virtual/node.go b/pkg/filesystem/virtual/node.go index 9b57b795..63deba1b 100644 --- a/pkg/filesystem/virtual/node.go +++ b/pkg/filesystem/virtual/node.go @@ -93,3 +93,21 @@ type ApplyAppendOutputPathPersistencyDirectoryNode struct { Directory *outputpathpersistency.Directory Name path.Component } + +// ApplyOpenReadFrozen is an operation for VirtualApply that opens a +// regular file for reading. The file's contents are guaranteed to be +// immutable as long as the file is kept open. +// +// If the file is still opened for writing through the virtual file +// system, implementations should wait for the file to be closed to +// ensure that all data is flushed from the page cache. The +// WritableFileDelay channel can be used to place a bound on the maximum +// amount of time to wait. +type ApplyOpenReadFrozen struct { + // Inputs. + WritableFileDelay <-chan struct{} + + // Outputs. + Reader filesystem.FileReader + Err error +} diff --git a/pkg/filesystem/virtual/pool_backed_file_allocator.go b/pkg/filesystem/virtual/pool_backed_file_allocator.go index 166fad8c..48733a63 100644 --- a/pkg/filesystem/virtual/pool_backed_file_allocator.go +++ b/pkg/filesystem/virtual/pool_backed_file_allocator.go @@ -379,6 +379,12 @@ func (f *fileBackedFile) VirtualApply(data any) bool { IsExecutable: f.isExecutable, }) } + case *ApplyOpenReadFrozen: + if frozenFile, success := f.waitAndOpenReadFrozen(p.WritableFileDelay); success { + p.Reader = frozenFile + } else { + p.Err = status.Error(codes.NotFound, "File was unlinked before file could be opened") + } default: return false }