Search in sources :

Example 1 with AutoScaleRequest

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

the class ChildAutoScaler method chooseRootContainer.

protected String chooseRootContainer(AutoScaleRequest request, List<String> containerIds) {
    ChildScalingRequirements scalingRequirements = getChildScalingRequirements(request);
    if (scalingRequirements != null) {
        List<String> rootContainerPatterns = scalingRequirements.getRootContainerPatterns();
        if (rootContainerPatterns != null && !rootContainerPatterns.isEmpty()) {
            Filter<String> filter = Filters.createStringFilters(rootContainerPatterns);
            List<String> matchingRootContainers = Filters.filter(containerIds, filter);
            return Filters.matchRandomElement(matchingRootContainers);
        }
    }
    return Filters.matchRandomElement(containerIds);
}
Also used : ChildScalingRequirements(io.fabric8.api.ChildScalingRequirements)

Example 2 with AutoScaleRequest

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

the class SshAutoScalerTest method assertSshAutoScale.

public static HostProfileCounter assertSshAutoScale(FabricRequirements requirements, AutoScaleStatus status) {
    HostProfileCounter hostProfileCounter = new HostProfileCounter();
    String version = requirements.getVersion();
    if (Strings.isEmpty(version)) {
        version = "1.0";
    }
    List<ProfileRequirements> profileRequirements = requirements.getProfileRequirements();
    for (ProfileRequirements profileRequirement : profileRequirements) {
        Integer minimumInstances = profileRequirement.getMinimumInstances();
        if (minimumInstances != null) {
            for (int i = 0; i < minimumInstances; i++) {
                String profileId = profileRequirement.getProfile();
                AutoScaleRequest request = new AutoScaleRequest(null, version, profileId, 1, requirements, profileRequirement, status);
                CreateSshContainerOptions.Builder builder = chooseHostContainerOptions(request, hostProfileCounter);
                assertNotNull("Should have found a builder for " + profileId, builder);
                String host = builder.getHost();
                hostProfileCounter.incrementContainers(host);
                hostProfileCounter.incrementProfileCount(host, profileId);
            }
        }
    }
    Map<String, CountingMap> hostToProfileCounts = hostProfileCounter.getHostToProfileCounts();
    assertProfilesUseSeparateHost(requirements, hostToProfileCounts);
    assertMaximumContainerCountNotExceeded(requirements, hostToProfileCounts);
    return hostProfileCounter;
}
Also used : CountingMap(io.fabric8.utils.CountingMap) ProfileRequirements(io.fabric8.api.ProfileRequirements) AutoScaleRequest(io.fabric8.api.AutoScaleRequest) HostProfileCounter(io.fabric8.internal.autoscale.HostProfileCounter)

Example 3 with AutoScaleRequest

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

the class ChildAutoScaler method getChildScalingRequirements.

protected ChildScalingRequirements getChildScalingRequirements(AutoScaleRequest request) {
    ChildScalingRequirements scalingRequirements = null;
    ProfileRequirements profileRequirements = request.getProfileRequirements();
    if (profileRequirements != null) {
        scalingRequirements = profileRequirements.getChildScalingRequirements();
    }
    return scalingRequirements;
}
Also used : ProfileRequirements(io.fabric8.api.ProfileRequirements) ChildScalingRequirements(io.fabric8.api.ChildScalingRequirements)

Example 4 with AutoScaleRequest

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

the class ChildAutoScaler method createContainers.

@Override
public void createContainers(AutoScaleRequest request) throws Exception {
    int count = request.getDelta();
    String profile = request.getProfile();
    String version = request.getVersion();
    FabricService fabricService = request.getFabricService();
    CreateChildContainerOptions.Builder builder = null;
    if (fabricService != null) {
        builder = createAutoScaleOptions(request, fabricService);
    }
    if (builder != null) {
        Set<String> ignoreContainerNames = new HashSet<>();
        for (int i = 0; i < count; i++) {
            final CreateChildContainerOptions.Builder configuredBuilder = builder.number(1).version(version).profiles(profile);
            Container[] containers = fabricService.getContainers();
            NameValidator nameValidator = Containers.createNameValidator(containers);
            String name = Containers.createAutoScaleContainerName(containers, profile, containerProvider.getScheme(), nameValidator);
            ignoreContainerNames.add(name);
            CreateChildContainerOptions options = configuredBuilder.name(name).build();
            LOG.info("Creating container name " + name + " version " + version + " profile " + profile + " " + count + " container(s)");
            fabricService.createContainers(options);
        }
    } else {
        LOG.warn("Could not create version " + version + " profile " + profile + " due to missing autoscale configuration");
    }
}
Also used : Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) CreateChildContainerOptions(io.fabric8.api.CreateChildContainerOptions) NameValidator(io.fabric8.api.NameValidator) HashSet(java.util.HashSet)

Example 5 with AutoScaleRequest

use of io.fabric8.api.AutoScaleRequest 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)

Aggregations

Container (io.fabric8.api.Container)4 FabricService (io.fabric8.api.FabricService)4 NameValidator (io.fabric8.api.NameValidator)3 ProfileRequirements (io.fabric8.api.ProfileRequirements)3 AutoScaleRequest (io.fabric8.api.AutoScaleRequest)2 ChildScalingRequirements (io.fabric8.api.ChildScalingRequirements)2 FabricRequirements (io.fabric8.api.FabricRequirements)2 HostProfileCounter (io.fabric8.internal.autoscale.HostProfileCounter)2 AutoScaleProfileStatus (io.fabric8.api.AutoScaleProfileStatus)1 CreateChildContainerOptions (io.fabric8.api.CreateChildContainerOptions)1 SshHostConfiguration (io.fabric8.api.SshHostConfiguration)1 SshScalingRequirements (io.fabric8.api.SshScalingRequirements)1 LoadSortedHostConfiguration (io.fabric8.internal.autoscale.LoadSortedHostConfiguration)1 CountingMap (io.fabric8.utils.CountingMap)1 HashSet (java.util.HashSet)1