use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
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.model.Resource in project che-server by eclipse-che.
the class WorkspaceResourceUsageTracker method getUsedResource.
@Override
public Optional<Resource> getUsedResource(String accountId) throws NotFoundException, ServerException {
final Account account = accountManager.getById(accountId);
final List<WorkspaceImpl> accountWorkspaces = Pages.stream((maxItems, skipCount) -> workspaceManagerProvider.get().getByNamespace(account.getName(), false, maxItems, skipCount)).collect(Collectors.toList());
if (!accountWorkspaces.isEmpty()) {
return Optional.of(new ResourceImpl(WorkspaceResourceType.ID, accountWorkspaces.size(), WorkspaceResourceType.UNIT));
} else {
return Optional.empty();
}
}
use of org.eclipse.che.multiuser.resource.model.Resource 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)));
}
}
use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class RuntimeResourceUsageTrackerTest method shouldReturnUsedRuntimesForGivenAccount.
@Test
public void shouldReturnUsedRuntimesForGivenAccount() throws Exception {
when(accountManager.getById(any())).thenReturn(account);
when(account.getName()).thenReturn("testAccount");
List<WorkspaceImpl> runtimes = Stream.of(WorkspaceStatus.values()).map(RuntimeResourceUsageTrackerTest::createWorkspace).collect(Collectors.toList());
when(workspaceManager.getByNamespace(anyString(), anyBoolean(), anyInt(), anyLong())).thenReturn(new Page<>(runtimes, 0, runtimes.size(), runtimes.size()));
Optional<Resource> usedRuntimesOpt = runtimeResourceUsageTracker.getUsedResource("account123");
assertTrue(usedRuntimesOpt.isPresent());
Resource usedRuntimes = usedRuntimesOpt.get();
assertEquals(usedRuntimes.getType(), RuntimeResourceType.ID);
assertEquals(usedRuntimes.getAmount(), // except stopped workspaces
WorkspaceStatus.values().length - 1);
assertEquals(usedRuntimes.getUnit(), RuntimeResourceType.UNIT);
verify(accountManager).getById(eq("account123"));
verify(workspaceManager).getByNamespace(eq("testAccount"), eq(false), anyInt(), anyLong());
}
use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
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());
}
Aggregations