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

Parse /_cat/shards output #1840

Merged
merged 7 commits into from
Oct 2, 2019
Merged

Conversation

barkbay
Copy link
Contributor

@barkbay barkbay commented Oct 2, 2019

Fix #1796
This PR also removes some unnecessary code from #1778

@@ -133,6 +134,16 @@ func (s Shards) GetShardsByNode() map[string]Shards {
return result
}

// fixNodeNames extracts the name of the node from the output of the /_cat/shards API call
// see https://github.com/elastic/cloud-on-k8s/issues/1796
func (s Shards) fixNodeNames() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I find it a bit weird/dangerous to have this belong to and mutate the Shards object (which is not a pointer receiver).
We must not forget to call it otherwise the shards we get are not correct?

Another way of doing it is attaching our own UnmarshalJSON function to the Shards struct, which would get called automatically when parsing the API response. In that function we do call the default json unmarshallling function (giving us Shards with extra stuff in node names), and then patch the shards with the logic you used here. Related: http://choly.ca/post/go-json-marshalling/ to avoid the infinite JSON unmarshalling loop.

This would maybe be a bit safer (no need to think about "do I need to call fixNodeNames()?").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shards is a slice so the underlying array is updated even if it is not strictly a pointer receiver.
I didn't want to introduce a custom parser because I thought that at some point you may want to get the original data. I'll have a look to add it.

Copy link
Contributor

@sebgl sebgl left a comment

Choose a reason for hiding this comment

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

LGTM

"node": ""
}]`

func TestShards_fixNodeNames(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe adapt the function name

if err := json.Unmarshal(data, &aux); err != nil {
return err
}
for i, shard := range *aux {
Copy link
Contributor

Choose a reason for hiding this comment

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

Even though we have the link to the issue think I'd still appreciate a comment like:
// Strip extra information from the nodeName field (eg. "cluster-node-2 -> 10.56.2.33 8DqGuLtrSNyMfE2EfKNDgg" becomes "cluster-node-2").

@barkbay
Copy link
Contributor Author

barkbay commented Oct 2, 2019

For the records I have successfully run this PR against TestMutationLessNodes several times with the following stacks:

  • 6.8.2
  • 7.1.1
  • 7.4.0

@barkbay barkbay merged commit 6bed42b into elastic:master Oct 2, 2019
@barkbay barkbay added v1.0.0-beta1 >bug Something isn't working labels Oct 2, 2019
@barkbay barkbay deleted the follow-up-fix-1750-rc branch October 2, 2019 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug Something isn't working v1.0.0-beta1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

_cat/shards does not (only) returns the node name
2 participants