blob: b705e327ac187728bef6fa14e3e963241ea3d6d5 [file] [log] [blame]
Prashanth B489b91d2014-03-15 12:17:16 -07001# 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. """
beepscc9fc702013-12-02 12:45:38 -08006
7import common
8from autotest_lib.scheduler import rdb
Prashanth B489b91d2014-03-15 12:17:16 -07009from autotest_lib.scheduler import rdb_hosts
10from autotest_lib.scheduler import rdb_requests
beepscc9fc702013-12-02 12:45:38 -080011from autotest_lib.server.cros import provision
12
13
beepscc9fc702013-12-02 12:45:38 -080014# Adapters for scheduler specific objects: Convert job information to a
15# format more ameanable to the rdb/rdb request managers.
16class 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 B9bc32fa2014-02-20 12:58:40 -080043 'host_id': queue_entry.host_id,
44 'priority': queue_entry.job.priority}
beepscc9fc702013-12-02 12:45:38 -080045
46
47def acquire_hosts(host_scheduler, queue_entries):
48 """Acquire hosts for the list of queue_entries.
49
Prashanth B489b91d2014-03-15 12:17:16 -070050 The act of acquisition involves leasing a host from the rdb.
51
beepscc9fc702013-12-02 12:45:38 -080052 @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 B489b91d2014-03-15 12:17:16 -070056 @yield: An rdb_hosts.RDBClientHostWrapper for each host acquired on behalf
57 of a queue_entry, or None if a host wasn't found.
beepscc9fc702013-12-02 12:45:38 -080058
59 @raises RDBException: If something goes wrong making the request.
60 """
61 job_query_manager = JobQueryManager(host_scheduler, queue_entries)
Prashanth B489b91d2014-03-15 12:17:16 -070062 request_manager = rdb_requests.BaseHostRequestManager(
63 rdb_requests.AcquireHostRequest, rdb.rdb_host_request_dispatcher)
beepscc9fc702013-12-02 12:45:38 -080064 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 B489b91d2014-03-15 12:17:16 -070068 yield (rdb_hosts.RDBClientHostWrapper(**host)
beepscc9fc702013-12-02 12:45:38 -080069 if host else None)
70
71
72def get_hosts(host_ids):
73 """Get information about the hosts with ids in host_ids.
74
Prashanth B489b91d2014-03-15 12:17:16 -070075 get_hosts is different from acquire_hosts in that it is completely
76 oblivious to the leased state of a host.
77
beepscc9fc702013-12-02 12:45:38 -080078 @param host_ids: A list of host_ids.
79
Prashanth B489b91d2014-03-15 12:17:16 -070080 @return: A list of rdb_hosts.RDBClientHostWrapper objects.
beepscc9fc702013-12-02 12:45:38 -080081
82 @raises RDBException: If something goes wrong in making the request.
83 """
Prashanth B489b91d2014-03-15 12:17:16 -070084 request_manager = rdb_requests.BaseHostRequestManager(
85 rdb_requests.HostRequest, rdb.get_hosts)
beepscc9fc702013-12-02 12:45:38 -080086 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 B489b91d2014-03-15 12:17:16 -070091 hosts.append(rdb_hosts.RDBClientHostWrapper(**host)
beepscc9fc702013-12-02 12:45:38 -080092 if host else None)
93 return hosts