#!/bin/bash -e
#
# Apache v2 license
# Copyright (C) 2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#

OPTION=${1:-cassandra_user_gated}
PLATFROM=${PLATFORM:-SPR}
WORKLOAD=${WORKLOAD:-cassandra}
BACKEND=${BACKEND:-kubernetes}
HOST_NUM=${HOST_NUM:-1} #This is for cluster-config.yaml.m4, no need to config by user
HOST_NETWORK_ENABLE=${HOST_NETWORK_ENABLE:-false}
NETWORK_RPS_TUNE_ENABLE=${NETWORK_RPS_TUNE_ENABLE:-false}
JDK_VERSION=${JDK_VERSION:-JDK11} # JDK11,JDK14
CLIENT_DURATION=${CLIENT_DURATION:-10m}
CASSANDRA_FILL_DATA=${CASSANDRA_FILL_DATA:-true}

if [[ "${JDK_VERSION}" =~ JDK14 ]]; then
#    CLIENT_DURATION=${CLIENT_DURATION:-20m} 
    INSTANCE_NUM=${INSTANCE_NUM:-8}
    CLIENT_POP_MAX=${CLIENT_POP_MAX:-380000000} #380million 
    CASSANDRA_CONCURENT_READS=${CASSANDRA_CONCURENT_READS:-84} 
    CASSANDRA_CONCURENT_WRITES=${CASSANDRA_CONCURENT_WRITES:-28}
else
#    CLIENT_DURATION=${CLIENT_DURATION:-10m}
    INSTANCE_NUM=${INSTANCE_NUM:-4}
    CLIENT_POP_MAX=${CLIENT_POP_MAX:-100}
    CASSANDRA_CONCURENT_READS=${CASSANDRA_CONCURENT_READS:-256}
    CASSANDRA_CONCURENT_WRITES=${CASSANDRA_CONCURENT_WRITES:-32}
fi

#cassandra client parameters
CLIENT_CL=${CLIENT_CL:-ONE}
CLIENT_THREADS=${CLIENT_THREADS:-256}
CLIENT_POP_MIN=${CLIENT_POP_MIN:-1}
CLIENT_POP_MAX_PERFORMANCE_DIV=${CLIENT_POP_MAX_PERFORMANCE_DIV:-1} # POP_MAX in performance = CLIENT_POP_MAX / CLIENT_POP_MAX_PERFORMANCE_DIV
CLIENT_INSERT=${CLIENT_INSERT:-20}
CLIENT_SIMPLE=${CLIENT_SIMPLE:-80}
STRESS_NUM_PER_INSTANCE=${STRESS_NUM_PER_INSTANCE:-1}
DATA_COMPACTION=${DATA_COMPACTION:-SizeTieredCompactionStrategy}
DATA_COMPRESSION=${DATA_COMPRESSION:-LZ4Compressor}
DATA_CHUNK_SIZE=${DATA_CHUNK_SIZE:-16}
REPLICATE_NUM=${REPLICATE_NUM:-1} #for cluster mode
DEBUG_MODE=${DEBUG_MODE:=0}
CLIENT_NODE_NUM=${CLIENT_NODE_NUM:-1}
#kubernets Pod QoS : Guaranteed,Burstable,BestEffort
SERVER_POD_LEVEL=${SERVER_POD_LEVEL:-Burstable}
SERVER_POD_MEMORY=${SERVER_POD_MEMORY:-30Gi} #Guaranteed used
SERVER_POD_CPU=${SERVER_POD_CPU:-1}
SERVER_POD_NUM=${SERVER_POD_NUM:-1}
CLIENT_POD_LEVEL=${CLIENT_POD_LEVEL:-Burstable}
CLIENT_POD_MEMORY=${CLIENT_POD_MEMORY:-30Gi} #Guaranteed used
CLIENT_POD_CPU=${CLIENT_POD_CPU:-1}

#cassandra server parameters
DEPLOY_MODE=${DEPLOY_MODE:-standalone} #standalone | cluster
CLUSTER_ON_SINGLE_NODE=${CLUSTER_ON_SINGLE_NODE:-false}
NODE_NUM=${NODE_NUM:-1} #only for cluster
CASSANDRA_DISK_MOUNT=${CASSANDRA_DISK_MOUNT:-false}
CASSANDRA_NUMACTL_ENABLE=${CASSANDRA_NUMACTL_ENABLE:-true}
CASSANDRA_ENDPOINT_SNITCH=${CASSANDRA_ENDPOINT_SNITCH:-SimpleSnitch} #default value in cassandra.yaml
CASSANDRA_NUM_TOKENS=${CASSANDRA_NUM_TOKENS:-16} #default value in cassandra.yaml
EPHEMERAL_STORAGE_SIZE=${EPHEMERAL_STORAGE_SIZE:-250Gi}
NUMA_OPTIONS=${NUMA_OPTIONS:-} #same as the paramerers in command numactl

# It is recommended to set min (-Xms) and max (-Xmx) heap sizes to
# the same value to avoid stop-the-world GC pauses during resize, and
# so that we can lock the heap in memory on startup to prevent any
# of it from being swapped out. 
#If exceeds free size, it will adjust before configure into cassandra
JVM_HEAP_SIZE=${JVM_HEAP_SIZE:-64} #GB
JVM_GC_TYPE=${JVM_GC_TYPE:-+UseG1GC}

# Logs Setting
DIR="$( cd "$( dirname "$0" )" &> /dev/null && pwd )"
. "$DIR/../../script/overwrite.sh"

if [[ "${TESTCASE}" =~ ^test.*_gated$ ]]; then
    CLIENT_DURATION=1m
    CLIENT_THREADS=10
    CLIENT_POD_LEVEL="BestEffort"
    INSTANCE_NUM=1
    DEPLOY_MODE="standalone"
    NODE_NUM=1
    HOST_NUM=1
    CLIENT_NODE_NUM=1 
    CLIENT_POP_MAX=100
    CLIENT_POP_MAX_DIV=1000
    STRESS_NUM_PER_INSTANCE=1
fi

TESTCASE_ORDER=${TESTCASE_ORDER:-}
if [[ "${TESTCASE}" =~ ^test_.*_jdk11-* ]]; then
    JDK_VERSION="JDK11"    
fi
if [[ "${TESTCASE}" =~ ^test_.*_jdk14-* ]]; then
    JDK_VERSION="JDK14"    
fi
DEPLOY_MODE="standalone"
if [[ "${TESTCASE}" =~ ^test_.*is_server_up$ ]]; then 
    TESTCASE_ORDER=1
    CLIENT_DURATION=1m
    CLIENT_THREADS=10    
    INSTANCE_NUM=1   
    NODE_NUM=1
    HOST_NUM=1
    CLIENT_NODE_NUM=1 
    CLIENT_POP_MAX=100
    CLIENT_POP_MAX_DIV=1000
    STRESS_NUM_PER_INSTANCE=1
fi

if [[ "${TESTCASE}" =~ ^test_.*is_chunk_length_correct$ ]]; then
    TESTCASE_ORDER=2
    CLIENT_DURATION=1m
    CLIENT_THREADS=10    
    INSTANCE_NUM=1   
    NODE_NUM=1
    HOST_NUM=1
    CLIENT_NODE_NUM=1 
    CLIENT_POP_MAX=100
    CLIENT_POP_MAX_DIV=1000
    STRESS_NUM_PER_INSTANCE=1
fi

case $PLATFORM in
    ARMv8 | ARMv9 )
        IMAGE_ARCH="-arm64"
        ;;
    MILAN | ROME )
        IMAGE_ARCH=""
        ;; 
    * )
        IMAGE_ARCH=""
        ;;       
esac


# Workload Setting
WORKLOAD_PARAMS=(
    INSTANCE_NUM CASSANDRA_CONCURENT_READS CASSANDRA_CONCURENT_WRITES
    CASSANDRA_NUMACTL_ENABLE CLIENT_DURATION CLIENT_CL CLIENT_THREADS
    CLIENT_INSERT CLIENT_SIMPLE STRESS_NUM_PER_INSTANCE JVM_HEAP_SIZE
    DATA_COMPACTION DATA_COMPRESSION DATA_CHUNK_SIZE CLIENT_POD_LEVEL
    CLIENT_POD_MEMORY CLIENT_POD_CPU SERVER_POD_LEVEL SERVER_POD_MEMORY
    SERVER_POD_CPU CLIENT_POP_MIN CLIENT_POP_MAX JVM_GC_TYPE HOST_NETWORK_ENABLE
    NETWORK_RPS_TUNE_ENABLE DEBUG_MODE JDK_VERSION CASSANDRA_FILL_DATA
    DCASSANDRA_DISK_MOUNT EPHEMERAL_STORAGE_SIZE DEPLOY_MODE NODE_NUM
    CLIENT_NODE_NUM REPLICATE_NUM CASSANDRA_ENDPOINT_SNITCH CASSANDRA_NUM_TOKENS
    NUMA_OPTIONS CLIENT_POP_MAX_PERFORMANCE_DIV CLUSTER_ON_SINGLE_NODE
    SERVER_POD_NUM HOST_NUM TESTCASE_ORDER
)

# Kubernetes Setting
RECONFIG_OPTIONS="-DCLIENT_DURATION=${CLIENT_DURATION} -DCLIENT_CL=${CLIENT_CL} -DCLIENT_THREADS=${CLIENT_THREADS} -DCLIENT_INSERT=${CLIENT_INSERT} \
    -DCLIENT_SIMPLE=${CLIENT_SIMPLE} -DCASSANDRA_CONCURENT_READS=${CASSANDRA_CONCURENT_READS} -DJVM_HEAP_SIZE=${JVM_HEAP_SIZE} \
    -DCASSANDRA_NUMACTL_ENABLE=${CASSANDRA_NUMACTL_ENABLE} -DCASSANDRA_CONCURENT_WRITES=${CASSANDRA_CONCURENT_WRITES} -DINSTANCE_NUM=${INSTANCE_NUM} \
    -DSTRESS_NUM_PER_INSTANCE=${STRESS_NUM_PER_INSTANCE} -DDATA_COMPACTION=${DATA_COMPACTION} -DDATA_COMPRESSION=${DATA_COMPRESSION} \
    -DDATA_CHUNK_SIZE=${DATA_CHUNK_SIZE} -DCLIENT_POD_LEVEL=${CLIENT_POD_LEVEL} -DCLIENT_POD_MEMORY=${CLIENT_POD_MEMORY} -DCLIENT_POD_CPU=${CLIENT_POD_CPU} \
    -DSERVER_POD_LEVEL=${SERVER_POD_LEVEL} -DSERVER_POD_MEMORY=${SERVER_POD_MEMORY} -DSERVER_POD_CPU=${SERVER_POD_CPU} -DIMAGE_ARCH=${IMAGE_ARCH} \
    -DCLIENT_POP_MIN=${CLIENT_POP_MIN} -DCLIENT_POP_MAX=${CLIENT_POP_MAX} -DJVM_GC_TYPE=${JVM_GC_TYPE} -DHOST_NETWORK_ENABLE=${HOST_NETWORK_ENABLE} \
    -DNETWORK_RPS_TUNE_ENABLE=${NETWORK_RPS_TUNE_ENABLE} -DDEBUG_MODE=${DEBUG_MODE} -DJDK_VERSION=${JDK_VERSION} -DCASSANDRA_FILL_DATA=${CASSANDRA_FILL_DATA} \
    -DCASSANDRA_DISK_MOUNT=${CASSANDRA_DISK_MOUNT} -DEPHEMERAL_STORAGE_SIZE=${EPHEMERAL_STORAGE_SIZE} -DDEPLOY_MODE=${DEPLOY_MODE} -DNODE_NUM=${NODE_NUM} \
    -DCLIENT_NODE_NUM=${CLIENT_NODE_NUM} -DREPLICATE_NUM=${REPLICATE_NUM} -DCASSANDRA_ENDPOINT_SNITCH=${CASSANDRA_ENDPOINT_SNITCH} \
    -DCASSANDRA_NUM_TOKENS=${CASSANDRA_NUM_TOKENS} -DNUMA_OPTIONS=${NUMA_OPTIONS} -DCLIENT_POP_MAX_PERFORMANCE_DIV=${CLIENT_POP_MAX_PERFORMANCE_DIV} \
    -DCLUSTER_ON_SINGLE_NODE=${CLUSTER_ON_SINGLE_NODE} -DSERVER_POD_NUM=${SERVER_POD_NUM} -DHOST_NUM=${HOST_NUM} -DTESTCASE_ORDER=${TESTCASE_ORDER}"

JOB_FILTER="job-name=benchmark"

. "$DIR/../../script/validate.sh"
