Github user chtyim commented on a diff in the pull request:

    https://github.com/apache/incubator-twill/pull/58#discussion_r35899172
  
    --- Diff: 
twill-yarn/src/main/java/org/apache/twill/internal/appmaster/ApplicationMasterService.java
 ---
    @@ -835,35 +850,57 @@ private boolean handleRestartRunnablesInstances(final 
Message message, final Run
                                                               new 
TypeToken<Set<Integer>>() {}.getType());
     
             LOG.debug("Start restarting runnable {} instances {}", 
runnableName, restartedInstanceIds);
    -        restartRunnableInstances(runnableName, restartedInstanceIds);
    +        restartRunnableInstances(runnableName, restartedInstanceIds, 
completion);
           }
         }
     
    -    completion.run();
         return true;
       }
     
       /**
        * Helper method to restart instances of runnables.
        */
    -  private void restartRunnableInstances(String runnableName, @Nullable 
Set<Integer> instanceIds) {
    -    LOG.debug("Begin restart runnable {} instances.", runnableName);
    +  private void restartRunnableInstances(String runnableName, @Nullable 
Set<Integer> instanceIds,
    +                                        final Runnable completion) {
    +    Runnable restartInstancesRunnable = 
createRestartInstancesRunnable(runnableName, instanceIds, completion);
    +    instanceChangeExecutor.execute(restartInstancesRunnable);
    +  }
     
    -    Set<Integer> instancesToRemove = instanceIds;
    -    if (instancesToRemove == null) {
    -      instancesToRemove = Ranges.closedOpen(0, 
runningContainers.count(runnableName)).asSet(DiscreteDomains.integers());
    -    }
    +  /**
    +   * Creates a Runnable for execution of restart instances request.
    +   */
    +  private Runnable createRestartInstancesRunnable(final String 
runnableName, @Nullable final Set<Integer> instanceIds,
    +                                                  final Runnable 
completion) {
    +    return new Runnable() {
    +      @Override
    +      public void run() {
    +        LOG.debug("Begin restart runnable {} instances.", runnableName);
     
    -    for (int instanceId : instancesToRemove) {
    -      LOG.debug("Remove instance {} for runnable {}", instanceId, 
runnableName);
    -      try {
    -        runningContainers.removeById(runnableName, instanceId);
    -      } catch (Exception ex) {
    -        // could be thrown if the container already stopped.
    -        LOG.info("Exception thrown when stopping instance {} probably 
already stopped.", instanceId);
    +        Set<Integer> instancesToRemove = instanceIds;
    +        if (instancesToRemove == null) {
    +          instancesToRemove =
    +            Ranges.closedOpen(0, 
runningContainers.count(runnableName)).asSet(DiscreteDomains.integers());
    +        }
    +
    +        LOG.info("Restarting instances {} for runnable {}", 
instancesToRemove, runnableName);
    +        RunnableContainerRequest containerRequest =
    +          createRunnableContainerRequest(runnableName, 
instancesToRemove.size(), false);
    +        runnableContainerRequests.add(containerRequest);
    +
    +        for (int instanceId : instancesToRemove) {
    +          LOG.debug("Remove instance {} for runnable {}", instanceId, 
runnableName);
    +          try {
    +            runningContainers.removeById(runnableName, instanceId);
    +          } catch (Exception ex) {
    +            // could be thrown if the container already stopped.
    +            LOG.info("Exception thrown when stopping instance {} probably 
already stopped.", instanceId);
    +          } finally {
    --- End diff --
    
    Should this finally be outside of the for loop? There is only one instance 
of `constainerRequest` and it seems unnecessary to set the ready to true for 
every loop but rather do it just once when the looping finished.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to