Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

feat: add cleanup test, and isolate id mapping code and cleanup code #218

Merged
merged 8 commits into from
Nov 11, 2023

Conversation

elijaharita
Copy link
Contributor

@elijaharita elijaharita commented Nov 7, 2023

Wrote a test for the cleanup code while investigating #201. This involved moving cleanup code to its own file. I also used this opportunity to isolate ID mapping code.

The test is passing from the start, so I'm not sure if it'll fix that issue, but there's a possibility. In any case, it cleans up a bit of mess.

The cleanup scheduler is a new object with the sole purpose of reading through the local bin store and removing files based on the result of a configurable callback, at some interval. A configurable callback allows it to be mocked easily for the test. In store.go, the callback is set to the hasDealForAllProviders() function. This should be tested next. It'll be a bit more involved since it'll have to be added to the integration test, since singularity has to be running, but I think I know how I'll do it.

A few additional functions have been added to local store: List() and Delete(). These were previously done inline in store.go by manually working with the store directory files, which was kinda hacky. This puts that stuff where it belongs.

About the new ID map object: a separation of concerns refactor. Motion maps blob IDs to Singularity IDs by storing files ending in ".id" in the same directory, named with the stringified motion ID, and containing the Singularity ID in the body. This processing was all done inline in store.go With these changes, this behavior is unmodified, so there should not be any compatibility issues. I originally wrote this because I thought I would be working with IDs in cleanup.go, but my final solution did not end up needing it. So in the end it is just a general improvement. It does pave the way for an ID mapping test.

should fix #201, awaiting confirmation by @Angelo-gh3990

Copy link
Collaborator

@gammazero gammazero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments

blob/local_store.go Show resolved Hide resolved
cancel()
}()

ticker := time.NewTicker(cs.cfg.interval)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this until after the first call to cs.clenaup(ctx), to line 57, so that the timer does not start until after the first cleanup is done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it in this order because after the initial call, the behavior of the ticker is to tick x duration after the start of the previous cleanup, not after the end of the previous cleanup. so this way the behavior stays consistent with the regular loop, and cleanups will happen at predictable times.

if it is desired to start waiting for the duration after the previous cleanup finishes instead of immediately after it starts, we would probably have to replace the ticker with a timer that is manually scheduled at the end of a cleanup. lmk what u think

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see.

Would be nice to put the defer ticker.Stop() right after the ticker is created to make clear it is stopped at exit.

// func (im *idMap) remove(blobID blob.ID) error {
// if err := os.Remove(im.path(blobID)); err != nil {
// if errors.Is(err, os.ErrNotExist) {
// return blob.ErrBlobNotFound
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it fix the warning by not returning an error here? It is reasonable to not return file-not-found error when removing a file, since it is already removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error isn't related to the return type, the function just isn't called anywhere yet and the linter doesn't like private functions that are unused

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively, i could make this struct and its methods public to get rid of the warning? does it make sense to be public though?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not export (make public) anything unless really needed. Otherwise, it risks becoming an API we need to support.

Fine like it is then

}

func (im *idMap) path(blobID blob.ID) string {
return path.Join(im.dir, blobID.String()+".id")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can im.dir be a filesystem path (possibly containing system-specific delimiters)? If yes, that it may be necessary to use filepath.Join.

integration/singularity/store.go Show resolved Hide resolved
integration/singularity/store.go Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Nov 7, 2023

Codecov Report

Merging #218 (7adcc08) into main (6ae65b1) will increase coverage by 3.83%.
The diff coverage is 58.85%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #218      +/-   ##
==========================================
+ Coverage   40.17%   44.00%   +3.83%     
==========================================
  Files           7        9       +2     
  Lines         941     1009      +68     
==========================================
+ Hits          378      444      +66     
+ Misses        515      513       -2     
- Partials       48       52       +4     
Files Coverage Δ
integration/singularity/cleanup.go 88.23% <88.23%> (ø)
blob/local_store.go 47.52% <51.72%> (+1.69%) ⬆️
integration/singularity/store.go 35.87% <41.46%> (-0.07%) ⬇️
integration/singularity/id_map.go 29.72% <29.72%> (ø)

... and 1 file with indirect coverage changes

Copy link
Collaborator

@gammazero gammazero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

cancel()
}()

ticker := time.NewTicker(cs.cfg.interval)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see.

Would be nice to put the defer ticker.Stop() right after the ticker is created to make clear it is stopped at exit.

// func (im *idMap) remove(blobID blob.ID) error {
// if err := os.Remove(im.path(blobID)); err != nil {
// if errors.Is(err, os.ErrNotExist) {
// return blob.ErrBlobNotFound
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not export (make public) anything unless really needed. Otherwise, it risks becoming an API we need to support.

Fine like it is then

integration/singularity/store.go Show resolved Hide resolved
@elijaharita elijaharita merged commit 4fc79c8 into main Nov 11, 2023
8 checks passed
@elijaharita elijaharita deleted the feat/cleanup-test branch November 11, 2023 00:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Files not cleaned up when all SPs are storing the fille
3 participants