KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > ReplicationMySqlController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.sql.ResultSet JavaDoc;
27
28 import org.infoglue.cms.util.ChangeNotificationController;
29 import org.infoglue.cms.util.NotificationMessage;
30
31
32 /**
33  * ReplicationController.java
34  * Created on 2002-okt-30
35  * @author Stefan Sik, ss@frovi.com
36  * ss
37  *
38  * In this description I use the words Master, Slave and versions
39  *
40  * Versions are rows in cmContentVersion and in cmSiteNodeVersion
41  *
42  * In this context Slave is synonym with the MySQL database
43  * running and feeding the deliver1.0 application on the Live Site
44  * (www.frovi.com)
45  *
46  * Master is the MySQL database used by cmstool1.0
47  *
48  * Refactoring needed.
49  * Should implement generic replication interface
50  * that is used by the tool. This controller is specific
51  * for mysql and for our current server topology.
52  *
53  * Also write another replication implementation that
54  * uses a simpler replication mechanism, to ensure that
55  * the interface is generic and complete. (nice to have)
56  *
57  *
58  * Purpose of this controller is to ensure that the replika
59  * (the Live Database that the deliver engine uses), are
60  * up to date with the tool database.
61  *
62  * But wait, there is more. The replika may NOT contain
63  * any versions that has state <> 3 and isActive flag <> 1
64  *
65  * To fulfill these requirements with our current setup,
66  * we are going to use the replication mechanism built in to mySQL.
67  *
68  * In mySQL repl. The responsibility for syncronizing the slave with the master
69  * lies on the slave in the form of a thread that runs in mySQL on the slave.
70  * The slave thread reads from the masters log-bin file and executes the sql
71  * commands required for updating itself. It's a pull type of arrangement.
72  *
73  * We can start and stop this slave thread with sql commands to the slave server.
74  *
75  * We are going to solve it like this:
76  * 1 - Lock the master tables (no updates during sync)
77  * 1.1 - maybe read lock (if possible) the slave tables
78  * 2 - Start the slave thread on the slave.
79  * 3 - Wait for the update to finish.
80  * 4 - Unlock the master tables.
81  * 5 - Delete all unwanted rows on the slave.
82  *
83  * This solution has one little problem. At one moment we do have working material
84  * on the slave server. What happens if this controller dies before it deletes the
85  * unwanted versions. Maybe we should read lock on the slave during update?
86  * What happens if for some unknown reason the slave-tread starts. There is no
87  * way (I know of) to filter data that is to be replicated.
88  *
89  * Alternate solutions:
90  * sol 1 - Always run the slave-thread.
91  * Ensure that the deliver engine only delivers versions with
92  * the required criterias. Then this controller class would be
93  * redundant.
94  *
95  * sol 2 - On the slave server run a process that check (and deletes) the versions.
96  * and always run the slave thread. (This class is redundant)
97  *
98  * sol 3 - In the tool, on publish, push the wanted versions to a publishing table
99  * or possibly a publishing database. Then setup slave to replicate only
100  * that database or tables. Always run the slave thread.
101  *
102  *
103  *
104  *
105  * This controller should be called from PublishingController.
106  *
107  * References:
108  * http://www.mysql.com/doc/en/Replication_HOWTO.html
109  * http://www.mysql.com/doc/en/Replication_SQL.html
110  * http://www.mysql.com/doc/en/Replication_FAQ.html
111  *
112  *
113  */

114 public class ReplicationMySqlController // implements IGenericCmsReplication
115
{
116     public static void updateSlaveServer() throws Exception JavaDoc
117     {
118         String JavaDoc bin_log = "";
119         Integer JavaDoc position = new Integer JavaDoc(0);
120
121         // Sync the servers
122
MysqlJDBCService mySql = new MysqlJDBCService();
123
124         // If mysql is not setup for replication, dont try it
125
if(mySql.isEnabled())
126         {
127             ResultSet JavaDoc res = mySql.executeMasterSQL("SHOW MASTER STATUS");
128             if (res.first())
129             {
130                 bin_log = res.getString("File");
131                 position = new Integer JavaDoc(res.getInt("Position"));
132                 // mySql.executeSlaveSQL("SLAVE START");
133
mySql.executeSlaveSQL("SELECT MASTER_POS_WAIT('" + bin_log + "', " + position + ")");
134                 // mySql.executeSlaveSQL("SLAVE STOP");
135
}
136
137             mySql.closeMaster();
138             mySql.closeSlave();
139
140             // Expire cache on livesites?
141
NotificationMessage notificationMessage = new NotificationMessage("ReplicationMySqlController.updateSlaveServer():", NotificationMessage.PUBLISHING_TEXT, "Editor - name unknown", NotificationMessage.PUBLISHING, new Integer JavaDoc(-1), "");
142             ChangeNotificationController.getInstance().addNotificationMessage(notificationMessage);
143         }
144     }
145 }
146
Popular Tags