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

[ws-manager-bridge] Skip stale prebuild events #9116

Merged
merged 1 commit into from
Apr 6, 2022
Merged
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
53 changes: 30 additions & 23 deletions components/ws-manager-bridge/ee/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { log, LogContext } from "@gitpod/gitpod-protocol/lib/util/logging";

@injectable()
export class WorkspaceManagerBridgeEE extends WorkspaceManagerBridge {

protected async cleanupProbeWorkspace(ctx: TraceContext, status: WorkspaceStatus.AsObject | undefined) {
if (!status) {
return;
Expand All @@ -29,16 +28,21 @@ export class WorkspaceManagerBridgeEE extends WorkspaceManagerBridge {
const span = TraceContext.startSpan("cleanupProbeWorkspace", ctx);
try {
const workspaceId = status.metadata!.metaId!;
await this.workspaceDB.trace({span}).hardDeleteWorkspace(workspaceId);
await this.workspaceDB.trace({ span }).hardDeleteWorkspace(workspaceId);
} catch (e) {
TraceContext.setError({span}, e);
TraceContext.setError({ span }, e);
throw e;
} finally {
span.finish();
}
}

protected async updatePrebuiltWorkspace(ctx: TraceContext, userId: string, status: WorkspaceStatus.AsObject, writeToDB: boolean) {
protected async updatePrebuiltWorkspace(
ctx: TraceContext,
userId: string,
status: WorkspaceStatus.AsObject,
writeToDB: boolean,
) {
if (status.spec && status.spec.type != WorkspaceType.PREBUILD) {
return;
}
Expand All @@ -49,31 +53,35 @@ export class WorkspaceManagerBridgeEE extends WorkspaceManagerBridge {

const span = TraceContext.startSpan("updatePrebuiltWorkspace", ctx);
try {
const prebuild = await this.workspaceDB.trace({span}).findPrebuildByWorkspaceID(status.metadata!.metaId!);
const prebuild = await this.workspaceDB.trace({ span }).findPrebuildByWorkspaceID(status.metadata!.metaId!);
if (!prebuild) {
log.warn(logCtx, "Headless workspace without prebuild");
TraceContext.setError({span}, new Error("headless workspace without prebuild"));
return
TraceContext.setError({ span }, new Error("headless workspace without prebuild"));
return;
}
span.setTag("updatePrebuiltWorkspace.prebuildId", prebuild.id);
span.setTag("updatePrebuiltWorkspace.workspaceInstance.statusVersion", status.statusVersion);

if (prebuild.statusVersion <= status.statusVersion) {
// prebuild.statusVersion = 0 is the default value in the DB, these shouldn't be counted as stale in our metrics
if (prebuild.statusVersion > 0) {
// We've gotten an event which is younger than one we've already processed. We shouldn't process the stale one.
span.setTag("updatePrebuiltWorkspace.staleEvent", true);
this.prometheusExporter.recordStalePrebuildEvent();
log.info(logCtx, "Stale prebuild event received, skipping.");
return;
}
}
prebuild.statusVersion = status.statusVersion
prebuild.statusVersion = status.statusVersion;

if (prebuild.state === 'queued') {
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.workspaceDB.trace({ span }).storePrebuiltWorkspace(prebuild);
}
await this.messagebus.notifyHeadlessUpdate({span}, userId, workspaceId, <HeadlessWorkspaceEvent>{
await this.messagebus.notifyHeadlessUpdate({ span }, userId, workspaceId, <HeadlessWorkspaceEvent>{
type: HeadlessWorkspaceEventType.Started,
workspaceID: workspaceId,
});
Expand All @@ -95,8 +103,7 @@ export class WorkspaceManagerBridgeEE extends WorkspaceManagerBridge {
headlessUpdateType = HeadlessWorkspaceEventType.Aborted;
} else if (!!status.conditions!.headlessTaskFailed) {
prebuild.state = "available";
if (status.conditions!.headlessTaskFailed)
prebuild.error = status.conditions!.headlessTaskFailed;
if (status.conditions!.headlessTaskFailed) prebuild.error = status.conditions!.headlessTaskFailed;
prebuild.snapshot = status.conditions!.snapshot;
headlessUpdateType = HeadlessWorkspaceEventType.FinishedButFailed;
} else if (!!status.conditions!.snapshot) {
Expand All @@ -108,28 +115,28 @@ export class WorkspaceManagerBridgeEE extends WorkspaceManagerBridge {
return;
}

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

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

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

// prebuild info
const info = (await this.workspaceDB.trace({span}).findPrebuildInfos([prebuild.id]))[0];
const info = (await this.workspaceDB.trace({ span }).findPrebuildInfos([prebuild.id]))[0];
if (info) {
this.messagebus.notifyOnPrebuildUpdate({ info, status: prebuild.state });
}
}
} catch (e) {
TraceContext.setError({span}, e);
TraceContext.setError({ span }, e);
throw e;
} finally {
span.finish();
Expand All @@ -142,16 +149,16 @@ export class WorkspaceManagerBridgeEE extends WorkspaceManagerBridge {
const prebuild = await this.workspaceDB.trace({}).findPrebuildByWorkspaceID(instance.workspaceId);
if (prebuild) {
// this is a prebuild - set it to aborted
prebuild.state = 'aborted';
prebuild.state = "aborted";
await this.workspaceDB.trace({}).storePrebuiltWorkspace(prebuild);

{ // notify about prebuild updated
const info = (await this.workspaceDB.trace({span}).findPrebuildInfos([prebuild.id]))[0];
{
// notify about prebuild updated
const info = (await this.workspaceDB.trace({ span }).findPrebuildInfos([prebuild.id]))[0];
if (info) {
this.messagebus.notifyOnPrebuildUpdate({ info, status: prebuild.state });
}
}
}
}

}