<?xml version="1.0" encoding="UTF-8"?>
<!--
  ! CCPL HEADER START
  !
  ! This work is licensed under the Creative Commons
  ! Attribution-NonCommercial-NoDerivs 3.0 Unported License.
  ! To view a copy of this license, visit
  ! http://creativecommons.org/licenses/by-nc-nd/3.0/
  ! or send a letter to Creative Commons, 444 Castro Street,
  ! Suite 900, Mountain View, California, 94041, USA.
  !
  ! You can also obtain a copy of the license at
  ! src/main/resources/legal-notices/CC-BY-NC-ND.txt.
  ! See the License for the specific language governing permissions
  ! and limitations under the License.
  !
  ! If applicable, add the following below this CCPL HEADER, with the fields
  ! enclosed by brackets "[]" replaced with your own identifying information:
  !      Portions Copyright [yyyy] [name of copyright owner]
  !
  ! CCPL HEADER END
  !
  !      Copyright 2011-2012 ForgeRock AS
  !    
-->
<chapter xml:id='chap-session-failover'
 xmlns='http://docbook.org/ns/docbook'
 version='5.0' xml:lang='en'
 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
 xsi:schemaLocation='http://docbook.org/ns/docbook http://docbook.org/xml/5.0/xsd/docbook.xsd'
 xmlns:xlink='http://www.w3.org/1999/xlink'
 xmlns:xinclude='http://www.w3.org/2001/XInclude'>
 <title>Setting Up OpenAM Session Failover</title>
 <indexterm>
  <primary>Installing</primary>
  <secondary>Session failover</secondary>
 </indexterm>
 <para>This chapter covers setting up session failover when using multiple
 instances of OpenAM in a site configuration for high availability. The basic
 idea followed here is that you configure load balancing to be sticky, based
 on the value of an OpenAM cookie, <literal>amlbcookie</literal>, different
 for each OpenAM server. Should that server become unavailable, the load
 balancer fails client requests over to another OpenAM server. The other OpenAM
 server must then fail over the user session associated with the client.</para>
 
 <para>Session failover uses a highly available data store for OpenAM
 session data, shared by OpenAM servers in a site configuration. When the
 OpenAM server where a user authenticated goes down, other OpenAM servers can
 read the user session information from the highly available store, so the
 user does not have to authenticate again. When the original OpenAM server
 becomes available again, it can also read session information from the
 store, and carry on serving users with active sessions.</para>
 
 <note>
  <para>Session failover is supported within a site or data center with a
  shared local area network. Session failover is not supported across sites
  and data centers linked by wide area networks (WAN). Latency over the WAN
  can cause issues with the underlying message queue, and therefore prevents
  reliable session failover.</para>
 </note>
 
 <procedure xml:id="before-you-start-site-failover">
  <title>Before You Start</title>
  
  <para>Before you set up session failover, first configure OpenAM in a site
  configuration with a load balancer as the entry point to the site. The most
  expedient way to configure the site is to set it up during initial OpenAM
  configuration, where OpenAM can manage and replicate server configuration
  for availability. If you did not set up the site during initial configuration,
  then follow all the steps below.</para>
  
  <step>
   <para>In the OpenAM console for one of the servers in the site, select
   Configuration &gt; Servers and Sites &gt; Sites &gt; New..., and then
   create a new site.</para>
   <para>The site URL is the URL to the load balancer in front of your OpenAM
   servers in the site. For example, if your load balancer listens on host
   <literal>lb.example.com</literal> and port <literal>8080</literal>,
   with OpenAM under <literal>/openam</literal>, then your site URL is
   <literal>http://lb.example.com:8080/openam</literal>.</para>
  </step>
  <step>
   <para>On each OpenAM server console in the site, select Configuration &gt;
   Servers and Sites &gt; Servers &gt; <replaceable>Server Name</replaceable>,
   and then set Parent Site to the site you created before saving your
   work.</para>
  </step>
  <step performance="optional">
   <para>If you want to use sticky load balancing, configure your load balancer
   to inspect the value of the <literal>amlbcookie</literal> to determine which
   OpenAM server should receive the client request.</para>
   <para>As your load balancer depends on the <literal>amlbcookie</literal>
   value, on each OpenAM server console in the site, select Configuration &gt;
   Servers and Sites &gt; Servers &gt; <replaceable>Server Name</replaceable>
   &gt; Advanced, set <literal>com.iplanet.am.lbcookie.value</literal> to a
   unique value, and save your work.</para>
   <para>After you restart the OpenAM server, <literal>amlbcookie</literal>
   values from OpenAM should be as you configured them. (To check, login to the
   console and check the cookie value in your browser.)</para>
  </step>
  <step>
   <para>Restart each OpenAM server or the web containers where the OpenAM
   servers run so that all configuration changes take effect.</para>
  </step>
 </procedure>
 
 <procedure xml:id="prepare-site-session-data-service">
  <title>To Prepare the Session Data Service</title>
  
  <para>The session data service relies on Java Message Queue and Berkeley DB
  Java Edition. You set up the session failover service in a site cluster to
  serve as the highly available session data store.</para>
  
  <step>
   <para>Install the session tools from <filename>ssoSessionTools.zip</filename>
   on at least two, and generally not more than four, servers where you run
   OpenAM.<footnote><para>You install more than one instance of the session
   tools in case an instance crashes and must fail over to another instance. At
   the same time, session failover requires that messages be sent across the
   network from one instance to another to stay in sync in case of failover. If
   you install too many instances, however, then the increase in network
   traffic for synchronization can impair performance.</para></footnote></para>
   <para>For example, you can install the session tools in the OpenAM
   configuration directory.</para>
   <screen>$ cd $HOME/openam
$ unzip /path/to/OpenAM/tools/ssoSessionTools.zip
...
$ ./setup
Name of the directory to install the scripts (example: sfoscripts):sfoscripts
The scripts are properly setup under directory: /home/user/openam/sfoscripts
JMQ is properly setup under directory /home/user/openam/jmq</screen>
  </step>
  <step>
   <para>Start the Message Queue broker in order to configure user
   accounts.</para>
   <screen>$ cd jmq/imq/bin
$ ./imqbrokerd -name aminstance -port 7777 &amp;</screen>
  </step>
  <step>
   <para>Change the default admin password from <literal>admin</literal> to
   something else.</para>
   <screen>$ ./imqusermgr update -u admin -p password -i aminstance
User repository for broker instance: aminstance
Are you sure you want to update user admin? (y/n)[n] y
User admin successfully updated.</screen>
  </step>
  <step>
   <para>Disable the default <literal>guest</literal> account.</para>
   <screen>$ ./imqusermgr update -u guest -a false -i aminstance
User repository for broker instance: aminstance
Are you sure you want to update user guest? (y/n)[n] y
User guest successfully updated.</screen>
  </step>
  <step>
   <para>Add a user for the session failover service.</para>
   <screen>$ ./imqusermgr add -u openamuser -g admin -p secret12 -i aminstance
User repository for broker instance: aminstance
User openamuser successfully added.</screen>
  </step>
  <step>
   <para>Create a password file for the session failover service.</para>
   <screen>$ cd sfoscripts/bin/
$ ./amsfopassword --encrypt secret12 --passwordfile /home/user/openam/mqpwd.txt
os.name=Linux
SUCCESSFUL</screen>
  </step>
  <step>
   <para>Stop the broker you started on port 7777.</para>
  </step>
  <step>
   <para>Configure the session failover service for the site.</para>
   <para>For each session tools installation, edit the
   <filename>/home/user/openam/sfoscripts/config/lib/amsfo.conf</filename>
   configuration file to change at least the <literal>USER_NAME</literal>,
   <literal>CLUSTER_LIST</literal>, and <literal>PASSWORDFILE</literal>
   parameters.</para>
   <para><literal>USER_NAME</literal> should match the user you created for the
   session failover service.</para>
   <screen>USER_NAME=openamuser</screen>
   <para><literal>CLUSTER_LIST</literal> specifies the <literal><replaceable
   >host</replaceable>:<replaceable>port</replaceable></literal> combinations
   for all the session failover services you configure for the site.</para>
   <screen>CLUSTER_LIST=openam.example.com:7777,openam2.example.com:7777</screen>
   <para><literal>PASSWORDFILE</literal> specifies the path to the password
   file you created.</para>
   <screen>PASSWORDFILE=/home/user/openam/mqpwd.txt</screen>
   <para>You can optionally set <literal>AMSESSIONDB_ARGS="-v"</literal> to log
   additional information.</para>
  </step>
 </procedure>
 
 <procedure xml:id="enable-site-session-failover">
  <title>To Enable Session Failover</title>
  
  <para>Enabling session failover at this point involves configuring OpenAM to
  use the session data store, and then starting services. Examples in this
  procedure show OpenAM running in Apache Tomcat.</para>
  
  <step>
   <para>On each OpenAM server console in the site, select Configuration &gt;
   Global &gt; Session &gt; Instances &gt; New... to set up session data store
   access.</para>

   <para>Provide a user name for the Session Store User, such as
   <literal>openamuser</literal>, with the same password you entered into the
   password file, and the Database Url is the <literal><replaceable
   >host</replaceable>:<replaceable>port</replaceable></literal> combination
   that you entered for the <literal>CLUSTER_LIST</literal> parameter.</para>

   <para>Be sure to Add the new instance, and then also Save your configuration
   changes. The configuration changes take effect after you restart
   OpenAM.</para>
  </step>
  <step>
   <para>Stop each OpenAM server in the site.</para>
   <screen>$ /etc/init.d/tomcat stop</screen>
  </step>
  <step>
   <para>Start each session data service in the cluster.</para>
   <screen>$ cd /home/user/openam/sfoscripts/bin
$ ./amsfo start</screen>
   <para>By default, the log and session data store are located under
   <filename>/tmp/amsession/</filename>.</para>
   <screen>$ tail -f /tmp/amsession/logs/amsessiondb.log
...
Initializing and connecting to the Message Queue server ...
Successfully started.</screen>
  </step>
  <step>
   <para>Start each OpenAM server in the site.</para>
   <screen>$ /etc/init.d/tomcat start</screen>
   <para>Wait for each OpenAM server to start before starting another.</para>
   <screen>$ tail -f /path/to/tomcat/logs/catalina.out
...
INFO: Server startup in 26047 ms</screen>
   <para>After OpenAM has started, you can test session failover.</para>
   <!-- TODO: Cover how to troubleshoot when things go wrong, either here
   or in the Admin Guide troubleshooting chapter. -->
  </step>
 </procedure>

<!--  <section xml:id="global-session-failover">
  <title>Global Session Failover</title>
  <para>OpenAM includes a new, multi-site session failover mechanism based on
  OpenDJ.</para>
 </section>
 
 <section xml:id="site-based-session-failover">
  <title>Site Based Session Failover</title>
  <para>OpenAM also provides the site-based session failover service available
  in earlier versions.</para>
 </section> -->
</chapter>

