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

[db] Add transformer to map MySQL BIGINT to Number, use for Prebuild.statusVersion #9152

Merged
merged 1 commit into from
Apr 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export class DBPrebuiltWorkspace implements PrebuiltWorkspace {
// statusVersion must only be set by controller/observer.
@Column({
default: 0,
type: "bigint",
Copy link
Member

@geropl geropl Apr 6, 2022

Choose a reason for hiding this comment

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

@easyCZ we could leave it an bigint/number and use a transformer to do the conversion from/to DB. see transformers.ts (or typeorm.ts? 🤔 )

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a great idea! Let me give that a go.

Copy link
Member Author

Choose a reason for hiding this comment

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

This worked! Thanks a lot for suggesting it, I really wasn't happy with the solution I had originally.

transformer: Transformer.MAP_BIGINT_TO_NUMBER,
})
statusVersion: number;
}
26 changes: 26 additions & 0 deletions components/gitpod-db/src/typeorm/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ export namespace Transformer {
export const compose = (upper: ValueTransformer, lower: ValueTransformer) => {
return new CompositeValueTransformer(upper, lower);
};

export const MAP_BIGINT_TO_NUMBER: ValueTransformer = {
to(value: any): any {
// we expect to receive a number, as that's what our type system gives us.
const isNumber = typeof value === "number";
if (!isNumber) {
return "0";
}

return value.toString();
},
from(value: any): any {
// the underlying representation of a BIGINT is a string
const isString = typeof value === "string" || value instanceof String;
if (!isString) {
return 0;
}

const num = Number(value);
if (isNaN(num)) {
return 0;
}

return num;
},
};
}

export class CompositeValueTransformer implements ValueTransformer {
Expand Down
7 changes: 3 additions & 4 deletions components/ws-manager-bridge/ee/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export class WorkspaceManagerBridgeEE extends WorkspaceManagerBridge {
const workspaceId = status.metadata!.metaId!;
const logCtx: LogContext = { instanceId, workspaceId, userId };

log.info("Handling prebuild workspace update.", status);

const span = TraceContext.startSpan("updatePrebuiltWorkspace", ctx);
try {
const prebuild = await this.workspaceDB.trace({ span }).findPrebuildByWorkspaceID(status.metadata!.metaId!);
Expand All @@ -61,15 +63,12 @@ export class WorkspaceManagerBridgeEE extends WorkspaceManagerBridge {
}
span.setTag("updatePrebuiltWorkspace.prebuildId", prebuild.id);
span.setTag("updatePrebuiltWorkspace.workspaceInstance.statusVersion", status.statusVersion);
log.info("Found prebuild record in database.", prebuild);

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;
Expand Down