use of com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.CreateLoadBalancerEntityRequest in project cloudbreak by hortonworks.
the class CreateLoadBalancerEntityHandler method doAccept.
@Override
protected Selectable doAccept(HandlerEvent<CreateLoadBalancerEntityRequest> event) {
CreateLoadBalancerEntityRequest request = event.getData();
Stack stack = stackService.getById(request.getResourceId());
try {
LOGGER.debug("Creating load balancer entity objects for stack {}", stack.getId());
DetailedEnvironmentResponse environment = environmentClientService.getByCrn(stack.getEnvironmentCrn());
if (environment != null && environment.getNetwork() != null && PublicEndpointAccessGateway.ENABLED.equals(environment.getNetwork().getPublicEndpointAccessGateway())) {
enableEndpointGateway(stack, environment);
}
Set<InstanceGroup> instanceGroups = instanceGroupService.getByStackAndFetchTemplates(stack.getId());
instanceGroups.forEach(ig -> ig.setTargetGroups(targetGroupPersistenceService.findByInstanceGroupId(ig.getId())));
Set<LoadBalancer> existingLoadBalancers = loadBalancerPersistenceService.findByStackId(stack.getId());
stack.setInstanceGroups(instanceGroups);
Set<LoadBalancer> newLoadBalancers = loadBalancerConfigService.createLoadBalancers(stack, environment, null);
Stack savedStack;
if (doLoadBalancersAlreadyExist(existingLoadBalancers, newLoadBalancers)) {
LOGGER.debug("Load balancer entities already exist. Continuing flow.");
savedStack = stack;
} else {
LOGGER.debug("Persisting stack and load balancer objects to database.");
stack.setLoadBalancers(newLoadBalancers);
String stackName = stack.getName();
savedStack = measure(() -> stackService.save(stack), LOGGER, "Stackrepository save took {} ms for stack {}", stackName);
measure(() -> loadBalancerPersistenceService.saveAll(newLoadBalancers), LOGGER, "Load balancers saved in {} ms for stack {}", stackName);
measure(() -> targetGroupPersistenceService.saveAll(newLoadBalancers.stream().flatMap(lb -> lb.getTargetGroupSet().stream()).collect(Collectors.toSet())), LOGGER, "Target groups saved in {} ms for stack {}", stackName);
measure(() -> instanceGroupService.saveAll(newLoadBalancers.stream().flatMap(lb -> lb.getAllInstanceGroups().stream()).collect(Collectors.toSet())), LOGGER, "Instance groups saved in {} ms for stack {}", stackName);
if (stack.getNetwork() != null) {
measure(() -> networkService.pureSave(stack.getNetwork()), LOGGER, "Network saved in {} ms for stack {}", stackName);
}
}
LOGGER.debug("Load balancer entities successfully persisted.");
savedStack.setInstanceGroups(newLoadBalancers.stream().flatMap(lb -> lb.getAllInstanceGroups().stream()).collect(Collectors.toSet()));
savedStack.setNetwork(stack.getNetwork());
return new CreateLoadBalancerEntitySuccess(request.getResourceId(), savedStack);
} catch (Exception e) {
LOGGER.warn("Failed create load balancer entities and persist them to the database.", e);
return new CreateLoadBalancerEntityFailure(request.getResourceId(), e);
}
}
Aggregations