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());
}
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);
}
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);
}
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);
}
}
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);
}
Aggregations