Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame^] | 1 | # Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """This module manages translation between monitor_db and the rdb. """ |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 6 | |
| 7 | import common |
| 8 | from autotest_lib.scheduler import rdb |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame^] | 9 | from autotest_lib.scheduler import rdb_hosts |
| 10 | from autotest_lib.scheduler import rdb_requests |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 11 | from autotest_lib.server.cros import provision |
| 12 | |
| 13 | |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 14 | # Adapters for scheduler specific objects: Convert job information to a |
| 15 | # format more ameanable to the rdb/rdb request managers. |
| 16 | class JobQueryManager(object): |
| 17 | """A caching query manager for all job related information.""" |
| 18 | def __init__(self, host_scheduler, queue_entries): |
| 19 | |
| 20 | # TODO(beeps): Break the dependency on the host_scheduler, |
| 21 | # crbug.com/336934. |
| 22 | self.host_scheduler = host_scheduler |
| 23 | jobs = [queue_entry.job_id for queue_entry in queue_entries] |
| 24 | self._job_acls = self.host_scheduler._get_job_acl_groups(jobs) |
| 25 | self._job_deps = self.host_scheduler._get_job_dependencies(jobs) |
| 26 | self._labels = self.host_scheduler._get_labels(self._job_deps) |
| 27 | |
| 28 | |
| 29 | def get_job_info(self, queue_entry): |
| 30 | """Extract job information from a queue_entry/host-scheduler. |
| 31 | |
| 32 | @param queue_entry: The queue_entry for which we need job information. |
| 33 | |
| 34 | @return: A dictionary representing job related information. |
| 35 | """ |
| 36 | job_id = queue_entry.job_id |
| 37 | job_deps = self._job_deps.get(job_id, []) |
| 38 | job_deps = [dep for dep in job_deps |
| 39 | if not provision.can_provision(self._labels[dep].name)] |
| 40 | job_acls = self._job_acls.get(job_id, []) |
| 41 | |
| 42 | return {'deps': job_deps, 'acls': job_acls, |
Prashanth B | 9bc32fa | 2014-02-20 12:58:40 -0800 | [diff] [blame] | 43 | 'host_id': queue_entry.host_id, |
| 44 | 'priority': queue_entry.job.priority} |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def acquire_hosts(host_scheduler, queue_entries): |
| 48 | """Acquire hosts for the list of queue_entries. |
| 49 | |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame^] | 50 | The act of acquisition involves leasing a host from the rdb. |
| 51 | |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 52 | @param queue_entries: A list of queue_entries that need hosts. |
| 53 | @param host_scheduler: The host_scheduler object, needed to get job |
| 54 | information. |
| 55 | |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame^] | 56 | @yield: An rdb_hosts.RDBClientHostWrapper for each host acquired on behalf |
| 57 | of a queue_entry, or None if a host wasn't found. |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 58 | |
| 59 | @raises RDBException: If something goes wrong making the request. |
| 60 | """ |
| 61 | job_query_manager = JobQueryManager(host_scheduler, queue_entries) |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame^] | 62 | request_manager = rdb_requests.BaseHostRequestManager( |
| 63 | rdb_requests.AcquireHostRequest, rdb.rdb_host_request_dispatcher) |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 64 | for entry in queue_entries: |
| 65 | request_manager.add_request(**job_query_manager.get_job_info(entry)) |
| 66 | |
| 67 | for host in request_manager.response(): |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame^] | 68 | yield (rdb_hosts.RDBClientHostWrapper(**host) |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 69 | if host else None) |
| 70 | |
| 71 | |
| 72 | def get_hosts(host_ids): |
| 73 | """Get information about the hosts with ids in host_ids. |
| 74 | |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame^] | 75 | get_hosts is different from acquire_hosts in that it is completely |
| 76 | oblivious to the leased state of a host. |
| 77 | |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 78 | @param host_ids: A list of host_ids. |
| 79 | |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame^] | 80 | @return: A list of rdb_hosts.RDBClientHostWrapper objects. |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 81 | |
| 82 | @raises RDBException: If something goes wrong in making the request. |
| 83 | """ |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame^] | 84 | request_manager = rdb_requests.BaseHostRequestManager( |
| 85 | rdb_requests.HostRequest, rdb.get_hosts) |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 86 | for host_id in host_ids: |
| 87 | request_manager.add_request(host_id=host_id) |
| 88 | |
| 89 | hosts = [] |
| 90 | for host in request_manager.response(): |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame^] | 91 | hosts.append(rdb_hosts.RDBClientHostWrapper(**host) |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 92 | if host else None) |
| 93 | return hosts |