Skip to content

Commit

Permalink
Merge pull request #9345 from sleshchenko/spi-changes
Browse files Browse the repository at this point in the history
Improve abstract InternalRuntime to be more flexible for recovery functionality
  • Loading branch information
sleshchenko authored Apr 11, 2018
2 parents 88a37ca + fa2fcbb commit 692aeb6
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.function.Consumer;
import javax.inject.Named;
import org.eclipse.che.api.core.model.workspace.Warning;
import org.eclipse.che.api.core.model.workspace.WorkspaceStatus;
import org.eclipse.che.api.core.model.workspace.runtime.Machine;
import org.eclipse.che.api.core.model.workspace.runtime.MachineStatus;
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
Expand Down Expand Up @@ -180,7 +181,7 @@ private DockerInternalRuntime(
WorkspaceProbesFactory probesFactory,
ParallelDockerImagesBuilderFactory imagesBuilderFactory,
int bootstrappingTimeoutMinutes) {
super(context, urlRewriter, warnings, running);
super(context, urlRewriter, warnings, running ? WorkspaceStatus.RUNNING : null);
this.networks = networks;
this.containerStarter = machineStarter;
this.eventService = eventService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
import javax.inject.Singleton;
import org.eclipse.che.api.core.ValidationException;
import org.eclipse.che.api.core.model.workspace.runtime.Machine;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment;
import org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig;

/** Checks whether runtime is consistent with its configuration. */
@Singleton
class RuntimeConsistencyChecker {
void check(InternalEnvironment environment, DockerInternalRuntime runtime)
throws ValidationException {
throws ValidationException, InfrastructureException {
Map<String, InternalMachineConfig> configs = environment.getMachines();
Map<String, ? extends Machine> machines = runtime.getMachines();
if (configs.size() != machines.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class RuntimeConsistencyCheckerTest {

@Test(dataProvider = "consistentRuntimesProvider")
public void consistentRuntimes(InternalEnvironment environment, DockerInternalRuntime runtime)
throws ValidationException {
throws Exception {
new RuntimeConsistencyChecker().check(environment, runtime);
}

Expand All @@ -37,19 +37,19 @@ public void consistentRuntimes(InternalEnvironment environment, DockerInternalRu
expectedExceptions = ValidationException.class
)
public void inconsistentRuntimes(InternalEnvironment environment, DockerInternalRuntime runtime)
throws ValidationException {
throws Exception {
new RuntimeConsistencyChecker().check(environment, runtime);
}

@DataProvider
private static Object[][] consistentRuntimesProvider() {
private static Object[][] consistentRuntimesProvider() throws Exception {
return new Object[][] {
{environment("a", "b"), runtime("b", "a")}, {environment("b", "a"), runtime("b", "a")}
};
}

@DataProvider
private static Object[][] inconsistentRuntimesProvider() {
private static Object[][] inconsistentRuntimesProvider() throws Exception {
return new Object[][] {
{environment("a", "b"), runtime("a")},
{environment("a", "b"), runtime("a", "c")},
Expand All @@ -71,7 +71,7 @@ private static InternalEnvironment environment(String... names) {
return environment;
}

private static DockerInternalRuntime runtime(String... names) {
private static DockerInternalRuntime runtime(String... names) throws Exception {
Map<String, DockerMachine> machines = new HashMap<>();
for (String name : names) {
machines.put(name, mock(DockerMachine.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public KubernetesInternalRuntime(
@Assisted T context,
@Assisted KubernetesNamespace namespace,
@Assisted List<Warning> warnings) {
super(context, urlRewriter, warnings, false);
super(context, urlRewriter, warnings);
this.bootstrapperFactory = bootstrapperFactory;
this.serverCheckerFactory = serverCheckerFactory;
this.volumesStrategy = volumesStrategy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ public WorkspaceImpl getWorkspace(String name, String namespace)
/**
* Gets list of workspaces which user can read
*
* <p>
*
* <p>Returned workspaces have either {@link WorkspaceStatus#STOPPED} status or status defined by
* their runtime instances(if those exist).
*
Expand All @@ -175,7 +173,7 @@ public WorkspaceImpl getWorkspace(String name, String namespace)
* @return the list of workspaces or empty list if user can't read any workspace
* @throws NullPointerException when {@code user} is null
* @throws ServerException when any server error occurs while getting workspaces with {@link
* WorkspaceDao#getWorkspaces(String)}
* WorkspaceDao#getWorkspaces(String, int, long)}
*/
public Page<WorkspaceImpl> getWorkspaces(
String user, boolean includeRuntimes, int maxItems, long skipCount) throws ServerException {
Expand All @@ -201,7 +199,7 @@ public Page<WorkspaceImpl> getWorkspaces(
* @return the list of workspaces or empty list if no matches
* @throws NullPointerException when {@code namespace} is null
* @throws ServerException when any server error occurs while getting workspaces with {@link
* WorkspaceDao#getByNamespace(String)}
* WorkspaceDao#getByNamespace(String, int, long)}
*/
public Page<WorkspaceImpl> getByNamespace(
String namespace, boolean includeRuntimes, int maxItems, long skipCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,25 @@ public void validate(Environment environment)
*
* @param workspace the workspace to inject runtime into
*/
public void injectRuntime(WorkspaceImpl workspace) {
public void injectRuntime(WorkspaceImpl workspace) throws ServerException {
RuntimeState runtimeState = runtimes.get(workspace.getId());
if (runtimeState != null) {
workspace.setRuntime(new RuntimeImpl(runtimeState.runtime));
try {
workspace.setRuntime(asRuntime(runtimeState));
} catch (InfrastructureException e) {
throw new ServerException(
"Error occurred while runtime state inspection. " + e.getMessage());
}
workspace.setStatus(runtimeState.status);
} else {
workspace.setStatus(STOPPED);
}
}

private RuntimeImpl asRuntime(RuntimeState runtimeState) throws InfrastructureException {
InternalRuntime<?> runtime = runtimeState.runtime;
return new RuntimeImpl(runtime.getActiveEnv(), runtime.getMachines(), runtime.getOwner());
}
/**
* Gets workspace status by its identifier.
*
Expand Down Expand Up @@ -461,9 +470,11 @@ void recoverOne(RuntimeInfrastructure infra, RuntimeIdentity identity) throws Se
}

InternalRuntime runtime;
WorkspaceStatus status;
try {
InternalEnvironment internalEnv = createInternalEnvironment(environment);
runtime = infra.prepare(identity, internalEnv).getRuntime();
status = runtime.getStatus();
} catch (InfrastructureException | ValidationException | NotFoundException x) {
LOG.error(
"Couldn't recover runtime '{}:{}'. Error: {}",
Expand All @@ -474,7 +485,7 @@ void recoverOne(RuntimeInfrastructure infra, RuntimeIdentity identity) throws Se
}

RuntimeState prev =
runtimes.putIfAbsent(identity.getWorkspaceId(), new RuntimeState(runtime, RUNNING));
runtimes.putIfAbsent(identity.getWorkspaceId(), new RuntimeState(runtime, status));
if (prev == null) {
LOG.info(
"Successfully recovered workspace runtime '{}'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Objects;
import java.util.stream.Collectors;
import org.eclipse.che.api.core.model.workspace.Runtime;
import org.eclipse.che.api.core.model.workspace.Warning;
import org.eclipse.che.api.core.model.workspace.runtime.Machine;

/**
Expand All @@ -36,6 +37,17 @@ public RuntimeImpl(String activeEnv, Map<String, ? extends Machine> machines, St
this.owner = owner;
}

public RuntimeImpl(
String activeEnv,
Map<String, ? extends Machine> machines,
String owner,
List<? extends Warning> warnings) {
this.activeEnv = activeEnv;
this.machines = machines;
this.owner = owner;
this.warnings = warnings.stream().map(WarningImpl::new).collect(Collectors.toList());
}

public RuntimeImpl(Runtime runtime) {
this.activeEnv = runtime.getActiveEnv();
this.machines =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public class ServerImpl implements Server {

public ServerImpl() {}

public ServerImpl(String url, ServerStatus status, Map<String, String> attributes) {
this.url = url;
this.status = status;
if (attributes != null) {
this.attributes = new HashMap<>(attributes);
} else {
this.attributes = new HashMap<>();
}
}

public ServerImpl(Server server) {
this.url = server.getUrl();
this.status = server.getStatus();
Expand Down
Loading

0 comments on commit 692aeb6

Please sign in to comment.