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

deploy: ApplyParams: do not write params.env if there are no updates #1186

Conversation

ykaliuta
Copy link
Contributor

@ykaliuta ykaliuta commented Aug 16, 2024

Optimize file operations, do not rewrite params.env on every reconcile if no updates for it. In practice updates happen only on the first run.

Jira: https://issues.redhat.com/browse/RHOAIENG-11594

Description

How Has This Been Tested?

Screenshot or short clip

Merge criteria

  • You have read the contributors guide.
  • Commit messages are meaningful - have a clear and concise summary and detailed explanation of what was changed and why.
  • Pull Request contains a description of the solution, a link to the JIRA issue, and to any dependent or related Pull Request.
  • Testing instructions have been added in the PR body (for PRs involving changes that are not immediately obvious).
  • The developer has manually tested the changes and verified that the changes work

}
}

if count == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

need a count?
or just a true/false enough?
if !needUpdateImage && !needUpdateEnv {
return nil
}

Copy link
Contributor Author

@ykaliuta ykaliuta Aug 17, 2024

Choose a reason for hiding this comment

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

I do not know a way to accumulate booleans. With every individual check it looks pretty clumsy. But actually I missed for some reason that bitwise operations work in go, so will replace += with |= to be safe from overflow :) (practically impossible here)

Copy link
Member

Choose a reason for hiding this comment

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

but to call writeParamsToTmp() or not, this is based on if any key-value from passing-in map does not match the key-value from params.env, isnt it? regardless all of them mismatch or just one of them mismatch.

as for writeParamsToTmp(), it is a full dump all key-values into the temp. file

so i still do not get the reason behind why need count them.

if you have a first mistmatch in the updateMap, mark needUpdateImage/needUpdateEnv to true, isnt that enough? something like

func updateMap(m *map[string]string, key, val string) int {
	old := (*m)[key]
	if old != val {
		(*m)[key] = val
        needUpdateImage = true
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be a package scope variable then, which is not nice as well. But if you prefer that, I'll change that way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And negated condition :)

Copy link
Contributor Author

@ykaliuta ykaliuta Aug 17, 2024

Choose a reason for hiding this comment

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

so i still do not get the reason behind why need count them.

There is no need to really count, it acts as boolean actually just to accumulate results (a bit like multierror does for error) since I cannot do var += true and var += false which is true if one of that was true

UPD: forgot to commit actual changes, ykaliuta@4e93635

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be a package scope variable then, which is not nice as well. But if you prefer that, I'll change that way

Actually, it's more serious issue. Since it addresses the possible race, package scoped variable requires synchronized access then (mutex). Basically, setting it "true" from two invocations does not make real harm with the implementation, since tmpfile name is unique, update of boolean is atomic and params.env file update with os.Rename() is atomic as well, but it's not nice too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, it's more serious issue. Since it addresses the possible race, package scoped variable requires synchronized access then (mutex). Basically, setting it "true" from two invocations does not make real harm with the implementation, since tmpfile name is unique, update of boolean is atomic and params.env file update with os.Rename() is atomic as well, but it's not nice too.

Forget what I said. It's an issue since there are 2 places of writes: before the loop and when a field gets updated. They may interlace and previously set "true" be reset with next invocation "false".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In general, these params.env corrections look like one time initialization operation and should not be called for every reconciliation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In general, these params.env corrections look like one time initialization operation and should not be called for every reconciliation.

#1191 like that, what do you think?

@ykaliuta ykaliuta force-pushed the apply-params-save-writes branch 4 times, most recently from 72b3bd7 to 8bdc195 Compare August 17, 2024 10:55
Optimize file operations, do not rewrite params.env on every
reconcile if no updates for it. In practice updates happen only on
the first run.

Since updates happen in 2 loops, move actual update to own function
which returns "number of updates" (actually 0 or 1, so acts as
boolean) and use bitwise operations to accumulate updates for every
field.

It could be possible to use global boolean flag, but since it
requires to be initiated before the loops and set when updates
happen, the function should be guarded with mutex. Otherwise "true"
value from one invocation can be reset to "false" by the next
invocation".

Signed-off-by: Yauheni Kaliuta <[email protected]>
@zdtsw zdtsw requested review from VaishnaviHire and removed request for jackdelahunt August 28, 2024 18:25
@zdtsw
Copy link
Member

zdtsw commented Aug 28, 2024

@ykaliuta @VaishnaviHire lets try sort this PR out tomorrow/this week.

the check part will somehow help preventing wrong image being used.

@ykaliuta
Copy link
Contributor Author

@ykaliuta @VaishnaviHire lets try sort this PR out tomorrow/this week.

the check part will somehow help preventing wrong image being used.

With pleasure :) Anything required from my side?

Copy link

openshift-ci bot commented Aug 29, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: zdtsw

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@zdtsw
Copy link
Member

zdtsw commented Aug 29, 2024

@ykaliuta @VaishnaviHire lets try sort this PR out tomorrow/this week.
the check part will somehow help preventing wrong image being used.

With pleasure :) Anything required from my side?

i am fine with this change, i thought it got merged weeks ago, but when i checked code yesterday found the PR was still open.
FYI this for @VaishnaviHire

@openshift-merge-bot openshift-merge-bot bot merged commit 450e19f into opendatahub-io:incubation Aug 29, 2024
8 checks passed
zdtsw pushed a commit to zdtsw-forking/rhods-operator that referenced this pull request Aug 29, 2024
…pendatahub-io#1186)

Optimize file operations, do not rewrite params.env on every
reconcile if no updates for it. In practice updates happen only on
the first run.

Since updates happen in 2 loops, move actual update to own function
which returns "number of updates" (actually 0 or 1, so acts as
boolean) and use bitwise operations to accumulate updates for every
field.

It could be possible to use global boolean flag, but since it
requires to be initiated before the loops and set when updates
happen, the function should be guarded with mutex. Otherwise "true"
value from one invocation can be reset to "false" by the next
invocation".

Signed-off-by: Yauheni Kaliuta <[email protected]>
(cherry picked from commit 450e19f)
openshift-merge-bot bot pushed a commit to red-hat-data-services/rhods-operator that referenced this pull request Aug 29, 2024
…pendatahub-io#1186)

Optimize file operations, do not rewrite params.env on every
reconcile if no updates for it. In practice updates happen only on
the first run.

Since updates happen in 2 loops, move actual update to own function
which returns "number of updates" (actually 0 or 1, so acts as
boolean) and use bitwise operations to accumulate updates for every
field.

It could be possible to use global boolean flag, but since it
requires to be initiated before the loops and set when updates
happen, the function should be guarded with mutex. Otherwise "true"
value from one invocation can be reset to "false" by the next
invocation".

Signed-off-by: Yauheni Kaliuta <[email protected]>
(cherry picked from commit 450e19f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants