<workflow-app name="promote_action_payload_for_otherresearchproduct_table" xmlns="uri:oozie:workflow:0.5">
    <parameters>
        <property>
            <name>activePromoteOtherResearchProductActionPayload</name>
            <description>when true will promote actions with eu.dnetlib.dhp.schema.oaf.OtherResearchProduct payload</description>
        </property>
        <property>
            <name>activePromoteResultActionPayload</name>
            <description>when true will promote actions with eu.dnetlib.dhp.schema.oaf.Result payload</description>
        </property>
        <property>
            <name>inputGraphRootPath</name>
            <description>root location of input materialized graph</description>
        </property>
        <property>
            <name>inputActionPayloadRootPath</name>
            <description>root location of action payloads to promote</description>
        </property>
        <property>
            <name>outputGraphRootPath</name>
            <description>root location for output materialized graph</description>
        </property>
        <property>
            <name>mergeAndGetStrategy</name>
            <description>strategy for merging graph table objects with action payload instances, MERGE_FROM_AND_GET or SELECT_NEWER_AND_GET</description>
        </property>
        <property>
            <name>sparkDriverMemory</name>
            <description>memory for driver process</description>
        </property>
        <property>
            <name>sparkExecutorMemory</name>
            <description>memory for individual executor</description>
        </property>
        <property>
            <name>sparkExecutorCores</name>
            <description>number of cores used by single executor</description>
        </property>
        <property>
            <name>oozieActionShareLibForSpark2</name>
            <description>oozie action sharelib for spark 2.*</description>
        </property>
        <property>
            <name>spark2ExtraListeners</name>
            <value>com.cloudera.spark.lineage.NavigatorAppListener</value>
            <description>spark 2.* extra listeners classname</description>
        </property>
        <property>
            <name>spark2SqlQueryExecutionListeners</name>
            <value>com.cloudera.spark.lineage.NavigatorQueryListener</value>
            <description>spark 2.* sql query execution listeners classname</description>
        </property>
        <property>
            <name>spark2YarnHistoryServerAddress</name>
            <description>spark 2.* yarn history server address</description>
        </property>
        <property>
            <name>spark2EventLogDir</name>
            <description>spark 2.* event log dir location</description>
        </property>
    </parameters>

    <global>
        <job-tracker>${jobTracker}</job-tracker>
        <name-node>${nameNode}</name-node>
        <configuration>
            <property>
                <name>oozie.action.sharelib.for.spark</name>
                <value>${oozieActionShareLibForSpark2}</value>
            </property>
        </configuration>
    </global>

    <start to="DecisionPromoteOtherResearchProductActionPayload"/>

    <kill name="Kill">
        <message>Action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
    </kill>

    <decision name="DecisionPromoteOtherResearchProductActionPayload">
        <switch>
            <case to="PromoteOtherResearchProductActionPayloadForOtherResearchProductTable">
                ${(activePromoteOtherResearchProductActionPayload eq "true") and
                (fs:exists(concat(concat(concat(concat(wf:conf('nameNode'),'/'),wf:conf('inputGraphRootPath')),'/'),'otherresearchproduct')) eq "true") and
                (fs:exists(concat(concat(concat(concat(wf:conf('nameNode'),'/'),wf:conf('inputActionPayloadRootPath')),'/'),'clazz=eu.dnetlib.dhp.schema.oaf.OtherResearchProduct')) eq "true")}
            </case>
            <default to="SkipPromoteOtherResearchProductActionPayloadForOtherResearchProductTable"/>
        </switch>
    </decision>

    <action name="PromoteOtherResearchProductActionPayloadForOtherResearchProductTable">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>PromoteOtherResearchProductActionPayloadForOtherResearchProductTable</name>
            <class>eu.dnetlib.dhp.actionmanager.promote.PromoteActionPayloadForGraphTableJob</class>
            <jar>dhp-actionmanager-${projectVersion}.jar</jar>
            <spark-opts>
                --executor-memory=${sparkExecutorMemory}
                --executor-cores=${sparkExecutorCores}
                --driver-memory=${sparkDriverMemory}
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
            </spark-opts>
            <arg>--inputGraphTablePath</arg><arg>${inputGraphRootPath}/otherresearchproduct</arg>
            <arg>--graphTableClassName</arg><arg>eu.dnetlib.dhp.schema.oaf.OtherResearchProduct</arg>
            <arg>--inputActionPayloadPath</arg><arg>${inputActionPayloadRootPath}/clazz=eu.dnetlib.dhp.schema.oaf.OtherResearchProduct</arg>
            <arg>--actionPayloadClassName</arg><arg>eu.dnetlib.dhp.schema.oaf.OtherResearchProduct</arg>
            <arg>--outputGraphTablePath</arg><arg>${workingDir}/otherresearchproduct</arg>
            <arg>--mergeAndGetStrategy</arg><arg>${mergeAndGetStrategy}</arg>
        </spark>
        <ok to="DecisionPromoteResultActionPayloadForOtherResearchProductTable"/>
        <error to="Kill"/>
    </action>

    <action name="SkipPromoteOtherResearchProductActionPayloadForOtherResearchProductTable">
        <distcp xmlns="uri:oozie:distcp-action:0.2">
            <prepare>
                <delete path="${workingDir}/otherresearchproduct"/>
            </prepare>
            <arg>-pb</arg>
            <arg>${inputGraphRootPath}/otherresearchproduct</arg>
            <arg>${workingDir}/otherresearchproduct</arg>
        </distcp>
        <ok to="DecisionPromoteResultActionPayloadForOtherResearchProductTable"/>
        <error to="Kill"/>
    </action>

    <decision name="DecisionPromoteResultActionPayloadForOtherResearchProductTable">
        <switch>
            <case to="PromoteResultActionPayloadForOtherResearchProductTable">
                ${(activePromoteResultActionPayload eq "true") and
                (fs:exists(concat(concat(concat(concat(wf:conf('nameNode'),'/'),wf:conf('inputActionPayloadRootPath')),'/'),'clazz=eu.dnetlib.dhp.schema.oaf.Result')) eq "true")}
            </case>
            <default to="SkipPromoteResultActionPayloadForOtherResearchProductTable"/>
        </switch>
    </decision>

    <action name="PromoteResultActionPayloadForOtherResearchProductTable">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>PromoteResultActionPayloadForOtherResearchProductTable</name>
            <class>eu.dnetlib.dhp.actionmanager.promote.PromoteActionPayloadForGraphTableJob</class>
            <jar>dhp-actionmanager-${projectVersion}.jar</jar>
            <spark-opts>
                --executor-memory=${sparkExecutorMemory}
                --executor-cores=${sparkExecutorCores}
                --driver-memory=${sparkDriverMemory}
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
                --conf spark.sql.shuffle.partitions=2560
            </spark-opts>
            <arg>--inputGraphTablePath</arg><arg>${workingDir}/otherresearchproduct</arg>
            <arg>--graphTableClassName</arg><arg>eu.dnetlib.dhp.schema.oaf.OtherResearchProduct</arg>
            <arg>--inputActionPayloadPath</arg><arg>${inputActionPayloadRootPath}/clazz=eu.dnetlib.dhp.schema.oaf.Result</arg>
            <arg>--actionPayloadClassName</arg><arg>eu.dnetlib.dhp.schema.oaf.Result</arg>
            <arg>--outputGraphTablePath</arg><arg>${outputGraphRootPath}/otherresearchproduct</arg>
            <arg>--mergeAndGetStrategy</arg><arg>${mergeAndGetStrategy}</arg>
        </spark>
        <ok to="End"/>
        <error to="Kill"/>
    </action>

    <action name="SkipPromoteResultActionPayloadForOtherResearchProductTable">
        <distcp xmlns="uri:oozie:distcp-action:0.2">
            <prepare>
                <delete path="${outputGraphRootPath}/otherresearchproduct"/>
            </prepare>
            <arg>-pb</arg>
            <arg>${workingDir}/otherresearchproduct</arg>
            <arg>${outputGraphRootPath}/otherresearchproduct</arg>
        </distcp>
        <ok to="End"/>
        <error to="Kill"/>
    </action>

    <end name="End"/>
</workflow-app>