Skip to content
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

refactor: re-organize node loading code to make it easier to follow #1771

Merged
merged 4 commits into from
Oct 18, 2024

Conversation

pbitty
Copy link
Contributor

@pbitty pbitty commented Aug 21, 2024

While working in this part of the code I found it difficult to follow the different code paths when loading
a node's content. I realize readability is subjective, but I am suggesting this change in case you also find it improves readability and maybe maintainability.

In this change I am attempting to make the branches of the decision tree return early as much as possible. This way we discard each possibility as we work our way down, in this order:

  1. Non-remote nodes
  2. In offline mode, use cached remote nodes
  3. On network timeout, use cached remote nodes
  4. a. Use fetched remote node
    b. On checksum change, handle writing to cache

Tests are added to ensure this functionality does not break.

I broke up the changes into logical commits that are fairly isolated. It might be easier to review them individually.

What do you think?

(I got into this code path while exploring how to incorporate go-getter into the remote taskfile feature (as proposed in this comment). Thanks for maintaining this tool!)

Copy link
Member

@andreynering andreynering left a comment

Choose a reason for hiding this comment

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

Hi @pbitty!

I like the idea, but there a couple of thing that needs to be addressed before merging:

  1. Tests are failing unless TASK_X_REMOTE_TASKFILES=1 is set
  2. Can you re-write the commit messages to comply with Conventional Commits?

testdata/includes_remote/Taskfile.yml Outdated Show resolved Hide resolved
testdata/includes_remote/first/Taskfile.yml Outdated Show resolved Hide resolved
testdata/includes_remote/first/Taskfile.yml Outdated Show resolved Hide resolved
testdata/includes_remote/first/second/Taskfile.yml Outdated Show resolved Hide resolved
@pbitty
Copy link
Contributor Author

pbitty commented Aug 26, 2024

Hi @pbitty!

I like the idea, but there a couple of thing that needs to be addressed before merging:

Thanks, will do!

  1. Tests are failing unless TASK_X_REMOTE_TASKFILES=1 is set

Ha, I missed this because I have set it permanently in my shell environment. I figure the best place to add it is in the Go tests themselves. Let me know if you have another idea.

task_test.go Outdated
Comment on lines 2660 to 2824
// overrideExperimentValue allows one to change the value of an experiment value for a set of tests,
// with the value being restored to its previous state when tests complete.
//
// Typically experiments are controlled via TASK_X_ env vars, but we cannot use those in tests
// because the experiment settings are parsed during experiments.init(), before any tests run.
func overrideExperimentValue(t *testing.T, e *experiments.Experiment, enabled bool, val string) {
prev := *e
*e = experiments.Experiment{
Name: prev.Name,
Enabled: enabled,
Value: val,
}
t.Cleanup(func() { *e = prev })
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@andreynering Are you okay with this work-around? I figure anything deeper would require changing the experiments package and it doesn't seem worth it to accommodate tests.

Copy link
Member

Choose a reason for hiding this comment

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

I think this is good.
Imho, we do not need the enabled param, it'll be always true, as all experiments are disabled by default

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I will adjust it.

@pbitty pbitty requested a review from andreynering August 26, 2024 15:33
@pbitty
Copy link
Contributor Author

pbitty commented Aug 26, 2024

@andreynering , I did a force-push in order to alter the commit messages. Do they look ok?

I could also squash all the commits down to a single one, and call it

refactor: Re-organize node loading code to make it easier to follow

or something along those lines.

@vmaerten
Copy link
Member

Hey @pbitty !
Thanks for your contribution!
Like @andreynering, I like the idea, I find this more readable.
Can you also modify the PR title to follow Conventional Commits? because we will squash and merge.

I would like to merge #1713 first as it solves one issue (Timeout message is not correct)
What is your opinion about it @andreynering ?

@pbitty pbitty changed the title Re-organize node loading code to make it easier to follow refactor: Re-organize node loading code to make it easier to follow Aug 27, 2024
pbitty added a commit to Wattpad/go-task that referenced this pull request Aug 27, 2024
pbitty added a commit to Wattpad/go-task that referenced this pull request Aug 27, 2024
pbitty added a commit to Wattpad/go-task that referenced this pull request Aug 27, 2024
@pbitty pbitty requested a review from vmaerten September 3, 2024 16:56
@vmaerten vmaerten requested a review from pd93 September 15, 2024 15:07
Copy link
Member

@vmaerten vmaerten left a comment

Choose a reason for hiding this comment

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

I like the idea! Thanks a lot for the tests 🔥

Copy link
Member

@andreynering andreynering left a comment

Choose a reason for hiding this comment

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

Looks good. It just needs a rebase so we are able to merge it (there are conflicts).

Regarding conventional commits, just note that messages should usually be all lowercase, i.e. fix: Extract ... should be fix: extract ..., etc.

@pbitty pbitty changed the title refactor: Re-organize node loading code to make it easier to follow refactor: re-organize node loading code to make it easier to follow Oct 7, 2024
@pbitty
Copy link
Contributor Author

pbitty commented Oct 10, 2024

Just noting that I haven't forgotten about this, just trying to find some time this week to do it.

Readability is subjective, but while working in this part of the code
I found it difficult to follow the different code paths when loading
a node's content.

In this change I am attempting to make the branches of the decision
tree return early as much as possible.  This way we discard each
possibility as we work our way down, in this order:

1. Non-remote nodes
2. In offline mode, use cached remote nodes
3. On network timeout, use cached remote nodes
4a. Use fetched remote node
4b. On checksum change, handle writing to cache

Tests are added to ensure this functionality does not break.
This new package pre-seeds the global rand source on every process start
so we don't need to maintain our own rand.Source variable.
@pbitty
Copy link
Contributor Author

pbitty commented Oct 17, 2024

@andreynering , @vmaerten, the rebase is done! Ready for merge.

The Compare link above shows 94 changed files, but you can see the ones that had conflicts if you click on the files themselves. Here are the links if it helps:

I added one more commit which uses math/rand/v2 to make the tests I added a little bit cleaner.

@vmaerten
Copy link
Member

Thanks!

@vmaerten vmaerten merged commit 8dd3f4b into go-task:main Oct 18, 2024
14 checks passed
vmaerten added a commit that referenced this pull request Oct 18, 2024
@pbitty pbitty deleted the cache-reorganize branch October 21, 2024 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants