Search in sources :

Example 31 with InstanceMetaData

use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData in project cloudbreak by hortonworks.

the class StopStartDownscaleDecommissionViaCMHandler method updateInstanceStatuses.

private void updateInstanceStatuses(Map<String, InstanceMetaData> instanceMetadataMap, Set<String> fqdnsRemoved, InstanceStatus instanceStatus, String statusReason) {
    for (String fqdn : fqdnsRemoved) {
        InstanceMetaData instanceMetaData = instanceMetadataMap.get(fqdn);
        if (instanceMetaData == null) {
            throw new RuntimeException(String.format("Unexpected fqdn decommissioned. Not present in requsted list. unexpected fqdn=[%s], ExpectedSet=[%s]", fqdn, instanceMetadataMap));
        }
        instanceMetaDataService.updateInstanceStatus(instanceMetaData, instanceStatus, statusReason);
    }
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)

Example 32 with InstanceMetaData

use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData 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);
    }
}
Also used : Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) ClusterDecomissionService(com.sequenceiq.cloudbreak.cluster.api.ClusterDecomissionService) StopStartDownscaleDecommissionViaCMResult(com.sequenceiq.cloudbreak.reactor.api.event.orchestration.StopStartDownscaleDecommissionViaCMResult) CLUSTER_SCALING_STOPSTART_DOWNSCALE_ENTEREDCMMAINTMODE(com.sequenceiq.cloudbreak.event.ResourceEvent.CLUSTER_SCALING_STOPSTART_DOWNSCALE_ENTEREDCMMAINTMODE) LoggerFactory(org.slf4j.LoggerFactory) EventSelectorUtil(com.sequenceiq.flow.event.EventSelectorUtil) Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) HostGroupService(com.sequenceiq.cloudbreak.service.hostgroup.HostGroupService) StringUtils(org.apache.commons.lang3.StringUtils) Inject(javax.inject.Inject) ExceptionCatcherEventHandler(com.sequenceiq.flow.reactor.api.handler.ExceptionCatcherEventHandler) UPDATE_IN_PROGRESS(com.sequenceiq.cloudbreak.api.endpoint.v4.common.Status.UPDATE_IN_PROGRESS) CLUSTER_SCALING_STOPSTART_DOWNSCALE_ENTERINGCMMAINTMODE(com.sequenceiq.cloudbreak.event.ResourceEvent.CLUSTER_SCALING_STOPSTART_DOWNSCALE_ENTERINGCMMAINTMODE) Event(reactor.bus.Event) CloudbreakFlowMessageService(com.sequenceiq.cloudbreak.core.flow2.stack.CloudbreakFlowMessageService) Map(java.util.Map) InstanceMetaDataService(com.sequenceiq.cloudbreak.service.stack.InstanceMetaDataService) LinkedList(java.util.LinkedList) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) Logger(org.slf4j.Logger) InstanceStatus(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.InstanceStatus) Set(java.util.Set) HandlerEvent(com.sequenceiq.flow.reactor.api.handler.HandlerEvent) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) StopStartDownscaleDecommissionViaCMRequest(com.sequenceiq.cloudbreak.reactor.api.event.cluster.StopStartDownscaleDecommissionViaCMRequest) List(java.util.List) Component(org.springframework.stereotype.Component) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) ClusterApiConnectors(com.sequenceiq.cloudbreak.service.cluster.ClusterApiConnectors) Optional(java.util.Optional) Collections(java.util.Collections) StackService(com.sequenceiq.cloudbreak.service.stack.StackService) StopStartDownscaleDecommissionViaCMResult(com.sequenceiq.cloudbreak.reactor.api.event.orchestration.StopStartDownscaleDecommissionViaCMResult) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup) StopStartDownscaleDecommissionViaCMRequest(com.sequenceiq.cloudbreak.reactor.api.event.cluster.StopStartDownscaleDecommissionViaCMRequest) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) ClusterDecomissionService(com.sequenceiq.cloudbreak.cluster.api.ClusterDecomissionService)

Example 33 with InstanceMetaData

use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData in project cloudbreak by hortonworks.

the class RestartClusterManagerServerHandler method restartCMServer.

private void restartCMServer(Stack stack) throws Exception {
    Cluster cluster = stack.getCluster();
    InstanceMetaData gatewayInstance = stack.getPrimaryGatewayInstance();
    GatewayConfig gatewayConfig = gatewayConfigService.getGatewayConfig(stack, gatewayInstance, cluster.hasGateway());
    Set<String> gatewayFQDN = Collections.singleton(gatewayInstance.getDiscoveryFQDN());
    ExitCriteriaModel exitModel = ClusterDeletionBasedExitCriteriaModel.clusterDeletionBasedModel(stack.getId(), cluster.getId());
    Set<Node> nodes = stackUtil.collectReachableNodes(stack);
    Set<String> agentTargets = nodes.stream().map(Node::getHostname).collect(Collectors.toSet());
    hostOrchestrator.updateAgentCertDirectoryPermission(gatewayConfig, agentTargets, nodes, exitModel);
    hostOrchestrator.restartClusterManagerOnMaster(gatewayConfig, gatewayFQDN, nodes, exitModel);
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) ExitCriteriaModel(com.sequenceiq.cloudbreak.orchestrator.state.ExitCriteriaModel) ClusterDeletionBasedExitCriteriaModel(com.sequenceiq.cloudbreak.core.bootstrap.service.ClusterDeletionBasedExitCriteriaModel) Node(com.sequenceiq.cloudbreak.common.orchestration.Node) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) GatewayConfig(com.sequenceiq.cloudbreak.orchestrator.model.GatewayConfig)

Example 34 with InstanceMetaData

use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData in project cloudbreak by hortonworks.

the class DatabaseBackupHandler method doAccept.

@Override
protected Selectable doAccept(HandlerEvent<DatabaseBackupRequest> event) {
    DatabaseBackupRequest request = event.getData();
    Selectable result;
    Long stackId = request.getResourceId();
    LOGGER.debug("Backing up database on stack {}, backup id {}", stackId, request.getBackupId());
    try {
        Stack stack = stackService.getByIdWithListsInTransaction(stackId);
        Cluster cluster = stack.getCluster();
        InstanceMetaData gatewayInstance = stack.getPrimaryGatewayInstance();
        GatewayConfig gatewayConfig = gatewayConfigService.getGatewayConfig(stack, gatewayInstance, cluster.hasGateway());
        Set<String> gatewayFQDN = Collections.singleton(gatewayInstance.getDiscoveryFQDN());
        ExitCriteriaModel exitModel = ClusterDeletionBasedExitCriteriaModel.clusterDeletionBasedModel(stackId, cluster.getId());
        String rangerAdminGroup = rangerVirtualGroupService.getRangerVirtualGroup(stack);
        SaltConfig saltConfig = saltConfigGenerator.createSaltConfig(request.getBackupLocation(), request.getBackupId(), rangerAdminGroup, request.getCloseConnections(), stack);
        hostOrchestrator.backupDatabase(gatewayConfig, gatewayFQDN, stackUtil.collectReachableNodes(stack), saltConfig, exitModel);
        result = new DatabaseBackupSuccess(stackId);
    } catch (Exception e) {
        LOGGER.error("Database backup event failed", e);
        result = new DatabaseBackupFailedEvent(stackId, e, DetailedStackStatus.DATABASE_BACKUP_FAILED);
    }
    return result;
}
Also used : ExitCriteriaModel(com.sequenceiq.cloudbreak.orchestrator.state.ExitCriteriaModel) ClusterDeletionBasedExitCriteriaModel(com.sequenceiq.cloudbreak.core.bootstrap.service.ClusterDeletionBasedExitCriteriaModel) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) DatabaseBackupRequest(com.sequenceiq.cloudbreak.reactor.api.event.cluster.dr.backup.DatabaseBackupRequest) SaltConfig(com.sequenceiq.cloudbreak.orchestrator.model.SaltConfig) DatabaseBackupFailedEvent(com.sequenceiq.cloudbreak.reactor.api.event.cluster.dr.backup.DatabaseBackupFailedEvent) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) DatabaseBackupSuccess(com.sequenceiq.cloudbreak.reactor.api.event.cluster.dr.backup.DatabaseBackupSuccess) GatewayConfig(com.sequenceiq.cloudbreak.orchestrator.model.GatewayConfig)

Example 35 with InstanceMetaData

use of com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData 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);
    }
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) Logger(org.slf4j.Logger) RegisterPublicDnsRequest(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.RegisterPublicDnsRequest) LoggerFactory(org.slf4j.LoggerFactory) EventSelectorUtil(com.sequenceiq.flow.event.EventSelectorUtil) Set(java.util.Set) HandlerEvent(com.sequenceiq.flow.reactor.api.handler.HandlerEvent) Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) RegisterPublicDnsSuccess(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.RegisterPublicDnsSuccess) InstanceGroupService(com.sequenceiq.cloudbreak.service.stack.InstanceGroupService) ClusterPublicEndpointManagementService(com.sequenceiq.cloudbreak.service.publicendpoint.ClusterPublicEndpointManagementService) GatewayPublicEndpointManagementService(com.sequenceiq.cloudbreak.service.publicendpoint.GatewayPublicEndpointManagementService) Inject(javax.inject.Inject) List(java.util.List) Component(org.springframework.stereotype.Component) ExceptionCatcherEventHandler(com.sequenceiq.flow.reactor.api.handler.ExceptionCatcherEventHandler) Event(reactor.bus.Event) InstanceMetaData(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData) InstanceMetaDataService(com.sequenceiq.cloudbreak.service.stack.InstanceMetaDataService) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup) RegisterPublicDnsFailure(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.RegisterPublicDnsFailure) RegisterPublicDnsRequest(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.RegisterPublicDnsRequest) RegisterPublicDnsSuccess(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.RegisterPublicDnsSuccess) RegisterPublicDnsFailure(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.RegisterPublicDnsFailure) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup)

Aggregations

InstanceMetaData (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)415 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)165 InstanceGroup (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup)152 Test (org.junit.jupiter.api.Test)143 Map (java.util.Map)92 HashSet (java.util.HashSet)90 Set (java.util.Set)86 List (java.util.List)84 HostGroup (com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup)77 Cluster (com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster)73 Collectors (java.util.stream.Collectors)71 ArrayList (java.util.ArrayList)62 Test (org.junit.Test)60 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)57 Optional (java.util.Optional)52 StackService (com.sequenceiq.cloudbreak.service.stack.StackService)48 Inject (javax.inject.Inject)47 Logger (org.slf4j.Logger)47 LoggerFactory (org.slf4j.LoggerFactory)47 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)45