Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2016 The LUCI Authors. All rights reserved. | 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """Utilities for operating on instance group managers.""" | 5 """Utilities for operating on instance group managers.""" |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 | 8 |
| 9 from google.appengine.ext import ndb | 9 from google.appengine.ext import ndb |
| 10 | 10 |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 logging.warning('InstanceTemplateRevision does not exist: %s', key) | 302 logging.warning('InstanceTemplateRevision does not exist: %s', key) |
| 303 return | 303 return |
| 304 | 304 |
| 305 if not parent.project: | 305 if not parent.project: |
| 306 logging.warning('InstanceTemplateRevision project unspecified: %s', key) | 306 logging.warning('InstanceTemplateRevision project unspecified: %s', key) |
| 307 return | 307 return |
| 308 | 308 |
| 309 api = gce.Project(parent.project) | 309 api = gce.Project(parent.project) |
| 310 response = api.get_instance_group_manager(get_name(entity), key.id()) | 310 response = api.get_instance_group_manager(get_name(entity), key.id()) |
| 311 | 311 |
| 312 # Find out how many VMs are created (i.e. not currently being created | |
|
Vadim Sh.
2016/07/20 20:57:23
"are idle" ?
smut
2016/07/20 21:00:37
Done.
| |
| 313 # or deleted). This helps avoid doing too many VM actions simultaneously. | |
| 314 current_size = response.get('currentActions', {}).get('none') | |
| 315 if not current_size: | |
|
Vadim Sh.
2016/07/20 20:57:23
zero idle VMs is not allowed?..
should it be "if
smut
2016/07/20 21:00:37
True story. Probably not necessary anyway, unless
| |
| 316 logging.error('Unexpected response: %s', json.dumps(response, indent=2)) | |
| 317 return | |
| 318 | |
| 312 # Try to reach the target size, but avoid increasing the number of | 319 # Try to reach the target size, but avoid increasing the number of |
| 313 # instances by more than the resize limit. For now, the target size | 320 # instances by more than the resize limit. For now, the target size |
| 314 # is just the minimum size. | 321 # is just the minimum size. |
| 315 target_size = min(entity.minimum_size, response['targetSize'] + RESIZE_LIMIT) | 322 target_size = min(entity.minimum_size, current_size + RESIZE_LIMIT) |
| 316 if response['targetSize'] >= target_size: | 323 if current_size >= target_size: |
| 317 return | 324 return |
| 318 | 325 |
| 319 api.resize_managed_instance_group(response['name'], key.id(), target_size) | 326 api.resize_managed_instance_group(response['name'], key.id(), target_size) |
| 320 | 327 |
| 321 | 328 |
| 322 def schedule_resize(): | 329 def schedule_resize(): |
| 323 """Enqueues tasks to resize instance group managers.""" | 330 """Enqueues tasks to resize instance group managers.""" |
| 324 for instance_template in models.InstanceTemplate.query(): | 331 for instance_template in models.InstanceTemplate.query(): |
| 325 if instance_template.active: | 332 if instance_template.active: |
| 326 instance_template_revision = instance_template.active.get() | 333 instance_template_revision = instance_template.active.get() |
| 327 if instance_template_revision: | 334 if instance_template_revision: |
| 328 for instance_group_manager_key in instance_template_revision.active: | 335 for instance_group_manager_key in instance_template_revision.active: |
| 329 if not utils.enqueue_task( | 336 if not utils.enqueue_task( |
| 330 '/internal/queues/resize-instance-group', | 337 '/internal/queues/resize-instance-group', |
| 331 'resize-instance-group', | 338 'resize-instance-group', |
| 332 params={ | 339 params={ |
| 333 'key': instance_group_manager_key.urlsafe(), | 340 'key': instance_group_manager_key.urlsafe(), |
| 334 }, | 341 }, |
| 335 ): | 342 ): |
| 336 logging.warning( | 343 logging.warning( |
| 337 'Failed to enqueue task for InstanceGroupManager: %s', | 344 'Failed to enqueue task for InstanceGroupManager: %s', |
| 338 instance_group_manager_key, | 345 instance_group_manager_key, |
| 339 ) | 346 ) |
| OLD | NEW |