Search in sources :

Example 6 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project che-server by eclipse-che.

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 7 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project che-server by eclipse-che.

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 8 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project che-server by eclipse-che.

the class OrganizationResourcesDistributorTest method shouldResourceAvailabilityCappingResourcesWhenResourceCapIsGreaterThanUsedOne.

@Test(expectedExceptions = ConflictException.class, expectedExceptionsMessageRegExp = "Resources are currently in use. Denied.")
public void shouldResourceAvailabilityCappingResourcesWhenResourceCapIsGreaterThanUsedOne() throws Exception {
    // given
    doCallRealMethod().when(manager).checkResourcesAvailability(anyString(), any());
    doReturn("Denied.").when(manager).getMessage(anyString());
    ResourceImpl used = createTestResource(1000);
    doReturn(singletonList(used)).when(resourceManager).getUsedResources(any());
    ResourceImpl toCap = createTestResource(700);
    doThrow(new NoEnoughResourcesException(emptyList(), emptyList(), singletonList(toCap))).when(resourceAggregator).deduct((Resource) any(), any());
    // when
    manager.checkResourcesAvailability(ORG_ID, singletonList(toCap));
    // then
    verify(resourceManager).getUsedResources(ORG_ID);
    verify(resourceAggregator).deduct(toCap, used);
}
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 9 with NoEnoughResourcesException

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

the class OrganizationalAccountAvailableResourcesProvider method getAvailableOrganizationResources.

/**
 * Returns total resources minus resources which are already used by organization or by any of its
 * suborganizations.
 *
 * @param organization organization id to calculate its available resources
 * @return resources which are available for usage by specified organization
 * @throws NotFoundException when organization with specified id doesn't exist
 * @throws ServerException when any other exception occurs on calculation of available resources
 */
@VisibleForTesting
List<? extends Resource> getAvailableOrganizationResources(Organization organization) throws NotFoundException, ServerException {
    final ResourceManager resourceManager = resourceManagerProvider.get();
    final List<? extends Resource> total = resourceManager.getTotalResources(organization.getId());
    final List<Resource> unavailable = new ArrayList<>(resourceManager.getUsedResources(organization.getId()));
    unavailable.addAll(getUsedResourcesBySuborganizations(organization.getQualifiedName()));
    try {
        return resourceAggregator.deduct(total, unavailable);
    } catch (NoEnoughResourcesException e) {
        LOG.warn("Organization with id {} uses more resources {} than it has {}.", organization.getId(), format(unavailable), format(total));
        return resourceAggregator.excess(total, unavailable);
    }
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList) ResourceManager(org.eclipse.che.multiuser.resource.api.usage.ResourceManager) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 10 with NoEnoughResourcesException

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

the class LimitsCheckingWorkspaceManagerTest method shouldThrowLimitExceedExceptionIfAccountDoesNotHaveEnoughAvailableRamResource.

@Test(expectedExceptions = LimitExceededException.class, expectedExceptionsMessageRegExp = "Workspace namespace/workspace.. needs 3000MB to start\\. " + "Your account has 200MB available and 100MB in use\\. " + "The workspace can't be start. Stop other workspaces or grant more resources\\.")
public void shouldThrowLimitExceedExceptionIfAccountDoesNotHaveEnoughAvailableRamResource() throws Exception {
    doThrow(new NoEnoughResourcesException(singletonList(new ResourceImpl(RamResourceType.ID, 200L, RamResourceType.UNIT)), singletonList(new ResourceImpl(RamResourceType.ID, 3000L, RamResourceType.UNIT)), emptyList())).when(resourceManager).checkResourcesAvailability(any(), any());
    doReturn(singletonList(new ResourceImpl(RamResourceType.ID, 100L, RamResourceType.UNIT))).when(resourceManager).getUsedResources(any());
    // given
    LimitsCheckingWorkspaceManager manager = managerBuilder().setResourceManager(resourceManager).setEnvironmentRamCalculator(environmentRamCalculator).build();
    when(environmentRamCalculator.calculate(any(Environment.class))).thenReturn(3000L);
    WorkspaceConfig config = createConfig("3gb");
    // when
    manager.checkRamResourcesAvailability(ACCOUNT_ID, NAMESPACE, config, null);
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) WorkspaceConfig(org.eclipse.che.api.core.model.workspace.WorkspaceConfig) Test(org.testng.annotations.Test)

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