Skip to content

Commit 499141f

Browse files
committed
[server] Move WorkspaceDeleteionService into WorkspaceGC
1 parent 8d1fd9d commit 499141f

File tree

3 files changed

+85
-110
lines changed

3 files changed

+85
-110
lines changed

components/server/src/container-module.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ import { ReferrerPrefixParser } from "./workspace/referrer-prefix-context-parser
118118
import { SnapshotContextParser } from "./workspace/snapshot-context-parser";
119119
import { SnapshotService } from "./workspace/snapshot-service";
120120
import { WorkspaceClusterImagebuilderClientProvider } from "./workspace/workspace-cluster-imagebuilder-client-provider";
121-
import { WorkspaceDeletionService } from "./workspace/workspace-deletion-service";
122121
import { WorkspaceDownloadService } from "./workspace/workspace-download-service";
123122
import { WorkspaceFactory } from "./workspace/workspace-factory";
124123
import { WorkspaceStarter } from "./workspace/workspace-starter";
@@ -162,7 +161,6 @@ export const productionContainerModule = new ContainerModule(
162161
bind(SnapshotService).toSelf().inSingletonScope();
163162
bind(WorkspaceService).toSelf().inSingletonScope();
164163
bind(WorkspaceFactory).toSelf().inSingletonScope();
165-
bind(WorkspaceDeletionService).toSelf().inSingletonScope();
166164
bind(WorkspaceStarter).toSelf().inSingletonScope();
167165
bind(ImageSourceProvider).toSelf().inSingletonScope();
168166

components/server/src/jobs/workspace-gc.ts

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66

77
import { injectable, inject, postConstruct } from "inversify";
88
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
9-
import { WorkspaceDeletionService } from "../workspace/workspace-deletion-service";
109
import * as opentracing from "opentracing";
11-
import { TracedWorkspaceDB, DBWithTracing, WorkspaceDB } from "@gitpod/gitpod-db/lib";
10+
import {
11+
TracedWorkspaceDB,
12+
DBWithTracing,
13+
WorkspaceDB,
14+
WorkspaceAndOwner,
15+
WorkspaceOwnerAndSoftDeleted,
16+
} from "@gitpod/gitpod-db/lib";
1217
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
1318
import { Config } from "../config";
1419
import { Job } from "./runner";
1520
import { WorkspaceService } from "../workspace/workspace-service";
21+
import { StorageClient } from "../storage/storage-client";
1622

1723
/**
1824
* The WorkspaceGarbageCollector has two tasks:
@@ -21,9 +27,9 @@ import { WorkspaceService } from "../workspace/workspace-service";
2127
*/
2228
@injectable()
2329
export class WorkspaceGarbageCollector implements Job {
24-
@inject(WorkspaceDeletionService) protected readonly deletionService: WorkspaceDeletionService;
2530
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
2631
@inject(TracedWorkspaceDB) protected readonly workspaceDB: DBWithTracing<WorkspaceDB>;
32+
@inject(StorageClient) protected readonly storageClient: StorageClient;
2733
@inject(Config) protected readonly config: Config;
2834

2935
public name = "workspace-gc";
@@ -55,7 +61,7 @@ export class WorkspaceGarbageCollector implements Job {
5561
/**
5662
* Marks old, unused workspaces as softDeleted
5763
*/
58-
protected async softDeleteOldWorkspaces() {
64+
private async softDeleteOldWorkspaces() {
5965
if (Date.now() < this.config.workspaceGarbageCollection.startDate) {
6066
log.info("workspace-gc: garbage collection not yet active.");
6167
return;
@@ -89,7 +95,7 @@ export class WorkspaceGarbageCollector implements Job {
8995
}
9096
}
9197

92-
protected async deleteWorkspaceContentAfterRetentionPeriod() {
98+
private async deleteWorkspaceContentAfterRetentionPeriod() {
9399
const span = opentracing.globalTracer().startSpan("deleteWorkspaceContentAfterRetentionPeriod");
94100
try {
95101
const workspaces = await this.workspaceDB
@@ -98,9 +104,7 @@ export class WorkspaceGarbageCollector implements Job {
98104
this.config.workspaceGarbageCollection.contentRetentionPeriodDays,
99105
this.config.workspaceGarbageCollection.contentChunkLimit,
100106
);
101-
const deletes = await Promise.all(
102-
workspaces.map((ws) => this.deletionService.garbageCollectWorkspace({ span }, ws)),
103-
);
107+
const deletes = await Promise.all(workspaces.map((ws) => this.garbageCollectWorkspace({ span }, ws)));
104108

105109
log.info(`workspace-gc: successfully deleted the content of ${deletes.length} workspaces`);
106110
span.addTags({ nrOfCollectedWorkspaces: deletes.length });
@@ -115,7 +119,7 @@ export class WorkspaceGarbageCollector implements Job {
115119
/**
116120
* This method is meant to purge all traces of a Workspace and it's WorkspaceInstances from the DB
117121
*/
118-
protected async purgeWorkspacesAfterPurgeRetentionPeriod() {
122+
private async purgeWorkspacesAfterPurgeRetentionPeriod() {
119123
const span = opentracing.globalTracer().startSpan("purgeWorkspacesAfterPurgeRetentionPeriod");
120124
try {
121125
const now = new Date();
@@ -149,9 +153,7 @@ export class WorkspaceGarbageCollector implements Job {
149153
this.config.workspaceGarbageCollection.minAgePrebuildDays,
150154
this.config.workspaceGarbageCollection.chunkLimit,
151155
);
152-
const deletes = await Promise.all(
153-
workspaces.map((ws) => this.deletionService.garbageCollectPrebuild({ span }, ws)),
154-
);
156+
const deletes = await Promise.all(workspaces.map((ws) => this.garbageCollectPrebuild({ span }, ws)));
155157

156158
log.info(`workspace-gc: successfully deleted ${deletes.length} prebuilds`);
157159
span.addTags({ nrOfCollectedPrebuilds: deletes.length });
@@ -162,4 +164,75 @@ export class WorkspaceGarbageCollector implements Job {
162164
span.finish();
163165
}
164166
}
167+
168+
/**
169+
* This method garbageCollects a workspace. It deletes its contents and sets the workspaces 'contentDeletedTime'
170+
* @param ctx
171+
* @param ws
172+
*/
173+
private async garbageCollectWorkspace(ctx: TraceContext, ws: WorkspaceOwnerAndSoftDeleted): Promise<boolean> {
174+
const span = TraceContext.startSpan("garbageCollectWorkspace", ctx);
175+
176+
try {
177+
const successfulDeleted = await this.deleteWorkspaceStorage({ span }, ws, true);
178+
await this.workspaceDB
179+
.trace({ span })
180+
.updatePartial(ws.id, { contentDeletedTime: new Date().toISOString() });
181+
return successfulDeleted;
182+
} catch (err) {
183+
TraceContext.setError({ span }, err);
184+
throw err;
185+
} finally {
186+
span.finish();
187+
}
188+
}
189+
190+
/**
191+
* @param ctx
192+
* @param wsAndOwner
193+
*/
194+
private async garbageCollectPrebuild(ctx: TraceContext, ws: WorkspaceAndOwner): Promise<boolean> {
195+
const span = TraceContext.startSpan("garbageCollectPrebuild", ctx);
196+
197+
try {
198+
const successfulDeleted = await this.deleteWorkspaceStorage({ span }, ws, true);
199+
const now = new Date().toISOString();
200+
// Note: soft & content deletion happens at the same time, because prebuilds are reproducible so there's no need for the extra time span.
201+
await this.workspaceDB.trace({ span }).updatePartial(ws.id, {
202+
contentDeletedTime: now,
203+
softDeletedTime: now,
204+
softDeleted: "gc",
205+
});
206+
return successfulDeleted;
207+
} catch (err) {
208+
TraceContext.setError({ span }, err);
209+
throw err;
210+
} finally {
211+
span.finish();
212+
}
213+
}
214+
215+
/**
216+
* Performs the actual deletion of a workspace's backups (and optionally, snapshots). It:
217+
* - throws an error if something went wrong during deletion
218+
* - returns true in case of successful deletion
219+
* @param ws
220+
* @param includeSnapshots
221+
*/
222+
private async deleteWorkspaceStorage(
223+
ctx: TraceContext,
224+
ws: WorkspaceAndOwner,
225+
includeSnapshots: boolean,
226+
): Promise<boolean> {
227+
const span = TraceContext.startSpan("deleteWorkspaceStorage", ctx);
228+
try {
229+
await this.storageClient.deleteWorkspaceBackups(ws.ownerId, ws.id, includeSnapshots);
230+
} catch (err) {
231+
TraceContext.setError({ span }, err);
232+
throw err;
233+
} finally {
234+
span.finish();
235+
}
236+
return true;
237+
}
165238
}

components/server/src/workspace/workspace-deletion-service.ts

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)