Search in sources :

Example 16 with NoEnoughResourcesException

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

the class AbstractExhaustibleResource method deduct.

@Override
public Resource deduct(Resource total, Resource deduction) throws NoEnoughResourcesException {
    checkResource(total);
    checkResource(deduction);
    if (total.getAmount() == -1) {
        return total;
    }
    if (deduction.getAmount() == -1) {
        throw new NoEnoughResourcesException(total, deduction, deduction);
    }
    final long resultAmount = total.getAmount() - deduction.getAmount();
    if (resultAmount < 0) {
        throw new NoEnoughResourcesException(total, deduction, new ResourceImpl(getId(), -resultAmount, getDefaultUnit()));
    }
    return new ResourceImpl(getId(), resultAmount, getDefaultUnit());
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)

Example 17 with NoEnoughResourcesException

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

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

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

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

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

the class AbstractExhaustibleResource method deduct.

@Override
public Resource deduct(Resource total, Resource deduction) throws NoEnoughResourcesException {
    checkResource(total);
    checkResource(deduction);
    if (total.getAmount() == -1) {
        return total;
    }
    if (deduction.getAmount() == -1) {
        throw new NoEnoughResourcesException(total, deduction, deduction);
    }
    final long resultAmount = total.getAmount() - deduction.getAmount();
    if (resultAmount < 0) {
        throw new NoEnoughResourcesException(total, deduction, new ResourceImpl(getId(), -resultAmount, getDefaultUnit()));
    }
    return new ResourceImpl(getId(), resultAmount, getDefaultUnit());
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)

Example 20 with NoEnoughResourcesException

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

the class OrganizationalAccountAvailableResourcesProviderTest method shouldReturnExcessiveResourcesWhenUsedResourceAreGreaterThanTotal.

@Test
public void shouldReturnExcessiveResourcesWhenUsedResourceAreGreaterThanTotal() throws Exception {
    // given
    ResourceImpl totalResource = new ResourceImpl("test", 9000, "unit");
    ResourceImpl excessiveTotalResource = new ResourceImpl("test1", 1000, "unit");
    doReturn(asList(totalResource, excessiveTotalResource)).when(resourceManager).getTotalResources(anyString());
    ResourceImpl usedResource = new ResourceImpl("test", 10000, "unit");
    doReturn(singletonList(usedResource)).when(resourceManager).getUsedResources(anyString());
    doReturn(emptyList()).when(availableResourcesProvider).getUsedResourcesBySuborganizations(anyString());
    doThrow(new NoEnoughResourcesException(emptyList(), emptyList(), emptyList())).when(resourceAggregator).deduct(anyList(), anyList());
    doReturn(singletonList(excessiveTotalResource)).when(resourceAggregator).excess(anyList(), anyList());
    // when
    List<? extends Resource> availableResources = availableResourcesProvider.getAvailableOrganizationResources(rootOrganization);
    // then
    assertEquals(availableResources.size(), 1);
    assertEquals(availableResources.get(0), excessiveTotalResource);
    verify(resourceManager).getTotalResources(ROOT_ORG_ID);
    verify(resourceManager).getUsedResources(ROOT_ORG_ID);
    verify(availableResourcesProvider).getUsedResourcesBySuborganizations(ROOT_ORG_NAME);
    verify(resourceAggregator).deduct(asList(totalResource, excessiveTotalResource), singletonList(usedResource));
    verify(resourceAggregator).excess(asList(totalResource, excessiveTotalResource), singletonList(usedResource));
}
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)

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