Skip to content

Commit 2d71da5

Browse files
Make the forge's input limits settings, raising the artifact ones []
The artifact entry, central directory, diff, and sparse-checkout pattern caps are now settings (ACKNOWLEDG_FORGE_MAX_ARTIFACT_ENTRIES, ACKNOWLEDG_FORGE_MAX_ARTIFACT_DIRECTORY_BYTES, ACKNOWLEDG_FORGE_MAX_DIFF_BYTES, and ACKNOWLEDG_FORGE_MAX_SPARSE_PATTERNS), and their errors name the setting to raise. The artifact defaults rise to 500,000 entries and 64 MiB, and the diff's to 64 MiB.
1 parent dbe57a7 commit 2d71da5

9 files changed

Lines changed: 204 additions & 71 deletions

File tree

‎dev_tools/pylibs/edgacknowledg/forge/config.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@
2020

2121
ENV_PREFIX = 'ACKNOWLEDG_FORGE_'
2222

23+
# The defaults of the limits on what a pull request and its CI artifacts
24+
# may make the service do (``workspace``), which the settings may raise for
25+
# large ones, given the memory (and time) they take.
26+
#
27+
# The most sparse-checkout patterns a checkout may have: matching costs
28+
# patterns x paths, and they are built from pull request content. Their
29+
# total size must also stay within a small multiple of the diff's (see
30+
# DEFAULT_MAX_DIFF_BYTES): a test gets at most two patterns, each about as
31+
# long as its path, and the only text that is part of many patterns, a
32+
# suite's name, is short.
33+
DEFAULT_MAX_PATTERNS = 20_000
34+
# The largest diff (of tests/, in bytes) a pull request may have: it is
35+
# held in memory, and copied and split into lines more than once.
36+
DEFAULT_MAX_DIFF_BYTES = 64 * 1024 * 1024
37+
# The most entries an artifact may have (its zip's, and its tarballs'
38+
# together), and the largest its zip's central directory may be: zipfile
39+
# reads all of that directory into memory, before we can count. CI's test
40+
# results reach about 170,000 entries, of about 106 bytes each there.
41+
DEFAULT_MAX_ARTIFACT_ENTRIES = 500_000
42+
DEFAULT_MAX_ARTIFACT_DIRECTORY_BYTES = 64 * 1024 * 1024
43+
2344

2445
class ConfigError(Exception):
2546
'''Raised for a missing or malformed setting.'''
@@ -102,6 +123,10 @@ class ForgeServiceConfig:
102123
bench_artifact: str
103124
max_artifact_bytes: int
104125
max_extracted_bytes: int
126+
max_artifact_entries: int
127+
max_artifact_directory_bytes: int
128+
max_diff_bytes: int
129+
max_sparse_patterns: int
105130
max_concurrency: int
106131
rematerialize_timeout: float
107132
unsandboxed: bool
@@ -240,6 +265,19 @@ def get_list(name: str) -> tuple[str, ...]:
240265
max_extracted_bytes = int(
241266
get_number('MAX_EXTRACTED_BYTES', 2 * 1024 * 1024 * 1024)
242267
),
268+
max_artifact_entries = int(
269+
get_number('MAX_ARTIFACT_ENTRIES', DEFAULT_MAX_ARTIFACT_ENTRIES)
270+
),
271+
max_artifact_directory_bytes = int(get_number(
272+
'MAX_ARTIFACT_DIRECTORY_BYTES',
273+
DEFAULT_MAX_ARTIFACT_DIRECTORY_BYTES
274+
)),
275+
max_diff_bytes = int(
276+
get_number('MAX_DIFF_BYTES', DEFAULT_MAX_DIFF_BYTES)
277+
),
278+
max_sparse_patterns = int(
279+
get_number('MAX_SPARSE_PATTERNS', DEFAULT_MAX_PATTERNS)
280+
),
243281
max_concurrency = max_concurrency,
244282
rematerialize_timeout = get_number('REMATERIALIZE_TIMEOUT', 15 * 60),
245283
unsandboxed = unsandboxed,

‎dev_tools/pylibs/edgacknowledg/forge/processor.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,10 @@ async def _changes_section(self,
411411
record.review_id,
412412
'base',
413413
merge_base,
414-
changes_patterns(diff)
414+
changes_patterns(
415+
diff,
416+
max_patterns = self._config.max_sparse_patterns
417+
)
415418
)
416419
runs_dir = ensure_real_directory(worktree, PurePosixPath('tests', 'runs'))
417420
run_dir = runs_dir / f"remat-{_sha256(source_key.encode())[:16]}"
@@ -509,7 +512,9 @@ async def _stage_artifact(self,
509512
extract_artifact,
510513
zip_path,
511514
staging,
512-
max_bytes = self._config.max_extracted_bytes
515+
max_bytes = self._config.max_extracted_bytes,
516+
max_entries = self._config.max_artifact_entries,
517+
max_directory_bytes = self._config.max_artifact_directory_bytes
513518
)
514519
finally:
515520
zip_path.unlink(missing_ok = True)
@@ -649,7 +654,10 @@ async def _build_ci_sections(self,
649654
record.review_id,
650655
'head',
651656
tested_sha,
652-
failures_patterns(failed_tests)
657+
failures_patterns(
658+
failed_tests,
659+
max_patterns = self._config.max_sparse_patterns
660+
)
653661
)
654662

655663
sections: list[SectionRecord] = []

‎dev_tools/pylibs/edgacknowledg/forge/service.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ async def _run_components(config: ForgeServiceConfig,
104104
try:
105105
wake = asyncio.Event()
106106
ingestor = Ingestor(store, forge, config, wake = wake)
107-
workspaces = Workspaces(config.home, forge)
107+
workspaces = Workspaces(
108+
config.home,
109+
forge,
110+
max_diff_bytes = config.max_diff_bytes
111+
)
108112
# Serving reviewers happens in the publisher container.
109113
publisher = PublisherClient(
110114
url = publisher_service_url,

‎dev_tools/pylibs/edgacknowledg/forge/workspace.py‎

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@
4545
import edgutil
4646

4747
from edgacknowledg.forge import Forge, PullRequestInfo
48+
from edgacknowledg.forge.config import (
49+
DEFAULT_MAX_ARTIFACT_DIRECTORY_BYTES,
50+
DEFAULT_MAX_ARTIFACT_ENTRIES,
51+
DEFAULT_MAX_DIFF_BYTES,
52+
DEFAULT_MAX_PATTERNS,
53+
ENV_PREFIX,
54+
)
4855
from edgacknowledg.forge.runner.roles import BUILD_STAGING_DIR, REVIEWS_DIR
4956
from edgtest.runtest import get_bare_name
5057

@@ -61,21 +68,6 @@
6168

6269
BENCH_BASELINE_PATTERNS = ('/benchmarks/baselines/',)
6370

64-
# The most sparse-checkout patterns a checkout may have: matching costs
65-
# patterns x paths, and they are built from pull request content. Their
66-
# total size must also stay within a small multiple of the diff's (see
67-
# MAX_DIFF_BYTES): a test gets at most two patterns, each about as long as
68-
# its path, and the only text that is part of many patterns, a suite's
69-
# name, is short.
70-
MAX_PATTERNS = 20_000
71-
# The largest diff (of tests/, in bytes) a pull request may have: it is
72-
# held in memory, and copied and split into lines more than once.
73-
MAX_DIFF_BYTES = 16 * 1024 * 1024
74-
# The most entries an artifact may have (its zip's, and its tarballs'
75-
# together), and the largest its zip's central directory may be: zipfile
76-
# reads all of that directory into memory, before we can count.
77-
MAX_ARTIFACT_ENTRIES = 100_000
78-
MAX_CENTRAL_DIRECTORY_BYTES = 16 * 1024 * 1024
7971
# Seconds a git process being stopped has to clean up (e.g. remove its lock
8072
# files) before it is killed; well within docker stop's default 10.
8173
_TERMINATE_ALLOWANCE = 5.0
@@ -106,7 +98,7 @@ def patterns_for_test(test_path: PurePosixPath) -> list[str]:
10698
as 'tests/tests/suite/dir/name.sft.cpp'): the test file and its recordings
10799
(where the test runner keeps them, '.<bare name>.rto'), or the whole
108100
directory of a multi-file test. At most two, each about as long as the
109-
path, however it is named (see MAX_PATTERNS).
101+
path, however it is named (see config.DEFAULT_MAX_PATTERNS).
110102
'''
111103
directory = _escape_pattern(str(test_path.parent))
112104
if '.mft.' in test_path.name:
@@ -148,9 +140,12 @@ def changed_paths(diff: str) -> list[str]:
148140
return paths
149141

150142

151-
def changes_patterns(diff: str) -> list[str]:
143+
def changes_patterns(diff: str,
144+
*,
145+
max_patterns: int = DEFAULT_MAX_PATTERNS) -> list[str]:
152146
'''Sparse-checkout patterns for rematerializing ``diff`` (of tests/) on
153-
the base tree: the test home and every affected test.
147+
the base tree: the test home and every affected test; at most
148+
``max_patterns``.
154149
'''
155150
patterns = dict.fromkeys(_TEST_HOME_PATTERNS)
156151
current_suite: Optional[str] = None
@@ -160,12 +155,16 @@ def changes_patterns(diff: str) -> list[str]:
160155
path = PurePosixPath(match.group(1))
161156
current_suite = None
162157
if path.parts[:2] == ('tests', 'tests'):
163-
_add_patterns(patterns, _patterns_for_changed_test_file(path))
158+
_add_patterns(
159+
patterns,
160+
_patterns_for_changed_test_file(path),
161+
max_patterns
162+
)
164163
elif (path.parts[:2] == ('tests', 'expectations') and
165164
path.suffix == '.log'):
166165
# Expectation lines name tests relative to the suite, so its name is
167-
# part of the patterns of each (see MAX_PATTERNS). A file whose name
168-
# no suite may have (e.g. too long) names no tests.
166+
# part of the patterns of each (see config.DEFAULT_MAX_PATTERNS). A
167+
# file whose name no suite may have (e.g. too long) names no tests.
169168
if edgtest.is_valid_suite_name(path.stem):
170169
current_suite = path.stem
171170
continue
@@ -174,33 +173,43 @@ def changes_patterns(diff: str) -> list[str]:
174173
if line.startswith(('-', '+')):
175174
test_name = line[1:].split(':')[0].strip()
176175
if test_name:
177-
_add_patterns(patterns, patterns_for_test(
178-
PurePosixPath('tests', 'tests', current_suite, test_name)
179-
))
176+
_add_patterns(
177+
patterns,
178+
patterns_for_test(
179+
PurePosixPath('tests', 'tests', current_suite, test_name)
180+
),
181+
max_patterns
182+
)
180183
return list(patterns)
181184

182185

183-
def failures_patterns(test_ids: Iterable[str]) -> list[str]:
186+
def failures_patterns(test_ids: Iterable[str],
187+
*,
188+
max_patterns: int = DEFAULT_MAX_PATTERNS) -> list[str]:
184189
'''Sparse-checkout patterns for serving CI results naming ``test_ids``
185-
('<suite>/<path>', as in .elist files), plus bench baselines.
190+
('<suite>/<path>', as in .elist files), plus bench baselines; at most
191+
``max_patterns``.
186192
'''
187193
patterns = dict.fromkeys([*_TEST_HOME_PATTERNS, *BENCH_BASELINE_PATTERNS])
188194
for test_id in test_ids:
189195
test_path = PurePosixPath('tests', 'tests', test_id)
190196
if '..' in test_path.parts or test_path.is_absolute():
191197
continue
192-
_add_patterns(patterns, patterns_for_test(test_path))
198+
_add_patterns(patterns, patterns_for_test(test_path), max_patterns)
193199
return list(patterns)
194200

195201

196-
def _add_patterns(patterns: dict[str, None], more: list[str]) -> None:
202+
def _add_patterns(patterns: dict[str, None],
203+
more: list[str],
204+
max_patterns: int) -> None:
197205
'''Add ``more`` to ``patterns`` (an ordered set), within
198-
``MAX_PATTERNS``.
206+
``max_patterns``.
199207
'''
200208
patterns.update(dict.fromkeys(more))
201-
if len(patterns) > MAX_PATTERNS:
209+
if len(patterns) > max_patterns:
202210
raise WorkspaceError(
203-
f"more than {MAX_PATTERNS} sparse-checkout patterns (too many tests)"
211+
f"more than {max_patterns} sparse-checkout patterns (too many tests; "
212+
f"{ENV_PREFIX}MAX_SPARSE_PATTERNS)"
204213
)
205214

206215

@@ -293,9 +302,10 @@ def remove_tree(path: Path) -> None:
293302

294303
class _Budget:
295304
'''What extracting an artifact may still spend: bytes, and entries.'''
296-
def __init__(self, max_bytes: int) -> None:
305+
def __init__(self, max_bytes: int, max_entries: int) -> None:
297306
self.bytes = max_bytes
298-
self.entries = MAX_ARTIFACT_ENTRIES
307+
self.max_entries = max_entries
308+
self.entries = max_entries
299309

300310
def spend(self, count: int) -> None:
301311
self.bytes -= count
@@ -306,7 +316,8 @@ def count_entry(self) -> None:
306316
self.entries -= 1
307317
if self.entries < 0:
308318
raise UnsafeArchiveError(
309-
f"artifact has more than {MAX_ARTIFACT_ENTRIES} entries"
319+
f"artifact has more than {self.max_entries} entries "
320+
f"({ENV_PREFIX}MAX_ARTIFACT_ENTRIES)"
310321
)
311322

312323

@@ -353,24 +364,24 @@ def _check_inner_tarball(path: Path, budget: _Budget) -> None:
353364
raise UnsafeArchiveError(f"{path.name} is not a valid tarball") from ex
354365

355366

356-
def extract_artifact(zip_path: Path,
357-
destination: Path,
358-
*,
359-
max_bytes: int) -> None:
367+
def extract_artifact(
368+
zip_path: Path, destination: Path, *, max_bytes: int,
369+
max_entries: int = DEFAULT_MAX_ARTIFACT_ENTRIES,
370+
max_directory_bytes: int = DEFAULT_MAX_ARTIFACT_DIRECTORY_BYTES) -> None:
360371
'''Safely extract a CI artifact zip into the (new, empty) ``destination``.
361372
362-
Rejects absolute or escaping paths and symlinks, and stops once more than
363-
``max_bytes`` would be written, or ``MAX_ARTIFACT_ENTRIES`` entries made
364-
(counting inner tarballs' contents too).
373+
Rejects absolute or escaping paths and symlinks, and a zip whose central
374+
directory is over ``max_directory_bytes``, and stops once more than
375+
``max_bytes`` would be written, or ``max_entries`` entries made (counting
376+
inner tarballs' contents too).
365377
'''
366378
directory_size = _central_directory_size(zip_path)
367-
if (directory_size is not None and
368-
directory_size > MAX_CENTRAL_DIRECTORY_BYTES):
379+
if directory_size is not None and directory_size > max_directory_bytes:
369380
raise UnsafeArchiveError(
370-
f"artifact's central directory is over "
371-
f"{MAX_CENTRAL_DIRECTORY_BYTES} bytes"
381+
f"artifact's central directory is over {max_directory_bytes} bytes "
382+
f"({ENV_PREFIX}MAX_ARTIFACT_DIRECTORY_BYTES)"
372383
)
373-
budget = _Budget(max_bytes)
384+
budget = _Budget(max_bytes, max_entries)
374385
tarballs = []
375386
try:
376387
with zipfile.ZipFile(zip_path) as archive:
@@ -406,11 +417,18 @@ def extract_artifact(zip_path: Path,
406417

407418

408419
class Workspaces:
409-
'''Clones and worktrees under ``home`` for one forge.'''
420+
'''Clones and worktrees under ``home`` for one forge; a diff (``diff``)
421+
may be at most ``max_diff_bytes``.
422+
'''
410423

411-
def __init__(self, home: Path, forge: Forge) -> None:
424+
def __init__(self,
425+
home: Path,
426+
forge: Forge,
427+
*,
428+
max_diff_bytes: int = DEFAULT_MAX_DIFF_BYTES) -> None:
412429
self._home = home
413430
self._forge = forge
431+
self._max_diff_bytes = max_diff_bytes
414432
self._repos_dir = home / 'repos' / forge.name
415433
self._reviews_dir = home / REVIEWS_DIR
416434
self._locks: dict[str, asyncio.Lock] = {}
@@ -597,7 +615,7 @@ async def diff(self,
597615
'diff', '--no-renames', '--no-color', '--no-ext-diff', '--no-textconv',
598616
base, head, '--', *paths,
599617
git_dir = self.clone_dir(repository),
600-
max_output = MAX_DIFF_BYTES
618+
max_output = self._max_diff_bytes
601619
)
602620
return result.stdout
603621

‎dev_tools/services/acknowledg-forge/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ from the environment variable shown as a fallback.
112112
| `ACKNOWLEDG_FORGE_SETTLE_TIMEOUT` | `7200` | Seconds after a push to stop waiting for CI before commenting |
113113
| `ACKNOWLEDG_FORGE_MAX_ARTIFACT_BYTES` | 256 MiB | Largest artifact download |
114114
| `ACKNOWLEDG_FORGE_MAX_EXTRACTED_BYTES` | 2 GiB | Largest extracted artifact (including inner tarballs) |
115+
| `ACKNOWLEDG_FORGE_MAX_ARTIFACT_ENTRIES` | `500000` | Most entries in an artifact (including inner tarballs'). CI's test results reach about 170,000 |
116+
| `ACKNOWLEDG_FORGE_MAX_ARTIFACT_DIRECTORY_BYTES` | 64 MiB | Largest artifact zip central directory, which is read into memory whole (about 106 bytes per entry in CI's test results) |
117+
| `ACKNOWLEDG_FORGE_MAX_DIFF_BYTES` | 64 MiB | Largest diff of a pull request's `tests/`, which is held in memory and copied more than once: raising it may need a higher `mem_limit` for the service ([`compose.yaml`](compose.yaml)) |
118+
| `ACKNOWLEDG_FORGE_MAX_SPARSE_PATTERNS` | `20000` | Most sparse-checkout patterns in a review's worktree (up to two per test). Checking out takes time proportional to patterns times paths |
115119
| `ACKNOWLEDG_FORGE_MAX_CONCURRENCY` | `2` | Pull requests processed at once |
116120
| `ACKNOWLEDG_FORGE_REMATERIALIZE_TIMEOUT` | `900` | Seconds `edg-rematerialize-test-run` may run |
117121
| `ACKNOWLEDG_FORGE_UNSANDBOXED` | `0` | `1` runs without containers: the service serves the runner servers and the publisher server itself, on `127.0.0.1`, where any local process can reach them. **Local development on a single-user machine only.** |

‎dev_tools/services/acknowledg-forge/SANDBOXING.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ them. Several layers keep the service from reading through one:
526526
| Resource | Limit |
527527
| --- | --- |
528528
| Artifact download | `ACKNOWLEDG_FORGE_MAX_ARTIFACT_BYTES` (256 MiB), also counting the decompressed size of inner tarballs |
529+
| Artifact entries | `ACKNOWLEDG_FORGE_MAX_ARTIFACT_ENTRIES` (500,000), counting inner tarballs' too; and its zip's central directory, `ACKNOWLEDG_FORGE_MAX_ARTIFACT_DIRECTORY_BYTES` (64 MiB), checked before zipfile reads it |
530+
| A pull request's diff of `tests/` | `ACKNOWLEDG_FORGE_MAX_DIFF_BYTES` (64 MiB) |
531+
| A review worktree's sparse-checkout patterns | `ACKNOWLEDG_FORGE_MAX_SPARSE_PATTERNS` (20,000) |
529532
| A file read for a reviewer | 64 MiB; a larger one fails the request |
530533
| A source shown to a reviewer | only as much as can be displayed is read |
531534
| Each container's `/tmp` | 512 MiB tmpfs; copies made for diffing are streamed there |

‎dev_tools/services/acknowledg-forge/tests/conftest.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,11 @@ def __init__(self,
301301
builder.start()
302302
_builder_clients.append(builder)
303303
self.sandbox = Sandbox(builder = builder)
304-
self.workspaces = Workspaces(config.home, forge)
304+
self.workspaces = Workspaces(
305+
config.home,
306+
forge,
307+
max_diff_bytes = config.max_diff_bytes
308+
)
305309
self.ingestor = Ingestor(
306310
store,
307311
forge,

0 commit comments

Comments
 (0)