Skip to content
This repository has been archived by the owner on May 24, 2018. It is now read-only.

CDSK-873 - remove builds by 'brid'. Add logging and tests #351

Merged

Conversation

warcholprzemo
Copy link

  1. remove dict from unclaimedBrdicts / resumeBrdicts by 'brid' key instead full dict
  2. Log if the method does not remove anything from first or second list
  3. Add tests for removeBuildRequest

@coveralls
Copy link

coveralls commented Apr 11, 2018

Coverage Status

Coverage increased (+0.01%) to 68.544% when pulling debf674 on CDSK-873-upgrade-KatanaBuildChooser-removeBuildRequests into 460306a on staging.


msg = "removeBuildRequest does not remove anything. breq.brid: %s" % breq.brdict['brid']
if not is_removed:
klog.err_json(msg)
Copy link
Member

Choose a reason for hiding this comment

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

That looks more complicated than necessary :)

I thought about something like this (unless you see something that wouldn't work):

def removeBuildRequest(self, breq):
    # reset the checkMerges in case the breq still in the master cache
    def remove_dict(brdicts):
        for brdict in brdicts:
            if brdict['brid'] == breq.brdict['brid']:
                brdicts.remove(brdict)
                return True
        return False

    if not breq.brdict:
        pass # log some error here
    
    removed = remove_dict(self.unclaimedBrdicts) or remove_dict(self.resumeBrdicts)
    if removed:
        breq.checkMerges = True
        breq.retries = 0
        breq.hasBeenMerged = False
        self.breqCache.remove(breq.id)
    else:
        klog.err_json(
            "removeBuildRequest does not remove anything. breq.brid: %s" % \
            breq.brdict['brid']
        )

Copy link
Author

Choose a reason for hiding this comment

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

list comprehension removes all dicts with the ['brid'] so we don't leave a possible mess in memory. In your case we remove only one dict from list. @Mowinski What do you think?

Copy link
Member

@campos-ddc campos-ddc Apr 12, 2018

Choose a reason for hiding this comment

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

They shouldn't show up multiple times in there, but you can do it like this to feel safer:

def remove_dict(brdicts):
    removed = False
    for brdict in brdicts:
        if brdict['brid'] == breq.brdict['brid']:
            brdicts.remove(brdict)
            removed = True
    return removed

Copy link
Member

Choose a reason for hiding this comment

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

I'm not a huge fan of replacing the entire list with a comprehension because I don't know if anyone else has a hard reference to the old list.

Choose a reason for hiding this comment

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

As I said below, I am not a fan of inner function in this place :) and I agree with Diogo, whats more, as I see we modify the property of class so even we do not need an inner function to do it and a private method should be enough to do it.

Copy link
Author

Choose a reason for hiding this comment

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

OK, I've used Diogo's code

@@ -422,13 +422,35 @@ def claimBuildRequests(self, breqs):

def removeBuildRequest(self, breq):
# reset the checkMerges in case the breq still in the master cache

def remove_dict(brdicts, breq):

Choose a reason for hiding this comment

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

I think that using an inner function in methods is a little bit unpractical. There are three cases when we should use the inner function:

  1. To separate assertion/parameters checking from recursive code
  2. To encapsulate function, if there is a reason to hide it from the global scope
  3. To create factory function, that using closure.

removeBuildRequest isn't recursive function so the first point doesn't affect it.
This is also a method, so if you want to make it private, you can create remove_dict as a private method in the class.
And the last point, this method isn't factory, so using closure variable is an unwanted side effect.

So in my opinion, you can create remove_dict to a static method or a utils function and do not worry about scope :)

Here is link to great article about inner function: https://realpython.com/inner-functions-what-are-they-good-for/

Choose a reason for hiding this comment

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

Of course, it would be nice to add docstring and a little bit change name of this function, now it suggests that from brdicts remove dictionary. Ensure that u want to modify parameters or just return a copy.

self.resumeBrdicts = resume_data['brdicts']
is_removed |= resume_data['is_removed']

msg = "removeBuildRequest does not remove anything. breq.brid: %s" % breq.brdict['brid']

Choose a reason for hiding this comment

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

minor: this msg can be in if statement.

chooser = self.brd.katanaBuildChooser
chooser.unclaimedBrdicts = [{'brid': 1}, {'brid': 2}]
chooser.resumeBrdicts = [{'brid': 3}, {'brid': 4}]
breq = mock.Mock()

Choose a reason for hiding this comment

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

In this case, you do not need mock, you can use simple stub

BregClass = namedtuple('Breg', ['brdict'])
breq = BregClass(brdict={'brid': 3})

But please be aware and ensure you create all fields, which is used and checked in this test (as far as I see, checkMerges, retries and hasBeenMerged has no initialization)

@warcholprzemo warcholprzemo force-pushed the CDSK-873-upgrade-KatanaBuildChooser-removeBuildRequests branch from 270e478 to 8bee438 Compare April 13, 2018 07:24
if self.resumeBrdicts and breq.brdict and breq.brdict in self.resumeBrdicts:
self.resumeBrdicts.remove(breq.brdict)
self.breqCache.remove(breq.id)
def __remove_brdict_by_brid(self, brdicts, buildrequest):

Choose a reason for hiding this comment

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

Minor: You can move it to bottom of this class, just only for readability

Copy link
Author

Choose a reason for hiding this comment

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

Done

@warcholprzemo warcholprzemo merged commit 98e0e99 into staging Apr 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants