#!/bin/bash

conf=application.properties

durations=5000

APP_NAME=stream.alarm.ne_

APP_VERSION=2019.10.10

JAR_PATH=../lib/uml-stream-alarm-ne-1.0.0.jar

MAIN_CLASS=com.uml.tech.spark.StreamApp

executor_cores=1

num_executors=2

executor_memory=6G

option="-XX:MaxTenuringThreshold=10 -XX:ParallelGCThreads=2 -XX:SurvivorRatio=6 -XX:+UseConcMarkSweepGC"
dOption="-XX:+UseG1GC -XX:+UseStringDeduplication"

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

config=../resources/${conf}

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

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

echo "start submit spark application ......"

spark-submit \
  --master yarn \
  --deploy-mode cluster \
  --class ${MAIN_CLASS} \
  --conf spark.app.name=${APP_NAME}${APP_VERSION} \
  --conf spark.driver.memory=3G \
  --conf spark.driver.cores=1 \
  --conf spark.ui.retainedJobs=50 \
  --conf spark.ui.retailedStages=50 \
  --conf spark.ui.timeline.tasks.maximum=50 \
  --conf spark.sql.ui.retainedExecutions=50 \
  --conf spark.streaming.ui.retainedBatches=50 \
  --conf spark.yarn.maxAppAttempts=10 \
  --conf spark.streaming.concurrentJobs=1 \
  --conf spark.streaming.stopGracefullyOnShutdown=true \
  --conf spark.executor.extraJavaOptions="${dOption}" \
  --conf spark.streaming.kafka.maxRatePerPartition=100000 \
  --conf spark.dynamicAllocation.enabled=false \
  --jars ${LIBJARS} \
  --files ${config} \
  --conf spark.serializer=org.apache.spark.serializer.KryoSerializer \
  --driver-java-options "${option}" \
  --executor-cores ${executor_cores} \
  --num-executors ${num_executors} \
  --executor-memory ${executor_memory} \
  ${JAR_PATH} \
  ${durations}
