-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[db] long running migration for filling the organizationid in workspaces #16269
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
components/gitpod-db/src/long-running-migration/long-running-migration.spec.db.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright (c) 2020 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import * as chai from "chai"; | ||
import { testContainer } from "../test-container"; | ||
import { Synchronizer } from "../typeorm/synchronizer"; | ||
import { TypeORM } from "../typeorm/typeorm"; | ||
import { LongRunningMigration, LongRunningMigrationService } from "./long-running-migration"; | ||
const expect = chai.expect; | ||
|
||
class MultiBatchesMigration implements LongRunningMigration { | ||
constructor(public readonly name: string, public batches: number) {} | ||
|
||
public getName(): string { | ||
return this.name; | ||
} | ||
|
||
public async runMigrationBatch(): Promise<boolean> { | ||
return --this.batches <= 0; | ||
} | ||
} | ||
|
||
describe("long running migration service", () => { | ||
const typeORM = testContainer.get<TypeORM>(TypeORM); | ||
|
||
const wipeRepo = async () => { | ||
const conn = await typeORM.getConnection(); | ||
await conn.query("DELETE FROM d_b_long_running_migration"); | ||
}; | ||
|
||
it("should migrate until completed", async () => { | ||
await wipeRepo(); | ||
const threeBatches = new MultiBatchesMigration("threeBatches", 3); | ||
const migrationService = new LongRunningMigrationService( | ||
testContainer.get<TypeORM>(TypeORM), | ||
testContainer.get<Synchronizer>(Synchronizer), | ||
[threeBatches], | ||
); | ||
|
||
expect(await migrationService.runMigrationBatch(), "run #1").to.be.false; | ||
expect(await migrationService.runMigrationBatch(), "run #2").to.be.false; | ||
expect(await migrationService.runMigrationBatch(), "run #3").to.be.true; | ||
expect(threeBatches.batches).to.equal(0); | ||
}); | ||
|
||
it("should migrate until all are completed", async () => { | ||
await wipeRepo(); | ||
const threeBatches = new MultiBatchesMigration("threeBatches", 3); | ||
const runsOnce = new MultiBatchesMigration("runsOnce", 1); | ||
const migrationService = new LongRunningMigrationService( | ||
testContainer.get<TypeORM>(TypeORM), | ||
testContainer.get<Synchronizer>(Synchronizer), | ||
[threeBatches, runsOnce], | ||
); | ||
|
||
expect(await migrationService.runMigrationBatch(), "run #1").to.be.false; | ||
expect(await migrationService.runMigrationBatch(), "run #2").to.be.false; | ||
expect(await migrationService.runMigrationBatch(), "run #3").to.be.true; | ||
expect(runsOnce.batches).to.equal(0); | ||
expect(threeBatches.batches).to.equal(0); | ||
}); | ||
}); |
80 changes: 80 additions & 0 deletions
80
components/gitpod-db/src/long-running-migration/long-running-migration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { log } from "@gitpod/gitpod-protocol/lib/util/logging"; | ||
import { inject, injectable, multiInject } from "inversify"; | ||
import { DBLongRunningMigration } from "../typeorm/entity/db-long-running-migration"; | ||
import { Synchronizer } from "../typeorm/synchronizer"; | ||
import { TypeORM } from "../typeorm/typeorm"; | ||
|
||
export const LongRunningMigration = Symbol("LongRunningMigration"); | ||
export interface LongRunningMigration { | ||
/** | ||
* The name of the migration. This is used to store the completion state in the database. | ||
* Changing the name will cause the migration to be run again. | ||
*/ | ||
getName(): string; | ||
|
||
/** | ||
* Returns true if the migration has completed. | ||
*/ | ||
runMigrationBatch(): Promise<boolean>; | ||
} | ||
|
||
/** | ||
* This service runs all registered long running migrations in batches until they are completed. | ||
* It stores the completion state in the database. | ||
*/ | ||
@injectable() | ||
export class LongRunningMigrationService { | ||
public constructor( | ||
@inject(TypeORM) private typeorm: TypeORM, | ||
@inject(Synchronizer) private distributedLock: Synchronizer, | ||
@multiInject(LongRunningMigration) private migrations: LongRunningMigration[], | ||
) {} | ||
|
||
/** | ||
* Runs a batch for all registered long running migration. | ||
* | ||
* @returns true if all migrations are completed. | ||
*/ | ||
async runMigrationBatch(): Promise<boolean> { | ||
return this.distributedLock.synchronized("long-running-migration", "LongRunningMigrationService", async () => { | ||
const repo = (await this.typeorm.getConnection()).getRepository(DBLongRunningMigration); | ||
let allCompleted = true; | ||
for (const migration of this.migrations) { | ||
let migrationMetaData = await repo.findOne({ name: migration.getName() }); | ||
if (!migrationMetaData) { | ||
migrationMetaData = await repo.save({ | ||
name: migration.getName(), | ||
firstRun: new Date(), | ||
lastRun: new Date(), | ||
completed: false, | ||
}); | ||
} | ||
if (migrationMetaData.completed) { | ||
continue; | ||
} | ||
log.info(`Running long running migration '${migration.getName()}' ...`); | ||
const now = new Date(); | ||
try { | ||
const completed = await migration.runMigrationBatch(); | ||
log.info( | ||
`Long running migration ${migration.getName()} took ${new Date().getTime() - now.getTime()}ms`, | ||
{ completed }, | ||
); | ||
migrationMetaData.completed = completed; | ||
} catch (e) { | ||
log.error(`Long running migration ${migration.getName()} failed`, e); | ||
} | ||
allCompleted = allCompleted && migrationMetaData.completed; | ||
migrationMetaData.lastRun = new Date(); | ||
await repo.save(migrationMetaData); | ||
} | ||
return allCompleted; | ||
}); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...onents/gitpod-db/src/long-running-migration/workspace-organizationid-migration.spec.db.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Copyright (c) 2020 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import * as chai from "chai"; | ||
import { testContainer } from "../test-container"; | ||
import { TypeORM } from "../typeorm/typeorm"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { WorkspaceDB } from "../workspace-db"; | ||
import { WorkspaceOrganizationIdMigration } from "./workspace-organizationid-migration"; | ||
const expect = chai.expect; | ||
|
||
describe("Workspace organizationid migration", () => { | ||
const typeORM = testContainer.get<TypeORM>(TypeORM); | ||
const migrationService = testContainer.get<WorkspaceOrganizationIdMigration>(WorkspaceOrganizationIdMigration); | ||
const workspaceDB = testContainer.get<WorkspaceDB>(WorkspaceDB); | ||
|
||
const wipeRepo = async () => { | ||
const conn = await typeORM.getConnection(); | ||
await conn.query("DELETE FROM d_b_workspace"); | ||
await conn.query("DELETE FROM d_b_workspace_instance"); | ||
}; | ||
|
||
it("should migrate", async () => { | ||
await wipeRepo(); | ||
const orgId = uuidv4(); | ||
|
||
const now = new Date(); | ||
const ws = await workspaceDB.store({ | ||
id: "workspace-id", | ||
description: "workspace-description", | ||
creationTime: now.toISOString(), | ||
contextURL: "workspace-contextURL", | ||
context: { title: "workspace-context-title" }, | ||
ownerId: uuidv4(), | ||
type: "regular", | ||
config: { image: "workspace-config-image" }, | ||
}); | ||
await workspaceDB.storeInstance({ | ||
id: "workspace-instance-id", | ||
workspaceId: ws.id, | ||
creationTime: now.toISOString(), | ||
usageAttributionId: `team:${orgId}`, | ||
ideUrl: "workspace-instance-ideUrl", | ||
region: "workspace-instance-region", | ||
workspaceImage: "workspace-instance-workspaceImage", | ||
status: { | ||
phase: "stopped", | ||
conditions: {}, | ||
}, | ||
}); | ||
|
||
const nowMinus30 = new Date(now.getTime() - 24 * 60 * 60 * 1000 * 30); | ||
const ws1 = await workspaceDB.store({ | ||
id: "workspace-id1", | ||
description: "workspace-description", | ||
creationTime: nowMinus30.toISOString(), | ||
contextURL: "workspace-contextURL", | ||
context: { title: "workspace-context-title" }, | ||
ownerId: uuidv4(), | ||
type: "regular", | ||
config: { image: "workspace-config-image" }, | ||
}); | ||
await workspaceDB.storeInstance({ | ||
id: "workspace-instance-id1", | ||
workspaceId: ws1.id, | ||
creationTime: nowMinus30.toISOString(), | ||
usageAttributionId: `team:${orgId}`, | ||
ideUrl: "workspace-instance-ideUrl", | ||
region: "workspace-instance-region", | ||
workspaceImage: "workspace-instance-workspaceImage", | ||
status: { | ||
phase: "stopped", | ||
conditions: {}, | ||
}, | ||
}); | ||
|
||
const conn = await typeORM.getConnection(); | ||
const getMigrationCount = async () => { | ||
const result = await conn.query( | ||
`SELECT count(*) as migrated FROM d_b_workspace WHERE organizationId='${orgId}'`, | ||
); | ||
return Number.parseInt(result[0].migrated); | ||
}; | ||
|
||
expect(await getMigrationCount()).to.be.eq(0); | ||
|
||
await migrationService.runMigrationBatch(); | ||
|
||
expect(await getMigrationCount()).to.be.eq(1); | ||
|
||
await migrationService.runMigrationBatch(); | ||
|
||
expect(await getMigrationCount()).to.be.eq(2); | ||
}); | ||
}); |
68 changes: 68 additions & 0 deletions
68
components/gitpod-db/src/long-running-migration/workspace-organizationid-migration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { log } from "@gitpod/gitpod-protocol/lib/util/logging"; | ||
import { inject, injectable } from "inversify"; | ||
import { TypeORM } from "../typeorm/typeorm"; | ||
import { LongRunningMigration } from "./long-running-migration"; | ||
|
||
const BATCH_OFFSET = 5 * 24 * 60 * 60 * 1000; /* 5 days */ | ||
|
||
@injectable() | ||
export class WorkspaceOrganizationIdMigration implements LongRunningMigration { | ||
@inject(TypeORM) protected readonly typeorm: TypeORM; | ||
|
||
getName(): string { | ||
return "WorkspaceOrganizationIdMigration"; | ||
} | ||
|
||
public async runMigrationBatch(): Promise<boolean> { | ||
const conn = await this.typeorm.getConnection(); | ||
const runner = conn.createQueryRunner(); | ||
const minCreationTimeMigrated = ( | ||
await runner.query( | ||
"SELECT min(creationTime) as minTime FROM d_b_workspace WHERE organizationId IS NOT NULL", | ||
) | ||
)[0].minTime; | ||
const minCreationTimeTotal = (await runner.query("SELECT min(creationTime) as minTime FROM d_b_workspace"))[0] | ||
.minTime; | ||
|
||
let endDate = minCreationTimeMigrated ? new Date(Date.parse(minCreationTimeMigrated)) : new Date(); | ||
let startDate = new Date(endDate.getTime() - BATCH_OFFSET); | ||
log.info(`Running migration with start date: ${startDate}, end date: ${endDate}`); | ||
|
||
let result; | ||
do { | ||
const query = ` | ||
UPDATE d_b_workspace w | ||
JOIN ( | ||
SELECT workspaceId, MAX(creationTime) as maxCreationTime | ||
FROM d_b_workspace_instance | ||
WHERE usageAttributionId LIKE 'team:%' | ||
GROUP BY workspaceId | ||
) wi ON w.id = wi.workspaceId | ||
SET w.organizationid = ( | ||
SELECT substr(usageAttributionId, 6) | ||
FROM d_b_workspace_instance | ||
WHERE workspaceId = wi.workspaceId AND creationTime = wi.maxCreationTime | ||
) | ||
WHERE | ||
w.creationTime >= '${startDate.toISOString()}' and | ||
w.creationTime < '${endDate.toISOString()}' and | ||
w.organizationId IS NULL and | ||
w.softDeleted IS NULL | ||
`; | ||
result = await runner.query(query); | ||
log.info(`Migrated ${result.affectedRows} workspaces. Start date: ${startDate}, end date: ${endDate}`, { | ||
query, | ||
}); | ||
endDate = new Date(endDate.getTime() - BATCH_OFFSET); | ||
startDate = new Date(startDate.getTime() - BATCH_OFFSET); | ||
} while (result.affectedRows === 0 && endDate.toISOString() > minCreationTimeTotal); | ||
const completed = result.affectedRows === 0; | ||
return completed; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
components/gitpod-db/src/typeorm/entity/db-long-running-migration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { Entity, Column, PrimaryColumn } from "typeorm"; | ||
|
||
@Entity() | ||
export class DBLongRunningMigration { | ||
@PrimaryColumn() | ||
name: string; | ||
|
||
@Column({ type: "timestamp", precision: 6 }) | ||
firstRun: Date; | ||
|
||
@Column({ type: "timestamp", precision: 6 }) | ||
lastRun: Date; | ||
|
||
@Column() | ||
completed: boolean; | ||
} |
29 changes: 29 additions & 0 deletions
29
components/gitpod-db/src/typeorm/migration/1675863598550-LongRunningMigration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
import { tableExists } from "./helper/helper"; | ||
|
||
export class LongRunningMigration1675863598550 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
if (!(await tableExists(queryRunner, "d_b_long_running_migration"))) { | ||
await queryRunner.query( | ||
` | ||
CREATE TABLE IF NOT EXISTS d_b_long_running_migration ( | ||
name varchar(255) NOT NULL, | ||
firstRun timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), | ||
lastRun timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), | ||
completed tinyint(4) NOT NULL DEFAULT '0', | ||
_lastModified timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), | ||
PRIMARY KEY (name) | ||
) | ||
`, | ||
); | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> {} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a follow-up, it would be nice to export a metric if the batch+job failed, or succeeded such that we can check, or setup alerting for these. This would be in particular useful for Dedicated, where we don't have access to logs.