Search in sources :

Example 31 with Resource

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();
    }
}
Also used : WorkspaceManager(org.eclipse.che.api.workspace.server.WorkspaceManager) ResourceUsageTracker(org.eclipse.che.multiuser.resource.api.ResourceUsageTracker) RuntimeResourceType(org.eclipse.che.multiuser.resource.api.type.RuntimeResourceType) Provider(javax.inject.Provider) Resource(org.eclipse.che.multiuser.resource.model.Resource) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Singleton(javax.inject.Singleton) NotFoundException(org.eclipse.che.api.core.NotFoundException) Inject(javax.inject.Inject) ServerException(org.eclipse.che.api.core.ServerException) Optional(java.util.Optional) Pages(org.eclipse.che.api.core.Pages) AccountManager(org.eclipse.che.account.api.AccountManager) Account(org.eclipse.che.account.shared.model.Account) STOPPED(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED) Account(org.eclipse.che.account.shared.model.Account) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)

Example 32 with Resource

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)));
    }
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 33 with Resource

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));
}
Also used : ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Test(org.testng.annotations.Test)

Example 34 with Resource

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

Example 35 with Resource

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

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