Skip to content

Commit

Permalink
CHE-8265: Apply workspace next features in WorkspaceRuntimes
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Garagatyi <ogaragat@redhat.com>
  • Loading branch information
Oleksandr Garagatyi committed Jun 4, 2018
1 parent e010499 commit f6b639d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.eclipse.che.api.workspace.server.spi.StackDao;
import org.eclipse.che.api.workspace.server.spi.WorkspaceDao;
import org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironmentFactory;
import org.eclipse.che.api.workspace.server.wsnext.WorkspaceNextApplier;
import org.eclipse.che.commons.env.EnvironmentContext;
import org.eclipse.che.commons.lang.Pair;
import org.eclipse.che.commons.subject.SubjectImpl;
Expand Down Expand Up @@ -253,6 +254,10 @@ protected void configure() {
Multibinder.newSetBinder(binder(), ResourceLockKeyProvider.class);
Multibinder.newSetBinder(binder(), ResourceUsageTracker.class);
MapBinder.newMapBinder(binder(), String.class, AvailableResourcesProvider.class);
bind(String.class)
.annotatedWith(Names.named("che.workspace.feature.api"))
.toInstance("");
MapBinder.newMapBinder(binder(), String.class, WorkspaceNextApplier.class);
Multibinder.newSetBinder(binder(), ResourceType.class)
.addBinding()
.to(RamResourceType.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
Expand Down Expand Up @@ -65,6 +66,9 @@
import org.eclipse.che.api.workspace.server.spi.WorkspaceDao;
import org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment;
import org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironmentFactory;
import org.eclipse.che.api.workspace.server.wsnext.WorkspaceNextApplier;
import org.eclipse.che.api.workspace.server.wsnext.WorkspaceNextObjectsRetriever;
import org.eclipse.che.api.workspace.server.wsnext.model.CheService;
import org.eclipse.che.api.workspace.shared.dto.event.RuntimeStatusEvent;
import org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent;
import org.eclipse.che.commons.env.EnvironmentContext;
Expand Down Expand Up @@ -100,6 +104,8 @@ public class WorkspaceRuntimes {
private final ProbeScheduler probeScheduler;
// Unique identifier for this workspace runtimes
private final String workspaceRuntimesId;
private final Map<String, WorkspaceNextApplier> workspaceNextAppliers;
private final WorkspaceNextObjectsRetriever workspaceNextObjectsRetriever;

@VisibleForTesting
WorkspaceRuntimes(
Expand All @@ -112,7 +118,9 @@ public class WorkspaceRuntimes {
@SuppressWarnings("unused") DBInitializer ignored,
ProbeScheduler probeScheduler,
WorkspaceStatusCache statuses,
WorkspaceLockService lockService) {
WorkspaceLockService lockService,
Map<String, WorkspaceNextApplier> workspaceNextAppliers,
WorkspaceNextObjectsRetriever workspaceNextObjectsRetriever) {
this(
eventService,
envFactories,
Expand All @@ -122,7 +130,9 @@ public class WorkspaceRuntimes {
ignored,
probeScheduler,
statuses,
lockService);
lockService,
workspaceNextAppliers,
workspaceNextObjectsRetriever);
this.runtimes = runtimes;
}

Expand All @@ -136,7 +146,9 @@ public WorkspaceRuntimes(
@SuppressWarnings("unused") DBInitializer ignored,
ProbeScheduler probeScheduler,
WorkspaceStatusCache statuses,
WorkspaceLockService lockService) {
WorkspaceLockService lockService,
Map<String, WorkspaceNextApplier> workspaceNextAppliers,
WorkspaceNextObjectsRetriever workspaceNextObjectsRetriever) {
this.probeScheduler = probeScheduler;
this.runtimes = new ConcurrentHashMap<>();
this.statuses = statuses;
Expand All @@ -147,6 +159,8 @@ public WorkspaceRuntimes(
this.infrastructure = infra;
this.environmentFactories = ImmutableMap.copyOf(envFactories);
this.lockService = lockService;
this.workspaceNextAppliers = ImmutableMap.copyOf(workspaceNextAppliers);
this.workspaceNextObjectsRetriever = workspaceNextObjectsRetriever;
LOG.info("Configured factories for environments: '{}'", envFactories.keySet());
LOG.info("Registered infrastructure '{}'", infra.getName());
SetView<String> notSupportedByInfra =
Expand All @@ -172,7 +186,7 @@ public void validate(Environment environment)
throw new NotFoundException("Infrastructure not found for type: " + type);
}
// try to create internal environment to check if the specified environment is valid
createInternalEnvironment(environment);
createInternalEnvironment(environment, null);
}

/**
Expand Down Expand Up @@ -296,7 +310,8 @@ public CompletableFuture<Void> startAsync(
final String ownerId = EnvironmentContext.getCurrent().getSubject().getUserId();
final RuntimeIdentity runtimeId = new RuntimeIdentityImpl(workspaceId, envName, ownerId);
try {
InternalEnvironment internalEnv = createInternalEnvironment(environment);
InternalEnvironment internalEnv =
createInternalEnvironment(environment, workspace.getAttributes());
RuntimeContext runtimeContext = infrastructure.prepare(runtimeId, internalEnv);
InternalRuntime runtime = runtimeContext.getRuntime();

Expand Down Expand Up @@ -570,7 +585,8 @@ InternalRuntime<?> recoverOne(RuntimeInfrastructure infra, RuntimeIdentity ident

InternalRuntime runtime;
try {
InternalEnvironment internalEnv = createInternalEnvironment(environment);
InternalEnvironment internalEnv =
createInternalEnvironment(environment, workspace.getAttributes());
runtime = infra.prepare(identity, internalEnv).getRuntime();

try (Unlocker ignored = lockService.writeLock(workspace.getId())) {
Expand Down Expand Up @@ -723,15 +739,37 @@ public Set<String> getSupportedRecipes() {
return environmentFactories.keySet();
}

private InternalEnvironment createInternalEnvironment(Environment environment)
private InternalEnvironment createInternalEnvironment(
Environment environment, Map<String, String> workspaceAttributes)
throws InfrastructureException, ValidationException, NotFoundException {
String recipeType = environment.getRecipe().getType();
InternalEnvironmentFactory factory = environmentFactories.get(recipeType);
if (factory == null) {
throw new NotFoundException(
format("InternalEnvironmentFactory is not configured for recipe type: '%s'", recipeType));
}
return factory.create(environment);
InternalEnvironment internalEnvironment = factory.create(environment);

applyWorkspaceNext(internalEnvironment, workspaceAttributes, recipeType);

return internalEnvironment;
}

private void applyWorkspaceNext(
InternalEnvironment internalEnvironment,
Map<String, String> workspaceAttributes,
String recipeType)
throws InfrastructureException {
Collection<CheService> cheServices = workspaceNextObjectsRetriever.get(workspaceAttributes);
if (cheServices.isEmpty()) {
return;
}
WorkspaceNextApplier wsNext = workspaceNextAppliers.get(recipeType);
if (wsNext == null) {
throw new InfrastructureException(
"Workspace.Next features are not supported for recipe type " + recipeType);
}
wsNext.apply(internalEnvironment, cheServices);
}

private String sessionUserNameOr(String nameIfNoUser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.eclipse.che.api.workspace.server.spi.WorkspaceDao;
import org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment;
import org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironmentFactory;
import org.eclipse.che.api.workspace.server.wsnext.WorkspaceNextObjectsRetriever;
import org.eclipse.che.api.workspace.shared.dto.RuntimeIdentityDto;
import org.eclipse.che.api.workspace.shared.dto.event.RuntimeStatusEvent;
import org.eclipse.che.core.db.DBInitializer;
Expand Down Expand Up @@ -93,6 +94,8 @@ public class WorkspaceRuntimesTest {

@Mock private WorkspaceStatusCache statuses;

@Mock private WorkspaceNextObjectsRetriever workspaceNextObjectsRetriever;

private RuntimeInfrastructure infrastructure;

@Mock private InternalEnvironmentFactory<InternalEnvironment> testEnvFactory;
Expand All @@ -113,7 +116,9 @@ public void setUp() throws Exception {
dbInitializer,
probeScheduler,
statuses,
lockService);
lockService,
emptyMap(),
workspaceNextObjectsRetriever);
}

@Test
Expand Down Expand Up @@ -202,7 +207,9 @@ public void attributesIsSetWhenRuntimeAbnormallyStopped() throws Exception {
dbInitializer,
probeScheduler,
statuses,
lockService);
lockService,
emptyMap(),
workspaceNextObjectsRetriever);
localRuntimes.init();
RuntimeIdentityDto identity =
DtoFactory.newDto(RuntimeIdentityDto.class)
Expand Down Expand Up @@ -261,7 +268,9 @@ public void shouldInjectRuntime() throws Exception {
dbInitializer,
probeScheduler,
statuses,
lockService);
lockService,
emptyMap(),
workspaceNextObjectsRetriever);

// when
localRuntimes.injectRuntime(workspace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package org.eclipse.che.core.db.jpa;

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static org.eclipse.che.core.db.jpa.TestObjectsFactory.createAccount;
import static org.eclipse.che.core.db.jpa.TestObjectsFactory.createPreferences;
Expand All @@ -33,7 +34,6 @@
import com.google.inject.Injector;
import com.google.inject.Stage;
import com.google.inject.name.Names;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.Callable;
import javax.annotation.PostConstruct;
Expand Down Expand Up @@ -209,14 +209,16 @@ protected void configure() {
spy(
new WorkspaceRuntimes(
mock(EventService.class),
Collections.emptyMap(),
emptyMap(),
infra,
mock(WorkspaceSharedPool.class),
mock(WorkspaceDao.class),
mock(DBInitializer.class),
mock(ProbeScheduler.class),
new DefaultWorkspaceStatusCache(),
new DefaultWorkspaceLockService()));
new DefaultWorkspaceLockService(),
emptyMap(),
null));
when(wR.hasRuntime(anyString())).thenReturn(false);
bind(WorkspaceRuntimes.class).toInstance(wR);
bind(AccountManager.class);
Expand Down

0 comments on commit f6b639d

Please sign in to comment.