<workflow-app name="Orcid Updates Download" xmlns="uri:oozie:workflow:0.5">
    <parameters>
        <property>
            <name>spark2UpdateStepMaxExecutors</name>
            <value>50</value>
        </property>
        <property>
            <name>workingPath</name>
            <description>the working dir base path</description>
        </property>
        <property>
            <name>oozie.action.sharelib.for.java</name>
            <value>spark2</value>
        </property>
        <property>
            <name>oozie.launcher.mapreduce.user.classpath.first</name>
            <value>true</value>
        </property>
        <property>
            <name>oozie.launcher.mapreduce.map.java.opts</name>
            <value>-Xmx4g</value>
        </property>
        <property>
            <name>token</name>
            <description>access token</description>
        </property>
        <property>
            <name>sparkDriverMemory</name>
            <value>7G</value>
            <description>memory for driver process</description>
        </property>
        <property>
            <name>sparkExecutorMemory</name>
            <value>4G</value>
            <description>memory for individual executor</description>
        </property>
        <property>
            <name>sparkExecutorCores</name>
            <value>1</value>
            <description>number of cores used by single executor</description>
        </property>
        <property>
            <name>spark2DownloadingMaxExecutors</name>
            <value>10</value>
        </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="ResetLambda"/>

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

    <action name="ResetLambda">
        <fs>
            <delete path='${workingPath}/last_modified.csv.tar'/>
            <delete path='${workingPath}/last_modified.seq'/>
        </fs>
        <ok to="DownloadLambdaFile"/>
        <error to="Kill"/>
    </action>

    <action name="DownloadLambdaFile">
        <shell xmlns="uri:oozie:shell-action:0.1">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <exec>get_orcid_lambda_file.sh</exec>
            <file>get_orcid_lambda_file.sh</file>
            <capture-output/>
        </shell>
        <ok to="GenLastModifiedSeq"/>
        <error to="Kill"/>
    </action>

    <action name="GenLastModifiedSeq">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>GenLastModifiedSeq</name>
            <class>eu.dnetlib.doiboost.orcid.SparkGenLastModifiedSeq</class>
            <jar>dhp-doiboost-${projectVersion}.jar</jar>
            <spark-opts>
                --executor-memory=${sparkExecutorMemory}
                --executor-cores=${sparkExecutorCores}
                --driver-memory=${sparkDriverMemory}
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
            </spark-opts>
            <arg>-w</arg><arg>${workingPath}/</arg>
            <arg>-n</arg><arg>${nameNode}</arg>
            <arg>-f</arg><arg>last_modified.csv.tar</arg>
            <arg>-o</arg><arg>last_modified.seq</arg>
            <arg>-t</arg><arg>-</arg>
        </spark>
        <ok to="ResetDownloads"/>
        <error to="Kill"/>
    </action>

    <action name="ResetDownloads">
        <fs>
            <delete path='${workingPath}/downloads/updated_authors'/>
            <delete path='${workingPath}/downloads/updated_works'/>
        </fs>
        <ok to="DownloadOrcidAuthors"/>
        <error to="Kill"/>
    </action>

    <action name="DownloadOrcidAuthors">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>DownloadOrcidAuthors</name>
            <class>eu.dnetlib.doiboost.orcid.SparkDownloadOrcidAuthors</class>
            <jar>dhp-doiboost-${projectVersion}.jar</jar>
            <spark-opts>
                --conf spark.dynamicAllocation.enabled=true
                --conf spark.dynamicAllocation.maxExecutors=${spark2DownloadingMaxExecutors}
                --executor-memory=${sparkExecutorMemory}
                --driver-memory=${sparkDriverMemory}
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
            </spark-opts>
            <arg>-w</arg><arg>${workingPath}/</arg>
            <arg>-n</arg><arg>${nameNode}</arg>
            <arg>-f</arg><arg>last_modified.seq</arg>
            <arg>-o</arg><arg>downloads/updated_authors</arg>
            <arg>-t</arg><arg>${token}</arg>
        </spark>
        <ok to="DownloadOrcidWorks"/>
        <error to="Kill"/>
    </action>

    <action name="DownloadOrcidWorks">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>DownloadOrcidWorks</name>
            <class>eu.dnetlib.doiboost.orcid.SparkDownloadOrcidWorks</class>
            <jar>dhp-doiboost-${projectVersion}.jar</jar>
            <spark-opts>
                --conf spark.dynamicAllocation.enabled=true
                --conf spark.dynamicAllocation.maxExecutors=${spark2DownloadingMaxExecutors}
                --executor-memory=${sparkExecutorMemory}
                --driver-memory=${sparkDriverMemory}
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
            </spark-opts>
            <arg>-w</arg><arg>${workingPath}/</arg>
            <arg>-n</arg><arg>${nameNode}</arg>
            <arg>-f</arg><arg>-</arg>
            <arg>-o</arg><arg>downloads/updated_works</arg>
            <arg>-t</arg><arg>${token}</arg>
        </spark>
        <ok to="ResetNewAuthors"/>
        <error to="Kill"/>
    </action>

    <action name="ResetNewAuthors">
        <fs>
            <delete path='${workingPath}/orcid_dataset/new_authors'/>
        </fs>
        <ok to="UpdateOrcidAuthors"/>
        <error to="Kill"/>
    </action>

    <action name="UpdateOrcidAuthors">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>UpdateOrcidAuthors</name>
            <class>eu.dnetlib.doiboost.orcid.SparkUpdateOrcidAuthors</class>
            <jar>dhp-doiboost-${projectVersion}.jar</jar>
            <spark-opts>
                --conf spark.dynamicAllocation.enabled=true
                --conf spark.dynamicAllocation.maxExecutors=${spark2UpdateStepMaxExecutors}
                --executor-memory=${sparkExecutorMemory}
                --driver-memory=${sparkDriverMemory}
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
            </spark-opts>
            <arg>-w</arg><arg>${workingPath}/</arg>
            <arg>-n</arg><arg>${nameNode}</arg>
            <arg>-f</arg><arg>-</arg>
            <arg>-o</arg><arg>-</arg>
            <arg>-t</arg><arg>-</arg>
        </spark>
        <ok to="ResetNewWorks"/>
        <error to="Kill"/>
    </action>

    <action name="ResetNewWorks">
        <fs>
            <delete path='${workingPath}/orcid_dataset/new_works'/>
        </fs>
        <ok to="UpdateOrcidWorks"/>
        <error to="Kill"/>
    </action>

    <action name="UpdateOrcidWorks">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>UpdateOrcidWorks</name>
            <class>eu.dnetlib.doiboost.orcid.SparkUpdateOrcidWorks</class>
            <jar>dhp-doiboost-${projectVersion}.jar</jar>
            <spark-opts>
                --conf spark.dynamicAllocation.enabled=true
                --conf spark.dynamicAllocation.maxExecutors=${spark2UpdateStepMaxExecutors}
                --executor-memory=${sparkExecutorMemory}
                --driver-memory=${sparkDriverMemory}
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
            </spark-opts>
            <arg>-w</arg><arg>${workingPath}/</arg>
            <arg>-n</arg><arg>${nameNode}</arg>
            <arg>-f</arg><arg>-</arg>
            <arg>-o</arg><arg>-</arg>
            <arg>-t</arg><arg>-</arg>
        </spark>
        <ok to="promoteOrcidAuthorsDataset"/>
        <error to="Kill"/>
    </action>

    <action name="promoteOrcidAuthorsDataset">
        <distcp xmlns="uri:oozie:distcp-action:0.2">
            <prepare>
                <delete path="${workingPath}/orcid_dataset/authors"/>
                <mkdir path="${workingPath}/orcid_dataset/authors"/>
            </prepare>
            <arg>${workingPath}/orcid_dataset/new_authors/*</arg>
            <arg>${workingPath}/orcid_dataset/authors</arg>
        </distcp>
        <ok to="promoteOrcidWorksDataset"/>
        <error to="Kill"/>
    </action>

    <action name="promoteOrcidWorksDataset">
        <distcp xmlns="uri:oozie:distcp-action:0.2">
            <prepare>
                <delete path="${workingPath}/orcid_dataset/works"/>
                <mkdir path="${workingPath}/orcid_dataset/works"/>
            </prepare>
            <arg>${workingPath}/orcid_dataset/new_works/*</arg>
            <arg>${workingPath}/orcid_dataset/works</arg>
        </distcp>
        <ok to="updateLastOrcidAuthorsDataset"/>
        <error to="Kill"/>
    </action>

    <action name="updateLastOrcidAuthorsDataset">
        <distcp xmlns="uri:oozie:distcp-action:0.2">
            <prepare>
                <delete path="${workingPath}/last_orcid_dataset/authors"/>
                <mkdir path="${workingPath}/last_orcid_dataset/authors"/>
            </prepare>
            <arg>${workingPath}/orcid_dataset/authors/*</arg>
            <arg>${workingPath}/last_orcid_dataset/authors</arg>
        </distcp>
        <ok to="updateLastOrcidWorksDataset"/>
        <error to="Kill"/>
    </action>

    <action name="updateLastOrcidWorksDataset">
        <distcp xmlns="uri:oozie:distcp-action:0.2">
            <prepare>
                <delete path="${workingPath}/last_orcid_dataset/works"/>
                <mkdir path="${workingPath}/last_orcid_dataset/works"/>
            </prepare>
            <arg>${workingPath}/orcid_dataset/works/*</arg>
            <arg>${workingPath}/last_orcid_dataset/works</arg>
        </distcp>
        <ok to="End"/>
        <error to="Kill"/>
    </action>

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