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

Support new concurrency mode: contextPerRenderKey #314

Merged
merged 26 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8434842
allow scaled thumbnails
ryantxu Jan 6, 2022
44939a8
added support for full page images
ArturWierzbicki Jan 9, 2022
402223b
add `acceptBeforeUnload` handler
ArturWierzbicki Jan 10, 2022
72f315d
use png files
ryantxu Jan 11, 2022
1bcb5c6
produce requested thumbnail size
ryantxu Jan 11, 2022
bb82507
use browser per domain concurrency
ArturWierzbicki Jan 16, 2022
1e59136
Merge branch 'scaled-thumb-result' of https://github.com/grafana/graf…
ArturWierzbicki Jan 16, 2022
2fc3a0e
browser per render key
ArturWierzbicki Jan 17, 2022
a502d11
browser per render key
ArturWierzbicki Jan 17, 2022
d2ab6b9
poolpeteer!
ArturWierzbicki Jan 18, 2022
338b5ac
Merge branch 'master' of https://github.com/grafana/grafana-image-ren…
ArturWierzbicki Feb 1, 2022
ffdb80e
merge conflict fixes
ArturWierzbicki Feb 1, 2022
d06160d
add debugging options
ArturWierzbicki Feb 2, 2022
252b3b2
update yarnlock
ArturWierzbicki Feb 2, 2022
82b594c
fix: disable `headed` by default
ArturWierzbicki Feb 2, 2022
e320c56
reduce the default worker shutdownTimeout
ArturWierzbicki Feb 2, 2022
5290715
yarn lock update
ArturWierzbicki Feb 2, 2022
ca52dee
add the ability to emulate network conditions
ArturWierzbicki Feb 2, 2022
9ea496f
add domain to the groupId
ArturWierzbicki Feb 2, 2022
c9cdf01
#44449: rollback `mode` changes in default.json/dev.json
ArturWierzbicki Feb 11, 2022
f7d31ce
#44449: use poolpeteer only for `contextPerRenderKey`
ArturWierzbicki Feb 11, 2022
10222be
Update package.json
ArturWierzbicki Feb 15, 2022
c4bbe5c
Update src/config.ts
ArturWierzbicki Feb 15, 2022
e482bf6
Update src/config.ts
ArturWierzbicki Feb 15, 2022
3d75083
fix: remove `workerCreationDelay`
ArturWierzbicki Feb 15, 2022
908c186
yarn lock update
ArturWierzbicki Feb 15, 2022
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
2 changes: 2 additions & 0 deletions default.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"headed": false,

"mode": "default",
"emulateNetworkConditions": false,
"clustering": {
"monitor": false,
"mode": "browser",
"maxConcurrency": 5,
"timeout": 30
Expand Down
9 changes: 9 additions & 0 deletions dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@
"headed": false,

"mode": "default",
"emulateNetworkConditions": false,
"networkConditions": {
"downloadThroughput": 4000000,
"uploadThroughput": 4000000,
"latency": 100,
"offline": false
},

"clustering": {
"monitor": true,
"mode": "browser",
"maxConcurrency": 5,
"timeout": 30
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"prettier:write": "prettier --list-different \"**/*.ts\" --write",
"precommit": "npm run eslint & npm run typecheck",
"watch": "tsc-watch --onSuccess \"node build/app.js server --config=dev.json\"",
"watch:debug": "tsc-watch --onSuccess \"cross-env DEBUG=puppeteer-cluster:* node build/app.js server --config=dev.json\"",
"build": "tsc",
"start": "node build/app.js --config=dev.json"
},
Expand All @@ -30,8 +31,9 @@
"morgan": "^1.9.0",
"on-finished": "^2.3.0",
"prom-client": "^11.5.3",
"puppeteer": "^10.0.0",
"puppeteer": "13.1.3",
ArturWierzbicki marked this conversation as resolved.
Show resolved Hide resolved
"puppeteer-cluster": "^0.22.0",
"poolpeteer": "^0.22.0",
"sharp": "0.29.3",
"unique-filename": "^1.1.0",
"winston": "^3.2.1"
Expand All @@ -40,6 +42,7 @@
"@grafana/eslint-config": "^2.5.0",
"@types/express": "^4.11.1",
"@types/node": "^14.14.41",
"cross-env": "7.0.3",
"@typescript-eslint/eslint-plugin": "^4.32.0",
"@typescript-eslint/parser": "^4.32.0",
"eslint": "^7.32.0",
Expand Down
5 changes: 5 additions & 0 deletions src/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ export class Browser {
}

async preparePage(page: puppeteer.Page, options: RenderOptions) {
if (this.config.emulateNetworkConditions && this.config.networkConditions) {
const client = await page.target().createCDPSession();
await client.send('Network.emulateNetworkConditions', this.config.networkConditions);
}

if (options.renderKey) {
if (this.config.verboseLogging) {
this.log.debug('Setting cookie for page', 'renderKey', options.renderKey, 'domain', options.domain);
Expand Down
55 changes: 47 additions & 8 deletions src/browser/clustered.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Cluster } from 'puppeteer-cluster';
import { Cluster as PoolpeteerCluster } from 'poolpeteer';
import { Cluster as PuppeteerCluster } from 'puppeteer-cluster';
import { ImageRenderOptions, RenderOptions } from '../types';
import { Browser, RenderResponse, RenderCSVResponse, Metrics } from './browser';
import { Logger } from '../logger';
Expand All @@ -10,12 +11,17 @@ enum RenderType {
}

interface ClusterOptions {
groupId?: string;
options: RenderOptions | ImageRenderOptions;
renderType: RenderType;
}

type ClusterResponse = RenderResponse | RenderCSVResponse;

const contextPerRenderKey = 'contextPerRenderKey';

type Cluster<JobData = any, ReturnData = any> = PuppeteerCluster<JobData, ReturnData> | PoolpeteerCluster<JobData, ReturnData>;

export class ClusteredBrowser extends Browser {
cluster: Cluster<ClusterOptions, ClusterResponse>;
clusteringConfig: ClusteringConfig;
Expand All @@ -25,21 +31,46 @@ export class ClusteredBrowser extends Browser {
super(config, log, metrics);

this.clusteringConfig = config.clustering;
this.concurrency = Cluster.CONCURRENCY_BROWSER;
this.concurrency = PuppeteerCluster.CONCURRENCY_BROWSER;

if (this.clusteringConfig.mode === 'context') {
this.concurrency = Cluster.CONCURRENCY_CONTEXT;
this.concurrency = PuppeteerCluster.CONCURRENCY_CONTEXT;
}

if (this.clusteringConfig.mode === contextPerRenderKey) {
this.concurrency = PoolpeteerCluster.CONCURRENCY_CONTEXT_PER_REQUEST_GROUP;
}
}

async start(): Promise<void> {
shouldUsePoolpeteer(): boolean {
return this.clusteringConfig.mode === contextPerRenderKey;
}

async createCluster(): Promise<Cluster<ClusterOptions, ClusterResponse>> {
const launcherOptions = this.getLauncherOptions({});
this.cluster = await Cluster.launch({

const clusterOptions = {
concurrency: this.concurrency,
workerShutdownTimeout: 5000,
monitor: this.clusteringConfig.monitor,
maxConcurrency: this.clusteringConfig.maxConcurrency,
timeout: this.clusteringConfig.timeout * 1000,
workerCreationDelay: 1000,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be used only if needed as it could decrease performance in some cases.

puppeteerOptions: launcherOptions,
});
};

// TODO use poolpeteer by default after initial release and testing (8.5?)
if (this.shouldUsePoolpeteer()) {
this.log.debug('Launching Browser cluster with poolpeteer');
return PoolpeteerCluster.launch(clusterOptions);
}

this.log.debug('Launching Browser cluster with puppeteer-cluster');
return PuppeteerCluster.launch(clusterOptions);
}

async start(): Promise<void> {
this.cluster = await this.createCluster();
await this.cluster.task(async ({ page, data }) => {
if (data.options.timezone) {
// set timezone
Expand All @@ -61,13 +92,21 @@ export class ClusteredBrowser extends Browser {
});
}

private getGroupId = (options: ImageRenderOptions | RenderOptions) => {
if (this.clusteringConfig.mode === contextPerRenderKey) {
return `${options.domain}${options.renderKey}`;
}

return undefined;
};

async render(options: ImageRenderOptions): Promise<RenderResponse> {
this.validateImageOptions(options);
return this.cluster.execute({ options, renderType: RenderType.PNG });
return this.cluster.execute({ groupId: this.getGroupId(options), options, renderType: RenderType.PNG });
}

async renderCSV(options: RenderOptions): Promise<RenderCSVResponse> {
this.validateRenderOptions(options);
return this.cluster.execute({ options, renderType: RenderType.CSV });
return this.cluster.execute({ groupId: this.getGroupId(options), options, renderType: RenderType.CSV });
}
}
17 changes: 15 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import * as fs from 'fs';

export interface ClusteringConfig {
monitor: boolean;
mode: string;
maxConcurrency: number;
timeout: number;
}

// https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-emulateNetworkConditions
type NetworkConditions = {
offline: boolean;
downloadThroughput: number;
uploadThroughput: number;
latency: number;
};

export interface RenderingConfig {
chromeBin?: string;
args: string[];
Expand All @@ -24,6 +33,8 @@ export interface RenderingConfig {
dumpio: boolean;
timingMetrics: boolean;
headed?: boolean;
networkConditions?: NetworkConditions;
emulateNetworkConditions: boolean;
}

export interface MetricsConfig {
Expand Down Expand Up @@ -76,12 +87,14 @@ const defaultRenderingConfig: RenderingConfig = {
maxWidth: 3000,
maxHeight: 3000,
maxDeviceScaleFactor: 4,
mode: 'default',
mode: 'clustered',
ArturWierzbicki marked this conversation as resolved.
Show resolved Hide resolved
clustering: {
mode: 'browser',
monitor: false,
mode: 'browserPerRenderKey',
ArturWierzbicki marked this conversation as resolved.
Show resolved Hide resolved
maxConcurrency: 5,
timeout: 30,
},
emulateNetworkConditions: false,
verboseLogging: false,
dumpio: false,
timingMetrics: false,
Expand Down
Loading