Clustering Tutorial
- Install two instances of XWiki on the same MySQL database
- Configure event distribution
- Start XWiki instance on different ports
- Check the clustering setup
- Add load balancing with apache2 mod_proxy_balancer
- Monitoring and Troubleshooting
The goal of a cluster of XWiki instance is to provide several XWiki in different servers accessing the same database server to be able to do load balancing.
Install two instances of XWiki on the same MySQL database
Here we will use the XWiki standard distribution but instead of using the embedded hsqldb database we will use a MySql database. For this you need to:
- modify the hibernate.cfg.xml file to change hsqldb configuration by a proper MySQL configuration
- copy your database driver JAR in WEB-INF/lib or in some shared lib directory
See the installation instructions for more details on how to install XWiki.
At this point you almost have a XWiki cluster: you have two instances of XWiki which are using the same datas. But there is a remaining "detail": the problem is that for performance reason XWiki is using lots of different caches which mean that even if one instance of XWiki modify a document it's possible the other XWiki instance will not see it and keep showing the document from its cache.
Configure event distribution
To handle this we will use the network event distribution system. When anything happens in a XWiki instances it generate a local event which is used to update caches. Here we will make theses events send to other XWiki instances as well and emulate local events. This way all the code updating its cache each time something changed in a particular document for example will also be notified if it has been done by another instance of XWiki.
Enable remote observation
First you need to enable remote observation system.
For this go to xwiki.properties file and set the property observation.remote.enabled to true.
Set the channels
Then you need to indicate to remote observation manager which communications channels it should start when XWiki starts.
For this you need to list in property observation.remote.channels the names of the channels.
Here we set udp in both instances to use embedded JGroups udp.xml configuration file which auto discover cluster members. In general you should always start by looking at the example configurations files in jgroups jar. Most of them are even configurable with system properties so you don't even have to copy and modify them.
This tutorial is working with two instances on the same host but it's possible udp does not work if whatever router or switch behind the members of the cluster does not allow multicast. In that case you might want to try using tcp instead (see tcp.xml in JGroups jar).
Start XWiki instance on different ports
For this tutorial we run two instance of XWiki in the same server. So we need each instance to use different ports.
To run a XWik instance if a custom port you can provide it in parameter of the script start_xwiki.sh.
Here we start the first instance with:
and the second one with:
The second port is the port used to stop jetty server, it's not mandatory to run XWiki but it's better to have differents ports if you don't want to have to kill the java process instead of using the stop_xwiki.sh script
Sometimes you have to force the bind address, you can do it using -Djgroups.bind_addr=localhost
Check the clustering setup
- Load a page in both instances
- Modify a page in one instance
- Reload it in the other instance
- It should have taken into account the modification made on the other instance
Add load balancing with apache2 mod_proxy_balancer
You probably want to do load-balancing in front of the clustered XWiki instances. This section provides sample configurations to do that.
Replace if you have one your apache proxy configuration by the following one, or set it as proxy of your apache site configuration if you don't have one yet :
ProxyPreserveHost On
<Proxy balancer://mycluster>
BalancerMember ajp://127.0.0.1:8009 route=jvm1
BalancerMember ajp://127.0.0.1:9009 route=jvm2
ProxySet stickysession=JSESSIONID
</Proxy>
ProxyPass /xwiki balancer://mycluster/xwiki
You need also to precise the name of the JVM route for each member of the balancer. On Tomcat, in conf/server.xml, edit the Engine node as follow :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
On GlassFish and Tomcat and probably others, you can set the system property jvmRoute to the same value.
Where you replace jvm1 by the name of the route you have precised in the Apache configuration.
This setting will complement the JSESSIONID with a suffix made by this jvmRoute: thus any session will contain the node name and this will instruct Apache to choose that route the next time a request comes. Thus, the browser will remain hooked to that server as long as the session remains.
Testing the clustered setup
To test the load-balancing setup, you can edit the version.properties file of your wiki to add a suffix to be able to recognize each member of the cluster. For example version=2.6.33077-node1 for the first node of a cluster of XWiki 2.6 nodes. Then, connect to the wiki via the URL that points to the load-balancer with a first browser, and look at which node is being used. Just after connect with a second browser (to have a different session), and make sure the node used is the other one (since mod_proxy_rewrite affects balancing members sequentially.
You can also verify the JSESSIONID cookie value which will end with the route name.
Finally, to test that clustering works you can either:
- set-up a dedicated virtual host to each cluster node so that you can check that, for example, a changed document, is changed on every node
- force the route by adding the URL parameter JSESSIOND=jvm1: this will make sure that this route is used which, in turn, will set the cookie to a session-identifier suffixed with jvm1.
Monitoring and Troubleshooting
The error log of Apache reports an amount of details about the choice of load-balancing if the log-level is sufficient.
The balancer-manager is also precious:
SetHandler balancer-manager
AuthType digest
AuthName "XWiki Cluster Management"
AuthType Digest
AuthDigestDomain http://example.com
AuthDigestFile /path/to/file.htdigest
Require valid-user
</Location>
Using this configuration, properly specialized, provides a small interface at /balancer-manager where you can see the cluster nodes which are active, their load, and where you can disable them to take them offline for a bit.
Debugging
See Debugging Section in the Observation Reference documentation.
You can also easily include the following HTML to force your browser to change from one node of the cluster to the other. This is extremely important to verify that clustering works:
onclick="d = new Date(); d.setDate(d.getDate()+7); document.cookie='JSESSIONID=xxx.$hostname; ' + d.toUTCString()" ##
>$hostname</a>
(where you replace $hostname (twice) and $doc.name appropriately.