Search in sources :

Example 26 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.

the class RamResourceUsageTrackerTest method shouldNotSumRamOfStoppedWorkspaceWhenGettingUsedRamForGivenAccount.

@Test
public void shouldNotSumRamOfStoppedWorkspaceWhenGettingUsedRamForGivenAccount() throws Exception {
    final WorkspaceImpl stoppedWs = createWorkspace(WorkspaceStatus.STOPPED, 3500);
    final WorkspaceImpl runningWs = createWorkspace(WorkspaceStatus.RUNNING, 2500);
    mockWorkspaces(stoppedWs, runningWs);
    when(envRamCalculator.calculate(runningWs.getRuntime())).thenReturn(2500L);
    final Optional<Resource> usedRamOpt = ramUsageTracker.getUsedResource(ACCOUNT_ID);
    assertTrue(usedRamOpt.isPresent());
    final Resource usedRam = usedRamOpt.get();
    assertEquals(usedRam.getType(), RamResourceType.ID);
    assertEquals(usedRam.getAmount(), 2500L);
    assertEquals(usedRam.getUnit(), RamResourceType.UNIT);
    verify(accountManager).getById(ACCOUNT_ID);
    verify(workspaceManager).getByNamespace(anyString(), anyBoolean(), anyInt(), anyLong());
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Test(org.testng.annotations.Test)

Example 27 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.

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

Example 28 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.

the class ResourceManager method getResourceDetails.

/**
 * Returns detailed information about resources which given account can use.
 *
 * @param accountId account id
 * @return detailed information about resources which can be used by given account
 * @throws NotFoundException when account with specified id was not found
 * @throws ServerException when some exception occurs
 */
public ResourcesDetails getResourceDetails(String accountId) throws NotFoundException, ServerException {
    final List<ProvidedResources> resources = new ArrayList<>();
    for (ResourcesProvider resourcesProvider : resourcesProviders) {
        resources.addAll(resourcesProvider.getResources(accountId));
    }
    final List<Resource> allResources = resources.stream().flatMap(providedResources -> providedResources.getResources().stream()).collect(Collectors.toList());
    return new ResourcesDetailsImpl(accountId, resources, new ArrayList<>(resourceAggregator.aggregateByType(allResources).values()));
}
Also used : ResourceUsageTracker(org.eclipse.che.multiuser.resource.api.ResourceUsageTracker) Resource(org.eclipse.che.multiuser.resource.model.Resource) Set(java.util.Set) Singleton(javax.inject.Singleton) Collectors(java.util.stream.Collectors) AvailableResourcesProvider(org.eclipse.che.multiuser.resource.api.AvailableResourcesProvider) NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ProvidedResources(org.eclipse.che.multiuser.resource.model.ProvidedResources) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) ResourcesDetailsImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourcesDetailsImpl) List(java.util.List) ServerException(org.eclipse.che.api.core.ServerException) ResourcesProvider(org.eclipse.che.multiuser.resource.api.ResourcesProvider) Map(java.util.Map) Optional(java.util.Optional) ResourcesDetails(org.eclipse.che.multiuser.resource.model.ResourcesDetails) AccountManager(org.eclipse.che.account.api.AccountManager) Account(org.eclipse.che.account.shared.model.Account) ResourceAggregator(org.eclipse.che.multiuser.resource.api.ResourceAggregator) AvailableResourcesProvider(org.eclipse.che.multiuser.resource.api.AvailableResourcesProvider) ResourcesProvider(org.eclipse.che.multiuser.resource.api.ResourcesProvider) ProvidedResources(org.eclipse.che.multiuser.resource.model.ProvidedResources) ArrayList(java.util.ArrayList) Resource(org.eclipse.che.multiuser.resource.model.Resource) ResourcesDetailsImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourcesDetailsImpl)

Example 29 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.

the class ResourceManager method getUsedResources.

/**
 * Returns list of resources which are used by given account.
 *
 * @param accountId id of account
 * @return list of resources which are used by given account
 * @throws NotFoundException when account with specified id was not found
 * @throws ServerException when some exception occurred while resources fetching
 */
public List<? extends Resource> getUsedResources(String accountId) throws NotFoundException, ServerException {
    List<Resource> usedResources = new ArrayList<>();
    for (ResourceUsageTracker usageTracker : usageTrackers) {
        Optional<Resource> usedResource = usageTracker.getUsedResource(accountId);
        usedResource.ifPresent(usedResources::add);
    }
    return usedResources;
}
Also used : Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList) ResourceUsageTracker(org.eclipse.che.multiuser.resource.api.ResourceUsageTracker)

Example 30 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource 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();
    }
}
Also used : WorkspaceManager(org.eclipse.che.api.workspace.server.WorkspaceManager) ResourceUsageTracker(org.eclipse.che.multiuser.resource.api.ResourceUsageTracker) Provider(javax.inject.Provider) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Singleton(javax.inject.Singleton) Collectors(java.util.stream.Collectors) NotFoundException(org.eclipse.che.api.core.NotFoundException) Inject(javax.inject.Inject) List(java.util.List) 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) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) RamResourceType(org.eclipse.che.multiuser.resource.api.type.RamResourceType) STOPPED(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED) Account(org.eclipse.che.account.shared.model.Account) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)

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