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

Fix: append all slice data when range for it #16327

Closed
wants to merge 2 commits into from

Conversation

alingse
Copy link

@alingse alingse commented Jul 3, 2024

Description

I am writing a linter called sundrylint to address some real-world bugs that I discovered during my work.

When we range over a slice and call the append function within the loop body, we are not appending all elements, but only the ones that we specifically iterate over during the range operation.

for _, n := range ns {
   rs = append(rs, n)       # this is mostly wanted
   rs = append(rs, ns...)   # this is mostly wrong
}

I read the code in here and I think it was just want to append that one param to result

Related Issue(s)

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

Signed-off-by: alingse <alingse@foxmail.com>
Copy link
Contributor

vitess-bot bot commented Jul 3, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Jul 3, 2024
@github-actions github-actions bot added this to the v21.0.0 milestone Jul 3, 2024
Copy link
Member

@frouioui frouioui 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 to me, thank you for the contribution. Where you able to find any other cases where we do this?

Copy link

codecov bot commented Jul 10, 2024

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 68.72%. Comparing base (79c54e5) to head (81dfec1).
Report is 26 commits behind head on main.

Files Patch % Lines
go/vt/vtctl/vtctl.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16327      +/-   ##
==========================================
- Coverage   68.72%   68.72%   -0.01%     
==========================================
  Files        1547     1547              
  Lines      198270   198270              
==========================================
- Hits       136270   136258      -12     
- Misses      62000    62012      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@alingse
Copy link
Author

alingse commented Jul 11, 2024

Looks good to me, thank you for the contribution. Where you able to find any other cases where we do this?

the linter result only show this. I think the others are correct.

@@ -838,7 +838,7 @@ func keyspaceParamsToKeyspaces(ctx context.Context, wr *wrangler.Wrangler, param
}
if param[0] == '/' {
// this is a topology-specific path
result = append(result, params...)
result = append(result, param)
Copy link
Contributor

@mattlord mattlord Jul 11, 2024

Choose a reason for hiding this comment

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

Why do you think that this was not intentional? It seems to be intentional to me, in that we are passing along all of the arguments.

Also, please note that vtctl has been deprecated since v19 and is likely to be removed in v21 or v22.

In any event, this function is currently only called from here:

func commandRebuildKeyspaceGraph(ctx context.Context, wr *wrangler.Wrangler, subFlags *pflag.FlagSet, args []string) error {
	cells := subFlags.String("cells", "", "Specifies a comma-separated list of cells to update")
	allowPartial := subFlags.Bool("allow_partial", false, "Specifies whether a SNAPSHOT keyspace is allowed to serve with an incomplete set of shards. Ignored for all other types of keyspaces")
	if err := subFlags.Parse(args); err != nil {
		return err
	}
	if subFlags.NArg() == 0 {
		return fmt.Errorf("the <keyspace> argument must be used to specify at least one keyspace when calling the RebuildKeyspaceGraph command")
	}

	var cellArray []string
	if *cells != "" {
		cellArray = strings.Split(*cells, ",")
	}

	keyspaces, err := keyspaceParamsToKeyspaces(ctx, wr, subFlags.Args())

So I believe the topology path portion of this function is dead code anyway.

Copy link
Author

Choose a reason for hiding this comment

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

From the overall code perspective, I don't see any indication that this was intentional. If what you're saying is true, then any param in the params that starts with a '/', would cause the entire params to be appended to the result, even if one of the params is an empty string, or a wildcard matching path.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, from looking at the code I think the idea was that the parameters to that function could be from the RebuildKeyspaceGraph command, in which case they are keyspaces. Or it could be from some other function, which doesn't seem to exist any longer, and in which case the parameters were topology paths.

Copy link
Contributor

Choose a reason for hiding this comment

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

So I'd rather we remove that block entirely since it's dead code. But also, it's dead code in a deprecated client so I'm also fine leaving it as-is.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you want to just remove that IF/ELSE block along with what was in the IF condition? We'd achieve the same goal here while also removing dead code. Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

I don't know the whole logic of the vitess, so I can't do the things that fix it by removing the dead code, you can removing the dead code and close this PR.

@mattlord
Copy link
Contributor

You'll need to rebase your PR on origin/main or merge in origin/main to fix the test failures you're seeing here.

@mattlord mattlord added Type: Internal Cleanup Component: vtctl and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jul 11, 2024
@alingse
Copy link
Author

alingse commented Jul 12, 2024

You'll need to rebase your PR on origin/main or merge in origin/main to fix the test failures you're seeing here.

thank for notice, I have sync the commits from vitess:main

@mattlord mattlord closed this Jul 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants