use of org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl in project devspaces-images by redhat-developer.
the class ResourceManagerTest method testGetsAccountTotalResources.
@Test
public void testGetsAccountTotalResources() throws Exception {
final List<Resource> res = ImmutableList.of(new ResourceImpl("RAM", 2048, "mb"), new ResourceImpl("timeout", 15, "minutes"));
doReturn(res).when(providedResources).getResources();
List<? extends Resource> actual = resourceManager.getTotalResources(ACCOUNT_ID);
assertEquals(actual.size(), res.size());
assertTrue(actual.containsAll(res));
}
use of org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl in project devspaces-images by redhat-developer.
the class ResourceManagerTest method testReturnsResourceDetailsForGivenAccount.
@Test
public void testReturnsResourceDetailsForGivenAccount() throws Exception {
final ResourceImpl testResource = new ResourceImpl("RAM", 1000, "mb");
final ResourceImpl reducedResource = new ResourceImpl("timeout", 2000, "m");
final ProvidedResourcesImpl providedResource = new ProvidedResourcesImpl("test", null, ACCOUNT_ID, 123L, 321L, singletonList(testResource));
when(resourcesProvider.getResources(eq(ACCOUNT_ID))).thenReturn(singletonList(providedResource));
when(resourceAggregator.aggregateByType(any())).thenReturn(ImmutableMap.of(reducedResource.getType(), reducedResource));
final ResourcesDetails resourcesDetails = resourceManager.getResourceDetails(ACCOUNT_ID);
verify(resourcesProvider).getResources(eq(ACCOUNT_ID));
verify(resourceAggregator).aggregateByType(eq(singletonList(testResource)));
assertEquals(resourcesDetails.getAccountId(), ACCOUNT_ID);
assertEquals(resourcesDetails.getProvidedResources().size(), 1);
assertEquals(resourcesDetails.getProvidedResources().get(0), providedResource);
assertEquals(resourcesDetails.getTotalResources().size(), 1);
assertEquals(resourcesDetails.getTotalResources().get(0), reducedResource);
}
use of org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl in project devspaces-images by redhat-developer.
the class RamResourceUsageTracker method getUsedResource.
@Override
public Optional<Resource> getUsedResource(String accountId) throws NotFoundException, ServerException {
final Account account = accountManager.getById(accountId);
List<WorkspaceImpl> activeWorkspaces = Pages.stream((maxItems, skipCount) -> workspaceManagerProvider.get().getByNamespace(account.getName(), true, maxItems, skipCount)).filter(ws -> STOPPED != ws.getStatus()).collect(Collectors.toList());
long currentlyUsedRamMB = 0;
for (WorkspaceImpl activeWorkspace : activeWorkspaces) {
if (WorkspaceStatus.STARTING.equals(activeWorkspace.getStatus())) {
// starting workspace may not have all machine in runtime
// it is need to calculate ram from environment config
WorkspaceConfigImpl config = activeWorkspace.getConfig();
if (config != null) {
final EnvironmentImpl startingEnvironment = config.getEnvironments().get(activeWorkspace.getRuntime().getActiveEnv());
if (startingEnvironment != null) {
currentlyUsedRamMB += environmentRamCalculator.calculate(startingEnvironment);
}
}
// Estimation of memory for starting workspace with Devfile is not implemented yet
// just ignore such
} else {
currentlyUsedRamMB += environmentRamCalculator.calculate(activeWorkspace.getRuntime());
}
}
if (currentlyUsedRamMB > 0) {
return Optional.of(new ResourceImpl(RamResourceType.ID, currentlyUsedRamMB, RamResourceType.UNIT));
} else {
return Optional.empty();
}
}
use of org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl 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.spi.impl.ResourceImpl 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)));
}
}
Aggregations