#!/usr/bin/expect
############################################################################
# Purpose: Stress test with maximum slurmctld message concurrency.
#          We start a large number of job steps, the simultaneously 
#          cancel them all. Message can get lost/restransmitted, so
#          there is a delayed test for job step removal.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
#
############################################################################
# Copyright (C) 2002 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# UCRL-CODE-217948.
# 
# This file is part of SLURM, a resource management program.
# For details, see <http://www.llnl.gov/linux/slurm/>.
#  
# SLURM is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
# 
# SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
# 
# You should have received a copy of the GNU General Public License along
# with SLURM; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
############################################################################
source ./globals

set test_id      "9.8"
set exit_code    0
set file_in      "test$test_id.input"
set job_cnt      10
set delay        5
set sleep_time   300
set task_cnt     60

print_header $test_id

#
# A single slurmd can't handle a large task count without 
# running out of memory and pthreads
#
if {[test_front_end] != 0} {
	set task_cnt 2
}

#
# Build input script file
# NOTE: The initial sleep is so that all of the submissions have time 
#   to occur before contending with a multitude of job step creations.
#   This is especially important on very slow systems (e.g. AIX).
#
make_bash_script $file_in "
$bin_sleep $delay
for ((inx=0; inx < $task_cnt; inx++)) ; do
        $srun $bin_sleep $sleep_time &
done
$srun $bin_sleep $sleep_time
"

#
# Initiate $job_cnt batch jobs
#
set start_cnt 0
set timeout $delay
for {set inx 0} {$inx < $job_cnt} {incr inx} {
	spawn $srun --batch --output=/dev/null --error=/dev/null -t5 $file_in
	expect {
		-re "jobid ($number) submitted" {
			incr start_cnt
			exp_continue
		}
		-re "Unable to contact" {
			send_user "\nFAILURE: slurm appears to be down\n"
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: srun not responding\n"
			kill_srun
			exit 1
		}
		eof {
			wait
		}
	}
}
if {$start_cnt < 1} {
	send_user "\nFAILURE: no jobs submitted\n"
	exit 1
}
if {$start_cnt < $job_cnt} {
	send_user "\nFAILURE: not all jobs submitted\n"
	set exit_code 1
}

#
# Give the jobs a few seconds to get initiated, check for steps, 
# then kill them all
#
set user_name ""

exec $bin_sleep [expr $delay + 5]
spawn $bin_id -un
expect {
	-re "($alpha_numeric)" {
		set user_name $expect_out(1,string)
	}
	eof {
		wait
	}
	
}
#
# There could be hundreds of job steps, we don't want to see
# the details, but want to make sure that we did start a bunch
#
# Determine if this is AIX (for task count, federation switch 
# prevents each node from running more than 16 tasks)
#
set desired_tasks [expr $task_cnt * 2 / 3]
if {[test_aix]} {
	set desired_tasks 15
}

set unresponsive 1
while { $unresponsive } {
	log_user 0
	set matches 0
	set timeout 60
	spawn $squeue --steps --user $user_name
	expect {
		-re "sleep" {
			incr matches
			exp_continue
		}
		-re "error:" {
		}
		timeout {
		}
		eof {
			wait
			set unresponsive 0	
		}
	}
}
if {[test_aix]} {sleep 5}

log_user 1
if {$matches < $desired_tasks} {
	send_user "\nFAILURE: only started $matches job steps\n"
	send_user "  We expected at least $desired_tasks and possibly hundreds\n"
	set exit_code 1
} else {
	send_user "\nwe found $matches job steps\n"
}
spawn $scancel --quiet --user $user_name
expect {
	eof {
		wait
	}
}

#
# Give a few seconds for clean-up and insure things are still fine
# If message are lost, slurmctld re-sends job kill RPC 120 seconds later
# In any case, make sure that all jobs get completed
#
exec $bin_sleep 10
set completing_jobs 0
spawn $squeue --noheader --user $user_name
expect {
	-re "$user_name *CG" {
		incr completing_jobs
		exp_continue
	}
	-re "$user_name" {
		send_user "\nFAILURE: jobs not all gone\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$completing_jobs != 0} {
	send_user "\nWaiting for slurmctld to re-send job kill RPC\n"
	send_user "This will take 120 seconds...\n"
	exec $bin_sleep 120
	set completing_jobs 0
	spawn $squeue --noheader --user $user_name
	expect {
		-re "$user_name *CG" {
			incr completing_jobs
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$completing_jobs != 0} {
		send_user "\nFAILURE: jobs not completing\n"
		set exit_code 1
	}
}
if {$completing_jobs != 0} {
	set max_wait [expr $sleep_time - 120]
	if {$max_wait > 0} {
		set completing_jobs 0
		exec $bin_sleep $max_wait
		spawn $squeue --noheader --user $user_name
		expect {
			-re "$user_name *CG" {
				incr completing_jobs
				exp_continue
			}
			eof {
				wait
			}
		}
	}
}
if {$completing_jobs != 0} {
	send_user "\nFAILURE: Jobs not completing. Subsequent tests may fail!\n"
}		

if {$exit_code == 0} {
	exec $bin_rm -f $file_in
	send_user "\nSUCCESS\n"
}
exit $exit_code
