Search in sources :

Example 11 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource 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());
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList)

Example 12 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class ResourceAggregator method aggregateByType.

/**
 * Aggregates resources of the same type.
 *
 * @param resources resources list which can contain more that one instance for some type
 * @return map where key is resources type and value is aggregated resource
 * @throws IllegalArgumentException when resources list contains resource with not supported type
 */
public Map<String, Resource> aggregateByType(List<? extends Resource> resources) {
    checkSupporting(resources);
    Map<String, Resource> type2Resource = new HashMap<>();
    for (Resource resource : resources) {
        final Resource resource1 = type2Resource.get(resource.getType());
        if (resource1 != null) {
            type2Resource.put(resource.getType(), aggregate(resource1, resource));
        } else {
            type2Resource.put(resource.getType(), resource);
        }
    }
    return type2Resource;
}
Also used : HashMap(java.util.HashMap) Resource(org.eclipse.che.multiuser.resource.model.Resource)

Example 13 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class RamResourceUsageTrackerTest method shouldReturnUsedRamOfRunningWorkspaceForGivenAccount.

@Test
public void shouldReturnUsedRamOfRunningWorkspaceForGivenAccount() throws Exception {
    mockWorkspaces(createWorkspace(WorkspaceStatus.RUNNING, 1000, 500, 500));
    when(envRamCalculator.calculate(any(Runtime.class))).thenReturn(2000L);
    final Optional<Resource> usedRamOpt = ramUsageTracker.getUsedResource(ACCOUNT_ID);
    assertTrue(usedRamOpt.isPresent());
    final Resource usedRam = usedRamOpt.get();
    assertEquals(usedRam.getType(), RamResourceType.ID);
    assertEquals(usedRam.getAmount(), 2000L);
    assertEquals(usedRam.getUnit(), RamResourceType.UNIT);
    verify(accountManager).getById(ACCOUNT_ID);
    verify(workspaceManager).getByNamespace(anyString(), anyBoolean(), anyInt(), anyLong());
}
Also used : Runtime(org.eclipse.che.api.core.model.workspace.Runtime) Resource(org.eclipse.che.multiuser.resource.model.Resource) Test(org.testng.annotations.Test)

Example 14 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class RamResourceUsageTrackerTest method returnUsedRamOfStartingWorkspaceForGivenAccount.

@Test
public void returnUsedRamOfStartingWorkspaceForGivenAccount() throws Exception {
    mockWorkspaces(createWorkspace(WorkspaceStatus.STARTING, 1000, 500, 500));
    when(envRamCalculator.calculate(any(Environment.class))).thenReturn(2000L);
    final Optional<Resource> usedRamOpt = ramUsageTracker.getUsedResource(ACCOUNT_ID);
    assertTrue(usedRamOpt.isPresent());
    final Resource usedRam = usedRamOpt.get();
    assertEquals(usedRam.getType(), RamResourceType.ID);
    assertEquals(usedRam.getAmount(), 2000L);
    assertEquals(usedRam.getUnit(), RamResourceType.UNIT);
    verify(accountManager).getById(ACCOUNT_ID);
    verify(workspaceManager).getByNamespace(anyString(), anyBoolean(), anyInt(), anyLong());
}
Also used : Resource(org.eclipse.che.multiuser.resource.model.Resource) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) Test(org.testng.annotations.Test)

Example 15 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class FreeResourcesProvider method getResources.

@Override
public List<ProvidedResources> getResources(String accountId) throws ServerException, NotFoundException {
    Map<String, ResourceImpl> freeResources = new HashMap<>();
    String limitId = null;
    try {
        FreeResourcesLimit resourcesLimit = freeResourcesLimitManager.get(accountId);
        for (Resource resource : resourcesLimit.getResources()) {
            freeResources.put(resource.getType(), new ResourceImpl(resource));
        }
        limitId = resourcesLimit.getAccountId();
    } catch (NotFoundException ignored) {
    // there is no resources limit for given account
    }
    // add default resources which are not specified by limit
    for (ResourceImpl resource : getDefaultResources(accountId)) {
        freeResources.putIfAbsent(resource.getType(), resource);
    }
    if (!freeResources.isEmpty()) {
        return singletonList(new ProvidedResourcesImpl(FREE_RESOURCES_PROVIDER, limitId, accountId, -1L, -1L, freeResources.values()));
    } else {
        return emptyList();
    }
}
Also used : ProvidedResourcesImpl(org.eclipse.che.multiuser.resource.spi.impl.ProvidedResourcesImpl) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) HashMap(java.util.HashMap) FreeResourcesLimit(org.eclipse.che.multiuser.resource.model.FreeResourcesLimit) Resource(org.eclipse.che.multiuser.resource.model.Resource) NotFoundException(org.eclipse.che.api.core.NotFoundException)

Aggregations

Resource (org.eclipse.che.multiuser.resource.model.Resource)52 ResourceImpl (org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)24 Test (org.testng.annotations.Test)24 ArrayList (java.util.ArrayList)14 NotFoundException (org.eclipse.che.api.core.NotFoundException)12 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)12 Optional (java.util.Optional)10 Inject (javax.inject.Inject)10 Singleton (javax.inject.Singleton)10 AccountManager (org.eclipse.che.account.api.AccountManager)10 Account (org.eclipse.che.account.shared.model.Account)10 ServerException (org.eclipse.che.api.core.ServerException)10 ResourceUsageTracker (org.eclipse.che.multiuser.resource.api.ResourceUsageTracker)10 NoEnoughResourcesException (org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException)10 List (java.util.List)8 WorkspaceManager (org.eclipse.che.api.workspace.server.WorkspaceManager)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 HashMap (java.util.HashMap)6 Collectors (java.util.stream.Collectors)6 Provider (javax.inject.Provider)6