#!/bin/bash
numPartitions=4

APP_NAME=stream.fence.farm.
APP_VERSION=2020.01.17
APP_JAR=../lib/uml-stream-fence-farm-1.0.0.jar
MAIN_CLASS=uml.bigdata.stream.StreamingApp

APP_ID=`yarn application -list |grep ${APP_NAME} |awk '{print $1}'`

if [ "${APP_ID}" != "" ] ; then
  echo `yarn application -kill ${APP_ID}`
fi

echo "Start Spark Application ${APP_NAME}${APP_VERSION} ...."

for jar in ../lib/*.jar
do
  if [ ${jar} != ${APP_JAR} ] ; then
    LIBJARS=${jar},${LIBJARS}
  fi
done

for file in ../resources/*
do
  RESOURCES_FILES=${file},${RESOURCES_FILES}
done

spark-submit \
  --class ${MAIN_CLASS} \
  --master yarn \
  --deploy-mode cluster \
  --jars ${LIBJARS} \
  --files ${RESOURCES_FILES} \
  --conf spark.app.name=${APP_NAME}${APP_VERSION} \
  --conf spark.driver.cores=1 \
  --conf spark.driver.memory=2g \
  --conf spark.driver.maxResultSize=0 \
  --conf spark.executor.cores=1 \
  --conf spark.executor.instances=2 \
  --conf spark.executor.memory=4g \
  --conf spark.executor.extraJavaOptions=" -XX:+UseG1GC" \
  --conf spark.serializer=org.apache.spark.serializer.KryoSerializer \
  --conf spark.dynamicAllocation.enabled=false \
  --conf spark.scheduler.mode=FIFO \
  --conf spark.streaming.concurrentJobs=1 \
  --conf spark.streaming.backpressure.enabled=false \
  --conf spark.streaming.kafka.maxRatePerPartition=100000 \
  --conf spark.streaming.stopGracefullyOnShutdown=true \
  $APP_JAR \
  1000 ${numPartitions}
