blob: d8a42e6bb5b3d2ab27ae326050a1a1f7c380a07e [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,
Prashanth B2c1a22a2014-04-02 17:30:51 -070044 'parent_job_id': queue_entry.job.parent_job_id,
Prashanth B9bc32fa2014-02-20 12:58:40 -080045 'priority': queue_entry.job.priority}
beepscc9fc702013-12-02 12:45:38 -080046
47
48def acquire_hosts(host_scheduler, queue_entries):
49 """Acquire hosts for the list of queue_entries.
50
Prashanth B489b91d2014-03-15 12:17:16 -070051 The act of acquisition involves leasing a host from the rdb.
52
beepscc9fc702013-12-02 12:45:38 -080053 @param queue_entries: A list of queue_entries that need hosts.
54 @param host_scheduler: The host_scheduler object, needed to get job
55 information.
56
Prashanth B489b91d2014-03-15 12:17:16 -070057 @yield: An rdb_hosts.RDBClientHostWrapper for each host acquired on behalf
58 of a queue_entry, or None if a host wasn't found.
beepscc9fc702013-12-02 12:45:38 -080059
60 @raises RDBException: If something goes wrong making the request.
61 """
62 job_query_manager = JobQueryManager(host_scheduler, queue_entries)
Prashanth B489b91d2014-03-15 12:17:16 -070063 request_manager = rdb_requests.BaseHostRequestManager(
64 rdb_requests.AcquireHostRequest, rdb.rdb_host_request_dispatcher)
beepscc9fc702013-12-02 12:45:38 -080065 for entry in queue_entries:
66 request_manager.add_request(**job_query_manager.get_job_info(entry))
67
68 for host in request_manager.response():
Prashanth B489b91d2014-03-15 12:17:16 -070069 yield (rdb_hosts.RDBClientHostWrapper(**host)
beepscc9fc702013-12-02 12:45:38 -080070 if host else None)
71
72
73def get_hosts(host_ids):
74 """Get information about the hosts with ids in host_ids.
75
Prashanth B489b91d2014-03-15 12:17:16 -070076 get_hosts is different from acquire_hosts in that it is completely
77 oblivious to the leased state of a host.
78
beepscc9fc702013-12-02 12:45:38 -080079 @param host_ids: A list of host_ids.
80
Prashanth B489b91d2014-03-15 12:17:16 -070081 @return: A list of rdb_hosts.RDBClientHostWrapper objects.
beepscc9fc702013-12-02 12:45:38 -080082
83 @raises RDBException: If something goes wrong in making the request.
84 """
Prashanth B489b91d2014-03-15 12:17:16 -070085 request_manager = rdb_requests.BaseHostRequestManager(
86 rdb_requests.HostRequest, rdb.get_hosts)
beepscc9fc702013-12-02 12:45:38 -080087 for host_id in host_ids:
88 request_manager.add_request(host_id=host_id)
89
90 hosts = []
91 for host in request_manager.response():
Prashanth B489b91d2014-03-15 12:17:16 -070092 hosts.append(rdb_hosts.RDBClientHostWrapper(**host)
beepscc9fc702013-12-02 12:45:38 -080093 if host else None)
94 return hosts