Search in sources :

Example 11 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project devspaces-images by redhat-developer.

the class LimitsCheckingWorkspaceManagerTest method shouldThrowLimitExceedExceptionIfAccountDoesNotHaveEnoughAvailableRuntimeResource.

@Test(expectedExceptions = LimitExceededException.class, expectedExceptionsMessageRegExp = "You are not allowed to start more workspaces\\.")
public void shouldThrowLimitExceedExceptionIfAccountDoesNotHaveEnoughAvailableRuntimeResource() throws Exception {
    // given
    doThrow(new NoEnoughResourcesException(emptyList(), emptyList(), emptyList())).when(resourceManager).checkResourcesAvailability(any(), any());
    doReturn(singletonList(new ResourceImpl(RuntimeResourceType.ID, 5, RuntimeResourceType.UNIT))).when(resourceManager).getTotalResources(anyString());
    LimitsCheckingWorkspaceManager manager = managerBuilder().setResourceManager(resourceManager).build();
    // when
    manager.checkRuntimeResourceAvailability(ACCOUNT_ID);
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Test(org.testng.annotations.Test)

Example 12 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project devspaces-images by redhat-developer.

the class LimitsCheckingWorkspaceManagerTest method shouldThrowLimitExceedExceptionIfAccountDoesNotHaveEnoughAvailableWorkspaceResource.

@Test(expectedExceptions = LimitExceededException.class, expectedExceptionsMessageRegExp = "You are not allowed to create more workspaces\\.")
public void shouldThrowLimitExceedExceptionIfAccountDoesNotHaveEnoughAvailableWorkspaceResource() throws Exception {
    // given
    doThrow(new NoEnoughResourcesException(emptyList(), emptyList(), emptyList())).when(resourceManager).checkResourcesAvailability(any(), any());
    doReturn(singletonList(new ResourceImpl(WorkspaceResourceType.ID, 5, WorkspaceResourceType.UNIT))).when(resourceManager).getTotalResources(anyString());
    LimitsCheckingWorkspaceManager manager = managerBuilder().setResourceManager(resourceManager).build();
    // when
    manager.checkWorkspaceResourceAvailability(ACCOUNT_ID);
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Test(org.testng.annotations.Test)

Example 13 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project devspaces-images by redhat-developer.

the class LimitsCheckingWorkspaceManager method checkRamResourcesAvailability.

@VisibleForTesting
void checkRamResourcesAvailability(String accountId, String namespace, WorkspaceConfig config, @Nullable String envName) throws NotFoundException, ServerException, ConflictException {
    if (config.getEnvironments().isEmpty()) {
        return;
    }
    final Environment environment = config.getEnvironments().get(firstNonNull(envName, config.getDefaultEnv()));
    final ResourceImpl ramToUse = new ResourceImpl(RamResourceType.ID, environmentRamCalculator.calculate(environment), RamResourceType.UNIT);
    try {
        resourceManager.checkResourcesAvailability(accountId, singletonList(ramToUse));
    } catch (NoEnoughResourcesException e) {
        final Resource requiredRam = // starting of workspace requires only RAM resource
        e.getRequiredResources().get(0);
        final Resource availableRam = getResourceOrDefault(e.getAvailableResources(), RamResourceType.ID, 0, RamResourceType.UNIT);
        final Resource usedRam = getResourceOrDefault(resourceManager.getUsedResources(accountId), RamResourceType.ID, 0, RamResourceType.UNIT);
        throw new LimitExceededException(format("Workspace %s/%s needs %s to start. Your account has %s available and %s in use. " + "The workspace can't be start. Stop other workspaces or grant more resources.", namespace, config.getName(), printResourceInfo(requiredRam), printResourceInfo(availableRam), printResourceInfo(usedRam)));
    }
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 14 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project devspaces-images by redhat-developer.

the class ResourceAggregator method deduct.

/**
 * Returns list which is result of deduction {@code resourceToDeduct} from {@code
 * sourceResources}.
 *
 * @param sourceResources the source resources
 * @param resourcesToDeduct the resources which should be deducted from {@code sourceResources}
 * @throws NoEnoughResourcesException when {@code sourceResources} list doesn't contain enough
 *     resources
 * @throws IllegalArgumentException when {@code sourceResources} or {@code resourcesToDeduct}
 *     contain resource with not supported type
 */
public List<? extends Resource> deduct(List<? extends Resource> sourceResources, List<? extends Resource> resourcesToDeduct) throws NoEnoughResourcesException {
    checkSupporting(sourceResources);
    checkSupporting(resourcesToDeduct);
    final Map<String, Resource> result = sourceResources.stream().collect(Collectors.toMap(Resource::getType, Function.identity()));
    final List<Resource> missingResources = new ArrayList<>();
    for (Resource toDeduct : resourcesToDeduct) {
        final Resource sourceResource = result.get(toDeduct.getType());
        if (sourceResource != null) {
            try {
                result.put(toDeduct.getType(), deduct(sourceResource, toDeduct));
            } catch (NoEnoughResourcesException e) {
                result.remove(toDeduct.getType());
                missingResources.addAll(e.getMissingResources());
            }
        } else {
            missingResources.add(toDeduct);
        }
    }
    if (!missingResources.isEmpty()) {
        throw new NoEnoughResourcesException(sourceResources, resourcesToDeduct, missingResources);
    }
    return new ArrayList<>(result.values());
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList)

Example 15 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project devspaces-images by redhat-developer.

the class ResourceAggregator method excess.

/**
 * Returns list which contains resources from specified {@code sourceResources} which have
 * excessive amount in compare to specified {@code resourcesToCompare}.
 *
 * <p>Example :
 *
 * <pre>
 * |      \      | Source    | To compare| Result   |
 * |:------------|:----------|:----------|:---------|
 * | Resource1   | 5         | 3         | 2        |
 * | ----------- | --------- | --------- | -------- |
 * | Resource2   | -         | 9         | -        |
 * | ----------- | --------- | --------- | -------- |
 * | Resource3   | 1         | -         | 1        |
 * | ----------- | --------- | --------- | -------- |
 * </pre>
 *
 * @param sourceResources the source resources
 * @param resourcesToCompare the resources which should be compared to {@code sourceResources}
 * @throws IllegalArgumentException when {@code sourceResources} or {@code resourcesToCompare}
 *     contain resource with not supported type
 */
public List<? extends Resource> excess(List<? extends Resource> sourceResources, List<? extends Resource> resourcesToCompare) {
    checkSupporting(sourceResources);
    checkSupporting(resourcesToCompare);
    final Map<String, Resource> result = sourceResources.stream().collect(Collectors.toMap(Resource::getType, Function.identity()));
    for (Resource toCompare : resourcesToCompare) {
        String resourceType = toCompare.getType();
        final Resource sourceResource = result.get(resourceType);
        if (sourceResource != null) {
            if (sourceResource.getAmount() == toCompare.getAmount()) {
                // source resource doesn't have excessive amount
                result.remove(resourceType);
                continue;
            }
            try {
                Resource excess = deduct(sourceResource, toCompare);
                if (excess.getAmount() == 0) {
                    // source resource doesn't have excessive amount
                    result.remove(resourceType);
                } else {
                    result.put(resourceType, excess);
                }
            } catch (NoEnoughResourcesException e) {
                // source resource doesn't have excessive amount
                result.remove(resourceType);
            }
        }
    }
    return new ArrayList<>(result.values());
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList)

Aggregations

NoEnoughResourcesException (org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException)26 ResourceImpl (org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)20 Test (org.testng.annotations.Test)16 Resource (org.eclipse.che.multiuser.resource.model.Resource)8 ArrayList (java.util.ArrayList)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Environment (org.eclipse.che.api.core.model.workspace.config.Environment)4 WorkspaceConfig (org.eclipse.che.api.core.model.workspace.WorkspaceConfig)2 ResourceManager (org.eclipse.che.multiuser.resource.api.usage.ResourceManager)2