Skip to content

Commit

Permalink
common: add jobs source configuration option
Browse files Browse the repository at this point in the history
By default, repo sync will run as many jobs as the amount of CPU cores on the
host machine.

This is fine for smaller development machines, but for some pipelines might want
to adjust this. For example, when rate-limiting is applied.

Add a 'jobs' source configuration field to do so.

Signed-off-by: Mattijs Korpershoek <[email protected]>
  • Loading branch information
makohoek authored and glaroque committed Jul 25, 2023
1 parent 1ddca89 commit 988dd5e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Track changes in a [repo](https://gerrit.googlesource.com/git-repo/+/master/#rep
* `depth`: *Optional.* shallow clone with a history truncated to the specified number of commits.
Defaults to full git clone for each project.

* `jobs`: *Optional.* number of jobs to run in parallel (default: 0; based on number of CPU cores)
Reduce this if you observe network errors.

### Example

Resource configuration for a public project using repo (Android)
Expand All @@ -47,6 +50,7 @@ resources:
branch: master
name: default.xml
depth: 1 # use shallow clone for faster syncing
jobs: 4 # run with -j4
```

## Behavior
Expand Down
2 changes: 1 addition & 1 deletion repo_resource/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def check(instream) -> list:
try:
repo = common.Repo()
repo.init(config.url, config.revision, config.name, config.depth)
repo.sync()
repo.sync(jobs=config.jobs)
version = repo.currentVersion()
except Exception as e:
raise e
Expand Down
7 changes: 6 additions & 1 deletion repo_resource/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class SourceConfiguration(NamedTuple):
name: str = 'default.xml'
private_key: str = '_invalid'
depth: int = -1
jobs: int = 0


def source_config_from_payload(payload):
Expand Down Expand Up @@ -171,7 +172,7 @@ def init(self, url, revision='HEAD', name='default.xml', depth=-1):
finally:
self.__restore_oldpwd()

def sync(self, version: Version = None):
def sync(self, version: Version = None, jobs: int = 0):
self.__change_to_workdir()
try:
with redirect_stdout(sys.stderr):
Expand All @@ -180,6 +181,10 @@ def sync(self, version: Version = None):
'--current-branch', '--detach', '--no-tags',
'--fail-fast', '--force-sync'
]

if jobs > 0:
repo_cmd.append('--jobs={}'.format(jobs))

if version is None:
repo._Main(repo_cmd)
else:
Expand Down
2 changes: 1 addition & 1 deletion repo_resource/in_.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def in_(instream, dest_dir='.'):
repo = common.Repo(workdir=Path(dest_dir))

repo.init(config.url, config.revision, config.name, config.depth)
repo.sync(requested_version)
repo.sync(requested_version, config.jobs)
fetched_version = repo.currentVersion()
except Exception as e:
raise e
Expand Down
33 changes: 33 additions & 0 deletions repo_resource/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from io import StringIO
import unittest
from pathlib import Path
from timeit import default_timer as timer
import shutil
import repo

Expand Down Expand Up @@ -37,6 +38,13 @@ def setUp(self):
'name': 'baylibre_ssh_project.xml',
}
}
self.demo_multiple_aosp_device_source = {
'source': {
'url': 'https://github.com/makohoek/demo-manifests.git',
'revision': 'main',
'name': 'aosp_multiple_device_fixed.xml'
}
}

def tearDown(self):
p = common.CACHEDIR
Expand Down Expand Up @@ -300,6 +308,31 @@ def test_ssh_private_key_without_manifest_access(self):

self.assertEquals(len(versions), 0)

# test that we can specify an amount of jobs
# This is a little flaky because it depends on network
def test_jobs_limit(self):
data = self.demo_multiple_aosp_device_source

data['source']['jobs'] = 24
start = timer()
instream = StringIO(json.dumps(data))
check.check(instream)
end = timer()
fast_duration = end - start

# call tearDown() manually to clear the CACHE dir
self.tearDown()

data['source']['jobs'] = 1
start = timer()
instream = StringIO(json.dumps(data))
check.check(instream)
end = timer()
slow_duration = end - start

print('fast: {} slow: {}'.format(fast_duration, slow_duration))
self.assertTrue(fast_duration < slow_duration)


if __name__ == '__main__':
unittest.main()

0 comments on commit 988dd5e

Please sign in to comment.