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.""" |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame^] | 18 | def __init__(self, queue_entries): |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 19 | |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame^] | 20 | # TODO(beeps): Break this dependency on the host_query_manager, |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 21 | # crbug.com/336934. |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame^] | 22 | from autotest_lib.scheduler import query_managers |
| 23 | self.query_manager = query_managers.AFEHostQueryManager() |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 24 | jobs = [queue_entry.job_id for queue_entry in queue_entries] |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame^] | 25 | self._job_acls = self.query_manager._get_job_acl_groups(jobs) |
| 26 | self._job_deps = self.query_manager._get_job_dependencies(jobs) |
| 27 | self._labels = self.query_manager._get_labels(self._job_deps) |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def get_job_info(self, queue_entry): |
| 31 | """Extract job information from a queue_entry/host-scheduler. |
| 32 | |
| 33 | @param queue_entry: The queue_entry for which we need job information. |
| 34 | |
| 35 | @return: A dictionary representing job related information. |
| 36 | """ |
| 37 | job_id = queue_entry.job_id |
| 38 | job_deps = self._job_deps.get(job_id, []) |
| 39 | job_deps = [dep for dep in job_deps |
Alex Miller | aa77200 | 2014-04-10 17:51:21 -0700 | [diff] [blame] | 40 | if not provision.is_for_special_action(self._labels[dep].name)] |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 41 | job_acls = self._job_acls.get(job_id, []) |
| 42 | |
| 43 | return {'deps': job_deps, 'acls': job_acls, |
Prashanth B | 9bc32fa | 2014-02-20 12:58:40 -0800 | [diff] [blame] | 44 | 'host_id': queue_entry.host_id, |
Prashanth B | 2c1a22a | 2014-04-02 17:30:51 -0700 | [diff] [blame] | 45 | 'parent_job_id': queue_entry.job.parent_job_id, |
Prashanth B | 9bc32fa | 2014-02-20 12:58:40 -0800 | [diff] [blame] | 46 | 'priority': queue_entry.job.priority} |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 47 | |
| 48 | |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame^] | 49 | def acquire_hosts(queue_entries): |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 50 | """Acquire hosts for the list of queue_entries. |
| 51 | |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 52 | The act of acquisition involves leasing a host from the rdb. |
| 53 | |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 54 | @param queue_entries: A list of queue_entries that need hosts. |
beeps | cc9fc70 | 2013-12-02 12:45:38 -0800 | [diff] [blame] | 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 | """ |
Prashanth B | f66d51b | 2014-05-06 12:42:25 -0700 | [diff] [blame^] | 61 | job_query_manager = JobQueryManager(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 |