use of org.eclipse.che.api.workspace.server.spi.InternalRuntime in project devspaces-images by redhat-developer.
the class WorkspaceRuntimes method getInternalRuntime.
/**
* Returns {@link InternalRuntime} implementation for workspace with the specified id.
*
* <p>If memory-storage does not contain internal runtime, then runtime will be recovered if it is
* active. Otherwise, an exception will be thrown.
*
* @param workspaceId identifier of workspace to fetch runtime
* @return {@link InternalRuntime} implementation for workspace with the specified id.
* @throws InfrastructureException if any infrastructure exception occurs
* @throws ServerException if there is no active runtime for the specified workspace
* @throws ServerException if any other exception occurs
*/
public InternalRuntime<?> getInternalRuntime(String workspaceId) throws InfrastructureException, ServerException {
try (Unlocker ignored = lockService.writeLock(workspaceId)) {
InternalRuntime<?> runtime = runtimes.get(workspaceId);
if (runtime == null) {
try {
final Optional<RuntimeIdentity> runtimeIdentity = infrastructure.getIdentities().stream().filter(id -> id.getWorkspaceId().equals(workspaceId)).findAny();
if (runtimeIdentity.isPresent()) {
LOG.info("Runtime for workspace '{}' is requested but there is no cached one. Recovering it.", workspaceId);
runtime = recoverOne(infrastructure, runtimeIdentity.get());
} else {
// runtime is not considered by Infrastructure as active
throw new ServerException("No active runtime is found");
}
} catch (ServerException e) {
statuses.remove(workspaceId);
throw e;
} catch (UnsupportedOperationException | ConflictException e) {
statuses.remove(workspaceId);
throw new ServerException(e.getMessage(), e);
}
}
return runtime;
}
}
use of org.eclipse.che.api.workspace.server.spi.InternalRuntime in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method shouldInjectRuntime.
@Test
public void shouldInjectRuntime() throws Exception {
// given
WorkspaceImpl workspace = new WorkspaceImpl();
workspace.setId("ws123");
when(statuses.get("ws123")).thenReturn(WorkspaceStatus.RUNNING);
ImmutableMap<String, Machine> machines = ImmutableMap.of("machine", new MachineImpl(emptyMap(), emptyMap(), MachineStatus.STARTING));
RuntimeIdentity identity = new RuntimeIdentityImpl("ws123", "my-env", "myId", "infraNamespace");
RuntimeContext context = mockContext(identity);
ConcurrentHashMap<String, InternalRuntime<?>> runtimesStorage = new ConcurrentHashMap<>();
TestInternalRuntime testRuntime = new TestInternalRuntime(context, machines, WorkspaceStatus.STARTING);
runtimesStorage.put("ws123", testRuntime);
WorkspaceRuntimes localRuntimes = new WorkspaceRuntimes(runtimesStorage, eventService, ImmutableMap.of(TEST_ENVIRONMENT_TYPE, testEnvFactory), infrastructure, sharedPool, workspaceDao, dbInitializer, probeScheduler, statuses, lockService, devfileConverter, false);
// when
localRuntimes.injectRuntime(workspace);
// then
assertEquals(workspace.getStatus(), WorkspaceStatus.RUNNING);
assertEquals(workspace.getRuntime(), asRuntime(testRuntime));
}
use of org.eclipse.che.api.workspace.server.spi.InternalRuntime in project che-server by eclipse-che.
the class WorkspaceRuntimes method startAsync.
/**
* Starts all machines from specified workspace environment, creates workspace runtime instance
* based on that environment.
*
* <p>During the start of the workspace its runtime is visible with {@link
* WorkspaceStatus#STARTING} status.
*
* @param workspace workspace which environment should be started
* @param envName optional environment name to run
* @param options whether machines should be recovered(true) or not(false)
* @return completable future of start execution.
* @throws ConflictException when workspace is already running
* @throws ConflictException when start is interrupted
* @throws NotFoundException when any not found exception occurs during environment start
* @throws ServerException other error occurs during environment start
* @see WorkspaceStatus#STARTING
* @see WorkspaceStatus#RUNNING
*/
@Traced
public CompletableFuture<Void> startAsync(WorkspaceImpl workspace, @Nullable String envName, Map<String, String> options) throws ConflictException, NotFoundException, ServerException {
TracingTags.WORKSPACE_ID.set(workspace.getId());
final String workspaceId = workspace.getId();
if (isStartRefused.get()) {
throw new ConflictException(format("Start of the workspace '%s' is rejected by the system, " + "no more workspaces are allowed to start", workspace.getName()));
}
WorkspaceConfigImpl config = workspace.getConfig();
if (config == null) {
config = devfileConverter.convert(workspace.getDevfile());
}
if (envName == null) {
envName = config.getDefaultEnv();
}
String infraNamespace = workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE);
if (isNullOrEmpty(infraNamespace)) {
throw new ServerException(String.format("Workspace does not have infrastructure namespace " + "specified. Please set value of '%s' workspace attribute.", WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE));
}
final RuntimeIdentity runtimeId = new RuntimeIdentityImpl(workspaceId, envName, EnvironmentContext.getCurrent().getSubject().getUserId(), infraNamespace);
try {
InternalEnvironment internalEnv = createInternalEnvironment(config.getEnvironments().get(envName), config.getAttributes(), config.getCommands(), config.getDevfile());
RuntimeContext runtimeContext = infrastructure.prepare(runtimeId, internalEnv);
InternalRuntime runtime = runtimeContext.getRuntime();
try (Unlocker ignored = lockService.writeLock(workspaceId)) {
final WorkspaceStatus existingStatus = statuses.putIfAbsent(workspaceId, STARTING);
if (existingStatus != null) {
throw new ConflictException(format("Could not start workspace '%s' because its state is '%s'", workspaceId, existingStatus));
}
setRuntimesId(workspaceId);
runtimes.put(workspaceId, runtime);
}
LOG.info("Starting workspace '{}/{}' with id '{}' by user '{}'", workspace.getNamespace(), workspace.getName(), workspace.getId(), sessionUserNameOr("undefined"));
publishWorkspaceStatusEvent(workspaceId, STARTING, STOPPED, null, true, options);
return CompletableFuture.runAsync(ThreadLocalPropagateContext.wrap(new StartRuntimeTask(workspace, options, runtime)), sharedPool.getExecutor());
} catch (ValidationException e) {
LOG.error(e.getLocalizedMessage(), e);
throw new ConflictException(e.getLocalizedMessage());
} catch (InfrastructureException e) {
LOG.error(e.getLocalizedMessage(), e);
throw new ServerException(e.getLocalizedMessage(), e);
}
}
use of org.eclipse.che.api.workspace.server.spi.InternalRuntime in project devspaces-images by redhat-developer.
the class WorkspaceRuntimes method recoverOne.
@VisibleForTesting
InternalRuntime<?> recoverOne(RuntimeInfrastructure infra, RuntimeIdentity identity) throws ServerException, ConflictException {
if (isStartRefused.get()) {
throw new ConflictException(format("Recovery of the workspace '%s' is rejected by the system, " + "no more workspaces are allowed to start", identity.getWorkspaceId()));
}
WorkspaceImpl workspace;
try {
workspace = workspaceDao.get(identity.getWorkspaceId());
} catch (NotFoundException x) {
throw new ServerException(format("Workspace configuration is missing for the runtime '%s:%s'. Runtime won't be recovered", identity.getWorkspaceId(), identity.getEnvName()));
}
Environment environment = null;
WorkspaceConfigImpl workspaceConfig = workspace.getConfig();
if (workspaceConfig == null) {
workspaceConfig = devfileConverter.convert(workspace.getDevfile());
}
if (identity.getEnvName() != null) {
environment = workspaceConfig.getEnvironments().get(identity.getEnvName());
if (environment == null) {
throw new ServerException(format("Environment configuration is missing for the runtime '%s:%s'. Runtime won't be recovered", identity.getWorkspaceId(), identity.getEnvName()));
}
}
InternalRuntime runtime;
try {
InternalEnvironment internalEnv = createInternalEnvironment(environment, workspaceConfig.getAttributes(), workspaceConfig.getCommands(), workspaceConfig.getDevfile());
runtime = infra.prepare(identity, internalEnv).getRuntime();
WorkspaceStatus runtimeStatus = runtime.getStatus();
try (Unlocker ignored = lockService.writeLock(workspace.getId())) {
statuses.replace(identity.getWorkspaceId(), runtimeStatus);
runtimes.putIfAbsent(identity.getWorkspaceId(), runtime);
}
LOG.info("Successfully recovered workspace runtime '{}'. Its status is '{}'", identity.getWorkspaceId(), runtimeStatus);
return runtime;
} catch (NotFoundException x) {
LOG.warn("Not able to create internal environment for '{}'. Reason: '{}'", identity.getWorkspaceId(), x.getMessage());
try (Unlocker ignored = lockService.writeLock(identity.getWorkspaceId())) {
runtimes.remove(identity.getWorkspaceId());
statuses.remove(identity.getWorkspaceId());
}
publishWorkspaceStatusEvent(identity.getWorkspaceId(), STOPPED, STOPPING, "Workspace is stopped. Reason: " + x.getMessage(), false);
throw new ServerException(format("Couldn't recover runtime '%s:%s'. Error: %s", identity.getWorkspaceId(), identity.getEnvName(), x.getMessage()));
} catch (InfrastructureException | ValidationException x) {
throw new ServerException(format("Couldn't recover runtime '%s:%s'. Error: %s", identity.getWorkspaceId(), identity.getEnvName(), x.getMessage()));
}
}
use of org.eclipse.che.api.workspace.server.spi.InternalRuntime in project devspaces-images by redhat-developer.
the class WorkspaceRuntimesTest method shouldInjectRuntime.
@Test
public void shouldInjectRuntime() throws Exception {
// given
WorkspaceImpl workspace = new WorkspaceImpl();
workspace.setId("ws123");
when(statuses.get("ws123")).thenReturn(WorkspaceStatus.RUNNING);
ImmutableMap<String, Machine> machines = ImmutableMap.of("machine", new MachineImpl(emptyMap(), emptyMap(), MachineStatus.STARTING));
RuntimeIdentity identity = new RuntimeIdentityImpl("ws123", "my-env", "myId", "infraNamespace");
RuntimeContext context = mockContext(identity);
ConcurrentHashMap<String, InternalRuntime<?>> runtimesStorage = new ConcurrentHashMap<>();
TestInternalRuntime testRuntime = new TestInternalRuntime(context, machines, WorkspaceStatus.STARTING);
runtimesStorage.put("ws123", testRuntime);
WorkspaceRuntimes localRuntimes = new WorkspaceRuntimes(runtimesStorage, eventService, ImmutableMap.of(TEST_ENVIRONMENT_TYPE, testEnvFactory), infrastructure, sharedPool, workspaceDao, dbInitializer, probeScheduler, statuses, lockService, devfileConverter, false);
// when
localRuntimes.injectRuntime(workspace);
// then
assertEquals(workspace.getStatus(), WorkspaceStatus.RUNNING);
assertEquals(workspace.getRuntime(), asRuntime(testRuntime));
}
Aggregations