Search in sources :

Example 6 with WorkflowJob

use of org.apache.oozie.client.WorkflowJob in project oozie by apache.

the class SubWorkflowActionExecutor method start.

public void start(Context context, WorkflowAction action) throws ActionExecutorException {
    LOG.info("Starting action");
    try {
        Element eConf = XmlUtils.parseXml(action.getConf());
        Namespace ns = eConf.getNamespace();
        Element e = eConf.getChild("oozie", ns);
        String oozieUri = (e == null) ? LOCAL : e.getTextTrim();
        OozieClient oozieClient = getWorkflowClient(context, oozieUri);
        String subWorkflowId = null;
        String extId = context.getRecoveryId();
        String runningJobId = null;
        if (extId != null) {
            runningJobId = checkIfRunning(oozieClient, extId);
        }
        if (runningJobId == null) {
            String appPath = eConf.getChild("app-path", ns).getTextTrim();
            XConfiguration subWorkflowConf = new XConfiguration();
            Configuration parentConf = new XConfiguration(new StringReader(context.getWorkflow().getConf()));
            if (eConf.getChild(("propagate-configuration"), ns) != null) {
                XConfiguration.copy(parentConf, subWorkflowConf);
            }
            // Propagate coordinator and bundle info to subworkflow
            if (OozieJobInfo.isJobInfoEnabled()) {
                if (parentConf.get(OozieJobInfo.COORD_ID) != null) {
                    subWorkflowConf.set(OozieJobInfo.COORD_ID, parentConf.get(OozieJobInfo.COORD_ID));
                    subWorkflowConf.set(OozieJobInfo.COORD_NAME, parentConf.get(OozieJobInfo.COORD_NAME));
                    subWorkflowConf.set(OozieJobInfo.COORD_NOMINAL_TIME, parentConf.get(OozieJobInfo.COORD_NOMINAL_TIME));
                }
                if (parentConf.get(OozieJobInfo.BUNDLE_ID) != null) {
                    subWorkflowConf.set(OozieJobInfo.BUNDLE_ID, parentConf.get(OozieJobInfo.BUNDLE_ID));
                    subWorkflowConf.set(OozieJobInfo.BUNDLE_NAME, parentConf.get(OozieJobInfo.BUNDLE_NAME));
                }
            }
            // the proto has the necessary credentials
            Configuration protoActionConf = context.getProtoActionConf();
            XConfiguration.copy(protoActionConf, subWorkflowConf);
            subWorkflowConf.set(OozieClient.APP_PATH, appPath);
            String group = ConfigUtils.getWithDeprecatedCheck(parentConf, OozieClient.JOB_ACL, OozieClient.GROUP_NAME, null);
            if (group != null) {
                subWorkflowConf.set(OozieClient.GROUP_NAME, group);
            }
            injectInline(eConf.getChild("configuration", ns), subWorkflowConf);
            injectCallback(context, subWorkflowConf);
            injectRecovery(extId, subWorkflowConf);
            injectParent(context.getWorkflow().getId(), subWorkflowConf);
            injectSuperParent(context.getWorkflow(), parentConf, subWorkflowConf);
            verifyAndInjectSubworkflowDepth(parentConf, subWorkflowConf);
            // TODO: this has to be refactored later to be done in a single place for REST calls and this
            JobUtils.normalizeAppPath(context.getWorkflow().getUser(), context.getWorkflow().getGroup(), subWorkflowConf);
            subWorkflowConf.set(OOZIE_ACTION_YARN_TAG, getActionYarnTag(parentConf, context.getWorkflow(), action));
            // rerun again.
            if (action.getExternalId() != null && parentConf.getBoolean(OozieClient.RERUN_FAIL_NODES, false)) {
                subWorkflowConf.setBoolean(SUBWORKFLOW_RERUN, true);
                oozieClient.reRun(action.getExternalId(), subWorkflowConf.toProperties());
                subWorkflowId = action.getExternalId();
            } else {
                subWorkflowId = oozieClient.run(subWorkflowConf.toProperties());
            }
        } else {
            subWorkflowId = runningJobId;
        }
        LOG.info("Sub workflow id: [{0}]", subWorkflowId);
        WorkflowJob workflow = oozieClient.getJobInfo(subWorkflowId);
        String consoleUrl = workflow.getConsoleUrl();
        context.setStartData(subWorkflowId, oozieUri, consoleUrl);
        if (runningJobId != null) {
            check(context, action);
        }
    } catch (Exception ex) {
        LOG.error(ex);
        throw convertException(ex);
    }
}
Also used : OozieClient(org.apache.oozie.client.OozieClient) LocalOozieClient(org.apache.oozie.LocalOozieClient) XConfiguration(org.apache.oozie.util.XConfiguration) XConfiguration(org.apache.oozie.util.XConfiguration) Configuration(org.apache.hadoop.conf.Configuration) Element(org.jdom.Element) StringReader(java.io.StringReader) WorkflowJob(org.apache.oozie.client.WorkflowJob) Namespace(org.jdom.Namespace) ActionExecutorException(org.apache.oozie.action.ActionExecutorException) OozieClientException(org.apache.oozie.client.OozieClientException) IOException(java.io.IOException) CommandException(org.apache.oozie.command.CommandException)

Example 7 with WorkflowJob

use of org.apache.oozie.client.WorkflowJob in project oozie by apache.

the class SubWorkflowActionExecutor method check.

public void check(Context context, WorkflowAction action) throws ActionExecutorException {
    try {
        String subWorkflowId = action.getExternalId();
        String oozieUri = action.getTrackerUri();
        OozieClient oozieClient = getWorkflowClient(context, oozieUri);
        WorkflowJob subWorkflow = oozieClient.getJobInfo(subWorkflowId);
        WorkflowJob.Status status = subWorkflow.getStatus();
        switch(status) {
            case FAILED:
            case KILLED:
            case SUCCEEDED:
                context.setExecutionData(status.toString(), null);
                break;
            default:
                context.setExternalStatus(status.toString());
                break;
        }
    } catch (Exception ex) {
        throw convertException(ex);
    }
}
Also used : OozieClient(org.apache.oozie.client.OozieClient) LocalOozieClient(org.apache.oozie.LocalOozieClient) WorkflowJob(org.apache.oozie.client.WorkflowJob) ActionExecutorException(org.apache.oozie.action.ActionExecutorException) OozieClientException(org.apache.oozie.client.OozieClientException) IOException(java.io.IOException) CommandException(org.apache.oozie.command.CommandException)

Example 8 with WorkflowJob

use of org.apache.oozie.client.WorkflowJob in project oozie by apache.

the class TestSubWorkflowActionExecutor method testConfigNotPropagation.

public void testConfigNotPropagation() throws Exception {
    Path subWorkflowAppPath = getFsTestCaseDir();
    FileSystem fs = getFileSystem();
    Path workflowPath = new Path(subWorkflowAppPath, "workflow.xml");
    Writer writer = new OutputStreamWriter(fs.create(workflowPath));
    writer.write(APP1);
    writer.close();
    XConfiguration protoConf = getBaseProtoConf();
    WorkflowJobBean workflow = createBaseWorkflow(protoConf, "W");
    String defaultConf = workflow.getConf();
    XConfiguration newConf = new XConfiguration(new StringReader(defaultConf));
    newConf.set("abc", "xyz");
    workflow.setConf(newConf.toXmlString());
    final WorkflowActionBean action = (WorkflowActionBean) workflow.getActions().get(0);
    action.setConf("<sub-workflow xmlns='uri:oozie:workflow:0.1' name='subwf'>" + "      <app-path>" + workflowPath.toString() + "</app-path>" + "      <configuration>" + "        <property>" + "          <name>a</name>" + "          <value>A</value>" + "        </property>" + "      </configuration>" + "</sub-workflow>");
    SubWorkflowActionExecutor subWorkflow = new SubWorkflowActionExecutor();
    subWorkflow.start(new Context(workflow, action), action);
    final OozieClient oozieClient = subWorkflow.getWorkflowClient(new Context(workflow, action), SubWorkflowActionExecutor.LOCAL);
    waitFor(JOB_TIMEOUT, new Predicate() {

        public boolean evaluate() throws Exception {
            return oozieClient.getJobInfo(action.getExternalId()).getStatus() == WorkflowJob.Status.SUCCEEDED;
        }
    });
    assertEquals(WorkflowJob.Status.SUCCEEDED, oozieClient.getJobInfo(action.getExternalId()).getStatus());
    subWorkflow.check(new Context(workflow, action), action);
    assertEquals(WorkflowAction.Status.DONE, action.getStatus());
    subWorkflow.end(new Context(workflow, action), action);
    assertEquals(WorkflowAction.Status.OK, action.getStatus());
    WorkflowJob wf = oozieClient.getJobInfo(action.getExternalId());
    Configuration childConf = getWorkflowConfig(wf);
    assertNull(childConf.get("abc"));
    assertEquals("A", childConf.get("a"));
}
Also used : Path(org.apache.hadoop.fs.Path) XConfiguration(org.apache.oozie.util.XConfiguration) Configuration(org.apache.hadoop.conf.Configuration) WorkflowJobBean(org.apache.oozie.WorkflowJobBean) WorkflowActionBean(org.apache.oozie.WorkflowActionBean) XConfiguration(org.apache.oozie.util.XConfiguration) OozieClient(org.apache.oozie.client.OozieClient) FileSystem(org.apache.hadoop.fs.FileSystem) WorkflowJob(org.apache.oozie.client.WorkflowJob)

Example 9 with WorkflowJob

use of org.apache.oozie.client.WorkflowJob in project oozie by apache.

the class TestSubWorkflowActionExecutor method testSubWorkflowSuspend.

public void testSubWorkflowSuspend() throws Exception {
    try {
        Path subWorkflowAppPath = getFsTestCaseDir();
        FileSystem fs = getFileSystem();
        Path subWorkflowPath = new Path(subWorkflowAppPath, "workflow.xml");
        Writer writer = new OutputStreamWriter(fs.create(subWorkflowPath));
        writer.write(getLazyWorkflow());
        writer.close();
        String workflowUri = getTestCaseFileUri("workflow.xml");
        String appXml = "<workflow-app xmlns=\"uri:oozie:workflow:0.4\" name=\"workflow\">" + "<start to=\"subwf\"/>" + "<action name=\"subwf\">" + "     <sub-workflow xmlns='uri:oozie:workflow:0.4'>" + "          <app-path>" + subWorkflowAppPath.toString() + "</app-path>" + "     </sub-workflow>" + "     <ok to=\"end\"/>" + "     <error to=\"fail\"/>" + "</action>" + "<kill name=\"fail\">" + "     <message>Sub workflow failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>" + "</kill>" + "<end name=\"end\"/>" + "</workflow-app>";
        writeToFile(appXml, workflowUri);
        LocalOozie.start();
        final OozieClient wfClient = LocalOozie.getClient();
        Properties conf = wfClient.createConfiguration();
        conf.setProperty(OozieClient.APP_PATH, workflowUri);
        conf.setProperty(OozieClient.USER_NAME, getTestUser());
        conf.setProperty("appName", "var-app-name");
        final String jobId = wfClient.submit(conf);
        wfClient.start(jobId);
        waitFor(JOB_TIMEOUT, new Predicate() {

            public boolean evaluate() throws Exception {
                return (wfClient.getJobInfo(jobId).getStatus() == WorkflowJob.Status.RUNNING) && (wfClient.getJobInfo(jobId).getActions().get(1).getStatus() == WorkflowAction.Status.RUNNING);
            }
        });
        WorkflowJob wf = wfClient.getJobInfo(jobId);
        // Suspending subworkflow
        new SuspendXCommand(wf.getActions().get(1).getExternalId()).call();
        // Check suspend for base workflow
        assertEquals(WorkflowJob.Status.SUSPENDED, wfClient.getJobInfo(jobId).getStatus());
        // Check suspend for sub workflow
        assertEquals(WorkflowJob.Status.SUSPENDED, wfClient.getJobInfo(wf.getActions().get(1).getExternalId()).getStatus());
    } finally {
        LocalOozie.stop();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) SuspendXCommand(org.apache.oozie.command.wf.SuspendXCommand) OozieClient(org.apache.oozie.client.OozieClient) FileSystem(org.apache.hadoop.fs.FileSystem) Properties(java.util.Properties) WorkflowJob(org.apache.oozie.client.WorkflowJob)

Example 10 with WorkflowJob

use of org.apache.oozie.client.WorkflowJob in project oozie by apache.

the class TestSubWorkflowActionExecutor method testParentGlobalConfWithConfigDefault.

public void testParentGlobalConfWithConfigDefault() throws Exception {
    try {
        Path subWorkflowAppPath = createSubWorkflowXml();
        createConfigDefaultXml();
        createSubWorkflowConfigDefaultXml();
        String workflowUri = createTestWorkflowXml(subWorkflowAppPath);
        LocalOozie.start();
        final OozieClient wfClient = LocalOozie.getClient();
        Properties conf = wfClient.createConfiguration();
        conf.setProperty(OozieClient.APP_PATH, workflowUri);
        conf.setProperty(OozieClient.USER_NAME, getTestUser());
        conf.setProperty("appName", "var-app-name");
        conf.setProperty("foo", "other");
        final String jobId = wfClient.submit(conf);
        wfClient.start(jobId);
        // configuration should have overridden value
        assertEquals("other", new XConfiguration(new StringReader(wfClient.getJobInfo(jobId).getConf())).get("foo"));
        waitFor(JOB_TIMEOUT, new Predicate() {

            public boolean evaluate() throws Exception {
                return (wfClient.getJobInfo(jobId).getStatus() == WorkflowJob.Status.SUCCEEDED) && (wfClient.getJobInfo(jobId).getActions().get(1).getStatus() == WorkflowAction.Status.OK);
            }
        });
        WorkflowJob subWorkflow = wfClient.getJobInfo(wfClient.getJobInfo(jobId).getActions().get(1).getExternalId());
        Configuration subWorkflowConf = getWorkflowConfig(subWorkflow);
        Element eConf = XmlUtils.parseXml(subWorkflow.getActions().get(1).getConf());
        Element element = eConf.getChild("configuration", eConf.getNamespace());
        Configuration actionConf = new XConfiguration(new StringReader(XmlUtils.prettyPrint(element).toString()));
        // configuration in subWorkflow should have overridden value
        assertEquals("other", subWorkflowConf.get("foo"));
        assertEquals("foo1", actionConf.get("foo1"));
        assertEquals("subconf", actionConf.get("foo2"));
        assertEquals("foo3", actionConf.get("foo3"));
        // Checking the action conf configuration.
        assertEquals("actionconf", subWorkflowConf.get("foo3"));
        assertEquals("subactionconf", actionConf.get("foo4"));
        // config defaults are present
        assertEquals("default", subWorkflowConf.get("parentConfigDefault"));
        assertEquals("default", actionConf.get("subwfConfigDefault"));
    } finally {
        LocalOozie.stop();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) OozieClient(org.apache.oozie.client.OozieClient) XConfiguration(org.apache.oozie.util.XConfiguration) XConfiguration(org.apache.oozie.util.XConfiguration) Configuration(org.apache.hadoop.conf.Configuration) Element(org.jdom.Element) Properties(java.util.Properties) WorkflowJob(org.apache.oozie.client.WorkflowJob)

Aggregations

WorkflowJob (org.apache.oozie.client.WorkflowJob)35 OozieClient (org.apache.oozie.client.OozieClient)18 Path (org.apache.hadoop.fs.Path)14 Properties (java.util.Properties)12 FileSystem (org.apache.hadoop.fs.FileSystem)12 Configuration (org.apache.hadoop.conf.Configuration)11 XConfiguration (org.apache.oozie.util.XConfiguration)11 IOException (java.io.IOException)10 Writer (java.io.Writer)6 WorkflowAction (org.apache.oozie.client.WorkflowAction)6 OutputStreamWriter (java.io.OutputStreamWriter)5 WorkflowActionBean (org.apache.oozie.WorkflowActionBean)5 ActionExecutorException (org.apache.oozie.action.ActionExecutorException)5 OozieClientException (org.apache.oozie.client.OozieClientException)5 Reader (java.io.Reader)4 StringReader (java.io.StringReader)4 WorkflowJobBean (org.apache.oozie.WorkflowJobBean)4 Element (org.jdom.Element)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 File (java.io.File)2