Coverage for python/lsst/ctrl/bps/panda/panda_service.py: 12%
225 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-14 04:01 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-14 04:01 -0800
1# This file is part of ctrl_bps_panda.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
23__all__ = ["PanDAService", "PandaBpsWmsWorkflow"]
26import binascii
27import concurrent.futures
28import json
29import logging
30import os
31import re
33import idds.common.utils as idds_utils
34import pandaclient.idds_api
35from idds.doma.workflowv2.domapandawork import DomaPanDAWork
36from idds.workflowv2.workflow import AndCondition
37from idds.workflowv2.workflow import Workflow as IDDS_client_workflow
38from lsst.ctrl.bps.bps_config import BpsConfig
39from lsst.ctrl.bps.panda.idds_tasks import IDDSWorkflowGenerator
40from lsst.ctrl.bps.wms_service import BaseWmsService, BaseWmsWorkflow, WmsRunReport, WmsStates
41from lsst.resources import ResourcePath
43_LOG = logging.getLogger(__name__)
46class PanDAService(BaseWmsService):
47 """PanDA version of WMS service"""
49 def prepare(self, config, generic_workflow, out_prefix=None):
50 """Convert generic workflow to an PanDA iDDS ready for submission
52 Parameters
53 ----------
54 config : `lsst.ctrl.bps.BpsConfig`
55 BPS configuration that includes necessary submit/runtime
56 information.
57 generic_workflow : `lsst.ctrl.bps.GenericWorkflow`
58 out_prefix : `str`
59 The root directory into which all WMS-specific files are written
61 Returns
62 -------
63 workflow : `lsst.ctrl.bps.panda.panda_service.PandaBpsWmsWorkflow`
64 PanDA workflow ready to be run.
65 """
66 _LOG.debug("out_prefix = '%s'", out_prefix)
67 workflow = PandaBpsWmsWorkflow.from_generic_workflow(
68 config, generic_workflow, out_prefix, f"{self.__class__.__module__}." f"{self.__class__.__name__}"
69 )
70 workflow.write(out_prefix)
71 return workflow
73 def convert_exec_string_to_hex(self, cmdline):
74 """Convert the command line into hex representation.
76 This step is currently involved because large blocks of command lines
77 including special symbols passed to the pilot/container. To make sure
78 the 1 to 1 matching and pass by the special symbol stripping
79 performed by the Pilot we applied the hexing.
81 Parameters
82 ----------
83 cmdline : `str`
84 UTF-8 command line string
86 Returns
87 -------
88 hex : `str`
89 Hex representation of string
90 """
91 return binascii.hexlify(cmdline.encode()).decode("utf-8")
93 def add_decoder_prefix(self, cmd_line, distribution_path, files):
94 """
95 Compose the command line sent to the pilot from the functional part
96 (the actual SW running) and the middleware part (containers invocation)
98 Parameters
99 ----------
100 cmd_line : `str`
101 UTF-8 based functional part of the command line
102 distribution_path : `str`
103 URI of path where all files are located for distribution
104 files `list` [`str`]
105 File names needed for a task
107 Returns
108 -------
109 decoder_prefix : `str`
110 Full command line to be executed on the edge node
111 """
113 cmdline_hex = self.convert_exec_string_to_hex(cmd_line)
114 _, decoder_prefix = self.config.search(
115 "runnerCommand", opt={"replaceEnvVars": False, "expandEnvVars": False}
116 )
117 decoder_prefix = decoder_prefix.replace(
118 "_cmd_line_",
119 str(cmdline_hex)
120 + " ${IN/L} "
121 + distribution_path
122 + " "
123 + "+".join(f"{k}:{v}" for k, v in files[0].items())
124 + " "
125 + "+".join(files[1]),
126 )
127 return decoder_prefix
129 def submit(self, workflow):
130 """Submit a single PanDA iDDS workflow
132 Parameters
133 ----------
134 workflow : `lsst.ctrl.bps.BaseWorkflow`
135 A single PanDA iDDS workflow to submit
136 """
137 idds_client_workflow = IDDS_client_workflow(name=workflow.name)
138 files = self.copy_files_for_distribution(
139 workflow.generated_tasks, self.config["fileDistributionEndPoint"]
140 )
141 DAG_end_work = []
142 DAG_final_work = None
144 _, processing_type = self.config.search("processing_type", opt={"default": None})
145 _, task_type = self.config.search("task_type", opt={"default": "test"})
146 _, prod_source_label = self.config.search("prodSourceLabel", opt={"default": None})
147 _, vo = self.config.search("vo", opt={"default": "wlcg"})
149 for idx, task in enumerate(workflow.generated_tasks):
150 work = DomaPanDAWork(
151 executable=self.add_decoder_prefix(
152 task.executable, self.config["fileDistributionEndPoint"], files
153 ),
154 primary_input_collection={
155 "scope": "pseudo_dataset",
156 "name": "pseudo_input_collection#" + str(idx),
157 },
158 output_collections=[
159 {"scope": "pseudo_dataset", "name": "pseudo_output_collection#" + str(idx)}
160 ],
161 log_collections=[],
162 dependency_map=task.dependencies,
163 task_name=task.name,
164 task_queue=task.queue,
165 task_log={
166 "destination": "local",
167 "value": "log.tgz",
168 "dataset": "PandaJob_#{pandaid}/",
169 "token": "local",
170 "param_type": "log",
171 "type": "template",
172 },
173 encode_command_line=True,
174 task_rss=task.max_rss,
175 task_cloud=task.cloud,
176 task_site=task.site,
177 task_priority=int(task.priority) if task.priority else 900,
178 core_count=task.core_count,
179 working_group=task.working_group,
180 processing_type=processing_type,
181 task_type=task_type,
182 prodSourceLabel=prod_source_label if prod_source_label else task.prod_source_label,
183 vo=vo,
184 maxattempt=task.max_attempt,
185 maxwalltime=task.max_walltime if task.max_walltime else 90000,
186 )
188 idds_client_workflow.add_work(work)
189 if task.is_final:
190 DAG_final_work = work
191 if task.is_dag_end:
192 DAG_end_work.append(work)
194 if DAG_final_work:
195 conditions = []
196 for work in DAG_end_work:
197 conditions.append(work.is_terminated)
198 and_cond = AndCondition(conditions=conditions, true_works=[DAG_final_work])
199 idds_client_workflow.add_condition(and_cond)
200 idds_client = self.get_idds_client()
201 ret = idds_client.submit(idds_client_workflow, username=None, use_dataset_name=False)
202 _LOG.debug("iDDS client manager submit returned = %s", ret)
204 # Check submission success
205 status, result, error = self.get_idds_result(ret)
206 if status:
207 request_id = int(result)
208 else:
209 raise RuntimeError(f"Error submitting to PanDA service: {error}")
211 _LOG.info("Submitted into iDDs with request id=%s", request_id)
212 workflow.run_id = request_id
214 @staticmethod
215 def copy_files_for_distribution(tasks, file_distribution_uri):
216 """
217 Brings locally generated files into Cloud for further
218 utilization them on the edge nodes.
220 Parameters
221 ----------
222 local_pfns: `list` of `tasks`
223 Tasks that input files needs to be placed for
224 distribution
225 file_distribution_uri: `str`
226 Path on the edge node accessed storage,
227 including access protocol, bucket name to place files
229 Returns
230 -------
231 files_plc_hldr, direct_IO_files : `dict` [`str`, `str`], `set` of `str`
232 First parameters is key values pairs
233 of file placeholder - file name
234 Second parameter is set of files which will be directly accessed.
235 """
236 local_pfns = {}
237 direct_IO_files = set()
238 for task in tasks:
239 for file in task.files_used_by_task:
240 if not file.delivered:
241 local_pfns[file.name] = file.submission_url
242 if file.direct_IO:
243 direct_IO_files.add(file.name)
245 files_to_copy = {}
247 # In case there are folders we iterate over its content
248 for local_pfn in local_pfns.values():
249 folder_name = os.path.basename(local_pfn)
250 if os.path.isdir(local_pfn):
251 files_in_folder = ResourcePath.findFileResources([local_pfn])
252 for file in files_in_folder:
253 file_name = file.basename()
254 files_to_copy[file] = ResourcePath(
255 os.path.join(file_distribution_uri, folder_name, file_name)
256 )
257 else:
258 files_to_copy[ResourcePath(local_pfn)] = ResourcePath(
259 os.path.join(file_distribution_uri, folder_name)
260 )
262 copy_executor = concurrent.futures.ThreadPoolExecutor(max_workers=10)
263 future_file_copy = []
264 for src, trgt in files_to_copy.items():
266 # S3 clients explicitly instantiate here to overpass this
267 # https://stackoverflow.com/questions/52820971/is-boto3-client-thread-safe
268 trgt.exists()
269 future_file_copy.append(copy_executor.submit(trgt.transfer_from, src, transfer="copy"))
270 for future in concurrent.futures.as_completed(future_file_copy):
271 if not future.result() is None:
272 raise RuntimeError("Error of placing files to the distribution point")
274 if len(direct_IO_files) == 0:
275 direct_IO_files.add("cmdlineplaceholder")
277 files_plc_hldr = {}
278 for file_placeholder, src_path in local_pfns.items():
279 files_plc_hldr[file_placeholder] = os.path.basename(src_path)
280 if os.path.isdir(src_path):
281 # this is needed to make isdir function working
282 # properly in ButlerURL instance on the egde node
283 files_plc_hldr[file_placeholder] += "/"
285 return files_plc_hldr, direct_IO_files
287 def get_idds_client(self):
288 """Get the idds client
290 Returns
291 -------
292 idds_client: `idds.client.clientmanager.ClientManager`
293 iDDS ClientManager object.
294 """
295 idds_server = None
296 if isinstance(self.config, BpsConfig):
297 _, idds_server = self.config.search("iddsServer", opt={"default": None})
298 elif isinstance(self.config, dict) and "iddsServer" in self.config:
299 idds_server = self.config["iddsServer"]
300 # if idds_server is None, a default value on the panda relay service
301 # will be used
302 idds_client = pandaclient.idds_api.get_api(
303 idds_utils.json_dumps, idds_host=idds_server, compress=True, manager=True
304 )
305 return idds_client
307 def get_idds_result(self, ret):
308 """Parse the results returned from iDDS.
310 Parameters
311 ----------
312 ret: `tuple` of (`int`, (`bool`, payload)).
313 The first part ret[0] is the status of PanDA relay service.
314 The part of ret[1][0] is the status of iDDS service.
315 The part of ret[1][1] is the returned payload.
316 If ret[1][0] is False, ret[1][1] can be error messages.
318 Returns
319 -------
320 status: `bool`
321 The status of iDDS calls.
322 result: `int` or `list` or `dict`
323 The result returned from iDDS.
324 error: `str`
325 Error messages.
326 """
327 # https://panda-wms.readthedocs.io/en/latest/client/rest_idds.html
328 if not (isinstance(ret, tuple) or isinstance(ret, list)) or ret[0] != 0:
329 # Something wrong with the PanDA relay service.
330 # The call may not be delivered to iDDS.
331 status = False
332 result = None
333 error = "PanDA relay service returns errors: %s" % str(ret)
334 else:
335 if ret[1][0]:
336 status = True
337 result = ret[1][1]
338 error = None
339 if isinstance(result, str) and "Authentication no permission" in result:
340 status = False
341 result = None
342 error = result
343 else:
344 # iDDS returns errors
345 status = False
346 result = None
347 error = "iDDS returns errors: %s" % str(ret[1][1])
348 return status, result, error
350 def restart(self, wms_workflow_id):
351 """Restart a workflow from the point of failure.
353 Parameters
354 ----------
355 wms_workflow_id : `str`
356 Id that can be used by WMS service to identify workflow that
357 need to be restarted.
359 Returns
360 -------
361 wms_id : `str`
362 Id of the restarted workflow. If restart failed, it will be set
363 to `None`.
364 run_name : `str`
365 Name of the restarted workflow. If restart failed, it will be set
366 to `None`.
367 message : `str`
368 A message describing any issues encountered during the restart.
369 If there were no issue, an empty string is returned.
370 """
371 idds_client = self.get_idds_client()
372 ret = idds_client.retry(request_id=wms_workflow_id)
373 _LOG.debug("Restart PanDA workflow returned = %s", ret)
375 status, result, error = self.get_idds_result(ret)
376 if status:
377 _LOG.info("Restarting PanDA workflow %s", result)
378 return wms_workflow_id, None, json.dumps(result)
379 else:
380 return None, None, "Error retry PanDA workflow: %s" % str(error)
382 def report(self, wms_workflow_id=None, user=None, hist=0, pass_thru=None, is_global=False):
383 """Stub for future implementation of the report method
384 Expected to return run information based upon given constraints.
386 Parameters
387 ----------
388 wms_workflow_id : `int` or `str`
389 Limit to specific run based on id.
390 user : `str`
391 Limit results to runs for this user.
392 hist : `float`
393 Limit history search to this many days.
394 pass_thru : `str`
395 Constraints to pass through to HTCondor.
396 is_global : `bool`, optional
397 If set, all available job queues will be queried for job
398 information. Defaults to False which means that only a local job
399 queue will be queried for information.
401 Returns
402 -------
403 runs : `list` [`lsst.ctrl.bps.WmsRunReport`]
404 Information about runs from given job information.
405 message : `str`
406 Extra message for report command to print. This could be
407 pointers to documentation or to WMS specific commands.
408 """
409 message = ""
410 run_reports = []
412 idds_client = self.get_idds_client()
413 ret = idds_client.get_requests(request_id=wms_workflow_id, with_detail=True)
414 _LOG.debug("PanDA get workflow status returned = %s", str(ret))
416 request_status = ret[0]
417 tasks = ret[1][1]
418 if request_status != 0 or not tasks:
419 raise RuntimeError(f"Error to get workflow status: {ret} for id: {wms_workflow_id}")
420 else:
421 head = tasks[0]
422 wms_report = WmsRunReport(
423 wms_id=str(head["request_id"]),
424 operator=head["username"],
425 project="",
426 campaign="",
427 payload="",
428 run=head["name"],
429 state=WmsStates.UNKNOWN,
430 total_number_jobs=0,
431 job_state_counts={state: 0 for state in WmsStates},
432 job_summary={},
433 run_summary="",
434 )
436 # The status of a task is taken from the first item of state_map.
437 # The workflow is in status WmsStates.FAILED when:
438 # All tasks have failed.
439 # SubFinished tasks has jobs in
440 # output_processed_files: Finished
441 # output_failed_files: Failed
442 # output_missing_files: Missing
443 state_map = {
444 "Finished": [WmsStates.SUCCEEDED],
445 "SubFinished": [
446 WmsStates.SUCCEEDED,
447 WmsStates.FAILED,
448 WmsStates.PRUNED,
449 ],
450 "Transforming": [
451 WmsStates.RUNNING,
452 WmsStates.SUCCEEDED,
453 WmsStates.FAILED,
454 WmsStates.UNREADY,
455 WmsStates.PRUNED,
456 ],
457 "Failed": [WmsStates.FAILED, WmsStates.PRUNED],
458 }
460 file_map = {
461 WmsStates.SUCCEEDED: "output_processed_files",
462 WmsStates.RUNNING: "output_processing_files",
463 WmsStates.FAILED: "output_failed_files",
464 WmsStates.UNREADY: "input_new_files",
465 WmsStates.PRUNED: "output_missing_files",
466 }
468 # workflow status to report as SUCCEEDED
469 wf_status = ["Finished", "SubFinished", "Transforming"]
471 wf_succeed = False
473 tasks.sort(key=lambda x: x["transform_workload_id"])
475 # Loop over all tasks data returned by idds_client
476 for task in tasks:
477 totaljobs = task["output_total_files"]
478 wms_report.total_number_jobs += totaljobs
479 tasklabel = task["transform_name"]
480 tasklabel = re.sub(wms_report.run + "_", "", tasklabel)
481 status = task["transform_status"]["attributes"]["_name_"]
482 taskstatus = {}
483 # Fill number of jobs in all WmsStates
484 for state in WmsStates:
485 njobs = 0
486 # Each WmsState have many iDDS status mapped to it.
487 for mappedstate in state_map[status]:
488 if state in file_map and mappedstate == state:
489 if task[file_map[mappedstate]] is not None:
490 njobs = task[file_map[mappedstate]]
491 if state == WmsStates.RUNNING:
492 njobs += task["output_new_files"] - task["input_new_files"]
493 break
494 wms_report.job_state_counts[state] += njobs
495 taskstatus[state] = njobs
496 wms_report.job_summary[tasklabel] = taskstatus
498 # To fill the EXPECTED column
499 if wms_report.run_summary:
500 wms_report.run_summary += ";"
501 wms_report.run_summary += "%s:%s" % (tasklabel, str(totaljobs))
503 if status in wf_status:
504 wf_succeed = True
505 wms_report.state = state_map[status][0]
507 # All tasks have failed, set the workflow FAILED
508 if not wf_succeed:
509 wms_report.state = WmsStates.FAILED
511 run_reports.append(wms_report)
513 return run_reports, message
515 def list_submitted_jobs(self, wms_id=None, user=None, require_bps=True, pass_thru=None, is_global=False):
516 """Query WMS for list of submitted WMS workflows/jobs.
518 This should be a quick lookup function to create list of jobs for
519 other functions.
521 Parameters
522 ----------
523 wms_id : `int` or `str`, optional
524 Id or path that can be used by WMS service to look up job.
525 user : `str`, optional
526 User whose submitted jobs should be listed.
527 require_bps : `bool`, optional
528 Whether to require jobs returned in list to be bps-submitted jobs.
529 pass_thru : `str`, optional
530 Information to pass through to WMS.
531 is_global : `bool`, optional
532 If set, all available job queues will be queried for job
533 information. Defaults to False which means that only a local job
534 queue will be queried for information.
536 Only applicable in the context of a WMS using distributed job
537 queues (e.g., HTCondor). A WMS with a centralized job queue
538 (e.g. PanDA) can safely ignore it.
540 Returns
541 -------
542 req_ids : `list` [`Any`]
543 Only job ids to be used by cancel and other functions. Typically
544 this means top-level jobs (i.e., not children jobs).
545 """
546 if wms_id is None and user is not None:
547 raise RuntimeError(
548 "Error to get workflow status report: wms_id is required"
549 " and filtering workflows with 'user' is not supported."
550 )
552 idds_client = self.get_idds_client()
553 ret = idds_client.get_requests(request_id=wms_id)
554 _LOG.debug("PanDA get workflows returned = %s", ret)
556 status, result, error = self.get_idds_result(ret)
557 if status:
558 req_ids = [req["request_id"] for req in result]
559 return req_ids
560 else:
561 raise RuntimeError(f"Error list PanDA workflow requests: {error}")
563 def cancel(self, wms_id, pass_thru=None):
564 """Cancel submitted workflows/jobs.
566 Parameters
567 ----------
568 wms_id : `str`
569 ID or path of job that should be canceled.
570 pass_thru : `str`, optional
571 Information to pass through to WMS.
573 Returns
574 -------
575 deleted : `bool`
576 Whether successful deletion or not. Currently, if any doubt or any
577 individual jobs not deleted, return False.
578 message : `str`
579 Any message from WMS (e.g., error details).
580 """
581 idds_client = self.get_idds_client()
582 ret = idds_client.abort(request_id=wms_id)
583 _LOG.debug("Abort PanDA workflow returned = %s", ret)
585 status, result, error = self.get_idds_result(ret)
586 if status:
587 _LOG.info("Aborting PanDA workflow %s", result)
588 return True, json.dumps(result)
589 else:
590 return False, "Error abort PanDA workflow: %s" % str(error)
592 def ping(self, pass_thru=None):
593 """Checks whether PanDA WMS services are up, reachable,
594 and can authenticate if authentication is required.
596 The services to be checked are those needed for submit, report, cancel,
597 restart, but ping cannot guarantee whether jobs would actually run
598 successfully. Any messages should be sent directly to the logger.
600 Parameters
601 ----------
602 pass_thru : `str`, optional
603 Information to pass through to WMS.
605 Returns
606 -------
607 status : `int`
608 0 for success, non-zero for failure
609 message : `str`
610 Any message from WMS (e.g., error details).
611 """
612 idds_client = self.get_idds_client()
613 ret = idds_client.ping()
614 _LOG.debug("Ping PanDA service returned = %s", ret)
616 status, result, error = self.get_idds_result(ret)
617 if status:
618 if "Status" in result and result["Status"] == "OK":
619 return 0, None
620 else:
621 return -1, "Error ping PanDA service: %s" % str(result)
622 else:
623 return -1, "Error ping PanDA service: %s" % str(error)
625 def run_submission_checks(self):
626 """Checks to run at start if running WMS specific submission steps.
628 Any exception other than NotImplementedError will halt submission.
629 Submit directory may not yet exist when this is called.
630 """
631 for key in ["PANDA_URL"]:
632 if key not in os.environ:
633 raise OSError(f"Missing environment variable {key}")
635 status, message = self.ping()
636 if status != 0:
637 raise RuntimeError(message)
640class PandaBpsWmsWorkflow(BaseWmsWorkflow):
641 """A single Panda based workflow
643 Parameters
644 ----------
645 name : `str`
646 Unique name for Workflow
647 config : `lsst.ctrl.bps.BpsConfig`
648 BPS configuration that includes necessary submit/runtime information
649 """
651 def __init__(self, name, config=None):
652 super().__init__(name, config)
653 self.generated_tasks = None
655 @classmethod
656 def from_generic_workflow(cls, config, generic_workflow, out_prefix, service_class):
657 # Docstring inherited from parent class
658 idds_workflow = cls(generic_workflow.name, config)
659 workflow_generator = IDDSWorkflowGenerator(generic_workflow, config)
660 idds_workflow.generated_tasks = workflow_generator.define_tasks()
661 _LOG.debug("panda dag attribs %s", generic_workflow.run_attrs)
662 return idds_workflow
664 def write(self, out_prefix):
665 """Not yet implemented"""