[ 
https://issues.apache.org/jira/browse/YARN-9359?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

WEI-HSIAO-LEE reassigned YARN-9359:
-----------------------------------

    Assignee: WEI-HSIAO-LEE

> Avoid code duplication in Resources for calculation methods
> -----------------------------------------------------------
>
>                 Key: YARN-9359
>                 URL: https://issues.apache.org/jira/browse/YARN-9359
>             Project: Hadoop YARN
>          Issue Type: Improvement
>            Reporter: Szilard Nemeth
>            Assignee: WEI-HSIAO-LEE
>            Priority: Minor
>              Labels: newbie, newbie++
>
> This is a follow-up for YARN-9318, dealing with code duplication issueas, as 
> discussed with [~templedf] earlier.
> Resources has many very similar calculation methods like addTo, subtractFrom, 
> multiply, etc.
> These are having extractable code as common, the only difference could be the 
> calculation they perform on the passed Resource object(s).
> These methods either receive one or two Resource objects and make some 
> calculations on these.
> One caveat that needs some attention is that some of them do clone the 
> Resource and do the calculation on the cloned resource and return the result 
> (leaving the passed Resource alone) and some of them perform the calculation 
> on the passed Resource object itself.
> The common code could be extracted like this: 
> {code:java}
> private static Resource applyFunctionOnValues(Resource lhs,
>       Function<Long, Long> valueFunction) {
>     int numResources = ResourceUtils.getNumberOfCountableResourceTypes();
>     for (int i = 0; i < numResources; i++) {
>       try {
>         ResourceInformation lhsValue = lhs.getResourceInformation(i);
>         Long modifiedValue = valueFunction.apply(lhsValue.getValue());
>         lhs.setResourceValue(i, modifiedValue);
>       } catch (ResourceNotFoundException ye) {
>         LOG.warn("Resource is missing:" + ye.getMessage());
>       }
>     }
>     return lhs;
>   }
> {code}
> And an example code could be like this: 
> {code:java}
> public static Resource multiplyAndRoundUp(Resource lhs, double by) {
>     return applyFunctionOnValues(clone(lhs),
>         (value) -> (long) Math.ceil(value * by));
>   }
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: yarn-issues-unsubscr...@hadoop.apache.org
For additional commands, e-mail: yarn-issues-h...@hadoop.apache.org

Reply via email to