use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.
the class RuntimeResourceUsageTracker method getUsedResource.
@Override
public Optional<Resource> getUsedResource(String accountId) throws NotFoundException, ServerException {
final Account account = accountManager.getById(accountId);
final long currentlyUsedRuntimes = Pages.stream((maxItems, skipCount) -> workspaceManagerProvider.get().getByNamespace(account.getName(), false, maxItems, skipCount)).filter(ws -> STOPPED != ws.getStatus()).count();
if (currentlyUsedRuntimes > 0) {
return Optional.of(new ResourceImpl(RuntimeResourceType.ID, currentlyUsedRuntimes, RuntimeResourceType.UNIT));
} else {
return Optional.empty();
}
}
use of org.eclipse.che.multiuser.resource.model.Resource 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)));
}
}
use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.
the class ResourceAggregatorTest method shouldTestResourcesAggregationByTypes.
@Test
public void shouldTestResourcesAggregationByTypes() throws Exception {
// given
final ResourceImpl aResource = new ResourceImpl(A_RESOURCE_TYPE, 123, "unit");
final ResourceImpl bResource = new ResourceImpl(B_RESOURCE_TYPE, 123, "unit");
final ResourceImpl anotherBResource = new ResourceImpl(B_RESOURCE_TYPE, 321, "unit");
final ResourceImpl aggregatedBResources = new ResourceImpl(B_RESOURCE_TYPE, 444, "unit");
when(bResourceType.aggregate(any(), any())).thenReturn(aggregatedBResources);
// when
final Map<String, Resource> aggregatedResources = resourceAggregator.aggregateByType(asList(aResource, bResource, anotherBResource));
// then
verify(bResourceType).aggregate(eq(bResource), eq(anotherBResource));
verify(aResourceType, never()).aggregate(any(), any());
assertEquals(aggregatedResources.size(), 2);
assertTrue(aggregatedResources.containsKey(A_RESOURCE_TYPE));
assertTrue(aggregatedResources.containsValue(aResource));
assertTrue(aggregatedResources.containsKey(B_RESOURCE_TYPE));
assertTrue(aggregatedResources.containsValue(aggregatedBResources));
}
use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.
the class ResourceAggregator method min.
/**
* Returns list that contains one resource with minimum amount for each resource type.
*
* @throws IllegalArgumentException when {@code resources} list contains resource with not
* supported type
*/
public List<? extends Resource> min(Collection<? extends Resource> resources) {
checkSupporting(resources);
Map<String, Resource> result = new HashMap<>();
for (Resource resource : resources) {
String type = resource.getType();
Resource min = result.get(type);
if (min == null) {
result.put(type, resource);
} else if (resource.getAmount() != -1) {
if (min.getAmount() == -1 || min.getAmount() > resource.getAmount()) {
result.put(type, resource);
}
}
}
return new ArrayList<>(result.values());
}
use of org.eclipse.che.multiuser.resource.model.Resource 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());
}
Aggregations