Skip to content

[prebuilds] no prebuilds for inactive repos #9976

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 3 commits into from
May 19, 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
6 changes: 6 additions & 0 deletions components/gitpod-db/src/typeorm/entity/db-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export class DBWorkspace implements Workspace {
@Column("simple-json")
context: WorkspaceContext;

@Column({
default: "",
transformer: Transformer.MAP_EMPTY_STR_TO_UNDEFINED,
})
cloneUrl?: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this can never be undefined right?

Suggested change
cloneUrl?: string;
cloneUrl: string;


@Column("simple-json")
config: WorkspaceConfig;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2021 Gitpod GmbH. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

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

Micro-nit:

Suggested change
* Copyright (c) 2021 Gitpod GmbH. All rights reserved.
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.

* Licensed under the Gitpod Enterprise Source Code License,
* See License.enterprise.txt in the project root folder.
*/

import { MigrationInterface, QueryRunner } from "typeorm";
import { columnExists, indexExists } from "./helper/helper";

export class CloneUrlIndexed1652365883273 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const TABLE_NAME = "d_b_workspace";
const COLUMN_NAME = "cloneURL";
const TYPE_INDEX_NAME = "d_b_workspace_cloneURL_idx";

if (!(await columnExists(queryRunner, TABLE_NAME, COLUMN_NAME))) {
await queryRunner.query(
`ALTER TABLE ${TABLE_NAME}
ADD COLUMN ${COLUMN_NAME} VARCHAR(255) NOT NULL DEFAULT ''`,
);
}
if (!(await indexExists(queryRunner, TABLE_NAME, TYPE_INDEX_NAME))) {
await queryRunner.query(`CREATE INDEX ${TYPE_INDEX_NAME} ON ${TABLE_NAME} (${COLUMN_NAME})`);
}
}

public async down(queryRunner: QueryRunner): Promise<void> {}
}
31 changes: 31 additions & 0 deletions components/gitpod-db/src/typeorm/workspace-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* See License-AGPL.txt in the project root for license information.
*/

import * as crypto from "crypto";
import { injectable, inject } from "inversify";
import { Repository, EntityManager, DeepPartial, UpdateQueryBuilder, Brackets } from "typeorm";
import {
Expand Down Expand Up @@ -138,8 +139,22 @@ export abstract class AbstractTypeORMWorkspaceDBImpl implements WorkspaceDB {
public async store(workspace: Workspace) {
const workspaceRepo = await this.getWorkspaceRepo();
const dbWorkspace = workspace as DBWorkspace;

// `cloneUrl` is stored redundandly to optimize for `getWorkspaceCountByCloneURL`.
// As clone URLs are lesser constrained we want to shorten the value to work well with the indexed column.
let cloneUrl: string = this.toCloneUrl255((workspace as any).context?.repository?.cloneUrl || "");

dbWorkspace.cloneUrl = cloneUrl;
return await workspaceRepo.save(dbWorkspace);
}

protected toCloneUrl255(cloneUrl: string) {
if (cloneUrl.length > 255) {
return `cloneUrl-sha:${crypto.createHash("sha256").update(cloneUrl, "utf8").digest("hex")}`;
}
return cloneUrl;
}

public async updatePartial(workspaceId: string, partial: DeepPartial<Workspace>) {
const workspaceRepo = await this.getWorkspaceRepo();
await workspaceRepo.update(workspaceId, partial);
Expand Down Expand Up @@ -356,6 +371,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("cloneURL = :cloneURL", { cloneURL: this.toCloneUrl255(cloneURL) })
.andWhere("creationTime > :since", { since: since.toISOString() })
.andWhere("type = :type", { type })
.getCount();
}

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
24 changes: 24 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.";
await this.workspaceDB.trace({ span }).storePrebuiltWorkspace(prebuild);
} else {
span.setTag("starting", true);
const projectEnvVars = await projectEnvVarsPromise;
Expand Down Expand Up @@ -356,4 +361,23 @@ 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);
const { inactivityPeriodForRepos } = this.config;
if (!inactivityPeriodForRepos) {
// skipping is disabled if `inactivityPeriodForRepos` is not set
return false;
}
try {
return (
(await this.workspaceDB
.trace({ span })
.getWorkspaceCountByCloneURL(cloneURL, inactivityPeriodForRepos /* in days */, "regular")) === 0
);
} catch (error) {
log.error("cannot compute activity for repository", { cloneURL }, error);
return false;
}
}
}
14 changes: 14 additions & 0 deletions components/server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type Config = Omit<
chargebeeProviderOptions?: ChargebeeProviderOptions;
builtinAuthProvidersConfigured: boolean;
blockedRepositories: { urlRegExp: RegExp; blockUser: boolean }[];
inactivityPeriodForRepos?: number;
};

export interface WorkspaceDefaults {
Expand Down Expand Up @@ -162,6 +163,12 @@ export interface ConfigSerialized {
* `blockUser` attribute to control handling of the user's account.
*/
blockedRepositories?: { urlRegExp: string; blockUser: boolean }[];

/**
* If a numeric value interpreted as days is set, repositories not beeing opened with Gitpod are
* considered inactive.
*/
inactivityPeriodForRepos?: number;
}

export namespace ConfigFile {
Expand Down Expand Up @@ -220,6 +227,12 @@ export namespace ConfigFile {
});
}
}
let inactivityPeriodForRepos: number | undefined;
if (typeof config.inactivityPeriodForRepos === "number") {
if (config.inactivityPeriodForRepos >= 1) {
inactivityPeriodForRepos = config.inactivityPeriodForRepos;
}
}
return {
...config,
hostUrl,
Expand All @@ -234,6 +247,7 @@ export namespace ConfigFile {
: Date.now(),
},
blockedRepositories,
inactivityPeriodForRepos,
};
}
}