Skip to content

Commit

Permalink
[ws-manager-bridge] Skip stale prebuild events
Browse files Browse the repository at this point in the history
  • Loading branch information
easyCZ committed Apr 5, 2022
1 parent da28f0c commit b477b15
Showing 1 changed file with 30 additions and 23 deletions.
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 });
}
}
}
}

}

0 comments on commit b477b15

Please sign in to comment.