Search in sources :

Example 11 with Account

use of org.eclipse.che.account.shared.model.Account 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 12 with Account

use of org.eclipse.che.account.shared.model.Account in project devspaces-images by redhat-developer.

the class JpaWorkerDaoTest method shouldThrowServerExceptionOnExistsWhenRuntimeExceptionOccursInDoGetMethod.

@Test(expectedExceptions = ServerException.class, expectedExceptionsMessageRegExp = "Database exception")
public void shouldThrowServerExceptionOnExistsWhenRuntimeExceptionOccursInDoGetMethod() throws Exception {
    final Account account = new AccountImpl("accountId", "namespace", "test");
    final WorkspaceImpl workspace = WorkspaceImpl.builder().setId("workspaceId").setAccount(account).build();
    // Persist the account
    manager.getTransaction().begin();
    manager.persist(account);
    manager.getTransaction().commit();
    manager.clear();
    // Persist the workspace
    manager.getTransaction().begin();
    manager.persist(workspace);
    manager.getTransaction().commit();
    manager.clear();
    final UserImpl user = new UserImpl("user0", "user0@com.com", "usr0");
    // Persist the user
    manager.getTransaction().begin();
    manager.persist(user);
    manager.getTransaction().commit();
    manager.clear();
    // Persist the worker
    WorkerImpl worker = new WorkerImpl("workspaceId", "user0", Collections.singletonList(SET_PERMISSIONS));
    manager.getTransaction().begin();
    manager.persist(worker);
    manager.getTransaction().commit();
    manager.clear();
    workerDao.exists("user0", "workspaceId", SET_PERMISSIONS);
}
Also used : Account(org.eclipse.che.account.shared.model.Account) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) AccountImpl(org.eclipse.che.account.spi.AccountImpl) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) WorkerImpl(org.eclipse.che.multiuser.permission.workspace.server.model.impl.WorkerImpl) Test(org.testng.annotations.Test)

Example 13 with Account

use of org.eclipse.che.account.shared.model.Account in project che-server by eclipse-che.

the class ResourceManager method getAvailableResources.

/**
 * Returns list of resources which are available for usage by given account.
 *
 * @param accountId id of account
 * @return list of resources which are available for usage 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> getAvailableResources(String accountId) throws NotFoundException, ServerException {
    final Account account = accountManager.getById(accountId);
    final AvailableResourcesProvider availableResourcesProvider = accountTypeToAvailableResourcesProvider.get(account.getType());
    if (availableResourcesProvider == null) {
        return defaultAvailableResourcesProvider.getAvailableResources(accountId);
    }
    return availableResourcesProvider.getAvailableResources(accountId);
}
Also used : Account(org.eclipse.che.account.shared.model.Account) AvailableResourcesProvider(org.eclipse.che.multiuser.resource.api.AvailableResourcesProvider)

Example 14 with Account

use of org.eclipse.che.account.shared.model.Account in project che-server by eclipse-che.

the class ResourcesLocks method lock.

/**
 * Acquire resources lock for specified account.
 *
 * @param accountId account id to lock resources
 * @return lock for unlocking resources when resources operation finishes
 * @throws NotFoundException when account with specified {@code account id} was not found
 * @throws ServerException when any other error occurs
 */
public Unlocker lock(String accountId) throws NotFoundException, ServerException {
    final Account account = accountManager.getById(accountId);
    final ResourceLockKeyProvider resourceLockKeyProvider = accountTypeToLockProvider.get(account.getType());
    String lockKey;
    if (resourceLockKeyProvider == null) {
        // this account type doesn't have custom lock provider.
        // Lock resources by current account
        lockKey = accountId;
    } else {
        lockKey = resourceLockKeyProvider.getLockKey(accountId);
    }
    return stripedLocks.writeLock(lockKey);
}
Also used : Account(org.eclipse.che.account.shared.model.Account) ResourceLockKeyProvider(org.eclipse.che.multiuser.resource.api.ResourceLockKeyProvider)

Example 15 with Account

use of org.eclipse.che.account.shared.model.Account 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();
    }
}
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

Account (org.eclipse.che.account.shared.model.Account)26 ServerException (org.eclipse.che.api.core.ServerException)14 NotFoundException (org.eclipse.che.api.core.NotFoundException)12 Optional (java.util.Optional)10 Inject (javax.inject.Inject)10 Singleton (javax.inject.Singleton)10 AccountManager (org.eclipse.che.account.api.AccountManager)10 Resource (org.eclipse.che.multiuser.resource.model.Resource)10 List (java.util.List)8 WorkspaceManager (org.eclipse.che.api.workspace.server.WorkspaceManager)8 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)8 ResourceUsageTracker (org.eclipse.che.multiuser.resource.api.ResourceUsageTracker)8 ResourceImpl (org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)8 Collectors (java.util.stream.Collectors)6 Provider (javax.inject.Provider)6 Pages (org.eclipse.che.api.core.Pages)6 ArrayList (java.util.ArrayList)4 ConflictException (org.eclipse.che.api.core.ConflictException)4 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)4 STOPPED (org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED)4