#!/bin/bash

source ../dmp_env.sh

LOG_TIME=$(date +%Y%m%d -d "-1 day $ScheduleTime")
year=${LOG_TIME:0:4}
month=${LOG_TIME:4:2}
day=${LOG_TIME:6:2}

YESTERDAY=$(date -d "$ScheduleTime 2 days ago" "+%Y%m%d")
old_year=${YESTERDAY:0:4}
old_month=${YESTERDAY:4:2}
old_day=${YESTERDAY:6:2}

business="dsp_req_unmatch"

DAILY_INPUT="${UNMATCH_DATA_PATH}/${year}/${month}/${day}/dsp"
check_await "${DAILY_INPUT}/_SUCCESS"

sleep 60

mount_partition "etl_dsp_request_unmatch" "dt='$LOG_TIME'" "$DAILY_INPUT"

sleep 20

OUTPUT="${DMP_INSTALL_LIST}/${year}/${month}/${day}/${business}"

expire_date=$(date +%Y%m%d -d "-4 day $LOG_TIME")
expire_date_path=$(date +"%Y/%m/%d" -d "-4 day ${LOG_TIME}")
EXPIRE_OUTPUT_PATH="${DMP_INSTALL_LIST}/${expire_date_path}/${business}"

spark-submit --class mobvista.dmp.common.InstallListLogic \
     --name "DmpInstallList.${business}.${LOG_TIME}" \
     --conf spark.sql.shuffle.partitions=1000 \
     --conf spark.default.parallelism=1000 \
     --conf spark.speculation=true \
     --conf spark.speculation.quantile=0.9 \
     --conf spark.speculation.multiplier=1.2 \
     --conf spark.kryoserializer.buffer.max=256m \
     --conf spark.executor.extraJavaOptions="-XX:+UseG1GC" \
     --conf spark.sql.files.maxPartitionBytes=536870912 \
     --files ${HIVE_SITE_PATH} \
     --jars ${SPARK_HOME}/auxlib/Common-SerDe-1.0-SNAPSHOT.jar \
     --master yarn --deploy-mode cluster --executor-memory 4g --driver-memory 4g  --executor-cores 2  --num-executors 20 \
     ../${JAR} -date ${LOG_TIME} -business ${business} -output ${OUTPUT} -coalesce 100

if [[ $? -ne 0 ]];then
  exit 255
fi

mount_partition "dmp_install_list" "dt='$LOG_TIME', business='$business'" "$OUTPUT"

# 删除过期的分区及删除对应路径
unmount_partition "dmp_install_list" "dt='${expire_date}', business='${business}'" "${EXPIRE_OUTPUT_PATH}"