Skip to content
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

[server, installer] Make PrebuildRateLimiter period configurable #14975

Merged
merged 1 commit into from
Nov 30, 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
34 changes: 10 additions & 24 deletions components/server/ee/src/prebuilds/prebuild-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import * as opentracing from "opentracing";
import { StopWorkspacePolicy } from "@gitpod/ws-manager/lib";
import { error } from "console";
import { IncrementalPrebuildsService } from "./incremental-prebuilds-service";
import { PrebuildRateLimiterConfig } from "../../../src/workspace/prebuild-rate-limiter";

export class WorkspaceRunningError extends Error {
constructor(msg: string, public instance: WorkspaceInstance) {
Expand All @@ -49,9 +50,6 @@ export interface StartPrebuildParams {
forcePrebuild?: boolean;
}

const PREBUILD_LIMITER_WINDOW_SECONDS = 60;
const PREBUILD_LIMITER_DEFAULT_LIMIT = 50;

@injectable()
export class PrebuildManager {
@inject(TracedWorkspaceDB) protected readonly workspaceDB: DBWithTracing<WorkspaceDB>;
Expand Down Expand Up @@ -389,36 +387,24 @@ export class PrebuildManager {
}

private async shouldRateLimitPrebuild(span: opentracing.Span, cloneURL: string): Promise<boolean> {
const windowStart = secondsBefore(new Date().toISOString(), PREBUILD_LIMITER_WINDOW_SECONDS);
const rateLimit = PrebuildRateLimiterConfig.getConfigForCloneURL(this.config.prebuildLimiter, cloneURL);

const windowStart = secondsBefore(new Date().toISOString(), rateLimit.period);
const unabortedCount = await this.workspaceDB
.trace({ span })
.countUnabortedPrebuildsSince(cloneURL, new Date(windowStart));
const limit = this.getPrebuildRateLimitForCloneURL(cloneURL);

if (unabortedCount >= limit) {
log.debug("Prebuild exceeds rate limit", { limit, unabortedPrebuildsCount: unabortedCount, cloneURL });
if (unabortedCount >= rateLimit.limit) {
log.debug("Prebuild exceeds rate limit", {
...rateLimit,
unabortedPrebuildsCount: unabortedCount,
cloneURL,
});
return true;
}
return false;
}

private getPrebuildRateLimitForCloneURL(cloneURL: string): number {
// First we use any explicit overrides for a given cloneURL
let limit = this.config.prebuildLimiter[cloneURL];
if (limit > 0) {
return limit;
}

// Find if there is a default value set under the '*' key
limit = this.config.prebuildLimiter["*"];
if (limit > 0) {
return limit;
}

// Last resort default
return PREBUILD_LIMITER_DEFAULT_LIMIT;
}

private async shouldSkipInactiveProject(project: Project): Promise<boolean> {
return await this.projectService.isProjectConsideredInactive(project.id);
}
Expand Down
5 changes: 3 additions & 2 deletions components/server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as yaml from "js-yaml";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { filePathTelepresenceAware } from "@gitpod/gitpod-protocol/lib/env";
import { WorkspaceClasses, WorkspaceClassesConfig } from "./workspace/workspace-classes";
import { PrebuildRateLimiters } from "./workspace/prebuild-rate-limiter";

export const Config = Symbol("Config");
export type Config = Omit<
Expand Down Expand Up @@ -200,10 +201,10 @@ export interface ConfigSerialized {
enablePayment?: boolean;

/**
* Number of prebuilds that can be started in the last 1 minute.
* Number of prebuilds that can be started in a given time period.
* Key '*' specifies the default rate limit for a cloneURL, unless overriden by a specific cloneURL.
*/
prebuildLimiter: { [cloneURL: string]: number } & { "*": number };
prebuildLimiter: PrebuildRateLimiters;

/**
* If a numeric value interpreted as days is set, repositories not beeing opened with Gitpod are
Expand Down
44 changes: 44 additions & 0 deletions components/server/src/workspace/prebuild-rate-limiter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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.
*/

export type PrebuildRateLimiters = { [cloneURL: string]: PrebuildRateLimiterConfig } & {
"*": PrebuildRateLimiterConfig;
};

export type PrebuildRateLimiterConfig = {
// maximum number of requests per period
limit: number;

// time period which the limit is enforce against in seconds
period: number;
};

export namespace PrebuildRateLimiterConfig {
const DEFAULT_CONFIG: PrebuildRateLimiterConfig = {
limit: 50,
period: 50,
geropl marked this conversation as resolved.
Show resolved Hide resolved
};

export function getConfigForCloneURL(
rateLimiters: PrebuildRateLimiters,
cloneURL: string,
): PrebuildRateLimiterConfig {
// First we use any explicit overrides for a given cloneURL
let config = rateLimiters[cloneURL];
Copy link
Member

Choose a reason for hiding this comment

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

What would be the scenario where we would want to change the rate limiting for one cloneURL but not for all?

Copy link
Member Author

@geropl geropl Nov 29, 2022

Choose a reason for hiding this comment

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

Note that this is the same code as before, and I'd argue out of scope for this PR.
The only change here is to pull up the time period to be configurable with changing the installer code, which is justified by the argument made above.

Ultimately, I think the whole approach is ill-suited for what we want to achieve. And I feel we have alignment on that we want to pursue a controller-pased/async approach for handling prebuild triggers instead.

But as long as we are not there yet (and it's still unclear how to prioritize that work) it's important to me that we have the ability to quickly and confidently react to incidents like the one we had the other week.

Copy link
Member

Choose a reason for hiding this comment

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

I think having a default, which can be overriden for specific clone URLs makes perfect sense. It exists largely for the case where you get hit hard, system pages you and you want to adjust the config only to mitigate the issue. But adjusting for all is outside of the scope of that incident and would require deeper investigation.

if (config) {
return config;
}

// Find if there is a default value set under the '*' key
config = rateLimiters["*"];
if (config) {
return config;
}

// Last resort default
return DEFAULT_CONFIG;
}
}
7 changes: 5 additions & 2 deletions install/installer/cmd/testdata/render/aws-setup/output.golden

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

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

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

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

7 changes: 5 additions & 2 deletions install/installer/cmd/testdata/render/gcp-setup/output.golden

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

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

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

7 changes: 5 additions & 2 deletions install/installer/cmd/testdata/render/kind-meta/output.golden

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

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

7 changes: 5 additions & 2 deletions install/installer/cmd/testdata/render/minimal/output.golden

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

7 changes: 5 additions & 2 deletions install/installer/cmd/testdata/render/shortname/output.golden

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

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

Loading