<commit_before>#!/usr/local/bin/tcsh -f
#################################################################
#								#
# Copyright (c) 2014-2015 Fidelity National Information		#
# Services, Inc. and/or its subsidiaries. All rights reserved.	#
#								#
# Copyright (c) 2017-2018 YottaDB LLC. and/or its subsidiaries.	#
# All rights reserved.                                          #
#								#
#	This source code contains the intellectual property	#
#	of its copyright holder(s), and is made available	#
#	under a license.  If you do not know the terms of	#
#	the license, please stop and do not read further.	#
#								#
#################################################################

# Test that autoswitch failure due to permissions on the directory containing the journal files is handled properly.
#
# Runs a number of imptp processes, waits for at least one journal switch, then chmod's the journal directory read-only.
# For non-replic, ydb_error_on_jnl_file_lost is set, so we expect all the imptp processes to exit with JNLEXTEND.
# For replic, gtm_test_freeze_on_error is configured, so we expect the instance to freeze.
# Restores write access to the journal directory and verifies that writes and reads proceed normally.

if ($?test_replic) then
	unsetenv gtm_test_fake_enospc
	setenv gtm_test_freeze_on_error 1
	setenv gtm_custom_errors $gtm_tools/custom_errors_sample.txt
	# The source server may get stuck on crit instead of reporting the freeze if reading from files, so disable jnlfileonly
	# and forced overflow.
	setenv gtm_test_jnlfileonly 0
	unsetenv gtm_test_jnlpool_sync
	$MULTISITE_REPLIC_PREPARE 2
else
	source $gtm_tst/com/set_ydb_env_var_random.csh ydb_error_on_jnl_file_lost gtm_error_on_jnl_file_lost 1
endif

set jnldir="jnldir"

echo ">>> Create database"

mkdir -p $jnldir
if ($?test_replic) then
	$MSR RUN INST2 mkdir -p $jnldir >& mkdir.out
endif

@ num_regions = 4

# Set autoswitch to the lowest setting to maximize switching
$gtm_tst/com/dbcreate.csh mumps $num_regions 125 1000 4096 2000 4096 2000 -jnl_auto=16384 -jnl_prefix=${jnldir}/ >& dbcreate.log

if ($?test_replic) then
	# Start creates the instance file, which we need for the set
	$MSR START INST1 INST2 RP >&! msr_start_`date +%H_%M_%S`.out
	# Randomly test that JNLSWITCHRETRY error scenario is handled properly by source server (YDB#235)
	# To do so, we stop the receiver server before any updates happen. And will start the receiver server
	# later after the instance has frozen (and the latest generation journal file has been closed in
	# shared memory but a new one has not been created due to write permission issues on the directory).
	# This way the source server will connect to the receiver server while the latest generation journal
	# has jfh->is_not_latest_jnl set to TRUE.
	set jnlswitchretry = `$gtm_exe/mumps -run rand 2`
	if ($jnlswitchretry) then
		$MSR STOPRCV INST1 INST2 >& jnlswitchretry_1.log
	endif
else
	$MUPIP set $tst_jnl_str -region "*" >& jnl_enable.out
endif

echo ">>> Start imptp"

setenv gtm_test_dbfill "IMPTP"
setenv gtm_test_jobcnt 20	# More jobs means more journal entries written when the switch fails
setenv gtm_test_jobid 1
$gtm_tst/com/imptp.csh >>&! imptp.out

@ logcount = $num_regions + 1	# Wait for at least one journal file switch

(cd $jnldir ; $gtm_tst/com/wait_for_n_jnl.csh -lognum $logcount -duration 3600)

echo ">>> Set jnldir read-only"

chmod a-w $jnldir

if ($?test_replic) then
	@ sleepcnt = 0
	# $MUPIP replic -source -freeze returns an error status when the instance is frozen.
	# Loop until we get an error status ({ cmd } returns false) or time out.
	while ( { $MUPIP replic -source -freeze } ) >& /dev/null < /dev/null
		@ sleepcnt++
		if ($sleepcnt > 120) then
			echo "TEST-E-timeout waiting for instance freeze"
			break
		endif
		sleep 1
	end
	echo $sleepcnt > instfreeze_sleepcnt.txt
	if ($jnlswitchretry) then
		$MSR STARTRCV INST1 INST2 >& jnlswitchretry_2.log
		$MSR SYNC INST1 INST2 >& jnlswitchretry_3.log
	endif
else
	@ jobnum=0
	set mjofiles=""
	while ($jobnum < $gtm_test_jobcnt)
		@ jobnum++
		set mjofiles="$mjofiles impjob_imptp1.mjo${jobnum}"
	end
	$gtm_tst/com/wait_for_log.csh -log "$mjofiles" -message 'JNLEXTEND|JNLSWITCHFAIL' -useE >& wfl.out
endif

ls -al $jnldir >& jnldir_files.out
lsof *.dat >& lsof.out

echo ">>> Set jnldir read-write"
<commit_msg>Fix occasional v62000/gtm8086 test failure due to slow systems taking slightly more than 2 minutes to freeze instance<commit_after>#!/usr/local/bin/tcsh -f
#################################################################
#								#
# Copyright (c) 2014-2015 Fidelity National Information		#
# Services, Inc. and/or its subsidiaries. All rights reserved.	#
#								#
# Copyright (c) 2017-2018 YottaDB LLC. and/or its subsidiaries.	#
# All rights reserved.                                          #
#								#
#	This source code contains the intellectual property	#
#	of its copyright holder(s), and is made available	#
#	under a license.  If you do not know the terms of	#
#	the license, please stop and do not read further.	#
#								#
#################################################################

# Test that autoswitch failure due to permissions on the directory containing the journal files is handled properly.
#
# Runs a number of imptp processes, waits for at least one journal switch, then chmod's the journal directory read-only.
# For non-replic, ydb_error_on_jnl_file_lost is set, so we expect all the imptp processes to exit with JNLEXTEND.
# For replic, gtm_test_freeze_on_error is configured, so we expect the instance to freeze.
# Restores write access to the journal directory and verifies that writes and reads proceed normally.

if ($?test_replic) then
	unsetenv gtm_test_fake_enospc
	setenv gtm_test_freeze_on_error 1
	setenv gtm_custom_errors $gtm_tools/custom_errors_sample.txt
	# The source server may get stuck on crit instead of reporting the freeze if reading from files, so disable jnlfileonly
	# and forced overflow.
	setenv gtm_test_jnlfileonly 0
	unsetenv gtm_test_jnlpool_sync
	$MULTISITE_REPLIC_PREPARE 2
else
	source $gtm_tst/com/set_ydb_env_var_random.csh ydb_error_on_jnl_file_lost gtm_error_on_jnl_file_lost 1
endif

set jnldir="jnldir"

echo ">>> Create database"

mkdir -p $jnldir
if ($?test_replic) then
	$MSR RUN INST2 mkdir -p $jnldir >& mkdir.out
endif

@ num_regions = 4

# Set autoswitch to the lowest setting to maximize switching
$gtm_tst/com/dbcreate.csh mumps $num_regions 125 1000 4096 2000 4096 2000 -jnl_auto=16384 -jnl_prefix=${jnldir}/ >& dbcreate.log

if ($?test_replic) then
	# Start creates the instance file, which we need for the set
	$MSR START INST1 INST2 RP >&! msr_start_`date +%H_%M_%S`.out
	# Randomly test that JNLSWITCHRETRY error scenario is handled properly by source server (YDB#235)
	# To do so, we stop the receiver server before any updates happen. And will start the receiver server
	# later after the instance has frozen (and the latest generation journal file has been closed in
	# shared memory but a new one has not been created due to write permission issues on the directory).
	# This way the source server will connect to the receiver server while the latest generation journal
	# has jfh->is_not_latest_jnl set to TRUE.
	set jnlswitchretry = `$gtm_exe/mumps -run rand 2`
	if ($jnlswitchretry) then
		$MSR STOPRCV INST1 INST2 >& jnlswitchretry_1.log
	endif
else
	$MUPIP set $tst_jnl_str -region "*" >& jnl_enable.out
endif

echo ">>> Start imptp"

setenv gtm_test_dbfill "IMPTP"
setenv gtm_test_jobcnt 20	# More jobs means more journal entries written when the switch fails
setenv gtm_test_jobid 1
$gtm_tst/com/imptp.csh >>&! imptp.out

@ logcount = $num_regions + 1	# Wait for at least one journal file switch

(cd $jnldir ; $gtm_tst/com/wait_for_n_jnl.csh -lognum $logcount -duration 3600)

echo ">>> Set jnldir read-only"

chmod a-w $jnldir

if ($?test_replic) then
	@ sleepcnt = 0
	# $MUPIP replic -source -freeze returns an error status when the instance is frozen.
	# Loop until we get an error status ({ cmd } returns false) or time out.
	while ( { $MUPIP replic -source -freeze } ) >& /dev/null < /dev/null
		@ sleepcnt++
		if ($sleepcnt > 900) then		# wait for 15 minutes
			echo "TEST-E-timeout waiting for instance freeze"
			break
		endif
		sleep 1
	end
	echo $sleepcnt > instfreeze_sleepcnt.txt
	if ($jnlswitchretry) then
		$MSR STARTRCV INST1 INST2 >& jnlswitchretry_2.log
		$MSR SYNC INST1 INST2 >& jnlswitchretry_3.log
	endif
else
	@ jobnum=0
	set mjofiles=""
	while ($jobnum < $gtm_test_jobcnt)
		@ jobnum++
		set mjofiles="$mjofiles impjob_imptp1.mjo${jobnum}"
	end
	$gtm_tst/com/wait_for_log.csh -log "$mjofiles" -message 'JNLEXTEND|JNLSWITCHFAIL' -useE >& wfl.out
endif

ls -al $jnldir >& jnldir_files.out
lsof *.dat >& lsof.out

echo ">>> Set jnldir read-write"

{'fraction_non_alphanumeric': 0.10597980256439352, 'fraction_numerical': 0.019516623170316577, 'mean_word_length': 4.212300413956239, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '894493', 'n_tokens_mistral': 3288, 'n_tokens_neox': 3037, 'n_words': 1045}