|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Remote task interface. |
| 15 | +
|
| 16 | +This module defines the interface for a remote task execution client. This |
| 17 | +abstraction allows ClusterFuzz to support multiple remote execution |
| 18 | +environments, such as GCP Batch and Kubernetes, without tightly coupling |
| 19 | +the task creation logic to a specific implementation. |
| 20 | +""" |
| 21 | + |
| 22 | +import collections |
| 23 | +import random |
| 24 | +from typing import List |
| 25 | + |
| 26 | +from clusterfuzz._internal.metrics import logs |
| 27 | +from clusterfuzz._internal.remote_task import remote_task_types |
| 28 | +from clusterfuzz._internal.remote_task import remote_task_adapters |
| 29 | + |
| 30 | + |
| 31 | +class RemoteTaskGate(remote_task_types.RemoteTaskInterface): |
| 32 | + """A generic dispatcher for remote task execution. |
| 33 | +
|
| 34 | + This class acts as a high-level manager that abstracts away the specific |
| 35 | + details of the underlying remote execution backends. It uses the frequencies |
| 36 | + defined in this module to dynamically choose a backend for each task, |
| 37 | + allowing for flexible distribution and A/B testing. |
| 38 | + """ |
| 39 | + |
| 40 | + def __init__(self): |
| 41 | + # Instantiate and cache the service clients for each defined adapter. |
| 42 | + self._service_map = { |
| 43 | + adapter.id: adapter.service() |
| 44 | + for adapter in remote_task_adapters.RemoteTaskAdapters |
| 45 | + } |
| 46 | + self._adapters = remote_task_adapters.RemoteTaskAdapters |
| 47 | + |
| 48 | + def _get_adapter(self) -> str: |
| 49 | + """Performs a weighted random choice to select a remote backend. |
| 50 | +
|
| 51 | + This method is used when creating a single task, ensuring that the |
| 52 | + distribution of tasks over time aligns with the configured frequencies. |
| 53 | + """ |
| 54 | + frequencies = self.get_job_frequency() |
| 55 | + population = list(frequencies.keys()) |
| 56 | + weights = list(frequencies.values()) |
| 57 | + return random.choices(population, weights)[0] |
| 58 | + |
| 59 | + def get_job_frequency(self): |
| 60 | + """Returns the frequency distribution for all remote task adapters. |
| 61 | +
|
| 62 | + This function calculates the proportion of tasks that should be sent to each |
| 63 | + remote backend defined in the `RemoteTaskAdapters` enum. The calculation |
| 64 | + is based on feature flags, default weights, and ensures the total |
| 65 | + distribution sums to 1.0. |
| 66 | +
|
| 67 | + The order of adapters in the enum matters, as this function processes them |
| 68 | + sequentially, and any remaining weight to sum to 1.0 is assigned to the |
| 69 | + last adapter. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + A dictionary mapping each adapter's ID (e.g., 'gcp_batch') to its |
| 73 | + calculated frequency (a float between 0.0 and 1.0). |
| 74 | + """ |
| 75 | + frequencies = {adapter.id: 0.0 for adapter in self._adapters} |
| 76 | + total_weight = 0.0 |
| 77 | + |
| 78 | + for adapter in self._adapters: |
| 79 | + default_weight = adapter.default_weight |
| 80 | + feature_flag = adapter.feature_flag |
| 81 | + weight = default_weight |
| 82 | + |
| 83 | + # A feature flag can override the default weight for an adapter, allowing |
| 84 | + # for dynamic adjustments to task distribution. |
| 85 | + if (feature_flag and feature_flag.enabled and |
| 86 | + isinstance(feature_flag.content, float)): |
| 87 | + feature_flag_weight = feature_flag.content |
| 88 | + if 0 <= feature_flag_weight <= 1: |
| 89 | + weight = feature_flag_weight |
| 90 | + |
| 91 | + if total_weight >= 1.0 and weight > 0.0: |
| 92 | + logs.warning( |
| 93 | + 'Total weight for jobs frequency bigger than 1.0. Adapter starving', |
| 94 | + adapter=adapter.id) |
| 95 | + break |
| 96 | + |
| 97 | + # Ensure the cumulative weight does not exceed 1.0. If adding the |
| 98 | + # current weight would push the total over, we cap it. |
| 99 | + if weight + total_weight > 1.0: |
| 100 | + weight = 1.0 - total_weight |
| 101 | + |
| 102 | + total_weight += weight |
| 103 | + frequencies[adapter.id] = weight if weight >= 0 else 0.0 |
| 104 | + |
| 105 | + logs.info('Job frequencies', frequencies=frequencies) |
| 106 | + return frequencies |
| 107 | + |
| 108 | + def create_utask_main_job(self, module, job_type, input_download_url): |
| 109 | + adapter_id = self._get_adapter() |
| 110 | + service = self._service_map[adapter_id] |
| 111 | + return service.create_utask_main_job(module, job_type, input_download_url) |
| 112 | + |
| 113 | + def create_utask_main_jobs( |
| 114 | + self, remote_tasks: List[remote_task_types.RemoteTask]): |
| 115 | + """Creates a batch of remote tasks, distributing them across backends. |
| 116 | +
|
| 117 | + This method handles two cases: |
| 118 | + 1. If there is only one task, it uses a weighted random choice to select |
| 119 | + a backend, similar to `create_utask_main_job`. |
| 120 | + 2. If there are multiple tasks, it distributes them deterministically |
| 121 | + across the available backends based on their configured frequencies. |
| 122 | + This ensures that a batch of 100 tasks with a 70/30 split sends |
| 123 | + exactly 70 tasks to one backend and 30 to the other. |
| 124 | + """ |
| 125 | + tasks_by_adapter = collections.defaultdict(list) |
| 126 | + |
| 127 | + if len(remote_tasks) == 1: |
| 128 | + # For a single task, use a random distribution. |
| 129 | + adapter_id = self._get_adapter() |
| 130 | + tasks_by_adapter[adapter_id].extend(remote_tasks) |
| 131 | + else: |
| 132 | + # For multiple tasks, use deterministic slicing to ensure the |
| 133 | + # distribution precisely matches the frequency configuration. |
| 134 | + frequencies = self.get_job_frequency() |
| 135 | + start_index = 0 |
| 136 | + for adapter_id, frequency in frequencies.items(): |
| 137 | + count = int(len(remote_tasks) * frequency) |
| 138 | + tasks_by_adapter[adapter_id].extend( |
| 139 | + remote_tasks[start_index:start_index + count]) |
| 140 | + start_index += count |
| 141 | + |
| 142 | + # Distribute any remainder tasks (due to rounding) one by one. This |
| 143 | + # ensures that all tasks are assigned to a backend. |
| 144 | + remaining_tasks = remote_tasks[start_index:] |
| 145 | + for i, task in enumerate(remaining_tasks): |
| 146 | + adapter_id = list(frequencies.keys())[i % len(frequencies)] |
| 147 | + tasks_by_adapter[adapter_id].append(task) |
| 148 | + |
| 149 | + results = [] |
| 150 | + for adapter_id, tasks in tasks_by_adapter.items(): |
| 151 | + if tasks: |
| 152 | + logs.info(f'Sending {len(tasks)} tasks to {adapter_id}.') |
| 153 | + service = self._service_map[adapter_id] |
| 154 | + results.extend(service.create_utask_main_jobs(tasks)) |
| 155 | + return results |
0 commit comments