Identifying and connecting to an HDR secondary

Abstract

In cases where a simple IBM Informix Primary / HDR Secondary server pair exists, it may be considered unnecessary to implement Informix Connection Manager to manage switching between the two, as OLTP applications can simply connect to whichever is the active Primary in a group defined in the sqlhosts file. This article describes a unique method for connecting appropriate applications to the active HDR Secondary in this scenario.

Content

An identical set of sqlhosts file entries on both Primary and HDR Secondary servers can define a fail-over group in this format:

my_server      group     -           -
my_server_pri  onsoctcp  *my-host-1  sqlexec  g=my_server
my_server_sec  onsoctcp  *my-host-2  sqlexec  g=my_server

(Note that the asterisk causes the specific Informix server to listen on all available TCP interfaces including "localhost".)

Prior to starting a read-only application, the relevant INFORMIXSERVER environment variable can be set with

export INFORMIXSERVER=$(ifx_server.sh my_server secondary)

where ifx_server.sh (detailed below) is an executable shell script (bash or ksh) available in the current PATH, with my_server and secondary being arguments to this script.

Applications that should connect to the Primary would continue to use the group name as usual with:

export INFORMIXSERVER=my_server

If the script is called with primary as the second argument, the Primary server name (DBSERVERNAME or DBSERVERALIAS) is returned for information.

A listing of shell script ifx_server.sh follows:

#!/bin/ksh
# Choose an Informix server from an sqlhosts group
# Doug Lawry, Oninit Consulting, 15/11/2012
 
case "$2" in
pri*)   mode=5 ;;
sec*)   mode=2 ;;
*)      echo "Usage: $0 sqlhosts-group-name {primary|secondary}" 1>&2
        exit 1 ;;
esac
 
FILE=${INFORMIXSQLHOSTS:=$INFORMIXDIR/etc/sqlhosts}
LIST=$(awk "/g=$1/ {print \$1}" $FILE) || exit 1
 
if [ "$LIST" = "" ]
then
        echo "Group $1 not found in $FILE" 1>&2
        exit 1
fi
 
umask 0
TMP=/tmp/$(basename $i .sh).tmp
trap "rm -f $TMP" 0 2
 
for INFORMIXSERVER in $LIST
do
        echo "UNLOAD TO '$TMP' SELECT sh_mode FROM sysshmvals" |
        dbaccess sysmaster > /dev/null 2>&1 || continue

        if [ "$(cut -d '|' -f1 $TMP)" = "$mode" ]
        then
                echo $INFORMIXSERVER
                exit 0
        fi
done
 
echo "No server found in requested mode" 1>&2
exit 1

Conclusion

It is not necessary to install and maintain Connection Manager for simple HDR server pairs, as the ifx_server.sh script makes it possible to soft-code connection to whichever server is in the appropriate mode.


Disclaimer

The code fix suggested above is provided "as is" without warranty of any kind, either express or implied, including without limitation any implied warranties of condition, uninterrupted use, merchantability, fitness for a particular purpose, or non-infringement.