Github user chtyim commented on a diff in the pull request:
https://github.com/apache/incubator-twill/pull/52#discussion_r34333689
--- Diff:
twill-core/src/main/java/org/apache/twill/internal/AbstractTwillController.java
---
@@ -109,6 +123,74 @@ public final ServiceDiscovered discoverService(String
serviceName) {
return sendMessage(SystemMessages.setInstances(runnable, newCount),
newCount);
}
+ @Override
+ public final ListenableFuture<String> restartAllInstances(String
runnableName) {
+ Command updateStateCommand =
Command.Builder.of(Constants.RESTART_ALL_RUNNABLE_INSTANCES).
+ build();
+ Message message =
SystemMessages.updateRunnableInstances(updateStateCommand, runnableName);
+ return sendMessage(message, updateStateCommand.getCommand());
+ }
+
+ @Override
+ public final ListenableFuture<Map<String, Set<Integer>>>
restartInstances(Map<String,
+ Set<Integer>> runnableToInstanceIds) {
+ for (Map.Entry<String, Set<Integer>> entry :
runnableToInstanceIds.entrySet()) {
+ validateInstanceIds(entry.getKey(), entry.getValue());
+ }
+
+ Map<String, String> runnableToStringInstanceIds =
+ Maps.transformEntries(runnableToInstanceIds, new
Maps.EntryTransformer<String, Set<Integer>, String>() {
+ @Override
+ public String transformEntry(String key, Set<Integer> value) {
+ return GSON.toJson(value, new TypeToken<Set<Integer>>()
{}.getType());
+ }
+ });
+
+ Command updateStateCommand =
Command.Builder.of(Constants.RESTART_RUNNABLES_INSTANCES).
+ addOptions(runnableToStringInstanceIds).build();
+ Message message =
SystemMessages.updateRunnablesInstances(updateStateCommand);
+
+ return sendMessage(message, runnableToInstanceIds);
+ }
+
+ @Override
+ public ListenableFuture<String> restartInstances(String runnable, int
instanceId, int... moreInstanceIds) {
+ Set<Integer> instanceIds = Sets.newLinkedHashSet();
+ instanceIds.add(instanceId);
+ for (int id : moreInstanceIds) {
+ instanceIds.add(id);
+ }
+
+ validateInstanceIds(runnable, instanceIds);
+
+ Map<String, String> runnableToStringInstanceIds = Maps.newHashMap();
+ runnableToStringInstanceIds.put(runnable, GSON.toJson(instanceIds, new
TypeToken<Set<Integer>>() {}.getType()));
+
+ Command updateStateCommand =
Command.Builder.of(Constants.RESTART_RUNNABLES_INSTANCES).
+ addOptions(runnableToStringInstanceIds).build();
+ Message message =
SystemMessages.updateRunnablesInstances(updateStateCommand);
+
+ return sendMessage(message, runnable);
+ }
+
+ private void validateInstanceIds(String runnable, Set<Integer>
instanceIds) {
+ ResourceReport rr = getResourceReport();
+ Collection<TwillRunResources> runnableResources =
rr.getRunnableResources(runnable);
+ if (runnableResources != null) {
+ Set<Integer> existingInstanceIds = Sets.newHashSet();
+ for (TwillRunResources trr : runnableResources) {
+ existingInstanceIds.add(trr.getInstanceId());
+ }
+ for (int instanceId : instanceIds) {
+ if (!existingInstanceIds.contains(instanceId)) {
+ throw new IllegalArgumentException("Unable to find instance id "
+ instanceId + " for " + runnable);
+ }
+ }
+ } else {
+ throw new RuntimeException("Unable to verify run resources for
runnable " + runnable);
--- End diff --
Prefer to check and throw first, which gives easier to read code:
```java
if (runnableResources == null) {
throw new RuntimeException(...);
}
// Normal logic.
```
---
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.
---