KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > raidb1 > recoverylog > TransactionReplayScenario


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.raidb1.recoverylog;
26
27 import java.sql.Connection JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.objectweb.cjdbc.scenario.templates.Raidb1RecoveryTemplate;
31 import org.objectweb.cjdbc.scenario.tools.ScenarioUtility;
32 import org.objectweb.cjdbc.scenario.tools.util.RequestSender;
33
34 /**
35  * This class defines a TransactionReplayScenario
36  *
37  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk</a>
38  * @version 1.0
39  */

40 public class TransactionReplayScenario extends Raidb1RecoveryTemplate
41 {
42
43   static final int LOOPS = 500;
44   
45   /**
46    * Replay with no transactions and no concurrent requests
47    *
48    * @throws Exception if fails
49    */

50   public void testReplayMissingRequestsNoTransactionNoConcurrentRequest()
51       throws Exception JavaDoc
52   {
53     executeTestReplay(false, false, LOOPS, 1);
54   }
55
56   /**
57    * Replay with no transactions and no concurrent requests
58    *
59    * @throws Exception if fails
60    */

61   public void testReplayMissingRequestsTransactionAndConcurrentRequests()
62       throws Exception JavaDoc
63   {
64     executeTestReplay(true, true, LOOPS, 1);
65   }
66
67   /**
68    * Execute the recovery test, with given parameters
69    *
70    * @param concurrent should write/read queries be executed during recovery
71    * @param transaction should we use transaction
72    * @param loops how many times do we loop
73    * @throws Exception if fails
74    */

75   private void executeTestReplay(boolean concurrent, boolean transaction,
76       int loops, int clients) throws Exception JavaDoc
77   {
78     // Execute select query
79
Connection JavaDoc con = getCJDBCConnection();
80     String JavaDoc sql = "select * from document";
81     ArrayList JavaDoc list = ScenarioUtility.getSingleQueryResult(sql, con);
82     ArrayList JavaDoc emptyList = new ArrayList JavaDoc();
83     assertNotSame("Empty list after requests", list, emptyList);
84
85     // Disable backend
86
String JavaDoc backend = "localhost2";
87     String JavaDoc checkpoint = "check" + System.currentTimeMillis();
88     mainVdb.disableBackendWithCheckpoint(backend);
89
90     // Write on tables
91
ArrayList JavaDoc threads = new ArrayList JavaDoc();
92     for (int i = 0; i < clients; i++)
93     {
94       RequestSender rs = new RequestSender(con);
95       rs.setUseTransactions(transaction);
96       rs.setDoWriteEvery(1); // write intensive test
97
rs.setLoopInThread(loops);
98       Thread JavaDoc t = new Thread JavaDoc(rs);
99       threads.add(t);
100       t.start();
101       rs.setQuit(true);
102     }
103
104     if (!concurrent)
105       joinThreads(threads);
106
107     // Check content has been updated
108
ArrayList JavaDoc listaafter = ScenarioUtility.getSingleQueryResult(sql, con);
109     assertNotSame("No Updates after requests", list, listaafter);
110
111     // Get content of recovery
112
Connection JavaDoc h3 = getHypersonicConnection(9003);
113     ArrayList JavaDoc recovery = ScenarioUtility.getSingleQueryResult(
114         "select * from recovery", h3);
115     ScenarioUtility.displayResultOnScreen(recovery);
116
117     // Enable backend, should start the recovery log
118
mainVdb.enableBackendFromCheckpoint(backend, checkpoint);
119
120     if (!concurrent)
121       joinThreads(threads);
122
123     // Test directely on hypersonic connection
124
Connection JavaDoc h1 = getHypersonicConnection(9001);
125     ArrayList JavaDoc hl1 = ScenarioUtility.getSingleQueryResult(sql, h1);
126     Connection JavaDoc h2 = getHypersonicConnection(9002);
127     ArrayList JavaDoc hl2 = ScenarioUtility.getSingleQueryResult(sql, h2);
128
129     // Check all updates have been replayed
130
assertEquals("Backend is missing some updates", hl1, hl2);
131   }
132
133   /**
134    * Joins all the threads in the arraylist
135    *
136    * @param threads a list of the threads
137    * @throws Exception if fails
138    */

139   private void joinThreads(ArrayList JavaDoc threads) throws Exception JavaDoc
140   {
141     for (int i = 0; i < threads.size(); i++)
142       ((Thread JavaDoc) threads.get(i)).join();
143   }
144
145
146 }
147
Popular Tags