Skip to content

[server] snapshots for PVC workspaces #14503

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 2 commits into from
Nov 9, 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
14 changes: 1 addition & 13 deletions components/server/ee/src/workspace/gitpod-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import {
} from "@gitpod/gitpod-protocol";
import { ResponseError } from "vscode-jsonrpc";
import {
TakeSnapshotRequest,
AdmissionLevel,
ControlAdmissionRequest,
StopWorkspacePolicy,
Expand Down Expand Up @@ -530,18 +529,7 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
}
await this.guardAccess({ kind: "workspaceInstance", subject: instance, workspace }, "get");

const client = await this.workspaceManagerClientProvider.get(
instance.region,
this.config.installationShortname,
);
const request = new TakeSnapshotRequest();
request.setId(instance.id);
request.setReturnImmediately(true);

// this triggers the snapshots, but returns early! cmp. waitForSnapshot to wait for it's completion
const resp = await client.takeSnapshot(ctx, request);

const snapshot = await this.snapshotService.createSnapshot(options, resp.getUrl());
const snapshot = await this.snapshotService.createSnapshot(ctx, instance);

// to be backwards compatible during rollout, we require new clients to explicitly pass "dontWait: true"
const waitOpts = { workspaceOwner: workspace.ownerId, snapshot };
Expand Down
61 changes: 51 additions & 10 deletions components/server/ee/src/workspace/snapshot-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import { inject, injectable } from "inversify";
import { v4 as uuidv4 } from "uuid";
import { WorkspaceDB } from "@gitpod/gitpod-db/lib";
import { Disposable, GitpodServer, Snapshot } from "@gitpod/gitpod-protocol";
import { Disposable, Snapshot, WorkspaceInstance } from "@gitpod/gitpod-protocol";
import { StorageClient } from "../../../src/storage/storage-client";
import { ConsensusLeaderQorum } from "../../../src/consensus/consensus-leader-quorum";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { repeat } from "@gitpod/gitpod-protocol/lib/util/repeat";
import { WorkspaceManagerClientProvider } from "@gitpod/ws-manager/lib/client-provider";
import { GetVolumeSnapshotRequest, TakeSnapshotRequest } from "@gitpod/ws-manager/lib";
import { Config } from "../../../src/config";
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";

const SNAPSHOT_TIMEOUT_SECONDS = 60 * 30;
const SNAPSHOT_POLL_INTERVAL_SECONDS = 5;
Expand All @@ -31,6 +35,9 @@ export class SnapshotService {
@inject(WorkspaceDB) protected readonly workspaceDb: WorkspaceDB;
@inject(StorageClient) protected readonly storageClient: StorageClient;
@inject(ConsensusLeaderQorum) protected readonly leaderQuorum: ConsensusLeaderQorum;
@inject(WorkspaceManagerClientProvider)
protected readonly workspaceManagerClientProvider: WorkspaceManagerClientProvider;
@inject(Config) protected readonly config: Config;

protected readonly runningSnapshots: Map<string, Promise<void>> = new Map();

Expand Down Expand Up @@ -74,14 +81,24 @@ export class SnapshotService {
}
}

public async createSnapshot(options: GitpodServer.TakeSnapshotOptions, snapshotUrl: string): Promise<Snapshot> {
public async createSnapshot(ctx: TraceContext, instance: WorkspaceInstance): Promise<Snapshot> {
const client = await this.workspaceManagerClientProvider.get(
instance.region,
this.config.installationShortname,
);
const request = new TakeSnapshotRequest();
request.setId(instance.id);
request.setReturnImmediately(true);

// this triggers the snapshots, but returns early! cmp. waitForSnapshot to wait for it's completion
const resp = await client.takeSnapshot(ctx, request);
const id = uuidv4();
return await this.workspaceDb.storeSnapshot({
id,
creationTime: new Date().toISOString(),
state: "pending",
bucketId: snapshotUrl,
originalWorkspaceId: options.workspaceId,
bucketId: resp.getUrl(),
originalWorkspaceId: instance.workspaceId,
});
}

Expand Down Expand Up @@ -112,6 +129,24 @@ export class SnapshotService {

const { id: snapshotId, bucketId, originalWorkspaceId, creationTime } = opts.snapshot;
const start = new Date(creationTime).getTime();
const workspace = await this.workspaceDb.findWorkspaceAndInstance(originalWorkspaceId);
if (!workspace) {
const message = `Couldn't find original workspace for snapshot.`;
await this.workspaceDb.updateSnapshot({
id: snapshotId,
state: "error",
message,
});
throw new Error(message);
}
const client = await this.workspaceManagerClientProvider.get(
workspace.region,
this.config.installationShortname,
);
const req = new GetVolumeSnapshotRequest();
req.setId(workspace.instanceId);

const isPVC = workspace?.config._featureFlags?.some((f) => f === "persistent_volume_claim");
while (start + SNAPSHOT_TIMEOUT_SECONDS * 1000 > Date.now()) {
await new Promise((resolve) => setTimeout(resolve, SNAPSHOT_POLL_INTERVAL_SECONDS * 1000));

Expand All @@ -126,13 +161,19 @@ export class SnapshotService {
if (snapshot.state === "error") {
throw new Error(`snapshot error: ${snapshot.message}`);
}
let exists = false;
if (isPVC) {
const response = await client.getVolumeSnapshot({}, req);
exists = response.getReady();
} else {
// pending: check if the snapshot is there
exists = await this.storageClient.workspaceSnapshotExists(
opts.workspaceOwner,
originalWorkspaceId,
bucketId,
);
}

// pending: check if the snapshot is there
const exists = await this.storageClient.workspaceSnapshotExists(
opts.workspaceOwner,
originalWorkspaceId,
bucketId,
);
if (exists) {
await this.workspaceDb.updateSnapshot({
id: snapshotId,
Expand Down
35 changes: 35 additions & 0 deletions components/ws-manager-api/go/mock/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading