-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[fs] Add the file.close
method to the fs
module (3/3)
#3314
Conversation
file.close
method of the fs
modulefile.close
method to the fs
module
file.close
method to the fs
modulefile.close
method to the fs
module
Codecov ReportAll modified lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## experimental/fs-readseek #3314 +/- ##
===========================================================
Coverage ? 73.07%
===========================================================
Files ? 262
Lines ? 20125
Branches ? 0
===========================================================
Hits ? 14706
Misses ? 4470
Partials ? 949
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
@oleiade We are able to have |
Hey @codebien 👋🏻 I've spent a couple of hours more on my ref-counting POC to see if there was an alternative, as you suggested, to make sure we would release the memory once we don't need it anymore. I made some progress and was able to release the memory as soon as all VUs referencing it were done indeed. However, I encountered another issue, which I'm not sure can be addressed, nor if it should: Eventually, once we have the ability to open files in the VU stage as per the design proposal I submitted internally, it would be worth attempting to resume this approach (if the file is opened in the VU stage, the What do you think? Do you maybe have other approaches that might be worth considering? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just as a reminder note. We have discussed it internally and we've set a potential solution. @oleiade will write a proposal for integrating the current design doc.
516dace
to
79ba8f2
Compare
795e5a1
to
afdef39
Compare
file.close
method to the fs
modulefile.close
method to the fs
module (3/3)
The method closes the file and returns a promise that will resolve to null once the operation is complete. Closing a file is a no-op in k6, as we don't have to worry about file descriptors. However, we still expose this method to the user to be consistent with the existing APIs such as Node's or Deno's. The promise will resolve to null, regardless of whether the file was previously opened or not. One of the reasons this method is currently is a no-op is that as of today (v0.46), k6 does not support opening files in the VU context. As a result, the file is always opened in the init context, and thus closed when the init context is closed. Any attempt of clever strategies attempting to limit long-lived files' content in memory (e.g reference counting the VU instances of a file, and releasing the memory once the count reaches zero) would thus be premature.
afdef39
to
706a61c
Compare
export async function teardown() { | ||
file.close(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a distributed mode, what should the expectation be? Teardown runs only once per Test instead of the init context that tuns once per VU.
f.closed.Store(true) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f.closed.Store(true) | |
f.closed.Store(true) | |
f.data = nil |
I think here we may release the pointer. We don't expect to access them anymore, and in the bad case, there is an attempt we can just check the closed variable and return an error saying the file has been closed.
We have discussed this feature ( |
What?
This Pull Request builds (and is based) upon #3142 and adds a
close
method to theFile
object exposed by thek6/experimental/fs
module.The close method currently does not perform any operations. This is intentional.
Why?
Rationale Behind No-Op Close Method:
close
method for consistency. Thus, we're aligning with these expectations.Why Not Implement Functionality Now?
Certainly, functionality can be added. As mentioned in #3142, we maintain a file cache that users access through
File
instance pointers. This ensures a more efficient memory utilization across various VUs, compared to our existingopen
method.Envision a scenario where only one out of several user scenarios opens a file. In such a case, tracking how many VUs haven't closed the file (via reference counting) becomes feasible. When the count hits zero, the file content can be purged from memory.
I've crafted a proof of concept for this approach. However, a current k6 limitation hinders its realization. Presently, k6 doesn't support opening files within the VU context (refer to #3020). This implies that even with reference counting via
file.close
, we'd always have an uncollected reference, preventing the count from reaching zero.Checklist
make ci-like-lint
) and all checks pass.make tests
) and all tests pass.Related PR(s)/Issue(s)
ref #3142
ref #3149
ref #3165