NSMutableArray<MInstance> runningInstances = new NSMutableArray<MInstance>();
NSMutableSet<MHost> activeHosts = new NSMutableSet<MHost>();
NSMutableDictionary<MHost, NSMutableArray<MInstance>> inactiveInstancesByHost = new NSMutableDictionary<MHost, NSMutableArray<MInstance>>();
NSMutableDictionary<MHost, NSMutableArray<MInstance>> activeInstancesByHost = new NSMutableDictionary<MHost, NSMutableArray<MInstance>>();
for (MInstance instance : instances) {
MHost host = instance.host();
if (instance.isRunning_M()) {
runningInstances.addObject(instance);
activeHosts.addObject(host);
NSMutableArray<MInstance> currentInstances = activeInstancesByHost.objectForKey(host);
if (currentInstances == null) {
currentInstances = new NSMutableArray<MInstance>();
activeInstancesByHost.setObjectForKey(currentInstances, host);
}
currentInstances.addObject(instance);
} else {
NSMutableArray<MInstance> currentInstances = inactiveInstancesByHost.objectForKey(host);
if (currentInstances == null) {
currentInstances = new NSMutableArray<MInstance>();
inactiveInstancesByHost.setObjectForKey(currentInstances, host);
}
currentInstances.addObject(instance);
}
}
if (inactiveInstancesByHost.isEmpty()) {
addObjectsFromArrayIfAbsentToErrorMessageArray(
new NSArray<String>("You must have at least one inactive instance to perform a graceful bounce."));
return;
}
int numToStartPerHost = 1;
if (activeHosts.count() > 0) {
numToStartPerHost = (int) (runningInstances.count() / activeHosts.count() * .1);
}
if (numToStartPerHost < 1) {
numToStartPerHost = 1;
}
boolean useScheduling = true;
for (MInstance instance : runningInstances) {
useScheduling &= instance.schedulingEnabled() != null && instance.schedulingEnabled().booleanValue();
}
NSMutableArray<MInstance> startingInstances = new NSMutableArray<MInstance>();
for (int i = 0; i < numToStartPerHost; i++) {
for (MHost host : activeHosts) {
NSArray<MInstance> inactiveInstances = inactiveInstancesByHost.objectForKey(host);
if (inactiveInstances != null && inactiveInstances.count() >= i) {
MInstance instance = inactiveInstances.objectAtIndex(i);
log("Starting inactive instance " + instance.displayName() + " on host " + host.addressAsString());
startingInstances.addObject(instance);
} else {
log("Not enough inactive instances on host: " + host.addressAsString());
}
}
}
for (MInstance instance : startingInstances) {
if (useScheduling) {