Skip to content

[prebuilds] no prebuilds for inactive repos #9936

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

Merged
merged 1 commit into from
May 11, 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
16 changes: 16 additions & 0 deletions components/gitpod-db/src/typeorm/workspace-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,22 @@ export abstract class AbstractTypeORMWorkspaceDBImpl implements WorkspaceDB {
return workspaceRepo.find({ ownerId: userId });
}

public async getWorkspaceCountByCloneURL(
cloneURL: string,
sinceLastDays: number = 7,
type: string = "regular",
): Promise<number> {
const workspaceRepo = await this.getWorkspaceRepo();
const since = new Date();
since.setDate(since.getDate() - sinceLastDays);
return workspaceRepo
.createQueryBuilder("ws")
.where('context->"$.repository.cloneUrl" = :cloneURL', { cloneURL })
.andWhere("creationTime > :since", { since: since.toISOString() })
.andWhere("type = :type", { type })
.getCount();
Comment on lines +367 to +372
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow, many thanks for this clever fix! 👀

My only worry here is, how do we know that this query on all d_b_workspace entries won't be too slow/expensive? (Especially since we'll run it on every received webhook, which seems very frequent.)

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 is a good point. All I did is running the query below against the prod failovwer DB:

SELECT count(*) from d_b_workspace where type='regular' and creationTime > '2022-05-04' and context->"$.repository.cloneUrl" = 'https://github.com/gitpod-io/gitpod.git'

Note, that it is only running for those webhooks where we don't find a project but still it could be too much. Any ideas how to move forward?

Copy link
Member Author

@svenefftinge svenefftinge May 11, 2022

Choose a reason for hiding this comment

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

This is what EXPLAIN has to say:
Screenshot 2022-05-11 at 17 46 55

So there's a key on creationTime which narrows the query to all workspaces of the past seven days.

Copy link
Contributor

@jankeromnes jankeromnes May 11, 2022

Choose a reason for hiding this comment

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

Aha, that totally addresses the performance concern then, right? (Or, do we need to somehow test/qualify this further?)

Copy link
Member

Choose a reason for hiding this comment

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

@svenefftinge, the number of rows sent is also a perf issue sometimes.

If we're just checking for count === 0 down below, we could start out with a limited version.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would expect typeorm to translate to count(*) when using getCount. Doesn't it do that?

Copy link
Member

Choose a reason for hiding this comment

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

}

public async findCurrentInstance(workspaceId: string): Promise<MaybeWorkspaceInstance> {
const workspaceInstanceRepo = await this.getWorkspaceInstanceRepo();
const qb = workspaceInstanceRepo
Expand Down
48 changes: 47 additions & 1 deletion components/gitpod-db/src/workspace-db.spec.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const expect = chai.expect;
import { suite, test, timeout } from "mocha-typescript";
import { fail } from "assert";

import { WorkspaceInstance, Workspace, PrebuiltWorkspace } from "@gitpod/gitpod-protocol";
import { WorkspaceInstance, Workspace, PrebuiltWorkspace, CommitContext } from "@gitpod/gitpod-protocol";
import { testContainer } from "./test-container";
import { TypeORMWorkspaceDBImpl } from "./typeorm/workspace-db-impl";
import { TypeORM } from "./typeorm/typeorm";
Expand Down Expand Up @@ -539,6 +539,52 @@ class WorkspaceDBSpec {
expect(unabortedCount).to.eq(1);
}

@test(timeout(10000))
public async testGetWorkspaceCountForCloneURL() {
const now = new Date();
const eightDaysAgo = new Date();
eightDaysAgo.setDate(eightDaysAgo.getDate() - 8);
const activeRepo = "http://github.com/myorg/active.git";
const inactiveRepo = "http://github.com/myorg/inactive.git";
await Promise.all([
this.db.store({
id: "12345",
creationTime: eightDaysAgo.toISOString(),
description: "something",
contextURL: "http://github.com/myorg/inactive",
ownerId: "1221423",
context: <CommitContext>{
title: "my title",
repository: {
cloneUrl: inactiveRepo,
},
},
config: {},
type: "regular",
}),
this.db.store({
id: "12346",
creationTime: now.toISOString(),
description: "something",
contextURL: "http://github.com/myorg/active",
ownerId: "1221423",
context: <CommitContext>{
title: "my title",
repository: {
cloneUrl: activeRepo,
},
},
config: {},
type: "regular",
}),
]);

const inactiveCount = await this.db.getWorkspaceCountByCloneURL(inactiveRepo, 7, "regular");
expect(inactiveCount).to.eq(0, "there should be no regular workspaces in the past 7 days");
const activeCount = await this.db.getWorkspaceCountByCloneURL(activeRepo, 7, "regular");
expect(activeCount).to.eq(1, "there should be exactly one regular workspace");
}

private async storePrebuiltWorkspace(pws: PrebuiltWorkspace) {
// store the creationTime directly, before it is modified by the store function in the ORM layer
const creationTime = pws.creationTime;
Expand Down
1 change: 1 addition & 0 deletions components/gitpod-db/src/workspace-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export interface WorkspaceDB {
findInstancesByPhaseAndRegion(phase: string, region: string): Promise<WorkspaceInstance[]>;

getWorkspaceCount(type?: String): Promise<Number>;
getWorkspaceCountByCloneURL(cloneURL: string, sinceLastDays?: number, type?: string): Promise<number>;
getInstanceCount(type?: string): Promise<number>;

findAllWorkspaceInstances(
Expand Down
19 changes: 19 additions & 0 deletions components/server/ee/src/prebuilds/prebuild-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ export class PrebuildManager {
prebuild.error =
"Project is inactive. Please start a new workspace for this project to re-enable prebuilds.";
await this.workspaceDB.trace({ span }).storePrebuiltWorkspace(prebuild);
} else if (!project && (await this.shouldSkipInactiveRepository({ span }, cloneURL))) {
prebuild.state = "aborted";
prebuild.error =
"Repository is inactive. Please create a project for this repository to re-enable prebuilds.";
Copy link
Contributor

@jankeromnes jankeromnes May 11, 2022

Choose a reason for hiding this comment

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

Nit: I think this isn't totally accurate (I agree that we want users to create a Project, but the minimal fix is actually just to open a new workspace for that repository).

However, since this error isn't actually shown anywhere in the UI, users can never see it. 😅 So, for something only a Gitpod admin can see while looking into the DB, that's perfect. 💯

Copy link
Member Author

Choose a reason for hiding this comment

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

I know it's not technically correct, but it is what we would like people to do.😇

await this.workspaceDB.trace({ span }).storePrebuiltWorkspace(prebuild);
} else {
span.setTag("starting", true);
const projectEnvVars = await projectEnvVarsPromise;
Expand Down Expand Up @@ -356,4 +361,18 @@ export class PrebuildManager {
const inactiveProjectTime = 1000 * 60 * 60 * 24 * 7 * 1; // 1 week
return now - lastUse > inactiveProjectTime;
}

private async shouldSkipInactiveRepository(ctx: TraceContext, cloneURL: string): Promise<boolean> {
const span = TraceContext.startSpan("shouldSkipInactiveRepository", ctx);
try {
return (
(await this.workspaceDB
Copy link
Member

Choose a reason for hiding this comment

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

Q: Would it make sense to add some caching here? Just in-memory, time based eviction – let's say 1d.

Thinking out loud:

  • webhook events can be dense
  • checking for a project seems to be a great eviction policy in general

.trace({ span })
.getWorkspaceCountByCloneURL(cloneURL, 7 /* last week */, "regular")) === 0
);
} catch (error) {
log.error("cannot compute activity for repository", { cloneURL }, error);
return false;
}
}
}