KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > store > checkPoint


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.store.checkPoint
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.store;
23 import java.io.File JavaDoc;
24 import java.io.ByteArrayInputStream JavaDoc;
25
26 import java.math.BigDecimal JavaDoc;
27
28 import java.sql.Connection JavaDoc;
29 import java.sql.DriverManager JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.Statement JavaDoc;
34 import org.apache.derby.tools.ij;
35 import org.apache.derby.tools.JDBCDisplayUtil;
36
37 /**
38  * Test to make sure checkpoint or occuring as expected.
39  * Check is done by looking at the timestamp for "log.ctrl" file,
40  * If modified time is more than what it was in the last lookup
41  * means , we know that checkpoint occured.
42  * Other thing that is counted is in this program is number of log switches.
43  * @author suresht
44  */

45
46 public class checkPoint
47 {
48  
49     public static void main( String JavaDoc args[])
50     {
51         System.out.println("Test checkpoint starting");
52         
53         try
54         {
55             // use the ij utility to read the property file and
56
// make the initial connection.
57
ij.getPropertyArg(args);
58             Connection JavaDoc conn = ij.startJBMS();
59
60             //open all the internal derby files involved in this test.
61
setupAllTestFiles();
62
63             Statement JavaDoc stmt = conn.createStatement();
64             stmt.executeUpdate("CREATE PROCEDURE WAIT_FOR_POST_COMMIT() DYNAMIC RESULT SETS 0 LANGUAGE JAVA EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.T_Access.waitForPostCommitToFinish' PARAMETER STYLE JAVA");
65
66             stmt.executeUpdate( "create table t1(" +
67                                 "c1 int not null primary key , c2 varchar(200) not null unique , c3 char(200) not null unique)");
68             conn.setAutoCommit(true);
69             String JavaDoc ins_string = "insert into t1 values(?,?,?)";
70             PreparedStatement JavaDoc insStmt = conn.prepareStatement(ins_string);
71             //wait to make sure that checkpoint thread finished it's work
72
stmt.executeUpdate("CALL SYSCS_UTIL.SYSCS_CHECKPOINT_DATABASE()");
73             stmt.executeUpdate("call WAIT_FOR_POST_COMMIT()");
74             checkpointOccured();
75             boolean modifiedIntervals = false;
76             for(int uniqueid =0 ; uniqueid < 3500 ; uniqueid++)
77             {
78                 insStmt.setLong(1, uniqueid);
79                 insStmt.setString(2, "IBM GREAT COMPANY " + uniqueid);
80                 insStmt.setString(3, "IBM GREAT COMPANY " + uniqueid);
81                 insStmt.executeUpdate();
82                 
83                 //check every 300 rows inserted how many log files
84
//are there and whether a checkpoint occured
85
if((uniqueid % 400) == 0)
86                 {
87                     System.out.println("Checking logs and Checkpoint at Insert:"
88                                        + uniqueid);
89                     //wait to make sure that checkpoint thread finished it's work
90
stmt.executeUpdate("call WAIT_FOR_POST_COMMIT()");
91                     checkpointOccured();
92                 }
93
94                 //change the checkpointInterval and LogInterval to equal values
95
if(uniqueid > 2500 && !modifiedIntervals)
96                 {
97                     ResultSet JavaDoc rs;
98                     System.out.println("Modifying the checkpoint/log intervals");
99                     //modify the values.
100
String JavaDoc value = "150001";
101                     stmt.executeUpdate("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY" +
102                                        "('derby.storage.logSwitchInterval', " +
103                                        "'" + value + "'"+ ")");
104                     stmt.executeUpdate("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY" +
105                                        "('derby.storage.checkpointInterval', " +
106                                        "'" + value + "'" + ")");
107                     rs =
108                         stmt.executeQuery("values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY" +
109                                           "('derby.storage.checkpointInterval')");
110                     while(rs.next()){
111                         System.out.println("checkPointInterval:" + rs.getString(1));
112                     }
113                     
114                     rs =stmt.executeQuery("values SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY" +
115                                           "('derby.storage.logSwitchInterval')");
116                     while(rs.next()){
117                         System.out.println("logSwitchInterval:" + rs.getString(1));
118                     }
119
120                     modifiedIntervals = true;
121                 }
122             }
123             
124             //print the number of the last log file
125
//to make sure we are creating too many log files.
126
numberOfLogFiles();
127             conn.commit();
128             stmt.close();
129             insStmt.close();
130             conn.close();
131         }
132         catch( SQLException JavaDoc e)
133         {
134             dumpSQLExceptions(e);
135         } catch (Throwable JavaDoc e) {
136             System.out.println("FAIL -- unexpected exception:" + e.toString());
137         }
138
139         //shutdown the database ..
140
try{
141             //shutdown
142
Connection JavaDoc conn = DriverManager.getConnection("jdbc:derby:wombat;shutdown=true");
143         }catch(SQLException JavaDoc se){
144                 if (se.getSQLState() != null && se.getSQLState().equals("08006"))
145                     System.out.println("database shutdown properly\n");
146                 else
147                     dumpSQLExceptions(se);
148         } catch (Throwable JavaDoc e) {
149             System.out.println("FAIL -- unexpected exception:" + e.toString());
150         }
151
152         System.out.println("Test checkpoint finished");
153     }
154
155     
156     static private void dumpSQLExceptions (SQLException JavaDoc se) {
157         System.out.println("FAIL -- unexpected exception: " + se.toString());
158         while (se != null) {
159             System.out.print("SQLSTATE("+se.getSQLState()+"):");
160             se = se.getNextException();
161         }
162     }
163
164
165
166     //utility routines to trach number of log files
167
//and checkpoints.
168
private static String JavaDoc derbyHome;
169     private static File JavaDoc dbDir ;
170     private static File JavaDoc logDir;
171     private static File JavaDoc logControlFile;
172     private static long lastCheckPointTime = 0;
173
174     private static void setupAllTestFiles()
175     {
176         derbyHome = System.getProperty("derby.system.home");
177         dbDir = new File JavaDoc(derbyHome, "wombat");
178         logDir = new File JavaDoc(dbDir , "log");
179         logControlFile = new File JavaDoc(logDir , "log.ctrl");
180         lastCheckPointTime = logControlFile.lastModified();
181     }
182
183     private static boolean checkpointOccured()
184     {
185         long currentModifiedTime = logControlFile.lastModified();
186         if(currentModifiedTime > lastCheckPointTime)
187         {
188             lastCheckPointTime = currentModifiedTime ;
189             System.out.println("CHECKPOINT WAS DONE");
190             return true;
191         }
192         
193         return false;
194     }
195
196
197     private static int numberOfLogFiles()
198     {
199         //find out how many log files are in logDir
200
//-2 (control files log.ctrl, logmirror.ctrl)
201
File JavaDoc[] logFiles = logDir.listFiles();
202         int noFiles = (logFiles == null) ? 0 : logFiles.length;
203         String JavaDoc lastLogFile ="";
204         for(int i = 0 ; i < noFiles ; i++)
205         {
206             String JavaDoc current = logFiles[i].getName() ;
207             if(current.compareTo("log.ctrl")==0 || current.compareTo("logmirror.ctrl")==0)
208                 continue;
209             if(current.compareTo(lastLogFile) > 0)
210                 lastLogFile = current;
211         }
212
213         if(lastLogFile.compareTo("log21.dat") > 0)
214         {
215             System.out.println("There seems to be too many log files");
216             System.out.println(lastLogFile);
217         }
218         logFiles = null;
219         return noFiles -2 ;
220
221
222     }
223     
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
Popular Tags