<workflow-app xmlns='uri:oozie:workflow:0.5' name='migrate_actions'>
    <parameters>
        <property>
            <name>sourceNN</name>
            <description>the source name node</description>
        </property>
        <property>
            <name>isLookupUrl</name>
            <description>the isLookup service endpoint</description>
        </property>
        <property>
            <name>workingDirectory</name>
            <value>/tmp/actionsets</value>
            <description>working directory</description>
        </property>
        <property>
            <name>distcp_memory_mb</name>
            <value>6144</value>
            <description>memory for distcp copying actionsets from remote cluster</description>
        </property>
        <property>
            <name>distcp_task_timeout</name>
            <value>60000000</value>
            <description>timeout for distcp copying actions from remote cluster</description>
        </property>
        <property>
            <name>distcp_num_maps</name>
            <value>1</value>
            <description>mmaximum number of map tasks used in the distcp process</description>
        </property>
        <property>
            <name>transform_only</name>
            <description>activate tranform-only mode. Only apply transformation step</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>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>mapreduce.job.queuename</name>
                <value>${queueName}</value>
            </property>
            <property>
                <name>oozie.launcher.mapred.job.queue.name</name>
                <value>${oozieLauncherQueueName}</value>
            </property>
        </configuration>
    </global>

    <start to='migrate_actionsets' />

    <action name='migrate_actionsets'>
        <java>
            <main-class>eu.dnetlib.dhp.migration.actions.MigrateActionSet</main-class>
            <java-opt>-Dmapred.task.timeout=${distcp_task_timeout}</java-opt>
            <arg>-is</arg><arg>${isLookupUrl}</arg>
            <arg>-sn</arg><arg>${sourceNN}</arg>
            <arg>-tn</arg><arg>${nameNode}</arg>
            <arg>-w</arg><arg>${workingDirectory}</arg>
            <arg>-nm</arg><arg>${distcp_num_maps}</arg>
            <arg>-mm</arg><arg>${distcp_memory_mb}</arg>
            <arg>-tt</arg><arg>${distcp_task_timeout}</arg>
            <arg>-tr</arg><arg>${transform_only}</arg>
            <capture-output/>
        </java>
        <ok to="transform_actions" />
        <error to="fail" />
    </action>

    <action name="transform_actions">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn</master>
            <mode>cluster</mode>
            <name>transform_actions</name>
            <class>eu.dnetlib.dhp.migration.actions.TransformActions</class>
            <jar>dhp-aggregation-${projectVersion}.jar</jar>
            <spark-opts>
                --executor-cores ${sparkExecutorCores}
                --executor-memory ${sparkExecutorMemory}
                --driver-memory=${sparkDriverMemory}
                --conf spark.extraListeners="com.cloudera.spark.lineage.NavigatorAppListener"
                --conf spark.sql.queryExecutionListeners="com.cloudera.spark.lineage.NavigatorQueryListener"
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
            </spark-opts>
            <arg>-mt</arg><arg>yarn</arg>
            <arg>-is</arg><arg>${isLookupUrl}</arg>
            <arg>--inputPaths</arg><arg>${wf:actionData('migrate_actionsets')['target_paths']}</arg>
        </spark>
        <ok to="end"/>
        <error to="fail"/>
    </action>

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

    <end name="end" />

</workflow-app>