<workflow-app name="read Crossref dump from HDFS" xmlns="uri:oozie:workflow:0.5">
    <parameters>
                <property>
                    <name>crossrefDumpPath</name>
                    <description>the working dir base path</description>
                </property>
        <property>
            <name>inputPathCrossref</name>
            <description>the working dir base path</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>
            <value>2</value>
            <description>number of cores used by single executor</description>
        </property>

    </parameters>

    <start to="GenerateCrossrefDataset"/>

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

    <action name="ImportCrossRef">
        <java>
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <main-class>eu.dnetlib.doiboost.crossref.ExtractCrossrefRecords</main-class>
            <arg>--hdfsServerUri</arg><arg>${nameNode}</arg>
            <arg>--crossrefFileNameTarGz</arg><arg>${crossrefDumpPath}/crossref.tar.gz</arg>
            <arg>--workingPath</arg><arg>${crossrefDumpPath}</arg>
            <arg>--outputPath</arg><arg>${workingDir}/files/</arg>
        </java>
        <ok to="UnpackCrossrefEntries"/>
        <error to="Kill"/>
    </action>

    <action name="UnpackCrossrefEntries">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>SparkGenerateCrossrefDataset</name>
            <class>eu.dnetlib.doiboost.crossref.UnpackCrtossrefEntries</class>
            <jar>dhp-doiboost-${projectVersion}.jar</jar>
            <spark-opts>
                --executor-memory=${sparkExecutorMemory}
                --executor-cores=${sparkExecutorCores}
                --driver-memory=${sparkDriverMemory}
                --conf spark.sql.shuffle.partitions=3840
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
            </spark-opts>
            <arg>--master</arg><arg>yarn-cluster</arg>
            <arg>--sourcePath</arg><arg>${crossrefDumpPath}/files</arg>
            <arg>--targetPath</arg><arg>${inputPathCrossref}/crossref_ds</arg>

        </spark>
        <ok to="GenerateCrossrefDataset"/>
        <error to="Kill"/>
    </action>

    <action name="GenerateCrossrefDataset">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>SparkGenerateCrossrefDataset</name>
            <class>eu.dnetlib.doiboost.crossref.GenerateCrossrefDataset</class>
            <jar>dhp-doiboost-${projectVersion}.jar</jar>
            <spark-opts>
                --executor-memory=${sparkExecutorMemory}
                --executor-cores=${sparkExecutorCores}
                --driver-memory=${sparkDriverMemory}
                --conf spark.sql.shuffle.partitions=3840
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
            </spark-opts>
            <arg>--master</arg><arg>yarn-cluster</arg>
            <arg>--sourcePath</arg><arg>${inputPathCrossref}/crossref_ds</arg>
            <arg>--targetPath</arg><arg>${inputPathCrossref}/crossref_ds_updates</arg>

        </spark>
        <ok to="End"/>
        <error to="Kill"/>
    </action>

<!--    <action name="removeFiles">-->
<!--        <fs>-->
<!--        <delete path="${workingDir}/files"/>-->
<!--        </fs>-->
<!--        <ok to="renameDataset"/>-->
<!--        <error to="Kill"/>-->
<!--    </action>-->

<!--    <action name="renameDataset">-->
<!--        <fs>-->
<!--            <delete path="${inputPathCrossref}/crossref_ds"/>-->
<!--            <move source="${inputPathCrossref}/crossref_ds_updated"-->
<!--                  target="${inputPathCrossref}/crossref_ds"/>-->
<!--        </fs>-->
<!--        <ok to="End"/>-->
<!--        <error to="Kill"/>-->
<!--    </action>-->

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