KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > horizontal > TransparentHorizontalBalancingScenario


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.horizontal;
26
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29
30 import org.objectweb.cjdbc.controller.core.Controller;
31 import org.objectweb.cjdbc.driver.ControllerInfo;
32 import org.objectweb.cjdbc.scenario.templates.HorizontalTemplate;
33
34 /**
35  * This class defines a TransparentHorizontalBalancingScenario class
36  *
37  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
38  */

39 public class TransparentHorizontalBalancingScenario extends HorizontalTemplate
40 {
41
42   /**
43    * Test we can still get new connections even if a node failed
44    *
45    * @throws Exception if fails
46    */

47   public void testTransparentBalancing() throws Exception JavaDoc
48   {
49     ControllerInfo[] controllers = new ControllerInfo[]{
50         new ControllerInfo("localhost", 25322),
51         new ControllerInfo("localhost", 25323)};
52     String JavaDoc readQuery = "Select * from document";
53     int connectionsLoad = 50;
54
55     // Test balancing between controllers
56
int[] result1 = execute(controllers, connectionsLoad);
57     System.out.println(result1[0] + ":" + result1[1]);
58     assertTrue("controller1 did not get connection request before failure",
59         result1[0] > 0);
60     assertTrue("controller2 did not get connection request before failure",
61         result1[1] > 0);
62
63     // Get a specific connection
64
org.objectweb.cjdbc.driver.Connection con = (org.objectweb.cjdbc.driver.Connection) getCJDBCConnection(controllers);
65     Statement JavaDoc statement = con.createStatement();
66     ResultSet JavaDoc rs = statement.executeQuery(readQuery);
67     assertNotNull("ResultSet before failure is null", rs);
68
69     // Simulate a failure on the controller the connection was
70
// connected to
71
int wasStopped = -1;
72     System.out.println("########Stopping controller");
73     if (con.getControllerInfo().equals(controllers[0]))
74     {
75       cm.stop(controller1.getPortNumber());
76       assertFalse("Controller1 should be stopped now", cm.isStarted("25322"));
77       wasStopped = 1;
78     }
79     else
80     {
81       cm.stop(controller2.getPortNumber());
82       assertFalse("Controller2 should be stopped now", cm.isStarted("25323"));
83       wasStopped = 2;
84     }
85
86     // See if we can still get connections to non-stopped controller
87
int[] result2 = execute(controllers, connectionsLoad);
88     int fresult = (wasStopped == 1) ? result2[1] : result2[0];
89     assertTrue(
90         "remaining controller did not get connection request during failure",
91         fresult > 0);
92
93     // Execute other queries with the previously opened connection
94
rs = statement.executeQuery(readQuery);
95     Statement JavaDoc statement2 = con.createStatement();
96     ResultSet JavaDoc rs2 = statement2.executeQuery(readQuery);
97     assertNotNull("ResultSet after failure is null", rs2);
98
99     System.out.println("wasStopped" + wasStopped);
100
101     // Restart failed controller
102
if (wasStopped == 1)
103     {
104       String JavaDoc port1 = "25322";
105       controller1 = (Controller) cm.start(port1).getProcess();
106       cm.loadVirtualDatabases(controller1, "myDB",
107           "hsqldb-raidb1-distribution-1.xml");
108       mainVdb1 = controller1.getVirtualDatabase("myDB");
109       mainVdb1.enableAllBackends();
110     }
111     else if (wasStopped == 2)
112     {
113       String JavaDoc port2 = "25323";
114       controller2 = (Controller) cm.start(port2).getProcess();
115       cm.loadVirtualDatabases(controller2, "myDB",
116           "hsqldb-raidb1-distribution-2.xml");
117       mainVdb2 = controller2.getVirtualDatabase("myDB");
118       mainVdb2.enableAllBackends();
119     }
120
121     // Execute a last test to see if balancing was re-enabled
122
int[] result3 = execute(controllers, connectionsLoad);
123     assertTrue("controller1 did not get connection request after failure",
124         result3[0] > 0);
125     assertTrue("controller2 did not get connection request after failure",
126         result3[1] > 0);
127   }
128
129   /**
130    * Execute an update each time
131    *
132    * @param controllers the list of controllers to connect to
133    * @param connections how many connections to ask
134    * @return connections on each controller
135    * @throws Exception if fails
136    */

137   private int[] execute(ControllerInfo[] controllers, int connections)
138       throws Exception JavaDoc
139   {
140     org.objectweb.cjdbc.driver.Connection con = null;
141     int[] count = new int[controllers.length];
142     ControllerInfo connected = null;
143     for (int i = 0; i < connections; i++)
144     {
145       con = (org.objectweb.cjdbc.driver.Connection) getCJDBCConnection(controllers);
146       assertNotNull("Received null connection", con);
147       Statement JavaDoc statement = con.createStatement();
148       statement.executeUpdate("update product set name='horizontalTest'");
149       connected = con.getControllerInfo();
150       System.out.println("Client connected to:" + connected);
151       assertNotNull("Received null for connected controller", connected);
152       for (int j = 0; j < controllers.length; j++)
153       {
154         if (connected.equals(controllers[j]))
155         {
156           count[j]++;
157           break;
158         }
159       }
160     }
161     return count;
162   }
163 }
Popular Tags