[[ux_persistence]]

The UNICORE persistence layer 
-----------------------------

UNICORE/X stores its state in data bases. The information that is stored includes

 - user's resources (instances of storage, job and other services)
 
 - jobs on the XNJS

The job directories themselves reside on the target system, but UNICORE keeps 
additional information (like, which Grid user owns a particular job).

The data on user resources is organised by service name, i.e. each service 
(for example, JobManagement) stores its information
in a separate database table (having the same name as the service, e.g. "JobManagement").

Job information is stored in a table named "JOBS", while finished jobs go into a table
called "FINISHED_JOBS".

The UNICORE persistence layer offers two kinds of storage:

 - on the filesystem of the UNICORE/X server (using the H2 database engine)

 - on a database server (MySQL, or the so-called server mode of H2)

While the first one is very easy to setup, and easy to manage, the second option
allows advanced setups like clustering/load balancing configurations involving multiple
UNICORE/X servers sharing the same persistent data.

Data migration from one database system to another is in principle possible, but you
should select the storage carefully before going into production. In general, if you
do not require clustering/load balancing, you should choose the default filesystem option, 
since it is less administrative effort.

Configuring the persistence layer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Peristence properties are configured in two files:

 - wsrflite.xml for all service data

 - xnjs.xml (or xnjs_legacy.xml) for job data

It is recommended to specify a configuration file using the +persistence.config+ property. Thus, 
persistence configuration can be easily shared between the components (XNJS and WSRFlite).
If the "persistence.config" property is set, the given file will be read as a Java properties 
file, and the properties will be used. However, "local" properties will override the
ones given in the file. 


[NOTE]
===================
All properties can be specified on a "per table" basis, by appending ".<TABLENAME>"
to the property name. This means you can even select different storage systems for
different data, e.g. store service data on the filesystem and jobs in MySQL.
The table name is case-sensitive.
===================


.Basic configuration options
[options="header"]
|=======================================
|property name			| default value 	| description	
|persistence.config             |  | name of a config file to read  
|persistence.class              | de.fzj.unicore.persist.impl.H2Persist  | name of the Java class  
|persistence.driver	        | (implemention class may provide their own)  |Java class name of the JDBC driver 
|persistence.database           |     | name of the database to connect to 
|persistence.user	        |  | database user name 
|persistence.password	        |  | database password 
|persistence.host	        | localhost | database server host 
|persistence.port	        |  depends on the implementation | database server port 
|=======================================

==== Caching
  
By default, caching of data in memory is enabled. It can be switched off and configured on a 
per-table (i.e. per entity class) basis. If you have a lot of memory for your server, you might 
consider increasing the cache size for certain components.

The following properties are used to control the caching behaviour

.Caching options
[options="header"]
|=======================================
|property name			    | range of values | default value | description	
|persistence.cache.enable           | String	      | "true"  | "true" or "false" to enable/disable caching for the given table 
|persistence.cache.maxSize          | Integer	      | 50      | maximum number of elements to keep in the cache 
|========================================

For example, to set the maximum size of the JOBS cache to 1000, you'd configure

-------------
persistence.cache.maxSize.JOBS
-------------


==== The H2 engine

H2 is a pure Java database engine. It can be used in embedded mode (i.e. the engine runs in-process), or
in server mode, if multiple UNICORE servers should use the same database server. For more information, visit
http://www.h2database.com


.Additional configuration for H2
[options="header"]
|=======================================
|property name			    | range of values | default value | description	
|persistence.directory              |String	|   | name of the directory for storing data 
|persistence.h2.server_mode         |"true" or "false"	| false | whether to connect to a h2 server using tcp   
|persistence.h2.cache_size          |String denoting an Integer	| 4096  | in-memory cache size for H2 in kilobytes 
|=======================================


===== Connection URL


In embedded mode (i.e. the default non-server mode), the connection URL is constructed from the 
configuration properties as follows

-------
  jdbc:h2:file:<persistence.directory>/<table_name>
-------  
  
In server mode, the connection URL is constructed as follows
  
------
  jdbc:h2:tcp://<persistence.host>:<persistence.port>/<persistence.directory>/<table_name>
------


==== The MySQL Engine

The MySQL database engine does not need an introduction. To configure its use for UNICORE persistence 
data, you need to set

--------------
persistence.class=de.fzj.unicore.persist.impl.MySQLPersist
--------------

To use MySQL, you need access to an installed MySQL server. It is beyond the scope
of this guide to describe in detail how to setup and operate MySQL. 
The following is a simple sequence of steps to be performed for setting up the required 
database structures.

 - open the mysql console

 - create a dedicated user, say 'unicore' who will connect from some server in the domain "yourdomain.com" or
from the local host:
--------------
CREATE USER 'unicore'@'%.yourdomain.com' identified by 'some_password' ;
CREATE USER 'unicore'@'localhost' identified by 'some_password' ;
--------------

 - create a dedicated database for use by the UNICORE/X server:
--------------
CREATE DATABASE 'unicore_data_demo_site';
USE 'unicore_data_demo_site';
--------------

 - allow the unicore user access to that database:

----------------
GRANT ALL PRIVILEGES ON 'unicore_data_demo_site.*' to 'unicore'@'localhost';
GRANT ALL PRIVILEGES ON 'unicore_data_demo_site.*' to 'unicore'@'%.yourdomain.com';
----------------

The UNICORE persistence properties would in this case look like this:

-----------------------
persistence.class=de.fzj.unicore.persist.impl.MySQLPersist 
persistence.database=unicore_data_demo_site
persistence.user=unicore
persistence.password=some_password
persistence.host=<your_mysql_host>
persistence.port=<your_mysql_port>
-----------------------


.Additional configuration for MySQL
[options="header"]
|=======================================
|property name			    | default value | description	
|persistence.mysql.tabletype        | MyISAM        | MySQL table type to use |
|=======================================


Clustering
~~~~~~~~~~

If you intend to run a configuration with multiple UNICORE servers accessing a shared database, you need
to enable clustering mode by setting a property

-------------
persistence.cluster.enable=true
-------------

The clustering config file can be set using a (per-table) property 

-------------
persistence.cluster.config=<path to config file>
-------------
  
If this is not set, a default configuration is used.

For clustering, the Hazelcast library is used (http://www.hazelcast.com/documentation.jsp). 
A simple TCP based configuration is

--------------------

<hazelcast>
    <group>
        <name>dev</name>
        <password>dev-pass</password>
    </group>
    <network>
        <port auto-increment="true">5701</port>
        <join>
            <multicast enabled="false">
                <multicast-group>224.2.2.3</multicast-group>
                <multicast-port>54327</multicast-port>
            </multicast>
            <tcp-ip enabled="true">
                <interface>127.0.0.1</interface>
            </tcp-ip>
        </join>
        <interfaces enabled="false">
            <interface>10.3.17.*</interface>
        </interfaces>
    </network>
    <executor-service>
        <core-pool-size>16</core-pool-size>
        <max-pool-size>64</max-pool-size>
        <keep-alive-seconds>60</keep-alive-seconds>
    </executor-service>
    <queue name="default">
        <max-size-per-jvm>10000</max-size-per-jvm>
        <time-to-live-seconds>0</time-to-live-seconds>
    </queue>
    <map name="default">
        <backup-count>1</backup-count>
        <eviction-policy>NONE</eviction-policy>
        <max-size>0</max-size>
        <eviction-percentage>25</eviction-percentage>
    </map>
</hazelcast>

--------------------


The most important part is the "tcp-ip" setting, which must list at least one other node in the cluster.
The "group" setting allows to run multiple clusters on the same set of hosts, just make sure that the group name is the
same for all nodes in a cluster.

Most of the other settings (map, executor-service, etc) are currently not important, because only the distributed lock
feature of Hazelcast is used. Please read the Hazelcast documentation for further information. 
