Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[prebuilds] Fix prebuild updates received by ws-manager-bridge #10042

Merged
merged 2 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion components/ws-manager-bridge/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ packages:
srcs:
- "**/*.ts"
- package.json
- mocha.opts
deps:
- components/content-service-api/typescript:lib
- components/gitpod-db:lib
Expand All @@ -16,7 +17,8 @@ packages:
packaging: offline-mirror
yarnLock: ${coreYarnLockBase}/yarn.lock
tsconfig: tsconfig.json
dontTest: true
commands:
test: ["yarn", "test"]
- name: docker
type: docker
deps:
Expand Down
66 changes: 18 additions & 48 deletions components/ws-manager-bridge/ee/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
*/

import { WorkspaceManagerBridge } from "../../src/bridge";
import { injectable } from "inversify";
import { inject, injectable } from "inversify";
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
import { WorkspaceStatus, WorkspaceType, WorkspacePhase } from "@gitpod/ws-manager/lib";
import { HeadlessWorkspaceEvent, HeadlessWorkspaceEventType } from "@gitpod/gitpod-protocol/lib/headless-workspace-log";
import { HeadlessWorkspaceEvent } from "@gitpod/gitpod-protocol/lib/headless-workspace-log";
import { WorkspaceInstance } from "@gitpod/gitpod-protocol";
import { log, LogContext } from "@gitpod/gitpod-protocol/lib/util/logging";
import { PrebuildStateMapper } from "../../src/prebuild-state-mapper";

@injectable()
export class WorkspaceManagerBridgeEE extends WorkspaceManagerBridge {
@inject(PrebuildStateMapper)
protected readonly prebuildStateMapper: PrebuildStateMapper;

protected async cleanupProbeWorkspace(ctx: TraceContext, status: WorkspaceStatus.AsObject | undefined) {
if (!status) {
return;
Expand Down Expand Up @@ -75,65 +79,31 @@ export class WorkspaceManagerBridgeEE extends WorkspaceManagerBridge {
}
prebuild.statusVersion = status.statusVersion;

if (prebuild.state === "queued") {
// We've received an update from ws-man for this workspace, hence it must be running.
prebuild.state = "building";

if (writeToDB) {
await this.workspaceDB.trace({ span }).storePrebuiltWorkspace(prebuild);
}
await this.messagebus.notifyHeadlessUpdate({ span }, userId, workspaceId, <HeadlessWorkspaceEvent>{
type: HeadlessWorkspaceEventType.Started,
workspaceID: workspaceId,
});
}

if (status.phase === WorkspacePhase.STOPPING) {
let headlessUpdateType: HeadlessWorkspaceEventType = HeadlessWorkspaceEventType.Aborted;
if (!!status.conditions!.timeout) {
prebuild.state = "timeout";
prebuild.error = status.conditions!.timeout;
headlessUpdateType = HeadlessWorkspaceEventType.AbortedTimedOut;
} else if (!!status.conditions!.failed) {
prebuild.state = "failed";
prebuild.error = status.conditions!.failed;
headlessUpdateType = HeadlessWorkspaceEventType.Failed;
} else if (!!status.conditions!.stoppedByRequest) {
prebuild.state = "aborted";
prebuild.error = "Cancelled";
headlessUpdateType = HeadlessWorkspaceEventType.Aborted;
} else if (!!status.conditions!.headlessTaskFailed) {
prebuild.state = "available";
if (status.conditions!.headlessTaskFailed) prebuild.error = status.conditions!.headlessTaskFailed;
prebuild.snapshot = status.conditions!.snapshot;
headlessUpdateType = HeadlessWorkspaceEventType.FinishedButFailed;
} else if (!!status.conditions!.snapshot) {
prebuild.state = "available";
prebuild.snapshot = status.conditions!.snapshot;
headlessUpdateType = HeadlessWorkspaceEventType.FinishedSuccessfully;
} else {
// stopping event with no clear outcome (i.e. no snapshot yet)
return;
}
const update = this.prebuildStateMapper.mapWorkspaceStatusToPrebuild(status);
if (update) {
const updatedPrebuild = {
...prebuild,
...update.update,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈

};

span.setTag("updatePrebuildWorkspace.prebuild.state", prebuild.state);
span.setTag("updatePrebuildWorkspace.prebuild.error", prebuild.error);
span.setTag("updatePrebuildWorkspace.prebuild.state", updatedPrebuild.state);
span.setTag("updatePrebuildWorkspace.prebuild.error", updatedPrebuild.error);

if (writeToDB) {
await this.workspaceDB.trace({ span }).storePrebuiltWorkspace(prebuild);
await this.workspaceDB.trace({ span }).storePrebuiltWorkspace(updatedPrebuild);
}

// notify updates
// headless update
await this.messagebus.notifyHeadlessUpdate({ span }, userId, workspaceId, <HeadlessWorkspaceEvent>{
type: headlessUpdateType,
type: update.type,
workspaceID: workspaceId,
});

// prebuild info
const info = (await this.workspaceDB.trace({ span }).findPrebuildInfos([prebuild.id]))[0];
const info = (await this.workspaceDB.trace({ span }).findPrebuildInfos([updatedPrebuild.id]))[0];
if (info) {
this.messagebus.notifyOnPrebuildUpdate({ info, status: prebuild.state });
this.messagebus.notifyOnPrebuildUpdate({ info, status: updatedPrebuild.state });
}
}
} catch (e) {
Expand Down
6 changes: 6 additions & 0 deletions components/ws-manager-bridge/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--require ts-node/register
--require reflect-metadata/Reflect
--require source-map-support/register
--reporter spec
--watch-extensions ts
--exit
8 changes: 8 additions & 0 deletions components/ws-manager-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"start": "node ./dist/src/index.js",
"start-ee": "node ./dist/ee/src/index.js",
"test": "mocha --opts mocha.opts './**/*.spec.ts' --exclude './node_modules/**'",
"prepare": "yarn clean && yarn build",
"build": "npx tsc",
"build:clean": "yarn clean && yarn build",
Expand Down Expand Up @@ -36,8 +37,15 @@
},
"devDependencies": {
"@types/amqplib": "^0.8.2",
"@types/chai": "^4.2.21",
"@types/express": "^4.17.13",
"@types/google-protobuf": "^3.7.4",
"@types/mocha": "^2.2.45",
"chai": "^4.3.4",
"expect": "^1.20.2",
"mocha": "^5.0.0",
"mocha-typescript": "^1.1.11",
"ts-node": "<7.0.0",
"tslint": "^5.9.1",
"typescript": "~4.4.2",
"watch": "^1.0.2"
Expand Down
3 changes: 3 additions & 0 deletions components/ws-manager-bridge/src/container-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { MetaInstanceController } from "./meta-instance-controller";
import { IClientCallMetrics } from "@gitpod/content-service/lib/client-call-metrics";
import { PrometheusClientCallMetrics } from "@gitpod/gitpod-protocol/lib/messaging/client-call-metrics";
import { PreparingUpdateEmulator, PreparingUpdateEmulatorFactory } from "./preparing-update-emulator";
import { PrebuildStateMapper } from "./prebuild-state-mapper";

export const containerModule = new ContainerModule((bind) => {
bind(MessagebusConfiguration).toSelf().inSingletonScope();
Expand Down Expand Up @@ -80,4 +81,6 @@ export const containerModule = new ContainerModule((bind) => {

bind(PreparingUpdateEmulator).toSelf().inRequestScope();
bind(PreparingUpdateEmulatorFactory).toAutoFactory(PreparingUpdateEmulator);

bind(PrebuildStateMapper).toSelf().inSingletonScope();
});
126 changes: 126 additions & 0 deletions components/ws-manager-bridge/src/prebuild-state-mapper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { suite, test } from "mocha-typescript";
import * as chai from "chai";
import { PrebuildStateMapper } from "./prebuild-state-mapper";
import { WorkspaceConditionBool, WorkspacePhase, WorkspaceStatus } from "@gitpod/ws-manager/lib";
import { PrebuiltWorkspace } from "@gitpod/gitpod-protocol";

const expect = chai.expect;

@suite
class TestPrebuildStateMapper {
@test public testAll() {
const id = "12345";
const snapshot = "some-valid-snapshot";
const failed = "some system error";
const headlessTaskFailed = "some user/content error";

const table: {
name: string;
status: Pick<WorkspaceStatus.AsObject, "id" | "phase"> & {
conditions: Partial<WorkspaceStatus.AsObject["conditions"]>;
};
expected: (Omit<Partial<PrebuiltWorkspace>, "error"> & { hasError?: boolean }) | undefined;
}[] = [
{
name: "STOPPED",
expected: undefined,
status: {
id,
phase: WorkspacePhase.STOPPED,
conditions: {
snapshot,
},
},
},
{
name: "failed",
expected: {
state: "failed",
hasError: true,
},
status: {
id,
phase: WorkspacePhase.STOPPING,
conditions: {
failed,
},
},
},
{
name: "failed - no snapshot",
expected: {
state: "failed",
hasError: true,
},
status: {
id,
phase: WorkspacePhase.STOPPING,
conditions: {
snapshot: "",
},
},
},
{
name: "aborted",
expected: {
state: "aborted",
hasError: true,
},
status: {
id,
phase: WorkspacePhase.STOPPING,
conditions: {
stoppedByRequest: WorkspaceConditionBool.TRUE,
},
},
},
{
name: "user/content error",
expected: {
state: "available",
hasError: true,
snapshot,
},
status: {
id,
phase: WorkspacePhase.STOPPING,
conditions: {
headlessTaskFailed,
snapshot,
},
},
},
{
name: "available and no error",
expected: {
state: "available",
hasError: false,
snapshot,
},
status: {
id,
phase: WorkspacePhase.STOPPING,
conditions: {
snapshot,
},
},
},
];

for (const test of table) {
const cut = new PrebuildStateMapper();
const actual = cut.mapWorkspaceStatusToPrebuild(test.status as WorkspaceStatus.AsObject);
expect(!!actual?.update.error, test.name + ": hasError").to.be.equal(!!test.expected?.hasError);
delete actual?.update.error;
delete test.expected?.hasError;
expect(actual?.update, test.name).to.deep.equal(test.expected);
}
}
}
module.exports = new TestPrebuildStateMapper(); // Only to circumvent no usage warning :-/
94 changes: 94 additions & 0 deletions components/ws-manager-bridge/src/prebuild-state-mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { HeadlessWorkspaceEventType, PrebuiltWorkspace } from "@gitpod/gitpod-protocol";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { WorkspacePhase, WorkspaceStatus } from "@gitpod/ws-manager/lib";
import { injectable } from "inversify";

export interface PrebuildUpdate {
type: HeadlessWorkspaceEventType;
update: Partial<PrebuiltWorkspace>;
}

@injectable()
export class PrebuildStateMapper {
mapWorkspaceStatusToPrebuild(status: WorkspaceStatus.AsObject): PrebuildUpdate | undefined {
if (status.phase === WorkspacePhase.STOPPED) {
// Ideally, we'd love to hande STOPPED identical to STOPPING, because we want to assume that all conditions are stable.
// For reliabilies sake we don't do it because experiences shows that unstable conditions are one of the most common sources of errors.
return undefined;
}

if (status.phase === WorkspacePhase.STOPPING) {
if (!!status.conditions!.timeout) {
return {
type: HeadlessWorkspaceEventType.AbortedTimedOut,
update: {
state: "timeout",
error: status.conditions!.timeout,
},
};
} else if (!!status.conditions!.failed) {
return {
type: HeadlessWorkspaceEventType.Failed,
update: {
state: "failed",
error: status.conditions!.failed,
},
};
} else if (!!status.conditions!.stoppedByRequest) {
return {
type: HeadlessWorkspaceEventType.Aborted,
update: {
state: "aborted",
error: "Cancelled",
},
};
} else if (!!status.conditions!.headlessTaskFailed) {
const result: PrebuildUpdate = {
type: HeadlessWorkspaceEventType.FinishedButFailed,
update: {
state: "available",
snapshot: status.conditions!.snapshot,
error: status.conditions!.headlessTaskFailed,
},
};
return result;
} else if (!!status.conditions!.snapshot) {
return {
type: HeadlessWorkspaceEventType.FinishedSuccessfully,
update: {
state: "available",
snapshot: status.conditions!.snapshot,
},
};
} else if (!status.conditions!.snapshot) {
// STOPPING && no snapshot? Definitely an error case
return {
type: HeadlessWorkspaceEventType.Failed,
update: {
state: "failed",
error: "error while taking snapshot",
},
};
} else {
log.error({ instanceId: status.id }, "unhandled prebuild status update", {
phase: status.phase,
conditions: status.phase,
});
return undefined;
}
}

return {
type: HeadlessWorkspaceEventType.Started,
update: {
state: "building",
},
};
}
}