use of org.eclipse.che.commons.lang.concurrent.Unlocker 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.commons.lang.concurrent.Unlocker in project che by eclipse.
the class CheEnvironmentEngine method initializeEnvironment.
private void initializeEnvironment(String namespace, String workspaceId, String envName, EnvironmentImpl envConfig, String networkId, MessageConsumer<MachineLogMessage> messageConsumer) throws ServerException, ConflictException, EnvironmentException {
CheServicesEnvironmentImpl internalEnv = environmentParser.parse(envConfig);
internalEnv.setWorkspaceId(workspaceId);
infrastructureProvisioner.provision(envConfig, internalEnv);
normalize(namespace, workspaceId, internalEnv);
List<String> servicesOrder = startStrategy.order(internalEnv);
normalizeNames(internalEnv);
EnvironmentHolder environmentHolder = new EnvironmentHolder(servicesOrder, internalEnv, envConfig, messageConsumer, EnvStatus.STARTING, envName, networkId);
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(workspaceId)) {
if (environments.putIfAbsent(workspaceId, environmentHolder) != null) {
throw new ConflictException(format("Environment of workspace '%s' already exists", workspaceId));
}
}
}
use of org.eclipse.che.commons.lang.concurrent.Unlocker in project che by eclipse.
the class CheEnvironmentEngine method start.
/**
* Starts provided environment.
*
* <p>Environment starts if and only all machines in environment definition start successfully.<br/>
* Otherwise exception is thrown by this method.<br/>
* It is not defined whether environment start fails right after first failure or in the end of the process.<br/>
* Starting order of machines is not guarantied. Machines can start sequentially or in parallel.
*
* @param workspaceId
* ID of workspace that owns provided environment
* @param envName
* name of environment
* @param env
* environment to start
* @param recover
* whether machines from environment should be recovered or not
* @param messageConsumer
* consumer of log messages from machines in the environment
* @param startedHandler
* handler for started machines
* @return list of running machines of this environment
* @throws ServerException
* if other error occurs
*/
public List<Instance> start(String workspaceId, String envName, Environment env, boolean recover, MessageConsumer<MachineLogMessage> messageConsumer, MachineStartedHandler startedHandler) throws ServerException, EnvironmentException, ConflictException {
EnvironmentImpl environment = new EnvironmentImpl(env);
// TODO move to machines provider
// add random chars to ensure that old environments that weren't removed by some reason won't prevent start
String networkId = NameGenerator.generate(workspaceId + "_", 16);
String namespace = EnvironmentContext.getCurrent().getSubject().getUserName();
initializeEnvironment(namespace, workspaceId, envName, environment, networkId, messageConsumer);
String devMachineName = getDevMachineName(environment);
if (devMachineName == null) {
throw new ServerException("Agent 'org.eclipse.che.ws-agent' is not found in any of environment machines");
}
startEnvironmentQueue(namespace, workspaceId, devMachineName, networkId, recover, startedHandler);
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(workspaceId)) {
EnvironmentHolder environmentHolder = environments.get(workspaceId);
// possible only if environment was stopped during its start
if (environmentHolder == null) {
throw new ServerException("Environment start was interrupted by environment stopping");
}
environmentHolder.status = EnvStatus.RUNNING;
// prevent list modification
return new ArrayList<>(environmentHolder.machines);
}
}
use of org.eclipse.che.commons.lang.concurrent.Unlocker in project che by eclipse.
the class WorkspaceRuntimes method shutdown.
/**
* Terminates workspace runtimes service, so no more workspaces are allowed to start
* or to be stopped directly, all the running workspaces are going to be stopped,
* all the starting tasks will be eventually interrupted.
*
* @throws IllegalStateException
* if component shutdown is already called
*/
public void shutdown() throws InterruptedException {
if (!isShutdown.compareAndSet(false, true)) {
throw new IllegalStateException("Workspace runtimes service shutdown has been already called");
}
List<String> idsToStop;
try (@SuppressWarnings("unused") Unlocker u = locks.writeAllLock()) {
idsToStop = states.entrySet().stream().filter(e -> e.getValue().status != WorkspaceStatus.STOPPING).map(Map.Entry::getKey).collect(Collectors.toList());
states.clear();
}
if (!idsToStop.isEmpty()) {
LOG.info("Shutdown running environments, environments to stop: '{}'", idsToStop.size());
ExecutorService executor = Executors.newFixedThreadPool(2 * Runtime.getRuntime().availableProcessors(), new ThreadFactoryBuilder().setNameFormat("StopEnvironmentsPool-%d").setDaemon(false).build());
for (String id : idsToStop) {
executor.execute(() -> {
try {
envEngine.stop(id);
} catch (EnvironmentNotRunningException ignored) {
// might be already stopped
} catch (Exception x) {
LOG.error(x.getMessage(), x);
}
});
}
executor.shutdown();
try {
if (!executor.awaitTermination(30, TimeUnit.SECONDS)) {
executor.shutdownNow();
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
LOG.error("Unable to stop runtimes termination pool");
}
}
} catch (InterruptedException e) {
executor.shutdownNow();
Thread.currentThread().interrupt();
}
}
}
use of org.eclipse.che.commons.lang.concurrent.Unlocker in project che by eclipse.
the class WorkspaceRuntimes method injectRuntime.
/**
* Injects runtime information such as status and {@link WorkspaceRuntimeImpl}
* into the workspace object, if the workspace doesn't have runtime sets the
* status to {@link WorkspaceStatus#STOPPED}.
*
* @param workspace
* the workspace to inject runtime into
*/
public void injectRuntime(WorkspaceImpl workspace) {
requireNonNull(workspace, "Required non-null workspace");
RuntimeState state = null;
try (@SuppressWarnings("unused") Unlocker u = locks.readLock(workspace.getId())) {
if (states.containsKey(workspace.getId())) {
state = new RuntimeState(states.get(workspace.getId()));
}
}
if (state == null) {
workspace.setStatus(WorkspaceStatus.STOPPED);
} else {
workspace.setStatus(state.status);
try {
workspace.setRuntime(new WorkspaceRuntimeImpl(state.envName, envEngine.getMachines(workspace.getId())));
} catch (Exception x) {
workspace.setRuntime(new WorkspaceRuntimeImpl(state.envName, Collections.emptyList()));
}
}
}
Aggregations