blob: 8f0ee7333646932c651b00c64bfb08741247192a [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."""
Fang Deng52a23932014-11-20 18:30:22 -080018 def __init__(self, queue_entries, suite_min_duts=None):
19 """Initialize.
beepscc9fc702013-12-02 12:45:38 -080020
Fang Deng52a23932014-11-20 18:30:22 -080021 @param queue_entries: A list of HostQueueEntry objects.
22 @param suite_min_duts: A dictionary where the key is suite job id,
23 and the value is the value of 'suite_min_dut' in the suite's
24 job keyvals. It should cover all the suite jobs which
25 the jobs (associated with the queue_entries) belong to.
26 """
Prashanth Bf66d51b2014-05-06 12:42:25 -070027 # TODO(beeps): Break this dependency on the host_query_manager,
beepscc9fc702013-12-02 12:45:38 -080028 # crbug.com/336934.
Prashanth Bf66d51b2014-05-06 12:42:25 -070029 from autotest_lib.scheduler import query_managers
30 self.query_manager = query_managers.AFEHostQueryManager()
beepscc9fc702013-12-02 12:45:38 -080031 jobs = [queue_entry.job_id for queue_entry in queue_entries]
Prashanth Bf66d51b2014-05-06 12:42:25 -070032 self._job_acls = self.query_manager._get_job_acl_groups(jobs)
33 self._job_deps = self.query_manager._get_job_dependencies(jobs)
34 self._labels = self.query_manager._get_labels(self._job_deps)
Fang Deng52a23932014-11-20 18:30:22 -080035 self._suite_min_duts = suite_min_duts or {}
beepscc9fc702013-12-02 12:45:38 -080036
37
38 def get_job_info(self, queue_entry):
39 """Extract job information from a queue_entry/host-scheduler.
40
41 @param queue_entry: The queue_entry for which we need job information.
42
43 @return: A dictionary representing job related information.
44 """
45 job_id = queue_entry.job_id
Fang Denga9bc9592015-01-27 17:09:57 -080046 job_deps, job_preferred_deps = [], []
47 for dep in self._job_deps.get(job_id, []):
Dan Shie44f9c02016-02-18 13:25:05 -080048 if not provision.is_for_special_action(self._labels[dep].name):
49 job_deps.append(dep)
50 elif provision.Provision.acts_on(self._labels[dep].name):
51 job_preferred_deps.append(dep)
Fang Denga9bc9592015-01-27 17:09:57 -080052
beepscc9fc702013-12-02 12:45:38 -080053 job_acls = self._job_acls.get(job_id, [])
Fang Deng52a23932014-11-20 18:30:22 -080054 parent_id = queue_entry.job.parent_job_id
55 min_duts = self._suite_min_duts.get(parent_id, 0) if parent_id else 0
beepscc9fc702013-12-02 12:45:38 -080056
57 return {'deps': job_deps, 'acls': job_acls,
Fang Denga9bc9592015-01-27 17:09:57 -080058 'preferred_deps': job_preferred_deps,
Prashanth B9bc32fa2014-02-20 12:58:40 -080059 'host_id': queue_entry.host_id,
Prashanth B2c1a22a2014-04-02 17:30:51 -070060 'parent_job_id': queue_entry.job.parent_job_id,
Fang Deng52a23932014-11-20 18:30:22 -080061 'priority': queue_entry.job.priority,
62 'suite_min_duts': min_duts}
beepscc9fc702013-12-02 12:45:38 -080063
64
Fang Deng52a23932014-11-20 18:30:22 -080065def acquire_hosts(queue_entries, suite_min_duts=None):
beepscc9fc702013-12-02 12:45:38 -080066 """Acquire hosts for the list of queue_entries.
67
Prashanth B489b91d2014-03-15 12:17:16 -070068 The act of acquisition involves leasing a host from the rdb.
69
beepscc9fc702013-12-02 12:45:38 -080070 @param queue_entries: A list of queue_entries that need hosts.
Fang Deng52a23932014-11-20 18:30:22 -080071 @param suite_min_duts: A dictionary that maps suite job id to the minimum
72 number of duts required.
beepscc9fc702013-12-02 12:45:38 -080073
Prashanth B489b91d2014-03-15 12:17:16 -070074 @yield: An rdb_hosts.RDBClientHostWrapper for each host acquired on behalf
75 of a queue_entry, or None if a host wasn't found.
beepscc9fc702013-12-02 12:45:38 -080076
77 @raises RDBException: If something goes wrong making the request.
78 """
Fang Deng52a23932014-11-20 18:30:22 -080079 job_query_manager = JobQueryManager(queue_entries, suite_min_duts)
Prashanth B489b91d2014-03-15 12:17:16 -070080 request_manager = rdb_requests.BaseHostRequestManager(
81 rdb_requests.AcquireHostRequest, rdb.rdb_host_request_dispatcher)
beepscc9fc702013-12-02 12:45:38 -080082 for entry in queue_entries:
83 request_manager.add_request(**job_query_manager.get_job_info(entry))
84
85 for host in request_manager.response():
Prashanth B489b91d2014-03-15 12:17:16 -070086 yield (rdb_hosts.RDBClientHostWrapper(**host)
beepscc9fc702013-12-02 12:45:38 -080087 if host else None)
88
89
90def get_hosts(host_ids):
91 """Get information about the hosts with ids in host_ids.
92
Prashanth B489b91d2014-03-15 12:17:16 -070093 get_hosts is different from acquire_hosts in that it is completely
94 oblivious to the leased state of a host.
95
beepscc9fc702013-12-02 12:45:38 -080096 @param host_ids: A list of host_ids.
97
Prashanth B489b91d2014-03-15 12:17:16 -070098 @return: A list of rdb_hosts.RDBClientHostWrapper objects.
beepscc9fc702013-12-02 12:45:38 -080099
100 @raises RDBException: If something goes wrong in making the request.
101 """
Prashanth B489b91d2014-03-15 12:17:16 -0700102 request_manager = rdb_requests.BaseHostRequestManager(
103 rdb_requests.HostRequest, rdb.get_hosts)
beepscc9fc702013-12-02 12:45:38 -0800104 for host_id in host_ids:
105 request_manager.add_request(host_id=host_id)
106
107 hosts = []
108 for host in request_manager.response():
Prashanth B489b91d2014-03-15 12:17:16 -0700109 hosts.append(rdb_hosts.RDBClientHostWrapper(**host)
beepscc9fc702013-12-02 12:45:38 -0800110 if host else None)
111 return hosts