Skip to content

Fix cpuallocated value in findHostsForMIgration api#4220

Merged
yadvr merged 2 commits intoapache:4.14from
ravening:cpuallocated
Aug 5, 2020
Merged

Fix cpuallocated value in findHostsForMIgration api#4220
yadvr merged 2 commits intoapache:4.14from
ravening:cpuallocated

Conversation

@ravening
Copy link
Copy Markdown
Member

@ravening ravening commented Jul 22, 2020

Description

Fixes #4221

The findHostsForMigration api displays 0% always for
cpuallocated field which is wrong

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Screenshots (if appropriate):

How Has This Been Tested?

Tested from cloudmonkey api

Before the fix

local) > find hostsformigration virtualmachineid=01f155-fffc-493f-95b8-85a76414 filter=cpuallocated,suitableformigration
{
  "count": 221,
  "host": [
    {
      "cpuallocated": "0%",
      "suitableformigration": true
    },
    {
      "cpuallocated": "0%",
      "suitableformigration": true
    },
    {
      "cpuallocated": "0%",
      "suitableformigration": true
    },

After the fix

(local) mgt01 > list hosts  virtualmachineid=95b4270f-1354-4703-99cb-a769400b2d30 filter=cpuallocated,suitableformigration,name
{
  "count": 3,
  "host": [
    {
      "cpuallocated": "2.27%",
      "name": "node32",
      "suitableformigration": true
    },
    {
      "cpuallocated": "1.14%",
      "name": "node33",
      "suitableformigration": true
    }
  ]
}

The findHostsForMigration api displays 0% always for
cpuallocated field which is wrong
Copy link
Copy Markdown
Member

@GabrielBrascher GabrielBrascher left a comment

Choose a reason for hiding this comment

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

I hope to be helpful with the raised code enhancement suggestion.

Thanks for the PR @ravening.

Float cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId()));
String cpuAlloc = decimalFormat.format(((float)cpu / cpuWithOverprovisioning * 100f)).toString() + "%";
float cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId()));
String cpuAlloc = decimalFormat.format(((float)cpu / cpuWithOverprovisioning * 100f)) + "%";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see 100f a couple of times on the code, there is also 100.0f.

What do you think of extracting these magic 100f/100.0f into a constant?

Another approach that would be even nicer would be to have these pieces extracted to a method like calculateResourceAllocatedPercentage, and add a few unit test case methods.

public String calculateResourceAllocatedPercentage(float resource, float resourceWithOverprovisioning) {
	    return decimalFormat.format(((float)resource / resourceWithOverprovisioning * 100f)) + **"%"**
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@GabrielBrascher I made the changes. Also can you look into the issue #4221 ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@ravening thanks +1 :)

Can you please take a look at line 314, I think that it could also benefit from the created method calculateResourceAllocatedPercentage.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@GabrielBrascher I didn't make changes in line 314 because it has different behavior compared to line 165.
I have mentioned the same in issue #4221 also. list hosts api returns absolute value where as findhostsformigration returns percentage. If we decided what to return then I can make changes in these two places also

Copy link
Copy Markdown
Member

@GabrielBrascher GabrielBrascher Aug 4, 2020

Choose a reason for hiding this comment

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

@ravening sorry for the delay. I think that I was not clear about my point.

  • the method that you extract L464:
    decimalFormat.format(((float)resource / resourceWithOverProvision * 100.0f)) + "%"

  • line 314 that I mentioned:
    decimalFormat.format((float) mem / memWithOverprovisioning * 100.0f) +"%"

That is why I thought that it could also benefit from calculateResourceAllocatedPercentage.

@ravening ravening requested a review from GabrielBrascher July 23, 2020 08:21
@yadvr
Copy link
Copy Markdown
Member

yadvr commented Jul 29, 2020

@blueorangutan package

@blueorangutan
Copy link
Copy Markdown

@rhtyd a Jenkins job has been kicked to build packages. I'll keep you posted as I make progress.

@blueorangutan
Copy link
Copy Markdown

Packaging result: ✔centos7 ✔debian. JID-1623

@DaanHoogland
Copy link
Copy Markdown
Contributor

@blueorangutan test

@blueorangutan
Copy link
Copy Markdown

@DaanHoogland a Trillian-Jenkins test job (centos7 mgmt + kvm-centos7) has been kicked to run smoke tests

@andrijapanicsb
Copy link
Copy Markdown
Contributor

(tests failed, re-run after the weekend due to weekend maintenance)

@yadvr
Copy link
Copy Markdown
Member

yadvr commented Aug 4, 2020

@blueorangutan test

@blueorangutan
Copy link
Copy Markdown

@rhtyd a Trillian-Jenkins test job (centos7 mgmt + kvm-centos7) has been kicked to run smoke tests

Copy link
Copy Markdown
Member

@yadvr yadvr left a comment

Choose a reason for hiding this comment

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

LGTM, did not manually test it

@ravening
Copy link
Copy Markdown
Member Author

ravening commented Aug 4, 2020

LGTM, did not manually test it

@rhtyd @DaanHoogland its not yet ready for merge. Im waiting for @GabrielBrascher reply for my question.
If you can also reply it then it will be helpful

@DaanHoogland
Copy link
Copy Markdown
Contributor

@ravening if your question is whether a percentage or an absolute number is to be returned i would really not care. I don't think that the two API have to return the same type of value. I do think that the difference should be clear from the name of the parameter and hence both could be returned if so desired.
if that was not your question please correct me.

@GabrielBrascher
Copy link
Copy Markdown
Member

@DaanHoogland @ravening I am good with the code as it is, I just saw that maybe the method could be also used in the other context considering how similar those lines are.

Note that even DecimalFormat decimalFormat = new DecimalFormat("#.##"); is duplicated and used on the same way for cpuAllocated and memoryAllocated. However I don't want to stall this PR with that observation.

@blueorangutan
Copy link
Copy Markdown

Trillian test result (tid-2253)
Environment: kvm-centos7 (x2), Advanced Networking with Mgmt server 7
Total time taken: 54624 seconds
Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr4220-t2253-kvm-centos7.zip
Intermittent failure detected: /marvin/tests/smoke/test_certauthority_root.py
Intermittent failure detected: /marvin/tests/smoke/test_kubernetes_clusters.py
Intermittent failure detected: /marvin/tests/smoke/test_kubernetes_supported_versions.py
Smoke tests completed. 82 look OK, 1 have error(s)
Only failed tests results shown below:

Test Result Time (s) Test File
test_01_add_delete_kubernetes_supported_version Error 1807.11 test_kubernetes_supported_versions.py

@yadvr yadvr merged commit a529470 into apache:4.14 Aug 5, 2020
@yadvr
Copy link
Copy Markdown
Member

yadvr commented Aug 5, 2020

@ravening sorry I missed your comment, and merged based on @GabrielBrascher 's I am good with the code as it is. Do you need to do more work on this PR, should we revert? cc @DaanHoogland

@ravening
Copy link
Copy Markdown
Member Author

ravening commented Aug 5, 2020

@ravening sorry I missed your comment, and merged based on @GabrielBrascher 's I am good with the code as it is. Do you need to do more work on this PR, should we revert? cc @DaanHoogland

Since it's ok for them that two functions returns different value, it's fine with me as well

@yadvr
Copy link
Copy Markdown
Member

yadvr commented Aug 5, 2020

Alright I'll not revert it, thnx for replying.

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.

7 participants