Sunday, April 19, 2015

Sharding in Liferay

Sharding in Liferay

A Database shard is a Horizontal partitioning of databases. Each individual partition is referred as shard or database shard. Each shard is held on saperate database server to distribute the load on each individual database server. Sharding reduces number of rows in each table which reduces index size hence improve search time.

Here i am explaining the necessary steps to create sharded platform in Liferay 6.2


Liferay by default supports sharding of databases accross different portal instances, for assigning databases to a particular instance by default it uses Round Robing Selector  which selects databases evenly. If we want to select databases manually then we can use Manual Selector. Also there is a facility of writing our own Custom Selector. Here is the default properties in portal.ext .






    #
    # Specify an algorithm for selecting a new shard on portal instance
    # creation. Use ManualShardSelector for shard selection via the web
    # interface.
    #
    #shard.selector=com.liferay.portal.dao.shard.RoundRobinShardSelector
    shard.selector=com.liferay.portal.dao.shard.ManualShardSelector
    #shard.selector=[your implementation here]

Note: Here we are using Manual Selector which will allow to select a particular database while creating a portal instance.

Note: Here as you are seeing that while creating a new instance we have three options of selecting a shard name here each shard name refers to a particular database. And also These options will be available after complete configuration done just follow the steps.

Now , we need to enable sharding ,for which we have to provide different shard configuration in portal-ext.properties as below


##Here each database configuration refers to one shard
## Which will be mapped while creating a databse 
## instance 
jdbc.default.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.default.username=sharddef
jdbc.default.password=azaz786
jdbc.default.url=jdbc:oracle:thin:@localhost:1521:xe

jdbc.one.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.one.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.one.username=shardone
jdbc.one.password=azaz786

jdbc.two.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.two.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.two.username=shardtwo
jdbc.two.password=azaz786

shard.available.names=default,one,two








Note: Here we have provided only three database with the shard names on,two and three,
If you want to create another one then you just have to provide configuration with the convention jdbc.[your shard name].*

Important Note: Here each shard has a mapping in shard-data-source-spring.xml file which is kept inside Portal-impl.jar in your Liferay server. Liferay default provides only three shards mapped in shard-data-source-spring.xml file so you can only provide three shards here, If you want more shards then you have to get this file for liferay source and have to add your more database configuration.here you can see i have added another datasource configuration in shard-data-source-spring.xml for your convinience.

 




 
  
   
    
   
  
 
 
  
 
 
  
   
    
   
  
 
 
  
   
    
   
  
 
 
  
   
    
   
  
 
 
  
   
    
   
  
 
 
  
   
    
     
      default
     
     
    
    
     
      one
     
     
    
    
     
      two
     
     
    
    
     
      two
     
     
    
   
  
 
 
  
 
 
  
   
  
 
 
  
   
  
 
 
  
   
    
   
  
 
 
  
  
  
  
  
 
 
  
  
 
 
  
 
 
  
 
 
  
 
 
  
 
 
  
 
 
  
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
 






Note: Here shard is referring to a datasource. Now as in current example we are only using three shards so we don't need to add any datasource in shard-data-source-spring.xml. But Incase if you are using then after adding your addithional datasource you have to keep this file in you Liferay server's \ROOT\WEB-INF\classes\META-INF folder.


Now in last step we have to add spring.configs configuration in our portal-ext file and uncomment the shard-data-source-spring.xml "META-INF/shard-data-source-spring.xml" line. as below



##
## Spring
##

    #
    # Input a list of comma delimited Spring configurations. These will be
    # loaded after the bean definitions specified in the contextConfigLocation
    # parameter in web.xml.
    #
    # Note that there is a special case for hibernate-spring.xml and
    # jpa-spring.xml. Even though both files are specified, only one will
    # actually load at runtime based on the property "persistence.provider".
    #
    spring.configs=\
        META-INF/base-spring.xml,\
        \
        META-INF/hibernate-spring.xml,\
        META-INF/infrastructure-spring.xml,\
        META-INF/management-spring.xml,\
        \
        META-INF/util-spring.xml,\
        \
        META-INF/jpa-spring.xml,\
        \
        META-INF/executor-spring.xml,\
        \
        META-INF/audit-spring.xml,\
        META-INF/cluster-spring.xml,\
        META-INF/editor-spring.xml,\
        META-INF/jcr-spring.xml,\
        META-INF/ldap-spring.xml,\
        META-INF/messaging-core-spring.xml,\
        META-INF/messaging-misc-spring.xml,\
        META-INF/mobile-device-spring.xml,\
        META-INF/notifications-spring.xml,\
        META-INF/poller-spring.xml,\
        META-INF/rules-spring.xml,\
        META-INF/scheduler-spring.xml,\
        META-INF/search-spring.xml,\
        META-INF/workflow-spring.xml,\
        \
        META-INF/counter-spring.xml,\
        META-INF/mail-spring.xml,\
        META-INF/portal-spring.xml,\
        META-INF/portlet-container-spring.xml,\
        META-INF/staging-spring.xml,\
        META-INF/virtual-layouts-spring.xml,\
        \
        META-INF/monitoring-spring.xml,\
        \
        #META-INF/dynamic-data-source-spring.xml,\
        META-INF/shard-data-source-spring.xml,\
        #META-INF/memcached-spring.xml,\
        \
        classpath*:META-INF/ext-spring.xml



Before creating instances just make some instance mapping ing your hosts file in window you can find your hosts file in C:\Windows\System32\drivers\etc\hosts and in linux /etc/hosts as below and make sure you restart your system after making a mapping in hosts file. in window normally you can edit the file in notepad and in linux you can use gedit or any other tool.



 Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
 127.0.0.1       localhost
 127.0.0.1 www.shardone.com
 127.0.0.1 www.shartwo.com
 

# ::1             localhost




Now you are ready with sharding configuration you can restart your server and create instances of your chiose and enjoy the power feature Sharding in Liferay.
Thanks

0 comments:

Post a Comment