Search in sources :

Example 16 with ProfileRequirements

use of io.fabric8.api.ProfileRequirements in project fabric8 by jboss-fuse.

the class AutoScaleController method autoScaleProfile.

private void autoScaleProfile(FabricService service, final ContainerAutoScaler autoScaler, FabricRequirements requirements, ProfileRequirements profileRequirement, AutoScaleStatus status) {
    final String profile = profileRequirement.getProfile();
    Integer minimumInstances = profileRequirement.getMinimumInstances();
    Integer maximumInstances = profileRequirement.getMaximumInstances();
    String requirementsVersion = requirements.getVersion();
    final String version = Strings.isNotBlank(requirementsVersion) ? requirementsVersion : service.getDefaultVersionId();
    if (maximumInstances != null || minimumInstances != null) {
        if (maximumInstances != null) {
            List<Container> containers = Containers.aliveAndSuccessfulContainersForProfile(version, profile, service);
            int count = containers.size();
            int delta = count - maximumInstances;
            if (delta > 0) {
                stopContainers(containers, autoScaler, requirements, profileRequirement, status, delta);
            }
        }
        if (minimumInstances != null) {
            // lets check if we need to provision more
            List<Container> containers = Containers.aliveOrPendingContainersForProfile(version, profile, service);
            int count = containers.size();
            int delta = minimumInstances - count;
            try {
                AutoScaleProfileStatus profileStatus = status.profileStatus(profile);
                if (delta < 0) {
                    FabricService fs = this.fabricService.get();
                    if (fs != null) {
                        profileStatus.destroyingContainer();
                        for (int i = delta; i < 0; i++) {
                            while (!containers.isEmpty()) {
                                Container container = containers.remove(0);
                                if (container.getId().startsWith("auto_")) {
                                    fs.destroyContainer(container);
                                    break;
                                }
                            }
                        }
                    }
                } else if (delta > 0) {
                    if (AutoScalers.requirementsSatisfied(service, version, requirements, profileRequirement, status)) {
                        profileStatus.creatingContainer();
                        final AutoScaleRequest command = new AutoScaleRequest(service, version, profile, delta, requirements, profileRequirement, status);
                        new Thread("Creating container for " + command.getProfile()) {

                            @Override
                            public void run() {
                                try {
                                    autoScaler.createContainers(command);
                                } catch (Exception e) {
                                    LOGGER.error("Failed to create container of profile: " + profile + ". Caught: " + e, e);
                                }
                            }
                        }.start();
                    }
                } else {
                    profileStatus.provisioned();
                }
            } catch (Exception e) {
                LOGGER.error("Failed to auto-scale " + profile + ". Caught: " + e, e);
            }
        }
    }
}
Also used : Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) AutoScaleProfileStatus(io.fabric8.api.AutoScaleProfileStatus) AutoScaleRequest(io.fabric8.api.AutoScaleRequest)

Example 17 with ProfileRequirements

use of io.fabric8.api.ProfileRequirements in project fabric8 by jboss-fuse.

the class AutoScaleController method autoScale.

private void autoScale() {
    FabricService service = fabricService.get();
    FabricRequirements requirements = service.getRequirements();
    List<ProfileRequirements> profileRequirements = requirements.getProfileRequirements();
    if (profileRequirements != null && !profileRequirements.isEmpty()) {
        AutoScaleStatus status = new AutoScaleStatus();
        for (ProfileRequirements profileRequirement : profileRequirements) {
            ContainerAutoScaler autoScaler = createAutoScaler(requirements, profileRequirement);
            if (autoScaler != null) {
                autoScaleProfile(service, autoScaler, requirements, profileRequirement, status);
            } else {
                LOGGER.warn("No ContainerAutoScaler available for profile " + profileRequirement.getProfile());
            }
        }
        if (zkMasterCache != null) {
            try {
                String json = RequirementsJson.toJSON(status);
                String zkPath = ZkPath.AUTO_SCALE_STATUS.getPath();
                zkMasterCache.setStringData(zkPath, json, CreateMode.EPHEMERAL);
            } catch (Exception e) {
                LOGGER.warn("Failed to write autoscale status " + e, e);
            }
        } else {
            LOGGER.warn("No ZooKeeperMasterCache!");
        }
    }
}
Also used : ProfileRequirements(io.fabric8.api.ProfileRequirements) FabricService(io.fabric8.api.FabricService) AutoScaleStatus(io.fabric8.api.AutoScaleStatus) ContainerAutoScaler(io.fabric8.api.ContainerAutoScaler) FabricRequirements(io.fabric8.api.FabricRequirements)

Example 18 with ProfileRequirements

use of io.fabric8.api.ProfileRequirements in project fabric8 by jboss-fuse.

the class ProfileResource method setRequirements.

@POST
@Path("requirements")
public void setRequirements(ProfileRequirements profileRequirements) throws IOException {
    FabricService service = getFabricService();
    FabricRequirements requirements = service.getRequirements();
    if (requirements != null) {
        requirements.addOrUpdateProfileRequirements(profileRequirements);
        service.setRequirements(requirements);
    }
}
Also used : FabricService(io.fabric8.api.FabricService) FabricRequirements(io.fabric8.api.FabricRequirements) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 19 with ProfileRequirements

use of io.fabric8.api.ProfileRequirements in project fabric8 by jboss-fuse.

the class SshAutoScalerTest method assertProfilesUseSeparateHost.

/**
 * lets assert that no host has more than its maximum number of containers
 */
public static void assertProfilesUseSeparateHost(FabricRequirements requirements, Map<String, CountingMap> hostToProfileCounts) {
    for (Map.Entry<String, CountingMap> entry : hostToProfileCounts.entrySet()) {
        String hostName = entry.getKey();
        CountingMap counts = entry.getValue();
        Set<String> keys = counts.keySet();
        for (String profileId : keys) {
            int count = counts.count(profileId);
            // lets see if we have a maximum number of profile count
            ProfileRequirements profileRequirement = requirements.getOrCreateProfileRequirement(profileId);
            Integer maximum = profileRequirement.getMaximumInstancesPerHost();
            if (maximum != null) {
                assertTrue("Host " + hostName + " has " + count + " instances of " + profileId + " but this is configured to have a maximium of " + maximum + " per host", count <= maximum);
            }
        }
    }
}
Also used : CountingMap(io.fabric8.utils.CountingMap) ProfileRequirements(io.fabric8.api.ProfileRequirements) CountingMap(io.fabric8.utils.CountingMap) Map(java.util.Map)

Example 20 with ProfileRequirements

use of io.fabric8.api.ProfileRequirements in project fabric8 by jboss-fuse.

the class SshAutoScaler method chooseHostContainerOptions.

/**
 * This method is public for easier testing
 */
public static CreateSshContainerOptions.Builder chooseHostContainerOptions(AutoScaleRequest request, HostProfileCounter hostProfileCounter) {
    CreateSshContainerOptions.Builder builder = CreateSshContainerOptions.builder();
    FabricRequirements requirements = request.getFabricRequirements();
    ProfileRequirements profileRequirements = request.getProfileRequirements();
    SshScalingRequirements sshScalingRequirements = profileRequirements.getSshScalingRequirements();
    List<SshHostConfiguration> hosts = requirements.getSshHosts();
    SortedSet<LoadSortedHostConfiguration<SshHostConfiguration>> sortedHostConfigurations = AutoScalers.filterHosts(profileRequirements, sshScalingRequirements, hostProfileCounter, hosts);
    SshHostConfiguration sshHostConfig = null;
    if (!sortedHostConfigurations.isEmpty()) {
        LoadSortedHostConfiguration<SshHostConfiguration> first = sortedHostConfigurations.first();
        sshHostConfig = first.getConfiguration();
    }
    if (sshHostConfig == null) {
        LOG.warn("Could not create version " + request.getVersion() + " profile " + request.getProfile() + " as no matching hosts could be found for " + sshScalingRequirements);
        request.getProfileAutoScaleStatus().noSuitableHost("" + sshScalingRequirements);
        return null;
    }
    builder.configure(sshHostConfig, requirements, profileRequirements);
    return builder;
}
Also used : ProfileRequirements(io.fabric8.api.ProfileRequirements) SshHostConfiguration(io.fabric8.api.SshHostConfiguration) FabricRequirements(io.fabric8.api.FabricRequirements) LoadSortedHostConfiguration(io.fabric8.internal.autoscale.LoadSortedHostConfiguration) SshScalingRequirements(io.fabric8.api.SshScalingRequirements)

Aggregations

ProfileRequirements (io.fabric8.api.ProfileRequirements)15 FabricRequirements (io.fabric8.api.FabricRequirements)8 Container (io.fabric8.api.Container)5 ArrayList (java.util.ArrayList)4 FabricService (io.fabric8.api.FabricService)3 Profile (io.fabric8.api.Profile)3 AutoScaleProfileStatus (io.fabric8.api.AutoScaleProfileStatus)2 AutoScaleRequest (io.fabric8.api.AutoScaleRequest)2 ContainerAutoScaler (io.fabric8.api.ContainerAutoScaler)2 ProfileService (io.fabric8.api.ProfileService)2 Version (io.fabric8.api.Version)2 MQBrokerConfigDTO (io.fabric8.api.jmx.MQBrokerConfigDTO)2 MQBrokerStatusDTO (io.fabric8.api.jmx.MQBrokerStatusDTO)2 CountingMap (io.fabric8.utils.CountingMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 DownloadManager (io.fabric8.agent.download.DownloadManager)1 Feature (io.fabric8.agent.model.Feature)1 AutoScaleStatus (io.fabric8.api.AutoScaleStatus)1 ChildScalingRequirements (io.fabric8.api.ChildScalingRequirements)1