use of org.eclipse.che.api.core.model.machine.Machine in project che by eclipse.
the class CheEnvironmentEngine method startEnvironmentQueue.
/**
* Starts all machine from machine queue of environment.
*/
private void startEnvironmentQueue(String namespace, String workspaceId, String devMachineName, String networkId, boolean recover, MachineStartedHandler startedHandler) throws ServerException, EnvironmentException {
// Starting all machines in environment one by one by getting configs
// from the corresponding starting queue.
// Config will be null only if there are no machines left in the queue
String envName;
MessageConsumer<MachineLogMessage> envLogger;
String creator = EnvironmentContext.getCurrent().getSubject().getUserId();
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
EnvironmentHolder environmentHolder = environments.get(workspaceId);
if (environmentHolder == null) {
throw new ServerException("Environment start is interrupted.");
}
envName = environmentHolder.name;
envLogger = environmentHolder.logger;
}
try {
machineProvider.createNetwork(networkId);
String machineName = queuePeekOrFail(workspaceId);
while (machineName != null) {
boolean isDev = devMachineName.equals(machineName);
// Environment start is failed when any machine start is failed, so if any error
// occurs during machine creation then environment start fail is reported and
// start resources such as queue and descriptor must be cleaned up
CheServiceImpl service;
@Nullable ExtendedMachine extendedMachine;
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
EnvironmentHolder environmentHolder = environments.get(workspaceId);
if (environmentHolder == null) {
throw new ServerException("Environment start is interrupted.");
}
service = environmentHolder.environment.getServices().get(machineName);
extendedMachine = environmentHolder.environmentConfig.getMachines().get(machineName);
}
// should not happen
if (service == null) {
LOG.error("Start of machine with name {} in workspace {} failed. Machine not found in start queue", machineName, workspaceId);
throw new ServerException(format("Environment of workspace with ID '%s' failed due to internal error", workspaceId));
}
final String finalMachineName = machineName;
// needed to reuse startInstance method and
// create machine instances by different implementation-specific providers
MachineStarter machineStarter = (machineLogger, machineSource) -> {
CheServiceImpl serviceWithNormalizedSource = normalizeServiceSource(service, machineSource);
return machineProvider.startService(namespace, workspaceId, envName, finalMachineName, isDev, networkId, serviceWithNormalizedSource, machineLogger);
};
MachineImpl machine = MachineImpl.builder().setConfig(MachineConfigImpl.builder().setDev(isDev).setLimits(new MachineLimitsImpl(bytesToMB(service.getMemLimit()))).setType("docker").setName(machineName).setEnvVariables(service.getEnvironment()).build()).setId(service.getId()).setWorkspaceId(workspaceId).setStatus(MachineStatus.CREATING).setEnvName(envName).setOwner(creator).build();
checkInterruption(workspaceId, envName);
Instance instance = startInstance(recover, envLogger, machine, machineStarter);
checkInterruption(workspaceId, envName);
startedHandler.started(instance, extendedMachine);
checkInterruption(workspaceId, envName);
// Machine destroying is an expensive operation which must be
// performed outside of the lock, this section checks if
// the environment wasn't stopped while it is starting and sets
// polled flag to true if the environment wasn't stopped.
// Also polls the proceeded machine configuration from the queue
boolean queuePolled = false;
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(workspaceId)) {
ensurePreDestroyIsNotExecuted();
EnvironmentHolder environmentHolder = environments.get(workspaceId);
if (environmentHolder != null) {
final Queue<String> queue = environmentHolder.startQueue;
if (queue != null) {
queue.poll();
queuePolled = true;
}
}
}
// must be destroyed
if (!queuePolled) {
try {
eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.DESTROYING).withDev(isDev).withMachineName(machineName).withMachineId(instance.getId()).withWorkspaceId(workspaceId));
instance.destroy();
removeMachine(workspaceId, instance.getId());
eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.DESTROYED).withDev(isDev).withMachineName(machineName).withMachineId(instance.getId()).withWorkspaceId(workspaceId));
} catch (MachineException e) {
LOG.error(e.getLocalizedMessage(), e);
}
throw new ServerException("Workspace '" + workspaceId + "' start interrupted. Workspace stopped before all its machines started");
}
machineName = queuePeekOrFail(workspaceId);
}
} catch (RuntimeException | ServerException | EnvironmentStartInterruptedException e) {
boolean interrupted = Thread.interrupted();
EnvironmentHolder env;
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(workspaceId)) {
env = environments.remove(workspaceId);
}
try {
destroyEnvironment(env.networkId, env.machines);
} catch (Exception remEx) {
LOG.error(remEx.getLocalizedMessage(), remEx);
}
if (interrupted) {
throw new EnvironmentStartInterruptedException(workspaceId, envName);
}
try {
throw e;
} catch (ServerException | EnvironmentStartInterruptedException rethrow) {
throw rethrow;
} catch (Exception wrap) {
throw new ServerException(wrap.getMessage(), wrap);
}
}
}
use of org.eclipse.che.api.core.model.machine.Machine in project che by eclipse.
the class CheEnvironmentEngineTest method machineStopShouldFireEvents.
@Test
public void machineStopShouldFireEvents() throws Exception {
// given
List<Instance> instances = startEnv();
Optional<Instance> instanceOpt = instances.stream().filter(machine -> !machine.getConfig().isDev()).findAny();
assertTrue(instanceOpt.isPresent(), "Required for test non-dev machine is not found");
Instance instance = instanceOpt.get();
// when
engine.stopMachine(instance.getWorkspaceId(), instance.getId());
// then
verify(eventService).publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.CREATING).withDev(instance.getConfig().isDev()).withMachineName(instance.getConfig().getName()).withMachineId(instance.getId()).withWorkspaceId(instance.getWorkspaceId()));
verify(eventService).publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.RUNNING).withDev(instance.getConfig().isDev()).withMachineName(instance.getConfig().getName()).withMachineId(instance.getId()).withWorkspaceId(instance.getWorkspaceId()));
}
use of org.eclipse.che.api.core.model.machine.Machine in project che by eclipse.
the class CheEnvironmentEngineTest method startEnv.
private List<Instance> startEnv(EnvironmentImpl env, CheServicesEnvironmentImpl cheServicesEnv) throws Exception {
String envName = "env-1";
String workspaceId = "wsId";
when(machineProvider.startService(anyString(), eq(workspaceId), eq(envName), anyString(), anyBoolean(), anyString(), any(CheServiceImpl.class), any(LineConsumer.class))).thenAnswer(invocationOnMock -> {
Object[] arguments = invocationOnMock.getArguments();
String machineName = (String) arguments[3];
boolean isDev = (boolean) arguments[4];
CheServiceImpl service = (CheServiceImpl) arguments[6];
Machine machine = createMachine(workspaceId, envName, service, machineName, isDev);
return spy(new NoOpMachineInstance(machine));
});
when(environmentParser.parse(env)).thenReturn(cheServicesEnv);
// when
return engine.start(workspaceId, envName, env, false, messageConsumer);
}
use of org.eclipse.che.api.core.model.machine.Machine in project che by eclipse.
the class CheEnvironmentEngineTest method shouldBeAbleToStartEnvironment.
@Test
public void shouldBeAbleToStartEnvironment() throws Exception {
// given
EnvironmentImpl env = createEnv();
String envName = "env-1";
String workspaceId = "wsId";
List<Instance> expectedMachines = new ArrayList<>();
when(machineProvider.startService(anyString(), eq(workspaceId), eq(envName), anyString(), anyBoolean(), anyString(), any(CheServiceImpl.class), any(LineConsumer.class))).thenAnswer(invocationOnMock -> {
Object[] arguments = invocationOnMock.getArguments();
String machineName = (String) arguments[3];
boolean isDev = (boolean) arguments[4];
CheServiceImpl service = (CheServiceImpl) arguments[6];
Machine machine = createMachine(workspaceId, envName, service, machineName, isDev);
NoOpMachineInstance instance = spy(new NoOpMachineInstance(machine));
expectedMachines.add(instance);
return instance;
});
when(environmentParser.parse(env)).thenReturn(createCheServicesEnv());
// when
List<Instance> machines = engine.start(workspaceId, envName, env, false, messageConsumer, startedHandler);
// then
assertEquals(machines, expectedMachines);
for (Instance expectedMachine : expectedMachines) {
verify(startedHandler).started(eq(expectedMachine), any(ExtendedMachine.class));
}
}
use of org.eclipse.che.api.core.model.machine.Machine in project che by eclipse.
the class CheEnvironmentEngineTest method shouldBeAbleToStartEnvironmentWhenRecoverFailed.
@Test
public void shouldBeAbleToStartEnvironmentWhenRecoverFailed() throws Exception {
// given
String machineImage = "che/ubuntu_jdk";
when(snapshotDao.getSnapshot(anyString(), anyString(), anyString())).thenThrow(new NotFoundException("Snapshot not found"));
EnvironmentImpl env = createEnv();
String envName = "env-1";
String workspaceId = "wsId";
List<Instance> expectedMachines = new ArrayList<>();
when(machineProvider.startService(anyString(), eq(workspaceId), eq(envName), anyString(), anyBoolean(), anyString(), any(CheServiceImpl.class), any(LineConsumer.class))).thenAnswer(invocationOnMock -> {
Object[] arguments = invocationOnMock.getArguments();
String machineName = (String) arguments[3];
boolean isDev = (boolean) arguments[4];
CheServiceImpl service = (CheServiceImpl) arguments[6];
Machine machine = createMachine(workspaceId, envName, service, machineName, isDev);
NoOpMachineInstance instance = spy(new NoOpMachineInstance(machine));
expectedMachines.add(instance);
return instance;
});
CheServicesEnvironmentImpl servicesEnvironment = createCheServicesEnv();
for (CheServiceImpl service : servicesEnvironment.getServices().values()) {
service.setImage(machineImage);
}
when(environmentParser.parse(env)).thenReturn(servicesEnvironment);
// when
List<Instance> machines = engine.start(workspaceId, envName, env, true, messageConsumer);
// then
assertEquals(machines, expectedMachines);
ArgumentCaptor<CheServiceImpl> captor = ArgumentCaptor.forClass(CheServiceImpl.class);
verify(machineProvider).startService(anyString(), anyString(), anyString(), anyString(), eq(false), anyString(), captor.capture(), any(LineConsumer.class));
CheServiceImpl actualService = captor.getValue();
assertEquals(actualService.getImage(), machineImage);
}
Aggregations