use of com.sequenceiq.flow.reactor.api.handler.HandlerEvent in project cloudbreak by hortonworks.
the class StopStartDownscaleDecommissionViaCMHandler method doAccept.
@Override
protected Selectable doAccept(HandlerEvent<StopStartDownscaleDecommissionViaCMRequest> event) {
StopStartDownscaleDecommissionViaCMRequest request = event.getData();
LOGGER.info("StopStartDownscaleDecommissionViaCMHandler for: {}, {}", event.getData().getResourceId(), event.getData());
try {
Stack stack = stackService.getByIdWithLists(request.getResourceId());
Cluster cluster = stack.getCluster();
ClusterDecomissionService clusterDecomissionService = clusterApiConnectors.getConnector(stack).clusterDecomissionService();
Set<String> hostNames = getHostNamesForPrivateIds(request.getInstanceIdsToDecommission(), stack);
LOGGER.debug("Attempting to decommission hosts. count={}, hostnames={}", hostNames.size(), hostNames);
HostGroup hostGroup = hostGroupService.getByClusterIdAndName(cluster.getId(), request.getHostGroupName()).orElseThrow(NotFoundException.notFound("hostgroup", request.getHostGroupName()));
Map<String, InstanceMetaData> hostsToRemove = clusterDecomissionService.collectHostsToRemove(hostGroup, hostNames);
List<String> missingHostsInCm = Collections.emptyList();
if (hostNames.size() != hostsToRemove.size()) {
missingHostsInCm = hostNames.stream().filter(h -> !hostsToRemove.containsKey(h)).collect(Collectors.toList());
LOGGER.info("Found fewer instances in CM to decommission, as compared to initial ask. foundCount={}, initialCount={}, missingHostsInCm={}", hostsToRemove.size(), hostNames.size(), missingHostsInCm);
}
// TODO CB-14929: Potentially put the nodes into maintenance mode before decommissioning?
// TODO CB-15132: Eventually, try parsing the results of the CM decommission, and see if a partial decommission went through in the
// timebound specified.
Set<String> decommissionedHostNames = Collections.emptySet();
if (hostsToRemove.size() > 0) {
decommissionedHostNames = clusterDecomissionService.decommissionClusterNodesStopStart(hostsToRemove, POLL_FOR_10_MINUTES);
updateInstanceStatuses(hostsToRemove, decommissionedHostNames, InstanceStatus.DECOMMISSIONED, "decommission requested for instances");
}
// This doesn't handle failures. It handles scenarios where CM list APIs don't have the necessary hosts available.
List<String> allMissingHostnames = null;
if (missingHostsInCm.size() > 0) {
allMissingHostnames = new LinkedList<>(missingHostsInCm);
}
if (hostsToRemove.size() != decommissionedHostNames.size()) {
Set<String> finalDecommissionedHostnames = decommissionedHostNames;
List<String> additionalMissingDecommissionHostnames = hostsToRemove.keySet().stream().filter(h -> !finalDecommissionedHostnames.contains(h)).collect(Collectors.toList());
LOGGER.info("Decommissioned fewer instances than requested. decommissionedCount={}, expectedCount={}, initialCount={}, notDecommissioned=[{}]", decommissionedHostNames.size(), hostsToRemove.size(), hostNames.size(), additionalMissingDecommissionHostnames);
if (allMissingHostnames == null) {
allMissingHostnames = new LinkedList<>();
}
allMissingHostnames.addAll(additionalMissingDecommissionHostnames);
}
LOGGER.info("hostsDecommissioned: count={}, hostNames={}", decommissionedHostNames.size(), decommissionedHostNames);
if (decommissionedHostNames.size() > 0) {
LOGGER.debug("Attempting to put decommissioned hosts into maintenance mode. count={}", decommissionedHostNames.size());
flowMessageService.fireEventAndLog(stack.getId(), UPDATE_IN_PROGRESS.name(), CLUSTER_SCALING_STOPSTART_DOWNSCALE_ENTERINGCMMAINTMODE, String.valueOf(decommissionedHostNames.size()));
clusterDecomissionService.enterMaintenanceMode(decommissionedHostNames);
flowMessageService.fireEventAndLog(stack.getId(), UPDATE_IN_PROGRESS.name(), CLUSTER_SCALING_STOPSTART_DOWNSCALE_ENTEREDCMMAINTMODE, String.valueOf(decommissionedHostNames.size()));
LOGGER.debug("Successfully put decommissioned hosts into maintenance mode. count={}", decommissionedHostNames.size());
} else {
LOGGER.debug("No nodes decommissioned, hence no nodes being put into maintenance mode");
}
return new StopStartDownscaleDecommissionViaCMResult(request, decommissionedHostNames, allMissingHostnames);
} catch (Exception e) {
// TODO CB-15132: This can be improved based on where and when the Exception occurred to potentially rollback certain aspects.
// ClusterClientInitException is one which is explicitly thrown.
String message = "Failed while attempting to decommission nodes via CM";
LOGGER.error(message, e);
return new StopStartDownscaleDecommissionViaCMResult(message, e, request);
}
}
use of com.sequenceiq.flow.reactor.api.handler.HandlerEvent in project cloudbreak by hortonworks.
the class RegisterPublicDnsHandler method doAccept.
@Override
protected Selectable doAccept(HandlerEvent<RegisterPublicDnsRequest> event) {
RegisterPublicDnsRequest request = event.getData();
Stack stack = request.getStack();
if (gatewayPublicEndpointManagementService.isCertRenewalTriggerable(stack)) {
try {
LOGGER.debug("Fetching instance group and instance metadata for stack.");
InstanceGroup instanceGroup = instanceGroupService.getPrimaryGatewayInstanceGroupByStackId(stack.getId());
List<InstanceMetaData> instanceMetaData = instanceMetaDataService.findAliveInstancesInInstanceGroup(instanceGroup.getId());
if (!instanceMetaData.isEmpty()) {
stack.getInstanceGroups().stream().filter(ig -> ig.getId().equals(instanceGroup.getId())).forEach(ig -> ig.setInstanceMetaData(Set.copyOf(instanceMetaData)));
LOGGER.debug("Registering load balancer public DNS entry");
boolean success = clusterPublicEndpointManagementService.provisionLoadBalancer(stack);
if (!success) {
throw new CloudbreakException("Public DNS registration resulted in failed state. Please consult DNS registration logs.");
}
LOGGER.debug("Load balancer public DNS registration was successful");
return new RegisterPublicDnsSuccess(stack);
} else {
throw new CloudbreakException("Unable to find instance metadata for primary instance group. Certificates cannot " + "be updated.");
}
} catch (Exception e) {
LOGGER.warn("Failed to register load balancer public DNS entries.", e);
return new RegisterPublicDnsFailure(request.getResourceId(), e);
}
} else {
LOGGER.info("Certificates and DNS are not managed by PEM for stack {}. Skipping public DNS registration.", stack.getName());
return new RegisterPublicDnsSuccess(stack);
}
}
use of com.sequenceiq.flow.reactor.api.handler.HandlerEvent in project cloudbreak by hortonworks.
the class CreateCloudLoadBalancersHandler method doAccept.
@Override
protected Selectable doAccept(HandlerEvent<CreateCloudLoadBalancersRequest> event) {
CreateCloudLoadBalancersRequest request = event.getData();
CloudContext cloudContext = request.getCloudContext();
try {
LOGGER.info("Updating cloud stack with load balancer network information");
CloudStack origCloudStack = request.getCloudStack();
Stack stack = request.getStack();
CloudStack updatedCloudStack = new CloudStack(origCloudStack.getGroups(), cloudStackConverter.buildNetwork(stack), origCloudStack.getImage(), origCloudStack.getParameters(), origCloudStack.getTags(), origCloudStack.getTemplate(), origCloudStack.getInstanceAuthentication(), origCloudStack.getLoginUserName(), origCloudStack.getPublicKey(), origCloudStack.getFileSystem().orElse(null), origCloudStack.getLoadBalancers());
CloudConnector<Object> connector = cloudPlatformConnectors.get(cloudContext.getPlatformVariant());
AuthenticatedContext ac = connector.authentication().authenticate(cloudContext, request.getCloudCredential());
LOGGER.debug("Initiating cloud load balancer creation");
List<CloudResourceStatus> resourceStatus = connector.resources().launchLoadBalancers(ac, updatedCloudStack, persistenceNotifier);
LOGGER.debug("Waiting for cloud load balancers to be fully created");
List<CloudResource> resources = ResourceLists.transform(resourceStatus);
PollTask<ResourcesStatePollerResult> task = statusCheckFactory.newPollResourcesStateTask(ac, resources, true);
ResourcesStatePollerResult statePollerResult = ResourcesStatePollerResults.build(cloudContext, resourceStatus);
if (!task.completed(statePollerResult)) {
syncPollingScheduler.schedule(task);
}
if (resourceStatus.stream().anyMatch(CloudResourceStatus::isFailed)) {
Set<String> names = resourceStatus.stream().filter(CloudResourceStatus::isFailed).map(r -> r.getCloudResource().getName()).collect(Collectors.toSet());
throw new CloudbreakException("Creation failed for load balancers: " + names);
}
Set<String> types = updatedCloudStack.getLoadBalancers().stream().map(CloudLoadBalancer::getType).map(LoadBalancerType::toString).collect(Collectors.toSet());
LOGGER.info("Cloud load balancer creation for load balancer types {} successful", types);
return new CreateCloudLoadBalancersSuccess(stack);
} catch (Exception e) {
LOGGER.warn("Failed to created cloud load balance resources.", e);
return new CreateCloudLoadBalancersFailure(request.getResourceId(), e);
}
}
use of com.sequenceiq.flow.reactor.api.handler.HandlerEvent 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);
}
}
use of com.sequenceiq.flow.reactor.api.handler.HandlerEvent in project cloudbreak by hortonworks.
the class StopStartDownscaleDecommissionViaCMHandlerTest method testErrorFromCmHostCollection.
@Test
void testErrorFromCmHostCollection() {
int instancesToDecommissionCount = 5;
int expcetedInstanceToCollectCount = 5;
int expectedInstancesDecommissionedCount = 5;
List<InstanceMetaData> instancesToDecommission = getInstancesToDecommission(instancesToDecommissionCount);
HostGroup hostGroup = createHostGroup(instancesToDecommission);
Map<String, InstanceMetaData> collected = instancesToDecommission.stream().limit(expcetedInstanceToCollectCount).collect(Collectors.toMap(i -> i.getDiscoveryFQDN(), i -> i));
List<InstanceMetaData> decommissionedMetadataList = collected.values().stream().limit(expectedInstancesDecommissionedCount).collect(Collectors.toList());
Set<String> fqdnsDecommissioned = decommissionedMetadataList.stream().map(InstanceMetaData::getDiscoveryFQDN).collect(Collectors.toUnmodifiableSet());
Set<Long> instanceIdsToDecommission = instancesToDecommission.stream().map(InstanceMetaData::getPrivateId).collect(Collectors.toUnmodifiableSet());
Set<String> hostnamesToDecommission = instancesToDecommission.stream().map(InstanceMetaData::getDiscoveryFQDN).collect(Collectors.toUnmodifiableSet());
setupAdditionalMocks(hostGroup, instancesToDecommission, collected, fqdnsDecommissioned);
when(clusterDecomissionService.collectHostsToRemove(eq(hostGroup), eq(hostnamesToDecommission))).thenThrow(new RuntimeException("collectHostsToDecommissionError"));
StopStartDownscaleDecommissionViaCMRequest request = new StopStartDownscaleDecommissionViaCMRequest(1L, INSTANCE_GROUP_NAME, instanceIdsToDecommission);
HandlerEvent handlerEvent = new HandlerEvent(Event.wrap(request));
Selectable selectable = underTest.doAccept(handlerEvent);
verify(clusterDecomissionService).collectHostsToRemove(eq(hostGroup), eq(hostnamesToDecommission));
assertThat(selectable).isInstanceOf(StopStartDownscaleDecommissionViaCMResult.class);
StopStartDownscaleDecommissionViaCMResult result = (StopStartDownscaleDecommissionViaCMResult) selectable;
assertThat(result.getNotDecommissionedHostFqdns()).hasSize(0);
assertThat(result.getDecommissionedHostFqdns()).hasSize(0);
assertThat(result.getErrorDetails().getMessage()).isEqualTo("collectHostsToDecommissionError");
assertThat(result.getStatus()).isEqualTo(EventStatus.FAILED);
assertThat(result.selector()).isEqualTo("STOPSTARTDOWNSCALEDECOMMISSIONVIACMRESULT_ERROR");
verifyNoMoreInteractions(instanceMetaDataService);
verifyNoMoreInteractions(flowMessageService);
verifyNoMoreInteractions(clusterDecomissionService);
}
Aggregations