Tuesday, February 24, 2015

Database Sharding in liferay 6.1

(Multi Instances-->One Tomcat Server-->Multi Schemas)


Step 1:
Add the following entries in hosts file.i.e. /etc/hosts file in ubuntu:

127.0.0.1       instance1.com
127.0.0.1       instance2.com

Step 2:
Add following code in server/tomcat/webapps/root/web-inf/classes/portal-ext.properties

#Main Portal Configuration
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/mainportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=root

#SHARD 1 Configuration
jdbc.one.driverClassName=com.mysql.jdbc.Driver
jdbc.one.url=jdbc:mysql://localhost/shard1?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.one.username=root
jdbc.one.password=root

#SHARD 2 Configuration
jdbc.two.driverClassName=com.mysql.jdbc.Driver
jdbc.two.url=jdbc:mysql://localhost/shard2?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.two.username=root
jdbc.two.password=root

#Spring configuration files to be loaded. By adding shard-data-source-spring.xml in the list #database sharding feature
#can be enabled
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/scripting-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/shard-data-source-spring.xml,\
      META-INF/ext-spring.xml

Here mainportal,shard1,shard2 are database which we need to create.

Step 3:
Create mainportal,shard1,shard2 databases.

Step 4:
Now start the liferay server

Step 5:
Check your all three databases whether default tables is created or not.

Note:Please take dump from shard1 and keep it seperate for adding future shard.

Step 6:
Now create a new instance instance1.com by going to control panel/portal instances/add

web id:instance1.com
Virtual host:instance1.com (what we give in host file)
mail domain name:instance1.com (it helps to create @instance1.com)
Max users:0  (It says infinite user)

Then create it.

Note:You will not be selecting db manually as its not enabled.
For now let it be round robin method of database allocation.
Step 7:
create user and password.

Note:
Now you goto control panel there you will not be able to see server part as its a instance.

Step 8:
Now go and check your database mainportal company table you will get a new company
instance1 with already existing default liferay company.

Note:When you create a new instance company name will come to default main instance and other details about company such as username will go to new database.

Step 9:
And goto shard1 database user_  table you will get the user list with email id test and default@instance1.com

Step 10:
Follow step number 6,7 and 8.
for creating instance2.com as new instance.

Step 11:
And goto shard2 database user_  table you will get the user list with email id test and default@instance2.com.

Step 12:
Now shutdown the server.

                                             -------------------------------------------------
As default instances done hereafter
To add new portal instance (more than 2) and it helps to select database ourself:

Step 13:

Add in portal ext.properties that is in block.

shard.available.names=default,one,two,three
shard.selector=com.liferay.portal.dao.shard.ManualShardSelector

#Main Portal Configuration
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/mainportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=root

#SHARD 1 Configuration
jdbc.one.driverClassName=com.mysql.jdbc.Driver
jdbc.one.url=jdbc:mysql://localhost/shard1?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.one.username=root
jdbc.one.password=root

#SHARD 2 Configuration
jdbc.two.driverClassName=com.mysql.jdbc.Driver
jdbc.two.url=jdbc:mysql://localhost/shard2?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.two.username=root
jdbc.two.password=root

#SHARD 3 Configuration
jdbc.three.driverClassName=com.mysql.jdbc.Driver
jdbc.three.url=jdbc:mysql://localhost/shard3?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.three.username=root
jdbc.three.password=root


#Spring configuration files to be loaded. By adding shard-data-source-spring.xml in the list database sharding feature
#can be enabled
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/scripting-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/shard-data-source-spring.xml,\
      META-INF/ext-spring.xml


Step 14:

Create new database in mysql shard3.

Note:And in step 2 we took dump of shard1

Open the shard1.sql replace shard1 with shard3

And move the dump to shard3.

Step 15:
And open portal-impl.jar in server/tomcat/webapps/root/web-inf

And open meta-inf/shard-data-source-spring.xml

add code i blocked it for your reference:

<?xml version="1.0"?>
<beans
  default-destroy-method="destroy"
  default-init-method="afterPropertiesSet"
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
>
  <bean id="liferayDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
     <property name="targetDataSource">
         <bean class="org.springframework.aop.framework.ProxyFactoryBean">
             <property name="targetSource" ref="shardDataSourceTargetSource" />
         </bean>
     </property>
  </bean>
  <bean id="liferayHibernateSessionFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
     <property name="targetSource" ref="shardSessionFactoryTargetSource" />
  </bean>
  <bean id="shardDataSource0" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
     <property name="targetDataSource">
         <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
             <property name="propertyPrefix" value="jdbc.default." />
         </bean>
     </property>
  </bean>
  <bean id="shardDataSource1" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
     <property name="targetDataSource">
         <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
             <property name="propertyPrefix" value="jdbc.one." />
         </bean>
     </property>
  </bean>
  <bean id="shardDataSource2" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
     <property name="targetDataSource">
         <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
             <property name="propertyPrefix" value="jdbc.two." />
         </bean>
     </property>
  </bean>
  <bean id="shardDataSource3" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
     <property name="targetDataSource">
         <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
             <property name="propertyPrefix" value="jdbc.three." />
         </bean>
     </property>
  </bean>
  <bean id="shardDataSourceTargetSource" class="com.liferay.portal.dao.shard.ShardDataSourceTargetSource">
     <property name="dataSources">
         <map>
             <entry>
                 <key>
                     <value>default</value>
                 </key>
                 <ref bean="shardDataSource0" />
             </entry>
             <entry>
                 <key>
                     <value>one</value>
                 </key>
                 <ref bean="shardDataSource1" />
             </entry>
             <entry>
                 <key>
                     <value>two</value>
                 </key>
                 <ref bean="shardDataSource2" />
             </entry>
             <entry>
                 <key>
                     <value>three</value>
                 </key>
                 <ref bean="shardDataSource3" />
             </entry>
         </map>
     </property>
  </bean>
  <bean id="shardSessionFactoryTargetSource" class="com.liferay.portal.dao.shard.ShardSessionFactoryTargetSource">
        <property name="shardDataSourceTargetSource" ref="shardDataSourceTargetSource" />
  </bean>
  <bean id="com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil" class="com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil">
     <property name="mappingSqlQueryFactory">
         <bean class="com.liferay.portal.dao.shard.ShardMappingSqlQueryFactoryImpl" />
     </property>
  </bean>
  <bean id="com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil" class="com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil">
     <property name="sqlUpdateFactory">
         <bean class="com.liferay.portal.dao.shard.ShardSqlUpdateFactoryImpl" />
     </property>
  </bean>
  <bean id="com.liferay.portal.kernel.dao.shard.ShardUtil" class="com.liferay.portal.kernel.dao.shard.ShardUtil">
     <property name="shard">
         <bean class="com.liferay.portal.dao.shard.ShardImpl">
             <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
         </bean>
     </property>
  </bean>
  <bean id="com.liferay.portal.kernel.util.InfrastructureUtil" class="com.liferay.portal.kernel.util.InfrastructureUtil">
     <property name="dataSource" ref="liferayDataSource" />
     <property name="mailSession" ref="mailSession" />
     <property name="shardDataSourceTargetSource" ref="shardDataSourceTargetSource" />
     <property name="shardSessionFactoryTargetSource" ref="shardSessionFactoryTargetSource" />
     <property name="transactionManager" ref="liferayTransactionManager" />
  </bean>
  <bean id="com.liferay.portal.dao.shard.advice.ShardAdvice" class="com.liferay.portal.dao.shard.advice.ShardAdvice">
     <property name="shardDataSourceTargetSource" ref="shardDataSourceTargetSource" />
     <property name="shardSessionFactoryTargetSource" ref="shardSessionFactoryTargetSource" />
  </bean>
  <bean id="com.liferay.portal.dao.shard.advice.ShardCompanyAdvice" class="com.liferay.portal.dao.shard.advice.ShardCompanyAdvice">
     <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
  </bean>
  <bean id="com.liferay.portal.dao.shard.advice.ShardPortletAdvice" class="com.liferay.portal.dao.shard.advice.ShardPortletAdvice">
     <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
  </bean>
  <bean id="com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice" class="com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice">
     <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
  </bean>
  <bean id="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" class="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice">
     <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
  </bean>
  <bean id="com.liferay.portal.dao.shard.advice.ShardIterativelyAdvice" class="com.liferay.portal.dao.shard.advice.ShardIterativelyAdvice">
     <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
  </bean>
  <bean id="com.liferay.portal.dao.shard.advice.ShardParameterAdvice" class="com.liferay.portal.dao.shard.advice.ShardParameterAdvice">
     <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
  </bean>
  <aop:config proxy-target-class="false">
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardCompanyAdvice" pointcut="bean(com.liferay.portal.service.CompanyLocalService)" />
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardPortletAdvice" pointcut="bean(com.liferay.portal.service.PortletLocalService)" />
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice" pointcut="bean(*Persistence) || bean(*Finder)" />
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.convert.messaging.ConvertProcessMessageListener.receive(..))" />
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.events.StartupHelper.updateIndexes())" />
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.events.StartupHelper.upgradeProcess(int))" />
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardIterativelyAdvice" pointcut="execution(void com.liferay.portal.events.StartupHelper.verifyProcess(boolean))" />
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.service.ReleaseLocalService.createTablesAndPopulate())" />
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.service.ServiceComponentLocalService.upgradeDB(..))" />
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portlet.journal.service.JournalArticleLocalService.checkArticles())" />
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardParameterAdvice" pointcut="execution(* com.liferay.portal.service.AccountLocalService.getAccount(long, long))" />
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardParameterAdvice" pointcut="execution(* com.liferay.portal.service.ResourceCodeLocalService.checkResourceCodes(long, String))" />
     <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardParameterAdvice" pointcut="execution(* com.liferay.portal.service.UserLocalService.searchCount(..))" />
  </aop:config>
</beans>




Step 16:
Start the server and

Follow step number 6,7 and 8.
for creating instance3.com as new instance.

Here you will select the database.Here select shard3.

Step 17:
And goto shard3 database user_  table you will get the user list with email id test and default@instance3.com.

Step 18:
Follow from step 13 again to create more instances.








No comments:

Post a Comment