Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
configuration
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
configuration
Commits
99473025
Commit
99473025
authored
Aug 11, 2016
by
Michael Roytman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor balancecontainers to use docker_images script
parent
132fff9c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
40 deletions
+34
-40
util/balancecontainers.py
+34
-40
No files found.
util/balancecontainers.py
View file @
99473025
...
...
@@ -5,64 +5,62 @@ import itertools
import
sys
import
argparse
import
logging
import
docker_images
TRAVIS_BUILD_DIR
=
os
.
environ
.
get
(
"TRAVIS_BUILD_DIR"
)
CONFIG_FILE_PATH
=
pathlib2
.
Path
(
TRAVIS_BUILD_DIR
,
"util"
,
"parsefiles_config.yml"
)
LOGGER
=
logging
.
getLogger
(
__name__
)
def
pack_
containers
(
container
s
,
num_shards
):
def
pack_
shards
(
used_image
s
,
num_shards
):
"""
Determines an approximation of the optimal way to pack the
container
s into a given number of shards so as to
Determines an approximation of the optimal way to pack the
image
s into a given number of shards so as to
equalize the execution time amongst the shards.
Input:
containers: A set of Docker container
s
num_shards: A number of shards amongst which to distribute the Docker
container
s
used_images: A set of Docker images and their rank
s
num_shards: A number of shards amongst which to distribute the Docker
image
s
"""
# open config file containing container weights
config_file_path
=
pathlib2
.
Path
(
CONFIG_FILE_PATH
)
with
(
config_file_path
.
open
(
mode
=
'r'
))
as
file
:
try
:
config
=
yaml
.
load
(
file
)
except
yaml
.
YAMLError
,
exc
:
LOGGER
.
error
(
"error in configuration file:
%
s"
%
str
(
exc
))
sys
.
exit
(
1
)
# get container weights
weights
=
config
.
get
(
"weights"
)
# convert all containers in config file to a list of tuples (<container>, <weight>)
weights_list
=
[
x
.
items
()
for
x
in
weights
]
weights_list
=
list
(
itertools
.
chain
.
from_iterable
(
weights_list
))
# performs intersection between weighted containers and input containers
used_containers
=
[
x
for
x
in
weights_list
if
x
[
0
]
in
containers
]
# sorts used containers in descending order on the weight
sorted_
containers
=
sorted
(
used_container
s
,
key
=
lambda
x
:
x
[
1
],
reverse
=
True
)
sorted_
images
=
sorted
(
used_image
s
,
key
=
lambda
x
:
x
[
1
],
reverse
=
True
)
shards
=
[]
# for the number of shards
for
i
in
range
(
0
,
num_shards
):
# initialize initial dict
shards
.
append
({
"
container
s"
:
[],
"sum"
:
0
})
shards
.
append
({
"
image
s"
:
[],
"sum"
:
0
})
# for each container
for
container
in
sorted_container
s
:
for
image
in
sorted_image
s
:
# find the shard with the current minimum execution time
shard
=
min
(
shards
,
key
=
lambda
x
:
x
[
"sum"
])
# add the current container to the shard
shard
[
"
containers"
]
.
append
(
container
)
shard
[
"
images"
]
.
append
(
image
)
# add the current container's weight to the shard's total expected execution time
shard
[
"sum"
]
+=
container
[
1
]
shard
[
"sum"
]
+=
image
[
1
]
return
shards
def
read_input
():
"""
Reads input from standard input.
"""
images
=
[]
# get images from standard in
for
line
in
sys
.
stdin
:
line
=
line
.
strip
()
line
=
line
.
strip
(
"[]"
)
items
=
line
.
split
()
images
.
extend
(
items
)
return
images
def
arg_parse
():
parser
=
argparse
.
ArgumentParser
(
description
=
'Given a list of containers as input and a number of shards, '
...
...
@@ -79,24 +77,20 @@ if __name__ == '__main__':
# configure logging
logging
.
basicConfig
()
containers
=
[]
# get input from standard in
images
=
read_input
()
# get containers from standard in
for
line
in
sys
.
stdin
:
line
=
line
.
strip
()
line
=
line
.
strip
(
"[]"
)
items
=
line
.
split
()
containers
.
extend
(
items
)
# get images that are used and described in configuration file
used_images
=
docker_images
.
get_used_images
(
images
)
# find optimal packing of the
container
s amongst shards
shards
=
pack_
containers
(
container
s
,
args
.
num_shards
)
# find optimal packing of the
image
s amongst shards
shards
=
pack_
shards
(
used_image
s
,
args
.
num_shards
)
# print space separated list of containers for each shard
for
shard
in
shards
:
middle
=
" "
conts
=
[
x
[
0
]
for
x
in
shard
[
"
container
s"
]]
conts
=
[
x
[
0
]
for
x
in
shard
[
"
image
s"
]]
line
=
middle
.
join
(
conts
)
print
line
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment