#! /usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

function print_usage {
  cat <<EOF
Usage: accumulo-service <service> <command>

Services:
  gc                     Accumulo garbage collector
  monitor                Accumulo monitor
  manager                Accumulo manager
  master                 Deprecated. Use 'manager' instead
  tserver                Accumulo tserver
  compaction-coordinator Accumulo compaction coordinator (experimental)
  compactor              Accumulo compactor (experimental)
  sserver                Accumulo scan server (experimental)

Commands:
  start                   Starts service(s)
  stop [--all | [<name>]] Stops service(s)
  kill [--all | [<name>]] Kills service(s)
  list                    List running service(s)

EOF
}

function invalid_args {
  echo -e "Invalid arguments: $1\n"
  print_usage 1>&2
  exit 1
}

function rotate_log() {
  logfile="$1"
  max_retained="5"
  if [[ -f $logfile ]]; then
    while [[ $max_retained -gt 1 ]]; do
      prev=$((max_retained - 1))
      [ -f "$logfile.$prev" ] && mv -f "$logfile.$prev" "$logfile.$max_retained"
      max_retained=$prev
    done
    mv -f "$logfile" "$logfile.$max_retained"
  fi
}

function get_group() {
  # Find the group parameter if any
  local group="default"
  local group_param=""
  local param
  for param in "$@"; do
    if [[ -n $group_param ]]; then
      # grab the group if the previous arg was the group param
      group="$param"
      break
    elif [[ $param == '-q' || $param == '-g' ]]; then
      # found the group parameter, the next arg is the group
      group_param=$param
    fi
  done
  echo "$group"
}

function start_service() {
  local service_type=$1
  local service_name=$2
  shift 2

  local build_service_name="false"
  if [[ -n $service_name ]]; then
    # if service_name is supplied, then we are only starting one instance
    servers_per_host=1
  else
    build_service_name="true"
    servers_per_host=${ACCUMULO_CLUSTER_ARG:-1}
  fi

  for ((process_num = 1; process_num <= servers_per_host; process_num++)); do
    if [[ ${build_service_name} == "true" ]]; then
      service_name="${service_type}_${group}_${process_num}"
    fi
    # The ACCUMULO_SERVICE_INSTANCE variable is used in
    # accumulo-env.sh to set parameters on the command
    # line.
    export ACCUMULO_SERVICE_INSTANCE="${service_name}"

    local pid_file="${ACCUMULO_PID_DIR}/accumulo-${service_name}.pid"
    if [[ -f $pid_file ]]; then
      pid=$(cat "$pid_file")
      if kill -0 "$pid" 2>/dev/null; then
        echo "$HOST : ${service_name} already running (${pid})"
        continue
      fi
    fi
    echo "Starting $service_name on $HOST"

    if [[ ${service_type} == "manager" ]]; then
      "${bin}/accumulo" org.apache.accumulo.manager.state.SetGoalState NORMAL
    fi
    outfile="${ACCUMULO_LOG_DIR}/${service_name}_${HOST}.out"
    errfile="${ACCUMULO_LOG_DIR}/${service_name}_${HOST}.err"
    rotate_log "$outfile"
    rotate_log "$errfile"

    nohup "${bin}/accumulo" "$service_type" "$@" >"$outfile" 2>"$errfile" </dev/null &
    echo "$!" >"${pid_file}"

  done

  # Check the max open files limit and selectively warn
  max_files_open=$(ulimit -n)
  if [[ -n $max_files_open ]]; then
    max_files_recommended=32768
    if ((max_files_open < max_files_recommended)); then
      echo "WARN : Max open files on $HOST is $max_files_open, recommend $max_files_recommended" >&2
    fi
  fi
}

function control_process() {
  local kill_code=$1
  local service_name=$2
  local pid_file=$3
  if [[ -f $pid_file ]]; then
    echo "Stopping $service_name on $HOST"
    kill -s "$kill_code" "$(cat "$pid_file")" 2>/dev/null
    rm -f "${pid_file}" 2>/dev/null
  fi
}

function find_processes() {
  local service_type=$1
  local file
  local filepath
  local expected_pid
  local found_pid
  for filepath in "$ACCUMULO_PID_DIR"/*; do
    if [[ $filepath =~ ^.*/accumulo-("$service_type".*)[.]pid$ ]]; then
      file="${BASH_REMATCH[1]}"
      expected_pid=$(<"$filepath")
      found_pid=$(pgrep -F "$filepath" -f "$file")
      if [[ $found_pid != "$expected_pid" ]]; then
        echo "removing stale pid file $filepath" >&2
        rm "$filepath"
      else
        RUNNING_PROCESSES+=("$file")
      fi
    fi
  done
}

function stop_service() {
  local service_type=$1
  local service_name=$2
  local all_flag=$3
  if [[ $all_flag == 'true' ]]; then
    find_processes "$service_type"
    for process in "${RUNNING_PROCESSES[@]}"; do
      local pid_file="${ACCUMULO_PID_DIR}/accumulo-${process}.pid"
      control_process "TERM" "$process" "$pid_file"
    done
  elif [[ -n $ACCUMULO_CLUSTER_ARG ]]; then

    servers_per_host=${ACCUMULO_CLUSTER_ARG:-1}
    group=$(get_group "$@")

    for ((process_num = 1; process_num <= servers_per_host; process_num++)); do
      service_name="${service_type}_${group}_${process_num}"
      echo "Stopping service process: $service_name"
      local pid_file="${ACCUMULO_PID_DIR}/accumulo-${service_name}.pid"
      control_process "TERM" "$service_name" "$pid_file"
    done

  else
    echo "Stopping service process: $service_name"
    local pid_file="${ACCUMULO_PID_DIR}/accumulo-${service_name}.pid"
    control_process "TERM" "$service_name" "$pid_file"
  fi
}

function kill_service() {
  local service_type=$1
  local service_name=$2
  local all_flag=$3
  if [[ $all_flag == 'true' ]]; then
    find_processes "$service_type"
    for process in "${RUNNING_PROCESSES[@]}"; do
      local pid_file="${ACCUMULO_PID_DIR}/accumulo-${process}.pid"
      control_process "KILL" "$process" "$pid_file"
    done
  elif [[ -n $ACCUMULO_CLUSTER_ARG ]]; then

    servers_per_host=${ACCUMULO_CLUSTER_ARG:-1}
    group=$(get_group "$@")

    for ((process_num = 1; process_num <= servers_per_host; process_num++)); do
      service_name="${service_type}_${group}_${process_num}"
      echo "Stopping service process: $service_name"
      local pid_file="${ACCUMULO_PID_DIR}/accumulo-${service_name}.pid"
      control_process "KILL" "$service_name" "$pid_file"
    done

  else
    local pid_file="${ACCUMULO_PID_DIR}/accumulo-${service_name}.pid"
    control_process "KILL" "$service_name" "$pid_file"
  fi
}

function list_processes() {
  local service_type=$1
  find_processes "$service_type"
  echo "Currently running ${service_type} processes (fields: process pid port):"
  for process in "${RUNNING_PROCESSES[@]}"; do
    local pid_file
    local pid
    local port
    local loop_err
    pid_file="$ACCUMULO_PID_DIR/accumulo-$process.pid"
    pid=$(<"$pid_file") # read the contents of the file into the $pid variable
    port=$(ss -tnlp 2>/dev/null | grep -wF "pid=$pid" | awk '{print $4}' | awk -F : '{print $NF}' | paste -sd,)

    if [[ $port =~ ^[1-9][0-9]{1,4}(,[1-9][0-9]{1,4})*$ ]]; then
      echo "$process $pid $port"
    else
      echo "ERROR unexpected port format $(hostname) process:$process pid:$pid ports:$port" >&2
      loop_err=1
    fi
  done
  [[ -z $loop_err ]] # return non-zero if any errors occurred during the loop
}

function main() {
  if [[ -z $1 ]]; then
    invalid_args "<service> cannot be empty"
  fi

  # Create a global array for process tracking
  declare -a RUNNING_PROCESSES

  # Resolve base directory
  SOURCE="${BASH_SOURCE[0]}"
  while [ -h "${SOURCE}" ]; do
    bin="$(cd -P "$(dirname "${SOURCE}")" && pwd)"
    SOURCE="$(readlink "${SOURCE}")"
    [[ ${SOURCE} != /* ]] && SOURCE="${bin}/${SOURCE}"
  done
  # Set up variables needed by accumulo-env.sh
  bin="$(cd -P "$(dirname "${SOURCE}")" && pwd)"
  export bin
  basedir=$(cd -P "${bin}"/.. && pwd)
  export basedir
  export conf="${ACCUMULO_CONF_DIR:-${basedir}/conf}"
  export lib="${basedir}/lib"

  group=$(get_group "$@")
  export ACCUMULO_RESOURCE_GROUP="$group"

  HOST="$(hostname)"
  if [[ -z $HOST ]]; then
    HOST=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
  fi

  local service_type="$1"
  local command_name="$2"
  shift 2
  local service_name=""
  local all_flag=false

  if [[ $service_type == "master" ]]; then
    echo "WARN : Use of 'master' service name is deprecated; use 'manager' instead."
    service_type="manager"
  fi

  if [[ -f "${conf}/accumulo-env.sh" ]]; then
    #shellcheck source=../conf/accumulo-env.sh
    source "${conf}/accumulo-env.sh"
  fi
  ACCUMULO_LOG_DIR="${ACCUMULO_LOG_DIR:-${basedir}/logs}"
  ACCUMULO_PID_DIR="${ACCUMULO_PID_DIR:-${basedir}/run}"

  mkdir -p "$ACCUMULO_LOG_DIR" 2>/dev/null
  mkdir -p "$ACCUMULO_PID_DIR" 2>/dev/null

  # Check and see if accumulo-cluster is calling this script
  if [[ -z $ACCUMULO_CLUSTER_ARG ]]; then
    # The rest of the arguments are from a user
    if [[ $1 == "--all" ]]; then
      all_flag=true
    else
      # A named service has been specified
      if [[ $1 != "-a" ]]; then
        service_name="$1"
      fi
    fi
  fi

  case "$service_type" in
    gc | manager | monitor | tserver | compaction-coordinator | compactor | sserver)
      if [[ -z $command_name ]]; then
        invalid_args "<command> cannot be empty"
      fi
      case "$command_name" in
        start)
          start_service "$service_type" "$service_name" "$@"
          ;;
        stop)
          stop_service "$service_type" "$service_name" $all_flag "$@"
          ;;
        kill)
          kill_service "$service_type" "$service_name" $all_flag "$@"
          ;;
        list)
          list_processes "$service_type"
          ;;
        *)
          invalid_args "'$command_name' is an invalid <command>"
          ;;
      esac
      ;;
    *)
      invalid_args "'$service_type' is an invalid <service>"
      ;;
  esac
}

main "$@"
