private ConcurrentTaskExecutor resourceBuilderExecutor;
@Override
public void accept(Event<UpdateInstancesRequest> event) {
UpdateInstancesRequest request = event.getData();
final CloudPlatform cloudPlatform = request.getCloudPlatform();
Long stackId = request.getStackId();
Integer scalingAdjustment = request.getScalingAdjustment();
final Stack stack = stackRepository.findOneWithLists(stackId);
MDCBuilder.buildMdcContext(stack);
try {
LOGGER.info("Accepted {} event on stack.", ReactorConfig.UPDATE_INSTANCES_REQUEST_EVENT);
stackUpdater.updateMetadataReady(stackId, false);
if (scalingAdjustment > 0) {
if (cloudPlatform.isWithTemplate()) {
cloudPlatformConnectors.get(cloudPlatform)
.addInstances(stack, userDataBuilder.build(cloudPlatform, stack.getHash(), new HashMap<String, String>()), scalingAdjustment);
} else {
ResourceBuilderInit resourceBuilderInit = resourceBuilderInits.get(cloudPlatform);
final ProvisionContextObject pCO =
resourceBuilderInit.provisionInit(stack, userDataBuilder.build(cloudPlatform, stack.getHash(), new HashMap<String, String>()));
for (ResourceBuilder resourceBuilder : networkResourceBuilders.get(cloudPlatform)) {
pCO.getNetworkResources().addAll(stack.getResourcesByType(resourceBuilder.resourceType()));
}
List<Future<List<Resource>>> futures = new ArrayList<>();
Set<Resource> resourceSet = new HashSet<>();
for (int i = stack.getNodeCount(); i < stack.getNodeCount() + scalingAdjustment; i++) {
final int index = i;
Future<List<Resource>> submit = resourceBuilderExecutor.submit(new Callable<List<Resource>>() {
@Override
public List<Resource> call() throws Exception {
List<Resource> resources = new ArrayList<>();
for (final ResourceBuilder resourceBuilder : instanceResourceBuilders.get(cloudPlatform)) {
List<Resource> resourceList = resourceBuilder.create(pCO, index, resources);
resources.addAll(resourceList);
}
return resources;
}
});
futures.add(submit);
}
for (Future<List<Resource>> future : futures) {
try {
resourceSet.addAll(future.get());
} catch (Exception e) {
throw new BuildStackFailureException(e.getMessage(), e, resourceSet);
}
}
LOGGER.info("Publishing {} event.", ReactorConfig.ADD_INSTANCES_COMPLETE_EVENT);
reactor.notify(ReactorConfig.ADD_INSTANCES_COMPLETE_EVENT,
Event.wrap(new AddInstancesComplete(cloudPlatform, stack.getId(), resourceSet)));
}
} else {
Set<String> instanceIds = new HashSet<>();
int i = 0;
for (InstanceMetaData metadataEntry : stack.getInstanceMetaData()) {
if (metadataEntry.isRemovable()) {
instanceIds.add(metadataEntry.getInstanceId());
if (++i >= scalingAdjustment * -1) {
break;
}
}
}
if (cloudPlatform.isWithTemplate()) {
cloudPlatformConnectors.get(cloudPlatform).removeInstances(stack, instanceIds);
} else {
ResourceBuilderInit resourceBuilderInit = resourceBuilderInits.get(cloudPlatform);
final DeleteContextObject dCO = resourceBuilderInit.deleteInit(stack);