def _parse_action_node(self, action_node): """ Action nodes are the mechanism by which a workflow triggers the execution of a computation/processing task. Action nodes are required to have an action-choice (map-reduce, etc.), ok, and error node in the xml. """ # The 0th element of the node is the actual action tag. # In the form of 'action' action_name = action_node[0].tag if action_name not in self.ACTION_MAP: action_name = 'unknown' map_class = self.ACTION_MAP[action_name] operator = map_class(action_node[0], action_node.attrib['name'], params=self.PARAMS) p_node = parsed_node.ParsedNode(operator) p_node.add_downstream_node_name(action_node[1].attrib['to']) p_node.set_error_node_name(action_node[2].attrib['to']) logging.info("Parsed {} as Action Node of type {}.".format( operator.task_id, action_name)) self.DEPENDENCIES.update(operator.required_imports()) # TODO A hacky way to get the correct control flow for now, fix if operator.has_prepare(): print(operator.task_id) self.OPERATORS[operator.task_id] = ParsedNode( mappers.null_mapper.NullMapper(operator.task_id)) self.OPERATORS[operator.get_task_id()] = p_node
def parse_join_node(self, join_node): """ Join nodes wait for the corresponding beginning fork node paths to finish. As the parser we are assuming the Oozie workflow follows the schema perfectly. """ map_class = self.control_map["join"] mapper = map_class(oozie_node=join_node, name=join_node.attrib["name"]) p_node = ParsedNode(mapper) p_node.add_downstream_node_name(join_node.attrib["to"]) mapper.on_parse_node() logging.info(f"Parsed {mapper.name} as Join Node.") self.workflow.nodes[join_node.attrib["name"]] = p_node self.workflow.dependencies.update(mapper.required_imports())
def test_write_operators(self): node = ParsedNode( dummy_mapper.DummyMapper(oozie_node=Element("test"), name="task1")) nodes = {"task1": node} file = io.StringIO() self.converter.write_nodes(file=file, nodes=nodes, indent=0) file.seek(0) self.assertEqual(node.mapper.convert_to_text(), file.read())
def parse_fork_node(self, root, fork_node): """ Fork nodes need to be dummy operators with multiple parallel downstream tasks. This parses the fork node, the action nodes that it references and then the join node at the end. This will only parse well-formed xml-adhering workflows where all paths end at the join node. """ map_class = self.control_map["fork"] fork_name = fork_node.attrib["name"] mapper = map_class(oozie_node=fork_node, name=fork_name) p_node = ParsedNode(mapper) mapper.on_parse_node() logging.info(f"Parsed {mapper.name} as Fork Node.") paths = [] for node in fork_node: if "path" in node.tag: # Parse all the downstream tasks that can run in parallel. curr_name = node.attrib["start"] paths.append(utils.xml_utils.find_node_by_name( root, curr_name)) self.workflow.nodes[fork_name] = p_node self.workflow.dependencies.update(mapper.required_imports()) for path in paths: p_node.add_downstream_node_name(path.attrib["name"]) logging.info( f"Added {mapper.name}'s downstream: {path.attrib['name']}") # Theoretically these will all be action nodes, however I don't # think that is guaranteed. # The end of the execution path has not been reached self.parse_node(root, path) if path.attrib["name"] not in self.workflow.nodes: root.remove(path)
def parse_end_node(self, end_node): """ Upon reaching the end node, the workflow is considered completed successfully. Thus it gets mapped to a dummy node that always completes. """ map_class = self.control_map["end"] mapper = map_class(oozie_node=end_node, name=end_node.attrib["name"]) p_node = ParsedNode(mapper) mapper.on_parse_node() logging.info(f"Parsed {mapper.name} as End Node.") self.workflow.nodes[end_node.attrib["name"]] = p_node self.workflow.dependencies.update(mapper.required_imports())
def parse_start_node(self, start_node): """ The start node is the entry point for a workflow job, it indicates the first workflow node the workflow job must transition to. When a workflow is started, it automatically transitions to the node specified in the start. A workflow definition must have one start node. """ map_class = self.control_map["start"] # Theoretically this could cause conflicts, but it is very unlikely start_name = "start_node_" + str(uuid.uuid4())[:4] mapper = map_class(oozie_node=start_node, name=start_name) p_node = ParsedNode(mapper) p_node.add_downstream_node_name(start_node.attrib["to"]) mapper.on_parse_node() logging.info(f"Parsed {mapper.name} as Start Node.") self.workflow.nodes[start_name] = p_node self.workflow.dependencies.update(mapper.required_imports())
def parse_decision_node(self, decision_node): """ A decision node enables a workflow to make a selection on the execution path to follow. The behavior of a decision node can be seen as a switch-case statement. A decision node consists of a list of predicates-transition pairs plus a default transition. Predicates are evaluated in order or appearance until one of them evaluates to true and the corresponding transition is taken. If none of the predicates evaluates to true the default transition is taken. example oozie wf decision node: <decision name="[NODE-NAME]"> <switch> <case to="[NODE_NAME]">[PREDICATE]</case> ... <case to="[NODE_NAME]">[PREDICATE]</case> <default to="[NODE_NAME]"/> </switch> </decision> """ map_class = self.control_map["decision"] mapper = map_class(oozie_node=decision_node, name=decision_node.attrib["name"]) p_node = ParsedNode(mapper) for cases in decision_node[0]: p_node.add_downstream_node_name(cases.attrib["to"]) mapper.on_parse_node() logging.info(f"Parsed {mapper.name} as Decision Node.") self.workflow.nodes[decision_node.attrib["name"]] = p_node self.workflow.dependencies.update(mapper.required_imports())
def parse_kill_node(self, kill_node: ET.Element): """ When a workflow node reaches the `kill` node, it finishes in an error. A workflow definition may have zero or more kill nodes. """ map_class = self.control_map["kill"] mapper = map_class(oozie_node=kill_node, name=kill_node.attrib["name"], trigger_rule=TriggerRule.ONE_FAILED) p_node = ParsedNode(mapper) mapper.on_parse_node() logging.info(f"Parsed {mapper.name} as Kill Node.") self.workflow.nodes[kill_node.attrib["name"]] = p_node self.workflow.dependencies.update(mapper.required_imports())
def parse_action_node(self, action_node: ET.Element): """ Action nodes are the mechanism by which a workflow triggers the execution of a computation/processing task. Action nodes are required to have an action-choice (map-reduce, etc.), ok, and error node in the xml. """ # The 0th element of the node is the actual action tag. # In the form of 'action' action_name = action_node[0].tag if action_name not in self.action_map: action_name = "unknown" map_class = self.action_map[action_name] mapper = map_class( oozie_node=action_node[0], name=action_node.attrib["name"], params=self.params, dag_name=self.workflow.dag_name, input_directory_path=self.workflow.input_directory_path, output_directory_path=self.workflow.output_directory_path, ) p_node = ParsedNode(mapper) ok_node = action_node.find("ok") if ok_node is None: raise Exception("Missing ok node in {}".format(action_node)) p_node.add_downstream_node_name(ok_node.attrib["to"]) error_node = action_node.find("error") if error_node is None: raise Exception("Missing error node in {}".format(action_node)) p_node.set_error_node_name(error_node.attrib["to"]) mapper.on_parse_node() logging.info( f"Parsed {mapper.name} as Action Node of type {action_name}.") self.workflow.dependencies.update(mapper.required_imports()) self.workflow.nodes[mapper.name] = p_node